1 package polyglot.lex; 2 3 import java.io.Reader ; 4 import java.io.FilterReader ; 5 import java.io.IOException ; 6 7 8 public class EscapedUnicodeReader extends FilterReader { 9 10 int pushback=-1; 11 boolean isEvenSlash = true; 12 13 public EscapedUnicodeReader(Reader in) { 14 super(in); 15 } 16 public int read() throws IOException { 17 int r = (pushback==-1)?in.read():pushback; pushback=-1; 18 19 if (r!='\\') { 20 isEvenSlash=true; 21 return r; 22 } else { if (!isEvenSlash) { isEvenSlash=true; 25 return r; 26 } 27 28 pushback=in.read(); 30 if (pushback!='u') { 31 isEvenSlash=false; 32 return '\\'; 33 } 34 35 pushback=-1; 38 while((r=in.read())=='u') 39 ; 40 int val=0; 43 for (int i=0; i<4; i++, r=in.read()) { 44 int d=Character.digit((char)r, 16); 45 if (r<0 || d<0) 46 throw new Error ("Invalid unicode escape character."); 47 val = (val*16) + d; 48 } 49 pushback = r; 51 isEvenSlash=true; 52 return val; 53 } 54 } 55 public int read(char cbuf[], int off, int len) throws IOException { 57 for (int i=0; i<len; i++) { 58 int c = read(); 59 if (c==-1) return (i==0)?-1:i; else cbuf[i+off] = (char) c; 61 } 62 return len; 63 } 64 65 public boolean markSupported() { return false; } 66 67 public boolean ready() throws IOException { 68 if (pushback!=-1) return true; 69 else return in.ready(); 70 } 71 } 72 | Popular Tags |