src/share/classes/com/sun/tools/javac/parser/UnicodeReader.java

changeset 1125
56830d5cb5bb
parent 1113
d346ab55031b
child 1339
0e5899f09dab
equal deleted inserted replaced
1124:9e2eb4bc49eb 1125:56830d5cb5bb
24 */ 24 */
25 25
26 package com.sun.tools.javac.parser; 26 package com.sun.tools.javac.parser;
27 27
28 import com.sun.tools.javac.file.JavacFileManager; 28 import com.sun.tools.javac.file.JavacFileManager;
29 import com.sun.tools.javac.util.Log;
30 import com.sun.tools.javac.util.Name;
31 import com.sun.tools.javac.util.Names;
32
29 import java.nio.CharBuffer; 33 import java.nio.CharBuffer;
30 import com.sun.tools.javac.util.Log; 34
31 import static com.sun.tools.javac.util.LayoutCharacters.*; 35 import static com.sun.tools.javac.util.LayoutCharacters.*;
32 36
33 /** The char reader used by the javac lexer/tokenizer. Returns the sequence of 37 /** The char reader used by the javac lexer/tokenizer. Returns the sequence of
34 * characters contained in the input stream, handling unicode escape accordingly. 38 * characters contained in the input stream, handling unicode escape accordingly.
35 * Additionally, it provide features for saving chars into a buffer and to retrieve 39 * Additionally, it provide features for saving chars into a buffer and to retrieve
56 /** The buffer index of the last converted unicode character 60 /** The buffer index of the last converted unicode character
57 */ 61 */
58 protected int unicodeConversionBp = -1; 62 protected int unicodeConversionBp = -1;
59 63
60 protected Log log; 64 protected Log log;
65 protected Names names;
66
67 /** A character buffer for saved chars.
68 */
69 protected char[] sbuf = new char[128];
70 protected int sp;
61 71
62 /** 72 /**
63 * Create a scanner from the input array. This method might 73 * Create a scanner from the input array. This method might
64 * modify the array. To avoid copying the input array, ensure 74 * modify the array. To avoid copying the input array, ensure
65 * that {@code inputLength < input.length} or 75 * that {@code inputLength < input.length} or
74 this(sf, JavacFileManager.toArray(buffer), buffer.limit()); 84 this(sf, JavacFileManager.toArray(buffer), buffer.limit());
75 } 85 }
76 86
77 protected UnicodeReader(ScannerFactory sf, char[] input, int inputLength) { 87 protected UnicodeReader(ScannerFactory sf, char[] input, int inputLength) {
78 log = sf.log; 88 log = sf.log;
89 names = sf.names;
79 if (inputLength == input.length) { 90 if (inputLength == input.length) {
80 if (input.length > 0 && Character.isWhitespace(input[input.length - 1])) { 91 if (input.length > 0 && Character.isWhitespace(input[input.length - 1])) {
81 inputLength--; 92 inputLength--;
82 } else { 93 } else {
83 char[] newInput = new char[inputLength + 1]; 94 char[] newInput = new char[inputLength + 1];
99 ch = buf[++bp]; 110 ch = buf[++bp];
100 if (ch == '\\') { 111 if (ch == '\\') {
101 convertUnicode(); 112 convertUnicode();
102 } 113 }
103 } 114 }
115 }
116
117 /** Read next character in comment, skipping over double '\' characters.
118 */
119 protected void scanCommentChar() {
120 scanChar();
121 if (ch == '\\') {
122 if (peekChar() == '\\' && !isUnicode()) {
123 skipChar();
124 } else {
125 convertUnicode();
126 }
127 }
128 }
129
130 /** Append a character to sbuf.
131 */
132 protected void putChar(char ch, boolean scan) {
133 if (sp == sbuf.length) {
134 char[] newsbuf = new char[sbuf.length * 2];
135 System.arraycopy(sbuf, 0, newsbuf, 0, sbuf.length);
136 sbuf = newsbuf;
137 }
138 sbuf[sp++] = ch;
139 if (scan)
140 scanChar();
141 }
142
143 protected void putChar(char ch) {
144 putChar(ch, false);
145 }
146
147 protected void putChar(boolean scan) {
148 putChar(ch, scan);
149 }
150
151 Name name() {
152 return names.fromChars(sbuf, 0, sp);
153 }
154
155 String chars() {
156 return new String(sbuf, 0, sp);
104 } 157 }
105 158
106 /** Convert unicode escape; bp points to initial '\' character 159 /** Convert unicode escape; bp points to initial '\' character
107 * (Spec 3.3). 160 * (Spec 3.3).
108 */ 161 */

mercurial