1 17 18 19 package org.codehaus.groovy.antlr; 20 21 import java.io.IOException ; 22 import java.io.Reader ; 23 24 import antlr.CharScanner; 25 26 35 public class UnicodeEscapingReader extends Reader { 36 37 private Reader reader; 38 private CharScanner lexer; 39 private boolean hasNextChar = false; 40 private int nextChar; 41 private SourceBuffer sourceBuffer; 42 43 47 public UnicodeEscapingReader(Reader reader,SourceBuffer sourceBuffer) { 48 this.reader = reader; 49 this.sourceBuffer = sourceBuffer; 50 } 51 52 56 public void setLexer(CharScanner lexer) { 57 this.lexer = lexer; 58 } 59 60 64 public int read(char cbuf[], int off, int len) throws IOException { 65 int c = 0; 66 int count = 0; 67 while (count < len && (c = read())!= -1) { 68 cbuf[off + count] = (char) c; 69 count++; 70 } 71 return (count == 0 && c == -1) ? -1 : count; 72 } 73 74 79 public int read() throws IOException { 80 if (hasNextChar) { 81 hasNextChar = false; 82 write(nextChar); 83 return nextChar; 84 } 85 86 int c = reader.read(); 87 if (c != '\\') { 88 write(c); 89 return c; 90 } 91 92 c = reader.read(); 94 if (c != 'u') { 95 hasNextChar = true; 96 nextChar = c; 97 write('\\'); 98 return '\\'; 99 } 100 101 do { 103 c = reader.read(); 104 } while (c == 'u'); 105 106 checkHexDigit(c); 108 StringBuffer charNum = new StringBuffer (); 109 charNum.append((char) c); 110 111 for (int i = 0; i < 3; i++) { 113 c = reader.read(); 114 checkHexDigit(c); 115 charNum.append((char) c); 116 } 117 int rv = Integer.parseInt(charNum.toString(), 16); 118 write(rv); 119 return rv; 120 } 121 private void write(int c) { 122 if (sourceBuffer != null) {sourceBuffer.write(c);} 123 } 124 127 private void checkHexDigit(int c) throws IOException { 128 if (c >= '0' && c <= '9') { 129 return; 130 } 131 if (c >= 'a' && c <= 'f') { 132 return; 133 } 134 if (c >= 'A' && c <= 'F') { 135 return; 136 } 137 hasNextChar = true; 139 nextChar = c; 140 throw new IOException ("Did not find four digit hex character code." 141 + " line: " + lexer.getLine() + " col:" + lexer.getColumn()); 142 } 143 144 148 public void close() throws IOException { 149 reader.close(); 150 } 151 } 152 | Popular Tags |