author | vromero |
Thu, 01 Nov 2012 12:47:11 +0000 | |
changeset 14370 | eefd0e6642a8 |
parent 14259 | fb94a1df0d53 |
child 14720 | c24c61d0d9a6 |
permissions | -rw-r--r-- |
10815 | 1 |
/* |
13077 | 2 |
* Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. |
10815 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package com.sun.tools.javac.parser; |
|
27 |
||
28 |
import com.sun.tools.javac.code.Source; |
|
10948 | 29 |
import com.sun.tools.javac.parser.Tokens.Comment.CommentStyle; |
10815 | 30 |
import com.sun.tools.javac.util.*; |
31 |
||
10948 | 32 |
import java.nio.CharBuffer; |
10815 | 33 |
|
34 |
import static com.sun.tools.javac.parser.Tokens.*; |
|
35 |
import static com.sun.tools.javac.util.LayoutCharacters.*; |
|
36 |
||
37 |
/** The lexical analyzer maps an input stream consisting of |
|
38 |
* ASCII characters and Unicode escapes into a token sequence. |
|
39 |
* |
|
40 |
* <p><b>This is NOT part of any supported API. |
|
41 |
* If you write code that depends on this, you do so at your own risk. |
|
42 |
* This code and its internal interfaces are subject to change or |
|
43 |
* deletion without notice.</b> |
|
44 |
*/ |
|
45 |
public class JavaTokenizer { |
|
46 |
||
47 |
private static boolean scannerDebug = false; |
|
48 |
||
49 |
/** Allow hex floating-point literals. |
|
50 |
*/ |
|
51 |
private boolean allowHexFloats; |
|
52 |
||
53 |
/** Allow binary literals. |
|
54 |
*/ |
|
55 |
private boolean allowBinaryLiterals; |
|
56 |
||
57 |
/** Allow underscores in literals. |
|
58 |
*/ |
|
59 |
private boolean allowUnderscoresInLiterals; |
|
60 |
||
61 |
/** The source language setting. |
|
62 |
*/ |
|
63 |
private Source source; |
|
64 |
||
65 |
/** The log to be used for error reporting. |
|
66 |
*/ |
|
67 |
private final Log log; |
|
68 |
||
69 |
/** The token factory. */ |
|
70 |
private final Tokens tokens; |
|
71 |
||
72 |
/** The token kind, set by nextToken(). |
|
73 |
*/ |
|
74 |
protected TokenKind tk; |
|
75 |
||
76 |
/** The token's radix, set by nextToken(). |
|
77 |
*/ |
|
78 |
protected int radix; |
|
79 |
||
80 |
/** The token's name, set by nextToken(). |
|
81 |
*/ |
|
82 |
protected Name name; |
|
83 |
||
84 |
/** The position where a lexical error occurred; |
|
85 |
*/ |
|
86 |
protected int errPos = Position.NOPOS; |
|
87 |
||
10948 | 88 |
/** The Unicode reader (low-level stream reader). |
10815 | 89 |
*/ |
10948 | 90 |
protected UnicodeReader reader; |
10815 | 91 |
|
10948 | 92 |
protected ScannerFactory fac; |
10815 | 93 |
|
94 |
private static final boolean hexFloatsWork = hexFloatsWork(); |
|
95 |
private static boolean hexFloatsWork() { |
|
96 |
try { |
|
97 |
Float.valueOf("0x1.0p1"); |
|
98 |
return true; |
|
99 |
} catch (NumberFormatException ex) { |
|
100 |
return false; |
|
101 |
} |
|
102 |
} |
|
103 |
||
104 |
/** |
|
105 |
* Create a scanner from the input array. This method might |
|
106 |
* modify the array. To avoid copying the input array, ensure |
|
107 |
* that {@code inputLength < input.length} or |
|
108 |
* {@code input[input.length -1]} is a white space character. |
|
109 |
* |
|
110 |
* @param fac the factory which created this Scanner |
|
14259 | 111 |
* @param buf the input, might be modified |
10815 | 112 |
* Must be positive and less than or equal to input.length. |
113 |
*/ |
|
114 |
protected JavaTokenizer(ScannerFactory fac, CharBuffer buf) { |
|
115 |
this(fac, new UnicodeReader(fac, buf)); |
|
116 |
} |
|
117 |
||
118 |
protected JavaTokenizer(ScannerFactory fac, char[] buf, int inputLength) { |
|
119 |
this(fac, new UnicodeReader(fac, buf, inputLength)); |
|
120 |
} |
|
121 |
||
122 |
protected JavaTokenizer(ScannerFactory fac, UnicodeReader reader) { |
|
10948 | 123 |
this.fac = fac; |
124 |
this.log = fac.log; |
|
125 |
this.tokens = fac.tokens; |
|
126 |
this.source = fac.source; |
|
10815 | 127 |
this.reader = reader; |
10948 | 128 |
this.allowBinaryLiterals = source.allowBinaryLiterals(); |
129 |
this.allowHexFloats = source.allowHexFloats(); |
|
130 |
this.allowUnderscoresInLiterals = source.allowUnderscoresInLiterals(); |
|
10815 | 131 |
} |
132 |
||
133 |
/** Report an error at the given position using the provided arguments. |
|
134 |
*/ |
|
135 |
protected void lexError(int pos, String key, Object... args) { |
|
136 |
log.error(pos, key, args); |
|
137 |
tk = TokenKind.ERROR; |
|
138 |
errPos = pos; |
|
139 |
} |
|
140 |
||
141 |
/** Read next character in character or string literal and copy into sbuf. |
|
142 |
*/ |
|
143 |
private void scanLitChar(int pos) { |
|
144 |
if (reader.ch == '\\') { |
|
145 |
if (reader.peekChar() == '\\' && !reader.isUnicode()) { |
|
146 |
reader.skipChar(); |
|
10948 | 147 |
reader.putChar('\\', true); |
10815 | 148 |
} else { |
149 |
reader.scanChar(); |
|
150 |
switch (reader.ch) { |
|
151 |
case '0': case '1': case '2': case '3': |
|
152 |
case '4': case '5': case '6': case '7': |
|
153 |
char leadch = reader.ch; |
|
154 |
int oct = reader.digit(pos, 8); |
|
155 |
reader.scanChar(); |
|
156 |
if ('0' <= reader.ch && reader.ch <= '7') { |
|
157 |
oct = oct * 8 + reader.digit(pos, 8); |
|
158 |
reader.scanChar(); |
|
159 |
if (leadch <= '3' && '0' <= reader.ch && reader.ch <= '7') { |
|
160 |
oct = oct * 8 + reader.digit(pos, 8); |
|
161 |
reader.scanChar(); |
|
162 |
} |
|
163 |
} |
|
10948 | 164 |
reader.putChar((char)oct); |
10815 | 165 |
break; |
166 |
case 'b': |
|
10948 | 167 |
reader.putChar('\b', true); break; |
10815 | 168 |
case 't': |
10948 | 169 |
reader.putChar('\t', true); break; |
10815 | 170 |
case 'n': |
10948 | 171 |
reader.putChar('\n', true); break; |
10815 | 172 |
case 'f': |
10948 | 173 |
reader.putChar('\f', true); break; |
10815 | 174 |
case 'r': |
10948 | 175 |
reader.putChar('\r', true); break; |
10815 | 176 |
case '\'': |
10948 | 177 |
reader.putChar('\'', true); break; |
10815 | 178 |
case '\"': |
10948 | 179 |
reader.putChar('\"', true); break; |
10815 | 180 |
case '\\': |
10948 | 181 |
reader.putChar('\\', true); break; |
10815 | 182 |
default: |
183 |
lexError(reader.bp, "illegal.esc.char"); |
|
184 |
} |
|
185 |
} |
|
186 |
} else if (reader.bp != reader.buflen) { |
|
10948 | 187 |
reader.putChar(true); |
10815 | 188 |
} |
189 |
} |
|
190 |
||
191 |
private void scanDigits(int pos, int digitRadix) { |
|
192 |
char saveCh; |
|
193 |
int savePos; |
|
194 |
do { |
|
195 |
if (reader.ch != '_') { |
|
10948 | 196 |
reader.putChar(false); |
10815 | 197 |
} else { |
198 |
if (!allowUnderscoresInLiterals) { |
|
199 |
lexError(pos, "unsupported.underscore.lit", source.name); |
|
200 |
allowUnderscoresInLiterals = true; |
|
201 |
} |
|
202 |
} |
|
203 |
saveCh = reader.ch; |
|
204 |
savePos = reader.bp; |
|
205 |
reader.scanChar(); |
|
206 |
} while (reader.digit(pos, digitRadix) >= 0 || reader.ch == '_'); |
|
207 |
if (saveCh == '_') |
|
208 |
lexError(savePos, "illegal.underscore"); |
|
209 |
} |
|
210 |
||
211 |
/** Read fractional part of hexadecimal floating point number. |
|
212 |
*/ |
|
213 |
private void scanHexExponentAndSuffix(int pos) { |
|
214 |
if (reader.ch == 'p' || reader.ch == 'P') { |
|
10948 | 215 |
reader.putChar(true); |
10815 | 216 |
skipIllegalUnderscores(); |
217 |
if (reader.ch == '+' || reader.ch == '-') { |
|
10948 | 218 |
reader.putChar(true); |
10815 | 219 |
} |
220 |
skipIllegalUnderscores(); |
|
221 |
if ('0' <= reader.ch && reader.ch <= '9') { |
|
222 |
scanDigits(pos, 10); |
|
223 |
if (!allowHexFloats) { |
|
224 |
lexError(pos, "unsupported.fp.lit", source.name); |
|
225 |
allowHexFloats = true; |
|
226 |
} |
|
227 |
else if (!hexFloatsWork) |
|
228 |
lexError(pos, "unsupported.cross.fp.lit"); |
|
229 |
} else |
|
230 |
lexError(pos, "malformed.fp.lit"); |
|
231 |
} else { |
|
232 |
lexError(pos, "malformed.fp.lit"); |
|
233 |
} |
|
234 |
if (reader.ch == 'f' || reader.ch == 'F') { |
|
10948 | 235 |
reader.putChar(true); |
10815 | 236 |
tk = TokenKind.FLOATLITERAL; |
237 |
radix = 16; |
|
238 |
} else { |
|
239 |
if (reader.ch == 'd' || reader.ch == 'D') { |
|
10948 | 240 |
reader.putChar(true); |
10815 | 241 |
} |
242 |
tk = TokenKind.DOUBLELITERAL; |
|
243 |
radix = 16; |
|
244 |
} |
|
245 |
} |
|
246 |
||
247 |
/** Read fractional part of floating point number. |
|
248 |
*/ |
|
249 |
private void scanFraction(int pos) { |
|
250 |
skipIllegalUnderscores(); |
|
251 |
if ('0' <= reader.ch && reader.ch <= '9') { |
|
252 |
scanDigits(pos, 10); |
|
253 |
} |
|
10948 | 254 |
int sp1 = reader.sp; |
10815 | 255 |
if (reader.ch == 'e' || reader.ch == 'E') { |
10948 | 256 |
reader.putChar(true); |
10815 | 257 |
skipIllegalUnderscores(); |
258 |
if (reader.ch == '+' || reader.ch == '-') { |
|
10948 | 259 |
reader.putChar(true); |
10815 | 260 |
} |
261 |
skipIllegalUnderscores(); |
|
262 |
if ('0' <= reader.ch && reader.ch <= '9') { |
|
263 |
scanDigits(pos, 10); |
|
264 |
return; |
|
265 |
} |
|
266 |
lexError(pos, "malformed.fp.lit"); |
|
10948 | 267 |
reader.sp = sp1; |
10815 | 268 |
} |
269 |
} |
|
270 |
||
271 |
/** Read fractional part and 'd' or 'f' suffix of floating point number. |
|
272 |
*/ |
|
273 |
private void scanFractionAndSuffix(int pos) { |
|
274 |
radix = 10; |
|
275 |
scanFraction(pos); |
|
276 |
if (reader.ch == 'f' || reader.ch == 'F') { |
|
10948 | 277 |
reader.putChar(true); |
10815 | 278 |
tk = TokenKind.FLOATLITERAL; |
279 |
} else { |
|
280 |
if (reader.ch == 'd' || reader.ch == 'D') { |
|
10948 | 281 |
reader.putChar(true); |
10815 | 282 |
} |
283 |
tk = TokenKind.DOUBLELITERAL; |
|
284 |
} |
|
285 |
} |
|
286 |
||
287 |
/** Read fractional part and 'd' or 'f' suffix of floating point number. |
|
288 |
*/ |
|
289 |
private void scanHexFractionAndSuffix(int pos, boolean seendigit) { |
|
290 |
radix = 16; |
|
291 |
Assert.check(reader.ch == '.'); |
|
10948 | 292 |
reader.putChar(true); |
10815 | 293 |
skipIllegalUnderscores(); |
294 |
if (reader.digit(pos, 16) >= 0) { |
|
295 |
seendigit = true; |
|
296 |
scanDigits(pos, 16); |
|
297 |
} |
|
298 |
if (!seendigit) |
|
299 |
lexError(pos, "invalid.hex.number"); |
|
300 |
else |
|
301 |
scanHexExponentAndSuffix(pos); |
|
302 |
} |
|
303 |
||
304 |
private void skipIllegalUnderscores() { |
|
305 |
if (reader.ch == '_') { |
|
306 |
lexError(reader.bp, "illegal.underscore"); |
|
307 |
while (reader.ch == '_') |
|
308 |
reader.scanChar(); |
|
309 |
} |
|
310 |
} |
|
311 |
||
312 |
/** Read a number. |
|
313 |
* @param radix The radix of the number; one of 2, j8, 10, 16. |
|
314 |
*/ |
|
315 |
private void scanNumber(int pos, int radix) { |
|
316 |
// for octal, allow base-10 digit in case it's a float literal |
|
317 |
this.radix = radix; |
|
318 |
int digitRadix = (radix == 8 ? 10 : radix); |
|
319 |
boolean seendigit = false; |
|
320 |
if (reader.digit(pos, digitRadix) >= 0) { |
|
321 |
seendigit = true; |
|
322 |
scanDigits(pos, digitRadix); |
|
323 |
} |
|
324 |
if (radix == 16 && reader.ch == '.') { |
|
325 |
scanHexFractionAndSuffix(pos, seendigit); |
|
326 |
} else if (seendigit && radix == 16 && (reader.ch == 'p' || reader.ch == 'P')) { |
|
327 |
scanHexExponentAndSuffix(pos); |
|
328 |
} else if (digitRadix == 10 && reader.ch == '.') { |
|
10948 | 329 |
reader.putChar(true); |
10815 | 330 |
scanFractionAndSuffix(pos); |
331 |
} else if (digitRadix == 10 && |
|
332 |
(reader.ch == 'e' || reader.ch == 'E' || |
|
333 |
reader.ch == 'f' || reader.ch == 'F' || |
|
334 |
reader.ch == 'd' || reader.ch == 'D')) { |
|
335 |
scanFractionAndSuffix(pos); |
|
336 |
} else { |
|
337 |
if (reader.ch == 'l' || reader.ch == 'L') { |
|
338 |
reader.scanChar(); |
|
339 |
tk = TokenKind.LONGLITERAL; |
|
340 |
} else { |
|
341 |
tk = TokenKind.INTLITERAL; |
|
342 |
} |
|
343 |
} |
|
344 |
} |
|
345 |
||
346 |
/** Read an identifier. |
|
347 |
*/ |
|
348 |
private void scanIdent() { |
|
349 |
boolean isJavaIdentifierPart; |
|
350 |
char high; |
|
351 |
do { |
|
10948 | 352 |
reader.putChar(true); |
10815 | 353 |
switch (reader.ch) { |
354 |
case 'A': case 'B': case 'C': case 'D': case 'E': |
|
355 |
case 'F': case 'G': case 'H': case 'I': case 'J': |
|
356 |
case 'K': case 'L': case 'M': case 'N': case 'O': |
|
357 |
case 'P': case 'Q': case 'R': case 'S': case 'T': |
|
358 |
case 'U': case 'V': case 'W': case 'X': case 'Y': |
|
359 |
case 'Z': |
|
360 |
case 'a': case 'b': case 'c': case 'd': case 'e': |
|
361 |
case 'f': case 'g': case 'h': case 'i': case 'j': |
|
362 |
case 'k': case 'l': case 'm': case 'n': case 'o': |
|
363 |
case 'p': case 'q': case 'r': case 's': case 't': |
|
364 |
case 'u': case 'v': case 'w': case 'x': case 'y': |
|
365 |
case 'z': |
|
366 |
case '$': case '_': |
|
367 |
case '0': case '1': case '2': case '3': case '4': |
|
368 |
case '5': case '6': case '7': case '8': case '9': |
|
369 |
case '\u0000': case '\u0001': case '\u0002': case '\u0003': |
|
370 |
case '\u0004': case '\u0005': case '\u0006': case '\u0007': |
|
371 |
case '\u0008': case '\u000E': case '\u000F': case '\u0010': |
|
372 |
case '\u0011': case '\u0012': case '\u0013': case '\u0014': |
|
373 |
case '\u0015': case '\u0016': case '\u0017': |
|
374 |
case '\u0018': case '\u0019': case '\u001B': |
|
375 |
case '\u007F': |
|
376 |
break; |
|
377 |
case '\u001A': // EOI is also a legal identifier part |
|
378 |
if (reader.bp >= reader.buflen) { |
|
10948 | 379 |
name = reader.name(); |
10815 | 380 |
tk = tokens.lookupKind(name); |
381 |
return; |
|
382 |
} |
|
383 |
break; |
|
384 |
default: |
|
385 |
if (reader.ch < '\u0080') { |
|
386 |
// all ASCII range chars already handled, above |
|
387 |
isJavaIdentifierPart = false; |
|
388 |
} else { |
|
389 |
high = reader.scanSurrogates(); |
|
390 |
if (high != 0) { |
|
10948 | 391 |
reader.putChar(high); |
10815 | 392 |
isJavaIdentifierPart = Character.isJavaIdentifierPart( |
393 |
Character.toCodePoint(high, reader.ch)); |
|
394 |
} else { |
|
395 |
isJavaIdentifierPart = Character.isJavaIdentifierPart(reader.ch); |
|
396 |
} |
|
397 |
} |
|
398 |
if (!isJavaIdentifierPart) { |
|
10948 | 399 |
name = reader.name(); |
10815 | 400 |
tk = tokens.lookupKind(name); |
401 |
return; |
|
402 |
} |
|
403 |
} |
|
404 |
} while (true); |
|
405 |
} |
|
406 |
||
407 |
/** Return true if reader.ch can be part of an operator. |
|
408 |
*/ |
|
409 |
private boolean isSpecial(char ch) { |
|
410 |
switch (ch) { |
|
411 |
case '!': case '%': case '&': case '*': case '?': |
|
412 |
case '+': case '-': case ':': case '<': case '=': |
|
413 |
case '>': case '^': case '|': case '~': |
|
414 |
case '@': |
|
415 |
return true; |
|
416 |
default: |
|
417 |
return false; |
|
418 |
} |
|
419 |
} |
|
420 |
||
421 |
/** Read longest possible sequence of special characters and convert |
|
422 |
* to token. |
|
423 |
*/ |
|
424 |
private void scanOperator() { |
|
425 |
while (true) { |
|
10948 | 426 |
reader.putChar(false); |
427 |
Name newname = reader.name(); |
|
10815 | 428 |
TokenKind tk1 = tokens.lookupKind(newname); |
429 |
if (tk1 == TokenKind.IDENTIFIER) { |
|
10948 | 430 |
reader.sp--; |
10815 | 431 |
break; |
432 |
} |
|
433 |
tk = tk1; |
|
434 |
reader.scanChar(); |
|
435 |
if (!isSpecial(reader.ch)) break; |
|
436 |
} |
|
437 |
} |
|
438 |
||
439 |
/** Read token. |
|
440 |
*/ |
|
441 |
public Token readToken() { |
|
442 |
||
10948 | 443 |
reader.sp = 0; |
10815 | 444 |
name = null; |
445 |
radix = 0; |
|
10948 | 446 |
|
10815 | 447 |
int pos = 0; |
448 |
int endPos = 0; |
|
10948 | 449 |
List<Comment> comments = null; |
10815 | 450 |
|
451 |
try { |
|
452 |
loop: while (true) { |
|
453 |
pos = reader.bp; |
|
454 |
switch (reader.ch) { |
|
455 |
case ' ': // (Spec 3.6) |
|
456 |
case '\t': // (Spec 3.6) |
|
457 |
case FF: // (Spec 3.6) |
|
458 |
do { |
|
459 |
reader.scanChar(); |
|
460 |
} while (reader.ch == ' ' || reader.ch == '\t' || reader.ch == FF); |
|
461 |
processWhiteSpace(pos, reader.bp); |
|
462 |
break; |
|
463 |
case LF: // (Spec 3.4) |
|
464 |
reader.scanChar(); |
|
465 |
processLineTerminator(pos, reader.bp); |
|
466 |
break; |
|
467 |
case CR: // (Spec 3.4) |
|
468 |
reader.scanChar(); |
|
469 |
if (reader.ch == LF) { |
|
470 |
reader.scanChar(); |
|
471 |
} |
|
472 |
processLineTerminator(pos, reader.bp); |
|
473 |
break; |
|
474 |
case 'A': case 'B': case 'C': case 'D': case 'E': |
|
475 |
case 'F': case 'G': case 'H': case 'I': case 'J': |
|
476 |
case 'K': case 'L': case 'M': case 'N': case 'O': |
|
477 |
case 'P': case 'Q': case 'R': case 'S': case 'T': |
|
478 |
case 'U': case 'V': case 'W': case 'X': case 'Y': |
|
479 |
case 'Z': |
|
480 |
case 'a': case 'b': case 'c': case 'd': case 'e': |
|
481 |
case 'f': case 'g': case 'h': case 'i': case 'j': |
|
482 |
case 'k': case 'l': case 'm': case 'n': case 'o': |
|
483 |
case 'p': case 'q': case 'r': case 's': case 't': |
|
484 |
case 'u': case 'v': case 'w': case 'x': case 'y': |
|
485 |
case 'z': |
|
486 |
case '$': case '_': |
|
487 |
scanIdent(); |
|
488 |
break loop; |
|
489 |
case '0': |
|
490 |
reader.scanChar(); |
|
491 |
if (reader.ch == 'x' || reader.ch == 'X') { |
|
492 |
reader.scanChar(); |
|
493 |
skipIllegalUnderscores(); |
|
494 |
if (reader.ch == '.') { |
|
495 |
scanHexFractionAndSuffix(pos, false); |
|
496 |
} else if (reader.digit(pos, 16) < 0) { |
|
497 |
lexError(pos, "invalid.hex.number"); |
|
498 |
} else { |
|
499 |
scanNumber(pos, 16); |
|
500 |
} |
|
501 |
} else if (reader.ch == 'b' || reader.ch == 'B') { |
|
502 |
if (!allowBinaryLiterals) { |
|
503 |
lexError(pos, "unsupported.binary.lit", source.name); |
|
504 |
allowBinaryLiterals = true; |
|
505 |
} |
|
506 |
reader.scanChar(); |
|
507 |
skipIllegalUnderscores(); |
|
508 |
if (reader.digit(pos, 2) < 0) { |
|
509 |
lexError(pos, "invalid.binary.number"); |
|
510 |
} else { |
|
511 |
scanNumber(pos, 2); |
|
512 |
} |
|
513 |
} else { |
|
10948 | 514 |
reader.putChar('0'); |
10815 | 515 |
if (reader.ch == '_') { |
516 |
int savePos = reader.bp; |
|
517 |
do { |
|
518 |
reader.scanChar(); |
|
519 |
} while (reader.ch == '_'); |
|
520 |
if (reader.digit(pos, 10) < 0) { |
|
521 |
lexError(savePos, "illegal.underscore"); |
|
522 |
} |
|
523 |
} |
|
524 |
scanNumber(pos, 8); |
|
525 |
} |
|
526 |
break loop; |
|
527 |
case '1': case '2': case '3': case '4': |
|
528 |
case '5': case '6': case '7': case '8': case '9': |
|
529 |
scanNumber(pos, 10); |
|
530 |
break loop; |
|
531 |
case '.': |
|
532 |
reader.scanChar(); |
|
533 |
if ('0' <= reader.ch && reader.ch <= '9') { |
|
10948 | 534 |
reader.putChar('.'); |
10815 | 535 |
scanFractionAndSuffix(pos); |
536 |
} else if (reader.ch == '.') { |
|
11383 | 537 |
int savePos = reader.bp; |
10948 | 538 |
reader.putChar('.'); reader.putChar('.', true); |
10815 | 539 |
if (reader.ch == '.') { |
540 |
reader.scanChar(); |
|
10948 | 541 |
reader.putChar('.'); |
10815 | 542 |
tk = TokenKind.ELLIPSIS; |
543 |
} else { |
|
11383 | 544 |
lexError(savePos, "illegal.dot"); |
10815 | 545 |
} |
546 |
} else { |
|
547 |
tk = TokenKind.DOT; |
|
548 |
} |
|
549 |
break loop; |
|
550 |
case ',': |
|
551 |
reader.scanChar(); tk = TokenKind.COMMA; break loop; |
|
552 |
case ';': |
|
553 |
reader.scanChar(); tk = TokenKind.SEMI; break loop; |
|
554 |
case '(': |
|
555 |
reader.scanChar(); tk = TokenKind.LPAREN; break loop; |
|
556 |
case ')': |
|
557 |
reader.scanChar(); tk = TokenKind.RPAREN; break loop; |
|
558 |
case '[': |
|
559 |
reader.scanChar(); tk = TokenKind.LBRACKET; break loop; |
|
560 |
case ']': |
|
561 |
reader.scanChar(); tk = TokenKind.RBRACKET; break loop; |
|
562 |
case '{': |
|
563 |
reader.scanChar(); tk = TokenKind.LBRACE; break loop; |
|
564 |
case '}': |
|
565 |
reader.scanChar(); tk = TokenKind.RBRACE; break loop; |
|
566 |
case '/': |
|
567 |
reader.scanChar(); |
|
568 |
if (reader.ch == '/') { |
|
569 |
do { |
|
10948 | 570 |
reader.scanCommentChar(); |
10815 | 571 |
} while (reader.ch != CR && reader.ch != LF && reader.bp < reader.buflen); |
572 |
if (reader.bp < reader.buflen) { |
|
13077 | 573 |
comments = addComment(comments, processComment(pos, reader.bp, CommentStyle.LINE)); |
10815 | 574 |
} |
575 |
break; |
|
576 |
} else if (reader.ch == '*') { |
|
10948 | 577 |
boolean isEmpty = false; |
10815 | 578 |
reader.scanChar(); |
579 |
CommentStyle style; |
|
580 |
if (reader.ch == '*') { |
|
581 |
style = CommentStyle.JAVADOC; |
|
10948 | 582 |
reader.scanCommentChar(); |
583 |
if (reader.ch == '/') { |
|
584 |
isEmpty = true; |
|
585 |
} |
|
10815 | 586 |
} else { |
587 |
style = CommentStyle.BLOCK; |
|
10948 | 588 |
} |
589 |
while (!isEmpty && reader.bp < reader.buflen) { |
|
590 |
if (reader.ch == '*') { |
|
591 |
reader.scanChar(); |
|
592 |
if (reader.ch == '/') break; |
|
593 |
} else { |
|
594 |
reader.scanCommentChar(); |
|
10815 | 595 |
} |
596 |
} |
|
597 |
if (reader.ch == '/') { |
|
598 |
reader.scanChar(); |
|
13077 | 599 |
comments = addComment(comments, processComment(pos, reader.bp, style)); |
10815 | 600 |
break; |
601 |
} else { |
|
602 |
lexError(pos, "unclosed.comment"); |
|
603 |
break loop; |
|
604 |
} |
|
605 |
} else if (reader.ch == '=') { |
|
606 |
tk = TokenKind.SLASHEQ; |
|
607 |
reader.scanChar(); |
|
608 |
} else { |
|
609 |
tk = TokenKind.SLASH; |
|
610 |
} |
|
611 |
break loop; |
|
612 |
case '\'': |
|
613 |
reader.scanChar(); |
|
614 |
if (reader.ch == '\'') { |
|
615 |
lexError(pos, "empty.char.lit"); |
|
616 |
} else { |
|
617 |
if (reader.ch == CR || reader.ch == LF) |
|
618 |
lexError(pos, "illegal.line.end.in.char.lit"); |
|
619 |
scanLitChar(pos); |
|
620 |
char ch2 = reader.ch; |
|
621 |
if (reader.ch == '\'') { |
|
622 |
reader.scanChar(); |
|
623 |
tk = TokenKind.CHARLITERAL; |
|
624 |
} else { |
|
625 |
lexError(pos, "unclosed.char.lit"); |
|
626 |
} |
|
627 |
} |
|
628 |
break loop; |
|
629 |
case '\"': |
|
630 |
reader.scanChar(); |
|
631 |
while (reader.ch != '\"' && reader.ch != CR && reader.ch != LF && reader.bp < reader.buflen) |
|
632 |
scanLitChar(pos); |
|
633 |
if (reader.ch == '\"') { |
|
634 |
tk = TokenKind.STRINGLITERAL; |
|
635 |
reader.scanChar(); |
|
636 |
} else { |
|
637 |
lexError(pos, "unclosed.str.lit"); |
|
638 |
} |
|
639 |
break loop; |
|
640 |
default: |
|
641 |
if (isSpecial(reader.ch)) { |
|
642 |
scanOperator(); |
|
643 |
} else { |
|
644 |
boolean isJavaIdentifierStart; |
|
645 |
if (reader.ch < '\u0080') { |
|
646 |
// all ASCII range chars already handled, above |
|
647 |
isJavaIdentifierStart = false; |
|
648 |
} else { |
|
649 |
char high = reader.scanSurrogates(); |
|
650 |
if (high != 0) { |
|
10948 | 651 |
reader.putChar(high); |
10815 | 652 |
|
653 |
isJavaIdentifierStart = Character.isJavaIdentifierStart( |
|
654 |
Character.toCodePoint(high, reader.ch)); |
|
655 |
} else { |
|
656 |
isJavaIdentifierStart = Character.isJavaIdentifierStart(reader.ch); |
|
657 |
} |
|
658 |
} |
|
659 |
if (isJavaIdentifierStart) { |
|
660 |
scanIdent(); |
|
661 |
} else if (reader.bp == reader.buflen || reader.ch == EOI && reader.bp + 1 == reader.buflen) { // JLS 3.5 |
|
662 |
tk = TokenKind.EOF; |
|
663 |
pos = reader.buflen; |
|
664 |
} else { |
|
14370
eefd0e6642a8
8000483: cryptic error message when source file contains hash
vromero
parents:
14259
diff
changeset
|
665 |
String arg = (32 < reader.ch && reader.ch < 127) ? |
eefd0e6642a8
8000483: cryptic error message when source file contains hash
vromero
parents:
14259
diff
changeset
|
666 |
String.format("%s", reader.ch) : |
eefd0e6642a8
8000483: cryptic error message when source file contains hash
vromero
parents:
14259
diff
changeset
|
667 |
String.format("\\u%04x", (int)reader.ch); |
eefd0e6642a8
8000483: cryptic error message when source file contains hash
vromero
parents:
14259
diff
changeset
|
668 |
lexError(pos, "illegal.char", arg); |
10815 | 669 |
reader.scanChar(); |
670 |
} |
|
671 |
} |
|
672 |
break loop; |
|
673 |
} |
|
674 |
} |
|
675 |
endPos = reader.bp; |
|
676 |
switch (tk.tag) { |
|
10948 | 677 |
case DEFAULT: return new Token(tk, pos, endPos, comments); |
678 |
case NAMED: return new NamedToken(tk, pos, endPos, name, comments); |
|
679 |
case STRING: return new StringToken(tk, pos, endPos, reader.chars(), comments); |
|
680 |
case NUMERIC: return new NumericToken(tk, pos, endPos, reader.chars(), radix, comments); |
|
10815 | 681 |
default: throw new AssertionError(); |
682 |
} |
|
683 |
} |
|
684 |
finally { |
|
685 |
if (scannerDebug) { |
|
686 |
System.out.println("nextToken(" + pos |
|
687 |
+ "," + endPos + ")=|" + |
|
688 |
new String(reader.getRawCharacters(pos, endPos)) |
|
689 |
+ "|"); |
|
690 |
} |
|
691 |
} |
|
692 |
} |
|
10948 | 693 |
//where |
13077 | 694 |
List<Comment> addComment(List<Comment> comments, Comment comment) { |
695 |
return comments == null ? |
|
696 |
List.of(comment) : |
|
697 |
comments.prepend(comment); |
|
10948 | 698 |
} |
10815 | 699 |
|
700 |
/** Return the position where a lexical error occurred; |
|
701 |
*/ |
|
702 |
public int errPos() { |
|
703 |
return errPos; |
|
704 |
} |
|
705 |
||
706 |
/** Set the position where a lexical error occurred; |
|
707 |
*/ |
|
708 |
public void errPos(int pos) { |
|
709 |
errPos = pos; |
|
710 |
} |
|
711 |
||
712 |
/** |
|
713 |
* Called when a complete comment has been scanned. pos and endPos |
|
714 |
* will mark the comment boundary. |
|
715 |
*/ |
|
10948 | 716 |
protected Tokens.Comment processComment(int pos, int endPos, CommentStyle style) { |
10815 | 717 |
if (scannerDebug) |
718 |
System.out.println("processComment(" + pos |
|
719 |
+ "," + endPos + "," + style + ")=|" |
|
720 |
+ new String(reader.getRawCharacters(pos, endPos)) |
|
721 |
+ "|"); |
|
10948 | 722 |
char[] buf = reader.getRawCharacters(pos, endPos); |
723 |
return new BasicComment<UnicodeReader>(new UnicodeReader(fac, buf, buf.length), style); |
|
10815 | 724 |
} |
725 |
||
726 |
/** |
|
727 |
* Called when a complete whitespace run has been scanned. pos and endPos |
|
728 |
* will mark the whitespace boundary. |
|
729 |
*/ |
|
730 |
protected void processWhiteSpace(int pos, int endPos) { |
|
731 |
if (scannerDebug) |
|
732 |
System.out.println("processWhitespace(" + pos |
|
733 |
+ "," + endPos + ")=|" + |
|
734 |
new String(reader.getRawCharacters(pos, endPos)) |
|
735 |
+ "|"); |
|
736 |
} |
|
737 |
||
738 |
/** |
|
739 |
* Called when a line terminator has been processed. |
|
740 |
*/ |
|
741 |
protected void processLineTerminator(int pos, int endPos) { |
|
742 |
if (scannerDebug) |
|
743 |
System.out.println("processTerminator(" + pos |
|
744 |
+ "," + endPos + ")=|" + |
|
745 |
new String(reader.getRawCharacters(pos, endPos)) |
|
746 |
+ "|"); |
|
747 |
} |
|
748 |
||
749 |
/** Build a map for translating between line numbers and |
|
750 |
* positions in the input. |
|
751 |
* |
|
752 |
* @return a LineMap */ |
|
753 |
public Position.LineMap getLineMap() { |
|
754 |
return Position.makeLineMap(reader.getRawCharacters(), reader.buflen, false); |
|
755 |
} |
|
10948 | 756 |
|
757 |
||
758 |
/** |
|
759 |
* Scan a documentation comment; determine if a deprecated tag is present. |
|
760 |
* Called once the initial /, * have been skipped, positioned at the second * |
|
761 |
* (which is treated as the beginning of the first line). |
|
762 |
* Stops positioned at the closing '/'. |
|
763 |
*/ |
|
764 |
protected class BasicComment<U extends UnicodeReader> implements Comment { |
|
765 |
||
766 |
CommentStyle cs; |
|
767 |
U comment_reader; |
|
768 |
||
769 |
protected boolean deprecatedFlag = false; |
|
770 |
protected boolean scanned = false; |
|
771 |
||
772 |
protected BasicComment(U comment_reader, CommentStyle cs) { |
|
773 |
this.comment_reader = comment_reader; |
|
774 |
this.cs = cs; |
|
775 |
} |
|
776 |
||
777 |
public String getText() { |
|
778 |
return null; |
|
779 |
} |
|
780 |
||
13078
11d5e1c88864
7178297: provide mapping from doc comment position to source file position
jjg
parents:
13077
diff
changeset
|
781 |
public int getSourcePos(int pos) { |
11d5e1c88864
7178297: provide mapping from doc comment position to source file position
jjg
parents:
13077
diff
changeset
|
782 |
return -1; |
11d5e1c88864
7178297: provide mapping from doc comment position to source file position
jjg
parents:
13077
diff
changeset
|
783 |
} |
11d5e1c88864
7178297: provide mapping from doc comment position to source file position
jjg
parents:
13077
diff
changeset
|
784 |
|
10948 | 785 |
public CommentStyle getStyle() { |
786 |
return cs; |
|
787 |
} |
|
788 |
||
789 |
public boolean isDeprecated() { |
|
790 |
if (!scanned && cs == CommentStyle.JAVADOC) { |
|
791 |
scanDocComment(); |
|
792 |
} |
|
793 |
return deprecatedFlag; |
|
794 |
} |
|
795 |
||
796 |
@SuppressWarnings("fallthrough") |
|
797 |
protected void scanDocComment() { |
|
798 |
try { |
|
799 |
boolean deprecatedPrefix = false; |
|
800 |
||
801 |
comment_reader.bp += 3; // '/**' |
|
802 |
comment_reader.ch = comment_reader.buf[comment_reader.bp]; |
|
803 |
||
804 |
forEachLine: |
|
805 |
while (comment_reader.bp < comment_reader.buflen) { |
|
806 |
||
807 |
// Skip optional WhiteSpace at beginning of line |
|
808 |
while (comment_reader.bp < comment_reader.buflen && (comment_reader.ch == ' ' || comment_reader.ch == '\t' || comment_reader.ch == FF)) { |
|
809 |
comment_reader.scanCommentChar(); |
|
810 |
} |
|
811 |
||
812 |
// Skip optional consecutive Stars |
|
813 |
while (comment_reader.bp < comment_reader.buflen && comment_reader.ch == '*') { |
|
814 |
comment_reader.scanCommentChar(); |
|
815 |
if (comment_reader.ch == '/') { |
|
816 |
return; |
|
817 |
} |
|
818 |
} |
|
819 |
||
820 |
// Skip optional WhiteSpace after Stars |
|
821 |
while (comment_reader.bp < comment_reader.buflen && (comment_reader.ch == ' ' || comment_reader.ch == '\t' || comment_reader.ch == FF)) { |
|
822 |
comment_reader.scanCommentChar(); |
|
823 |
} |
|
824 |
||
825 |
deprecatedPrefix = false; |
|
826 |
// At beginning of line in the JavaDoc sense. |
|
827 |
if (!deprecatedFlag) { |
|
828 |
String deprecated = "@deprecated"; |
|
829 |
int i = 0; |
|
830 |
while (comment_reader.bp < comment_reader.buflen && comment_reader.ch == deprecated.charAt(i)) { |
|
831 |
comment_reader.scanCommentChar(); |
|
832 |
i++; |
|
833 |
if (i == deprecated.length()) { |
|
834 |
deprecatedPrefix = true; |
|
835 |
break; |
|
836 |
} |
|
837 |
} |
|
838 |
} |
|
839 |
||
840 |
if (deprecatedPrefix && comment_reader.bp < comment_reader.buflen) { |
|
841 |
if (Character.isWhitespace(comment_reader.ch)) { |
|
842 |
deprecatedFlag = true; |
|
843 |
} else if (comment_reader.ch == '*') { |
|
844 |
comment_reader.scanCommentChar(); |
|
845 |
if (comment_reader.ch == '/') { |
|
846 |
deprecatedFlag = true; |
|
847 |
return; |
|
848 |
} |
|
849 |
} |
|
850 |
} |
|
851 |
||
852 |
// Skip rest of line |
|
853 |
while (comment_reader.bp < comment_reader.buflen) { |
|
854 |
switch (comment_reader.ch) { |
|
855 |
case '*': |
|
856 |
comment_reader.scanCommentChar(); |
|
857 |
if (comment_reader.ch == '/') { |
|
858 |
return; |
|
859 |
} |
|
860 |
break; |
|
861 |
case CR: // (Spec 3.4) |
|
862 |
comment_reader.scanCommentChar(); |
|
863 |
if (comment_reader.ch != LF) { |
|
864 |
continue forEachLine; |
|
865 |
} |
|
866 |
/* fall through to LF case */ |
|
867 |
case LF: // (Spec 3.4) |
|
868 |
comment_reader.scanCommentChar(); |
|
869 |
continue forEachLine; |
|
870 |
default: |
|
871 |
comment_reader.scanCommentChar(); |
|
872 |
} |
|
873 |
} // rest of line |
|
874 |
} // forEachLine |
|
875 |
return; |
|
876 |
} finally { |
|
877 |
scanned = true; |
|
878 |
} |
|
879 |
} |
|
880 |
} |
|
10815 | 881 |
} |