Debian Codegraph @

rhino/src/org/mozilla/javascript/TokenStream.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 package org.mozilla.javascript;
8
9 import java.io.*;
10
11 /**
12 * This class implements the JavaScript scanner.
13 *
14 * It is based on the C source files jsscan.c and jsscan.h
15 * in the jsref package.
16 *
17 * @see org.mozilla.javascript.Parser
18 *
19 * @author Mike McCabe
20 * @author Brendan Eich
21 */
22
23 class TokenStream
24 {
25 /*
26 * For chars - because we need something out-of-range
27 * to check. (And checking EOF by exception is annoying.)
28 * Note distinction from EOF token type!
29 */
30 private final static int
31 EOF_CHAR = -1;
32
33 private final static char BYTE_ORDER_MARK = '\uFEFF';
34
35 TokenStream(Parser parser, Reader sourceReader, String sourceString,
36 int lineno)
37 {
38 this.parser = parser;
39 this.lineno = lineno;
40 if (sourceReader != null) {
41 if (sourceString != null) Kit.codeBug();
42 this.sourceReader = sourceReader;
43 this.sourceBuffer = new char[512];
44 this.sourceEnd = 0;
45 } else {
46 if (sourceString == null) Kit.codeBug();
47 this.sourceString = sourceString;
48 this.sourceEnd = sourceString.length();
49 }
50 this.sourceCursor = this.cursor = 0;
51 }
52
53 /* This function uses the cached op, string and number fields in
54 * TokenStream; if getToken has been called since the passed token
55 * was scanned, the op or string printed may be incorrect.
56 */
57 String tokenToString(int token)
58 {
59 if (Token.printTrees) {
60 String name = Token.name(token);
61
62 switch (token) {
63 case Token.STRING:
64 case Token.REGEXP:
65 case Token.NAME:
66 return name + " `" + this.string + "'";
67
68 case Token.NUMBER:
69 return "NUMBER " + this.number;
70 }
71
72 return name;
73 }
74 return "";
75 }
76
77 static boolean isKeyword(String s, int version, boolean isStrict)
78 {
79 return Token.EOF != stringToKeyword(s, version, isStrict);
80 }
81
82 private static int stringToKeyword(String name, int version, boolean isStrict)
83 {
84 if (version < Context.VERSION_ES6) {
85 return stringToKeywordForJS(name);
86 } else {
87 return stringToKeywordForES(name, isStrict);
88 }
89 }
90
91 /**
92 * JavaScript 1.8 and earlier
93 */
94 private static int stringToKeywordForJS(String name)
95 {
96 // #string_id_map#
97 // The following assumes that Token.EOF == 0
98 final int
99 Id_break = Token.BREAK,
100 Id_case = Token.CASE,
101 Id_continue = Token.CONTINUE,
102 Id_default = Token.DEFAULT,
103 Id_delete = Token.DELPROP,
104 Id_do = Token.DO,
105 Id_else = Token.ELSE,
106 Id_export = Token.RESERVED,
107 Id_false = Token.FALSE,
108 Id_for = Token.FOR,
109 Id_function = Token.FUNCTION,
110 Id_if = Token.IF,
111 Id_in = Token.IN,
112 Id_let = Token.LET, // reserved ES5 strict
113 Id_new = Token.NEW,
114 Id_null = Token.NULL,
115 Id_return = Token.RETURN,
116 Id_switch = Token.SWITCH,
117 Id_this = Token.THIS,
118 Id_true = Token.TRUE,
119 Id_typeof = Token.TYPEOF,
120 Id_var = Token.VAR,
121 Id_void = Token.VOID,
122 Id_while = Token.WHILE,
123 Id_with = Token.WITH,
124 Id_yield = Token.YIELD, // reserved ES5 strict
125
126 // the following are #ifdef RESERVE_JAVA_KEYWORDS in jsscan.c
127 Id_abstract = Token.RESERVED, // ES3 only
128 Id_boolean = Token.RESERVED, // ES3 only
129 Id_byte = Token.RESERVED, // ES3 only
130 Id_catch = Token.CATCH,
131 Id_char = Token.RESERVED, // ES3 only
132 Id_class = Token.RESERVED,
133 Id_const = Token.CONST, // reserved
134 Id_debugger = Token.DEBUGGER,
135 Id_double = Token.RESERVED, // ES3 only
136 Id_enum = Token.RESERVED,
137 Id_extends = Token.RESERVED,
138 Id_final = Token.RESERVED, // ES3 only
139 Id_finally = Token.FINALLY,
140 Id_float = Token.RESERVED, // ES3 only
141 Id_goto = Token.RESERVED, // ES3 only
142 Id_implements = Token.RESERVED, // ES3, ES5 strict
143 Id_import = Token.RESERVED,
144 Id_instanceof = Token.INSTANCEOF,
145 Id_int = Token.RESERVED, // ES3
146 Id_interface = Token.RESERVED, // ES3, ES5 strict
147 Id_long = Token.RESERVED, // ES3 only
148 Id_native = Token.RESERVED, // ES3 only
149 Id_package = Token.RESERVED, // ES3, ES5 strict
150 Id_private = Token.RESERVED, // ES3, ES5 strict
151 Id_protected = Token.RESERVED, // ES3, ES5 strict
152 Id_public = Token.RESERVED, // ES3, ES5 strict
153 Id_short = Token.RESERVED, // ES3 only
154 Id_static = Token.RESERVED, // ES3, ES5 strict
155 Id_super = Token.RESERVED,
156 Id_synchronized = Token.RESERVED, // ES3 only
157 Id_throw = Token.THROW,
158 Id_throws = Token.RESERVED, // ES3 only
159 Id_transient = Token.RESERVED, // ES3 only
160 Id_try = Token.TRY,
161 Id_volatile = Token.RESERVED; // ES3 only
162
163 int id;
164 String s = name;
165 // #generated# Last update: 2007-04-18 13:53:30 PDT
166 L0: { id = 0; String X = null; int c;
167 L: switch (s.length()) {
168 case 2: c=s.charAt(1);
169 if (c=='f') { if (s.charAt(0)=='i') {id=Id_if; break L0;} }
170 else if (c=='n') { if (s.charAt(0)=='i') {id=Id_in; break L0;} }
171 else if (c=='o') { if (s.charAt(0)=='d') {id=Id_do; break L0;} }
172 break L;
173 case 3: switch (s.charAt(0)) {
174 case 'f': if (s.charAt(2)=='r' && s.charAt(1)=='o') {id=Id_for; break L0;} break L;
175 case 'i': if (s.charAt(2)=='t' && s.charAt(1)=='n') {id=Id_int; break L0;} break L;
176 case 'l': if (s.charAt(2)=='t' && s.charAt(1)=='e') {id=Id_let; break L0;} break L;
177 case 'n': if (s.charAt(2)=='w' && s.charAt(1)=='e') {id=Id_new; break L0;} break L;
178 case 't': if (s.charAt(2)=='y' && s.charAt(1)=='r') {id=Id_try; break L0;} break L;
179 case 'v': if (s.charAt(2)=='r' && s.charAt(1)=='a') {id=Id_var; break L0;} break L;
180 } break L;
181 case 4: switch (s.charAt(0)) {
182 case 'b': X="byte";id=Id_byte; break L;
183 case 'c': c=s.charAt(3);
184 if (c=='e') { if (s.charAt(2)=='s' && s.charAt(1)=='a') {id=Id_case; break L0;} }
185 else if (c=='r') { if (s.charAt(2)=='a' && s.charAt(1)=='h') {id=Id_char; break L0;} }
186 break L;
187 case 'e': c=s.charAt(3);
188 if (c=='e') { if (s.charAt(2)=='s' && s.charAt(1)=='l') {id=Id_else; break L0;} }
189 else if (c=='m') { if (s.charAt(2)=='u' && s.charAt(1)=='n') {id=Id_enum; break L0;} }
190 break L;
191 case 'g': X="goto";id=Id_goto; break L;
192 case 'l': X="long";id=Id_long; break L;
193 case 'n': X="null";id=Id_null; break L;
194 case 't': c=s.charAt(3);
195 if (c=='e') { if (s.charAt(2)=='u' && s.charAt(1)=='r') {id=Id_true; break L0;} }
196 else if (c=='s') { if (s.charAt(2)=='i' && s.charAt(1)=='h') {id=Id_this; break L0;} }
197 break L;
198 case 'v': X="void";id=Id_void; break L;
199 case 'w': X="with";id=Id_with; break L;
200 } break L;
201 case 5: switch (s.charAt(2)) {
202 case 'a': X="class";id=Id_class; break L;
203 case 'e': c=s.charAt(0);
204 if (c=='b') { X="break";id=Id_break; }
205 else if (c=='y') { X="yield";id=Id_yield; }
206 break L;
207 case 'i': X="while";id=Id_while; break L;
208 case 'l': X="false";id=Id_false; break L;
209 case 'n': c=s.charAt(0);
210 if (c=='c') { X="const";id=Id_const; }
211 else if (c=='f') { X="final";id=Id_final; }
212 break L;
213 case 'o': c=s.charAt(0);
214 if (c=='f') { X="float";id=Id_float; }
215 else if (c=='s') { X="short";id=Id_short; }
216 break L;
217 case 'p': X="super";id=Id_super; break L;
218 case 'r': X="throw";id=Id_throw; break L;
219 case 't': X="catch";id=Id_catch; break L;
220 } break L;
221 case 6: switch (s.charAt(1)) {
222 case 'a': X="native";id=Id_native; break L;
223 case 'e': c=s.charAt(0);
224 if (c=='d') { X="delete";id=Id_delete; }
225 else if (c=='r') { X="return";id=Id_return; }
226 break L;
227 case 'h': X="throws";id=Id_throws; break L;
228 case 'm': X="import";id=Id_import; break L;
229 case 'o': X="double";id=Id_double; break L;
230 case 't': X="static";id=Id_static; break L;
231 case 'u': X="public";id=Id_public; break L;
232 case 'w': X="switch";id=Id_switch; break L;
233 case 'x': X="export";id=Id_export; break L;
234 case 'y': X="typeof";id=Id_typeof; break L;
235 } break L;
236 case 7: switch (s.charAt(1)) {
237 case 'a': X="package";id=Id_package; break L;
238 case 'e': X="default";id=Id_default; break L;
239 case 'i': X="finally";id=Id_finally; break L;
240 case 'o': X="boolean";id=Id_boolean; break L;
241 case 'r': X="private";id=Id_private; break L;
242 case 'x': X="extends";id=Id_extends; break L;
243 } break L;
244 case 8: switch (s.charAt(0)) {
245 case 'a': X="abstract";id=Id_abstract; break L;
246 case 'c': X="continue";id=Id_continue; break L;
247 case 'd': X="debugger";id=Id_debugger; break L;
248 case 'f': X="function";id=Id_function; break L;
249 case 'v': X="volatile";id=Id_volatile; break L;
250 } break L;
251 case 9: c=s.charAt(0);
252 if (c=='i') { X="interface";id=Id_interface; }
253 else if (c=='p') { X="protected";id=Id_protected; }
254 else if (c=='t') { X="transient";id=Id_transient; }
255 break L;
256 case 10: c=s.charAt(1);
257 if (c=='m') { X="implements";id=Id_implements; }
258 else if (c=='n') { X="instanceof";id=Id_instanceof; }
259 break L;
260 case 12: X="synchronized";id=Id_synchronized; break L;
261 }
262 if (X!=null && X!=s && !X.equals(s)) id = 0;
263 }
264 // #/generated#
265 // #/string_id_map#
266 if (id == 0) { return Token.EOF; }
267 return id & 0xff;
268 }
269
270 /**
271 * ECMAScript 6.
272 */
273 private static int stringToKeywordForES(String name, boolean isStrict)
274 {
275 // #string_id_map#
276 // The following assumes that Token.EOF == 0
277 final int
278 // 11.6.2.1 Keywords (ECMAScript2015)
279 Id_break = Token.BREAK,
280 Id_case = Token.CASE,
281 Id_catch = Token.CATCH,
282 Id_class = Token.RESERVED,
283 Id_const = Token.CONST,
284 Id_continue = Token.CONTINUE,
285 Id_debugger = Token.DEBUGGER,
286 Id_default = Token.DEFAULT,
287 Id_delete = Token.DELPROP,
288 Id_do = Token.DO,
289 Id_else = Token.ELSE,
290 Id_export = Token.RESERVED,
291 Id_extends = Token.RESERVED,
292 Id_finally = Token.FINALLY,
293 Id_for = Token.FOR,
294 Id_function = Token.FUNCTION,
295 Id_if = Token.IF,
296 Id_import = Token.RESERVED,
297 Id_in = Token.IN,
298 Id_instanceof = Token.INSTANCEOF,
299 Id_new = Token.NEW,
300 Id_return = Token.RETURN,
301 Id_super = Token.RESERVED,
302 Id_switch = Token.SWITCH,
303 Id_this = Token.THIS,
304 Id_throw = Token.THROW,
305 Id_try = Token.TRY,
306 Id_typeof = Token.TYPEOF,
307 Id_var = Token.VAR,
308 Id_void = Token.VOID,
309 Id_while = Token.WHILE,
310 Id_with = Token.WITH,
311 Id_yield = Token.YIELD,
312
313 // 11.6.2.2 Future Reserved Words
314 Id_await = Token.RESERVED,
315 Id_enum = Token.RESERVED,
316
317 // 11.6.2.2 NOTE Strict Future Reserved Words
318 Id_implements = Token.RESERVED,
319 Id_interface = Token.RESERVED,
320 Id_package = Token.RESERVED,
321 Id_private = Token.RESERVED,
322 Id_protected = Token.RESERVED,
323 Id_public = Token.RESERVED,
324
325 // 11.8 Literals
326 Id_false = Token.FALSE,
327 Id_null = Token.NULL,
328 Id_true = Token.TRUE,
329
330 // Non ReservedWord, but Non IdentifierName in strict mode code.
331 // 12.1.1 Static Semantics: Early Errors
332 Id_let = Token.LET, // TODO : Valid IdentifierName in non-strict mode.
333 Id_static = Token.RESERVED;
334
335 int id;
336 String s = name;
337 // #generated# Last update: 2007-04-18 13:53:30 PDT
338 L0: { id = 0; String X = null; int c;
339 L: switch (s.length()) {
340 case 2: c=s.charAt(1);
341 if (c=='f') { if (s.charAt(0)=='i') {id=Id_if; break L0;} }
342 else if (c=='n') { if (s.charAt(0)=='i') {id=Id_in; break L0;} }
343 else if (c=='o') { if (s.charAt(0)=='d') {id=Id_do; break L0;} }
344 break L;
345 case 3: switch (s.charAt(0)) {
346 case 'f': if (s.charAt(2)=='r' && s.charAt(1)=='o') {id=Id_for; break L0;} break L;
347 case 'l': if (s.charAt(2)=='t' && s.charAt(1)=='e') {id=Id_let; break L0;} break L;
348 case 'n': if (s.charAt(2)=='w' && s.charAt(1)=='e') {id=Id_new; break L0;} break L;
349 case 't': if (s.charAt(2)=='y' && s.charAt(1)=='r') {id=Id_try; break L0;} break L;
350 case 'v': if (s.charAt(2)=='r' && s.charAt(1)=='a') {id=Id_var; break L0;} break L;
351 } break L;
352 case 4: switch (s.charAt(0)) {
353 case 'c': c=s.charAt(3);
354 if (c=='e') { if (s.charAt(2)=='s' && s.charAt(1)=='a') {id=Id_case; break L0;} }
355 break L;
356 case 'e': c=s.charAt(3);
357 if (c=='e') { if (s.charAt(2)=='s' && s.charAt(1)=='l') {id=Id_else; break L0;} }
358 else if (c=='m') { if (s.charAt(2)=='u' && s.charAt(1)=='n') {id=Id_enum; break L0;} }
359 break L;
360 case 'n': X="null";id=Id_null; break L;
361 case 't': c=s.charAt(3);
362 if (c=='e') { if (s.charAt(2)=='u' && s.charAt(1)=='r') {id=Id_true; break L0;} }
363 else if (c=='s') { if (s.charAt(2)=='i' && s.charAt(1)=='h') {id=Id_this; break L0;} }
364 break L;
365 case 'v': X="void";id=Id_void; break L;
366 case 'w': X="with";id=Id_with; break L;
367 } break L;
368 case 5: switch (s.charAt(2)) {
369 case 'a': c=s.charAt(0);
370 if (c=='c') { X="class";id=Id_class; }
371 else if (c=='a') { X="await";id=Id_await; }
372 break L;
373 case 'e': c=s.charAt(0);
374 if (c=='b') { X="break";id=Id_break; }
375 else if (c=='y') { X="yield";id=Id_yield; }
376 break L;
377 case 'i': X="while";id=Id_while; break L;
378 case 'l': X="false";id=Id_false; break L;
379 case 'n': X="const";id=Id_const; break L;
380 case 'p': X="super";id=Id_super; break L;
381 case 'r': X="throw";id=Id_throw; break L;
382 case 't': X="catch";id=Id_catch; break L;
383 } break L;
384 case 6: switch (s.charAt(1)) {
385 case 'e': c=s.charAt(0);
386 if (c=='d') { X="delete";id=Id_delete; }
387 else if (c=='r') { X="return";id=Id_return; }
388 break L;
389 case 'm': X="import";id=Id_import; break L;
390 case 't': if (isStrict) { X="static";id=Id_static; break L; }
391 case 'u': if (isStrict) { X="public";id=Id_public; break L; }
392 case 'w': X="switch";id=Id_switch; break L;
393 case 'x': X="export";id=Id_export; break L;
394 case 'y': X="typeof";id=Id_typeof; break L;
395 } break L;
396 case 7: switch (s.charAt(1)) {
397 case 'a': if (isStrict) { X="package";id=Id_package; break L; }
398 case 'e': X="default";id=Id_default; break L;
399 case 'i': X="finally";id=Id_finally; break L;
400 case 'r': if (isStrict) { X="private";id=Id_private; break L; }
401 case 'x': X="extends";id=Id_extends; break L;
402 } break L;
403 case 8: switch (s.charAt(0)) {
404 case 'c': X="continue";id=Id_continue; break L;
405 case 'd': X="debugger";id=Id_debugger; break L;
406 case 'f': X="function";id=Id_function; break L;
407 } break L;
408 case 9: c=s.charAt(0);
409 if (c=='i' && isStrict) { X="interface";id=Id_interface; }
410 else if (c=='p' && isStrict) { X="protected";id=Id_protected; }
411 break L;
412 case 10: c=s.charAt(1);
413 if (c=='m' && isStrict) { X="implements";id=Id_implements; }
414 else if (c=='n') { X="instanceof";id=Id_instanceof; }
415 break L;
416 }
417 if (X!=null && X!=s && !X.equals(s)) id = 0;
418 }
419 // #/generated#
420 // #/string_id_map#
421 if (id == 0) { return Token.EOF; }
422 return id & 0xff;
423 }
424
425 final String getSourceString() { return sourceString; }
426
427 final int getLineno() { return lineno; }
428
429 final String getString() { return string; }
430
431 final char getQuoteChar() {
432 return (char) quoteChar;
433 }
434
435 final double getNumber() { return number; }
436 final boolean isNumberBinary() { return isBinary; }
437 final boolean isNumberOldOctal() { return isOldOctal; }
438 final boolean isNumberOctal() { return isOctal; }
439 final boolean isNumberHex() { return isHex; }
440
441 final boolean eof() { return hitEOF; }
442
443 final int getToken() throws IOException
444 {
445 int c;
446
447 retry:
448 for (;;) {
449 // Eat whitespace, possibly sensitive to newlines.
450 for (;;) {
451 c = getChar();
452 if (c == EOF_CHAR) {
453 tokenBeg = cursor - 1;
454 tokenEnd = cursor;
455 return Token.EOF;
456 } else if (c == '\n') {
457 dirtyLine = false;
458 tokenBeg = cursor - 1;
459 tokenEnd = cursor;
460 return Token.EOL;
461 } else if (!isJSSpace(c)) {
462 if (c != '-') {
463 dirtyLine = true;
464 }
465 break;
466 }
467 }
468
469 // Assume the token will be 1 char - fixed up below.
470 tokenBeg = cursor - 1;
471 tokenEnd = cursor;
472
473 if (c == '@') return Token.XMLATTR;
474
475 // identifier/keyword/instanceof?
476 // watch out for starting with a <backslash>
477 boolean identifierStart;
478 boolean isUnicodeEscapeStart = false;
479 if (c == '\\') {
480 c = getChar();
481 if (c == 'u') {
482 identifierStart = true;
483 isUnicodeEscapeStart = true;
484 stringBufferTop = 0;
485 } else {
486 identifierStart = false;
487 ungetChar(c);
488 c = '\\';
489 }
490 } else {
491 identifierStart = Character.isJavaIdentifierStart((char)c);
492 if (identifierStart) {
493 stringBufferTop = 0;
494 addToString(c);
495 }
496 }
497
498 if (identifierStart) {
499 boolean containsEscape = isUnicodeEscapeStart;
500 for (;;) {
501 if (isUnicodeEscapeStart) {
502 // strictly speaking we should probably push-back
503 // all the bad characters if the <backslash>uXXXX
504 // sequence is malformed. But since there isn't a
505 // correct context(is there?) for a bad Unicode
506 // escape sequence in an identifier, we can report
507 // an error here.
508 int escapeVal = 0;
509 for (int i = 0; i != 4; ++i) {
510 c = getChar();
511 escapeVal = Kit.xDigitToInt(c, escapeVal);
512 // Next check takes care about c < 0 and bad escape
513 if (escapeVal < 0) { break; }
514 }
515 if (escapeVal < 0) {
516 parser.addError("msg.invalid.escape");
517 return Token.ERROR;
518 }
519 addToString(escapeVal);
520 isUnicodeEscapeStart = false;
521 } else {
522 c = getChar();
523 if (c == '\\') {
524 c = getChar();
525 if (c == 'u') {
526 isUnicodeEscapeStart = true;
527 containsEscape = true;
528 } else {
529 parser.addError("msg.illegal.character");
530 return Token.ERROR;
531 }
532 } else {
533 if (c == EOF_CHAR || c == BYTE_ORDER_MARK
534 || !Character.isJavaIdentifierPart((char)c))
535 {
536 break;
537 }
538 addToString(c);
539 }
540 }
541 }
542 ungetChar(c);
543
544 String str = getStringFromBuffer();
545 if (!containsEscape) {
546 // OPT we shouldn't have to make a string (object!) to
547 // check if it's a keyword.
548
549 // Return the corresponding token if it's a keyword
550 int result = stringToKeyword(str, parser.compilerEnv.getLanguageVersion(), parser.inUseStrictDirective());
551 if (result != Token.EOF) {
552 if ((result == Token.LET || result == Token.YIELD) &&
553 parser.compilerEnv.getLanguageVersion()
554 < Context.VERSION_1_7)
555 {
556 // LET and YIELD are tokens only in 1.7 and later
557 string = result == Token.LET ? "let" : "yield";
558 result = Token.NAME;
559 }
560 // Save the string in case we need to use in
561 // object literal definitions.
562 this.string = (String)allStrings.intern(str);
563 if (result != Token.RESERVED) {
564 return result;
565 } else if (parser.compilerEnv.getLanguageVersion() >= Context.VERSION_ES6) {
566 return result;
567 } else if (!parser.compilerEnv.isReservedKeywordAsIdentifier()) {
568 return result;
569 }
570 }
571 } else if (isKeyword(str, parser.compilerEnv.getLanguageVersion(), parser.inUseStrictDirective())) {
572 // If a string contains unicodes, and converted to a keyword,
573 // we convert the last character back to unicode
574 str = convertLastCharToHex(str);
575 }
576 this.string = (String)allStrings.intern(str);
577 return Token.NAME;
578 }
579
580 // is it a number?
581 if (isDigit(c) || (c == '.' && isDigit(peekChar()))) {
582 stringBufferTop = 0;
583 int base = 10;
584 isHex = isOldOctal = isOctal = isBinary = false;
585 boolean es6 = parser.compilerEnv.getLanguageVersion() >= Context.VERSION_ES6;
586
587 if (c == '0') {
588 c = getChar();
589 if (c == 'x' || c == 'X') {
590 base = 16;
591 isHex = true;
592 c = getChar();
593 } else if (es6 && (c == 'o' || c == 'O')) {
594 base = 8;
595 isOctal = true;
596 c = getChar();
597 } else if (es6 && (c == 'b' || c == 'B')) {
598 base = 2;
599 isBinary = true;
600 c = getChar();
601 } else if (isDigit(c)) {
602 base = 8;
603 isOldOctal = true;
604 } else {
605 addToString('0');
606 }
607 }
608
609 boolean isEmpty = true;
610 if (base == 16) {
611 while (0 <= Kit.xDigitToInt(c, 0)) {
612 addToString(c);
613 c = getChar();
614 isEmpty = false;
615 }
616 } else {
617 while ('0' <= c && c <= '9') {
618 if (base == 8 && c >= '8') {
619 if (isOldOctal) {
620 /*
621 * We permit 08 and 09 as decimal numbers, which
622 * makes our behavior a superset of the ECMA
623 * numeric grammar. We might not always be so
624 * permissive, so we warn about it.
625 */
626 parser.addWarning("msg.bad.octal.literal",
627 c == '8' ? "8" : "9");
628 base = 10;
629 } else {
630 parser.addError("msg.caught.nfe");
631 return Token.ERROR;
632 }
633 } else if (base == 2 && c >= '2') {
634 parser.addError("msg.caught.nfe");
635 return Token.ERROR;
636 }
637 addToString(c);
638 c = getChar();
639 isEmpty = false;
640 }
641 }
642 if (isEmpty && (isBinary || isOctal || isHex)) {
643 parser.addError("msg.caught.nfe");
644 return Token.ERROR;
645 }
646
647 boolean isInteger = true;
648
649 if (base == 10 && (c == '.' || c == 'e' || c == 'E')) {
650 isInteger = false;
651 if (c == '.') {
652 do {
653 addToString(c);
654 c = getChar();
655 } while (isDigit(c));
656 }
657 if (c == 'e' || c == 'E') {
658 addToString(c);
659 c = getChar();
660 if (c == '+' || c == '-') {
661 addToString(c);
662 c = getChar();
663 }
664 if (!isDigit(c)) {
665 parser.addError("msg.missing.exponent");
666 return Token.ERROR;
667 }
668 do {
669 addToString(c);
670 c = getChar();
671 } while (isDigit(c));
672 }
673 }
674 ungetChar(c);
675 String numString = getStringFromBuffer();
676 this.string = numString;
677
678 double dval;
679 if (base == 10 && !isInteger) {
680 try {
681 // Use Java conversion to number from string...
682 dval = Double.parseDouble(numString);
683 }
684 catch (NumberFormatException ex) {
685 parser.addError("msg.caught.nfe");
686 return Token.ERROR;
687 }
688 } else {
689 dval = ScriptRuntime.stringToNumber(numString, 0, base);
690 }
691
692 this.number = dval;
693 return Token.NUMBER;
694 }
695
696 // is it a string?
697 if (c == '"' || c == '\'') {
698 // We attempt to accumulate a string the fast way, by
699 // building it directly out of the reader. But if there
700 // are any escaped characters in the string, we revert to
701 // building it out of a StringBuffer.
702
703 quoteChar = c;
704 stringBufferTop = 0;
705
706 c = getChar(false);
707 strLoop: while (c != quoteChar) {
708 if (c == '\n' || c == EOF_CHAR) {
709 ungetChar(c);
710 tokenEnd = cursor;
711 parser.addError("msg.unterminated.string.lit");
712 return Token.ERROR;
713 }
714
715 if (c == '\\') {
716 // We've hit an escaped character
717 int escapeVal;
718
719 c = getChar();
720 switch (c) {
721 case 'b': c = '\b'; break;
722 case 'f': c = '\f'; break;
723 case 'n': c = '\n'; break;
724 case 'r': c = '\r'; break;
725 case 't': c = '\t'; break;
726
727 // \v a late addition to the ECMA spec,
728 // it is not in Java, so use 0xb
729 case 'v': c = 0xb; break;
730
731 case 'u':
732 // Get 4 hex digits; if the u escape is not
733 // followed by 4 hex digits, use 'u' + the
734 // literal character sequence that follows.
735 int escapeStart = stringBufferTop;
736 addToString('u');
737 escapeVal = 0;
738 for (int i = 0; i != 4; ++i) {
739 c = getChar();
740 escapeVal = Kit.xDigitToInt(c, escapeVal);
741 if (escapeVal < 0) {
742 continue strLoop;
743 }
744 addToString(c);
745 }
746 // prepare for replace of stored 'u' sequence
747 // by escape value
748 stringBufferTop = escapeStart;
749 c = escapeVal;
750 break;
751 case 'x':
752 // Get 2 hex digits, defaulting to 'x'+literal
753 // sequence, as above.
754 c = getChar();
755 escapeVal = Kit.xDigitToInt(c, 0);
756 if (escapeVal < 0) {
757 addToString('x');
758 continue strLoop;
759 } else {
760 int c1 = c;
761 c = getChar();
762 escapeVal = Kit.xDigitToInt(c, escapeVal);
763 if (escapeVal < 0) {
764 addToString('x');
765 addToString(c1);
766 continue strLoop;
767 } else {
768 // got 2 hex digits
769 c = escapeVal;
770 }
771 }
772 break;
773
774 case '\n':
775 // Remove line terminator after escape to follow
776 // SpiderMonkey and C/C++
777 c = getChar();
778 continue strLoop;
779
780 default:
781 if ('0' <= c && c < '8') {
782 int val = c - '0';
783 c = getChar();
784 if ('0' <= c && c < '8') {
785 val = 8 * val + c - '0';
786 c = getChar();
787 if ('0' <= c && c < '8' && val <= 037) {
788 // c is 3rd char of octal sequence only
789 // if the resulting val <= 0377
790 val = 8 * val + c - '0';
791 c = getChar();
792 }
793 }
794 ungetChar(c);
795 c = val;
796 }
797 }
798 }
799 addToString(c);
800 c = getChar(false);
801 }
802
803 String str = getStringFromBuffer();
804 this.string = (String)allStrings.intern(str);
805 return Token.STRING;
806 }
807
808 switch (c) {
809 case ';': return Token.SEMI;
810 case '[': return Token.LB;
811 case ']': return Token.RB;
812 case '{': return Token.LC;
813 case '}': return Token.RC;
814 case '(': return Token.LP;
815 case ')': return Token.RP;
816 case ',': return Token.COMMA;
817 case '?': return Token.HOOK;
818 case ':':
819 if (matchChar(':')) {
820 return Token.COLONCOLON;
821 } else {
822 return Token.COLON;
823 }
824 case '.':
825 if (matchChar('.')) {
826 return Token.DOTDOT;
827 } else if (matchChar('(')) {
828 return Token.DOTQUERY;
829 } else {
830 return Token.DOT;
831 }
832
833 case '|':
834 if (matchChar('|')) {
835 return Token.OR;
836 } else if (matchChar('=')) {
837 return Token.ASSIGN_BITOR;
838 } else {
839 return Token.BITOR;
840 }
841
842 case '^':
843 if (matchChar('=')) {
844 return Token.ASSIGN_BITXOR;
845 } else {
846 return Token.BITXOR;
847 }
848
849 case '&':
850 if (matchChar('&')) {
851 return Token.AND;
852 } else if (matchChar('=')) {
853 return Token.ASSIGN_BITAND;
854 } else {
855 return Token.BITAND;
856 }
857
858 case '=':
859 if (matchChar('=')) {
860 if (matchChar('=')) {
861 return Token.SHEQ;
862 } else {
863 return Token.EQ;
864 }
865 } else if (matchChar('>')) {
866 return Token.ARROW;
867 } else {
868 return Token.ASSIGN;
869 }
870
871 case '!':
872 if (matchChar('=')) {
873 if (matchChar('=')) {
874 return Token.SHNE;
875 } else {
876 return Token.NE;
877 }
878 } else {
879 return Token.NOT;
880 }
881
882 case '<':
883 /* NB:treat HTML begin-comment as comment-till-eol */
884 if (matchChar('!')) {
885 if (matchChar('-')) {
886 if (matchChar('-')) {
887 tokenBeg = cursor - 4;
888 skipLine();
889 commentType = Token.CommentType.HTML;
890 return Token.COMMENT;
891 }
892 ungetCharIgnoreLineEnd('-');
893 }
894 ungetCharIgnoreLineEnd('!');
895 }
896 if (matchChar('<')) {
897 if (matchChar('=')) {
898 return Token.ASSIGN_LSH;
899 } else {
900 return Token.LSH;
901 }
902 } else {
903 if (matchChar('=')) {
904 return Token.LE;
905 } else {
906 return Token.LT;
907 }
908 }
909
910 case '>':
911 if (matchChar('>')) {
912 if (matchChar('>')) {
913 if (matchChar('=')) {
914 return Token.ASSIGN_URSH;
915 } else {
916 return Token.URSH;
917 }
918 } else {
919 if (matchChar('=')) {
920 return Token.ASSIGN_RSH;
921 } else {
922 return Token.RSH;
923 }
924 }
925 } else {
926 if (matchChar('=')) {
927 return Token.GE;
928 } else {
929 return Token.GT;
930 }
931 }
932
933 case '*':
934 if (matchChar('=')) {
935 return Token.ASSIGN_MUL;
936 } else {
937 return Token.MUL;
938 }
939
940 case '/':
941 markCommentStart();
942 // is it a // comment?
943 if (matchChar('/')) {
944 tokenBeg = cursor - 2;
945 skipLine();
946 commentType = Token.CommentType.LINE;
947 return Token.COMMENT;
948 }
949 // is it a /* or /** comment?
950 if (matchChar('*')) {
951 boolean lookForSlash = false;
952 tokenBeg = cursor - 2;
953 if (matchChar('*')) {
954 lookForSlash = true;
955 commentType = Token.CommentType.JSDOC;
956 } else {
957 commentType = Token.CommentType.BLOCK_COMMENT;
958 }
959 for (;;) {
960 c = getChar();
961 if (c == EOF_CHAR) {
962 tokenEnd = cursor - 1;
963 parser.addError("msg.unterminated.comment");
964 return Token.COMMENT;
965 } else if (c == '*') {
966 lookForSlash = true;
967 } else if (c == '/') {
968 if (lookForSlash) {
969 tokenEnd = cursor;
970 return Token.COMMENT;
971 }
972 } else {
973 lookForSlash = false;
974 tokenEnd = cursor;
975 }
976 }
977 }
978
979 if (matchChar('=')) {
980 return Token.ASSIGN_DIV;
981 } else {
982 return Token.DIV;
983 }
984
985 case '%':
986 if (matchChar('=')) {
987 return Token.ASSIGN_MOD;
988 } else {
989 return Token.MOD;
990 }
991
992 case '~':
993 return Token.BITNOT;
994
995 case '+':
996 if (matchChar('=')) {
997 return Token.ASSIGN_ADD;
998 } else if (matchChar('+')) {
999 return Token.INC;
1000 } else {
1001 return Token.ADD;
1002 }
1003
1004 case '-':
1005 if (matchChar('=')) {
1006 c = Token.ASSIGN_SUB;
1007 } else if (matchChar('-')) {
1008 if (!dirtyLine) {
1009 // treat HTML end-comment after possible whitespace
1010 // after line start as comment-until-eol
1011 if (matchChar('>')) {
1012 markCommentStart("--");
1013 skipLine();
1014 commentType = Token.CommentType.HTML;
1015 return Token.COMMENT;
1016 }
1017 }
1018 c = Token.DEC;
1019 } else {
1020 c = Token.SUB;
1021 }
1022 dirtyLine = true;
1023 return c;
1024
1025 default:
1026 parser.addError("msg.illegal.character");
1027 return Token.ERROR;
1028 }
1029 }
1030 }
1031
1032 private static boolean isAlpha(int c)
1033 {
1034 // Use 'Z' < 'a'
1035 if (c <= 'Z') {
1036 return 'A' <= c;
1037 } else {
1038 return 'a' <= c && c <= 'z';
1039 }
1040 }
1041
1042 static boolean isDigit(int c)
1043 {
1044 return '0' <= c && c <= '9';
1045 }
1046
1047 /* As defined in ECMA. jsscan.c uses C isspace() (which allows
1048 * \v, I think.) note that code in getChar() implicitly accepts
1049 * '\r' == \u000D as well.
1050 */
1051 static boolean isJSSpace(int c)
1052 {
1053 if (c <= 127) {
1054 return c == 0x20 || c == 0x9 || c == 0xC || c == 0xB;
1055 } else {
1056 return c == 0xA0 || c == BYTE_ORDER_MARK
1057 || Character.getType((char)c) == Character.SPACE_SEPARATOR;
1058 }
1059 }
1060
1061 private static boolean isJSFormatChar(int c)
1062 {
1063 return c > 127 && Character.getType((char)c) == Character.FORMAT;
1064 }
1065
1066 /**
1067 * Parser calls the method when it gets / or /= in literal context.
1068 */
1069 void readRegExp(int startToken)
1070 throws IOException
1071 {
1072 int start = tokenBeg;
1073 stringBufferTop = 0;
1074 if (startToken == Token.ASSIGN_DIV) {
1075 // Miss-scanned /=
1076 addToString('=');
1077 } else {
1078 if (startToken != Token.DIV) Kit.codeBug();
1079 }
1080
1081 boolean inCharSet = false; // true if inside a '['..']' pair
1082 int c;
1083 while ((c = getChar()) != '/' || inCharSet) {
1084 if (c == '\n' || c == EOF_CHAR) {
1085 ungetChar(c);
1086 tokenEnd = cursor - 1;
1087 this.string = new String(stringBuffer, 0, stringBufferTop);
1088 parser.reportError("msg.unterminated.re.lit");
1089 return;
1090 }
1091 if (c == '\\') {
1092 addToString(c);
1093 c = getChar();
1094 } else if (c == '[') {
1095 inCharSet = true;
1096 } else if (c == ']') {
1097 inCharSet = false;
1098 }
1099 addToString(c);
1100 }
1101 int reEnd = stringBufferTop;
1102
1103 while (true) {
1104 if (matchChar('g'))
1105 addToString('g');
1106 else if (matchChar('i'))
1107 addToString('i');
1108 else if (matchChar('m'))
1109 addToString('m');
1110 else if (matchChar('y')) // FireFox 3
1111 addToString('y');
1112 else
1113 break;
1114 }
1115 tokenEnd = start + stringBufferTop + 2; // include slashes
1116
1117 if (isAlpha(peekChar())) {
1118 parser.reportError("msg.invalid.re.flag");
1119 }
1120
1121 this.string = new String(stringBuffer, 0, reEnd);
1122 this.regExpFlags = new String(stringBuffer, reEnd,
1123 stringBufferTop - reEnd);
1124 }
1125
1126 String readAndClearRegExpFlags() {
1127 String flags = this.regExpFlags;
1128 this.regExpFlags = null;
1129 return flags;
1130 }
1131
1132 boolean isXMLAttribute()
1133 {
1134 return xmlIsAttribute;
1135 }
1136
1137 int getFirstXMLToken() throws IOException
1138 {
1139 xmlOpenTagsCount = 0;
1140 xmlIsAttribute = false;
1141 xmlIsTagContent = false;
1142 if (!canUngetChar())
1143 return Token.ERROR;
1144 ungetChar('<');
1145 return getNextXMLToken();
1146 }
1147
1148 int getNextXMLToken() throws IOException
1149 {
1150 tokenBeg = cursor;
1151 stringBufferTop = 0; // remember the XML
1152
1153 for (int c = getChar(); c != EOF_CHAR; c = getChar()) {
1154 if (xmlIsTagContent) {
1155 switch (c) {
1156 case '>':
1157 addToString(c);
1158 xmlIsTagContent = false;
1159 xmlIsAttribute = false;
1160 break;
1161 case '/':
1162 addToString(c);
1163 if (peekChar() == '>') {
1164 c = getChar();
1165 addToString(c);
1166 xmlIsTagContent = false;
1167 xmlOpenTagsCount--;
1168 }
1169 break;
1170 case '{':
1171 ungetChar(c);
1172 this.string = getStringFromBuffer();
1173 return Token.XML;
1174 case '\'':
1175 case '"':
1176 addToString(c);
1177 if (!readQuotedString(c)) return Token.ERROR;
1178 break;
1179 case '=':
1180 addToString(c);
1181 xmlIsAttribute = true;
1182 break;
1183 case ' ':
1184 case '\t':
1185 case '\r':
1186 case '\n':
1187 addToString(c);
1188 break;
1189 default:
1190 addToString(c);
1191 xmlIsAttribute = false;
1192 break;
1193 }
1194
1195 if (!xmlIsTagContent && xmlOpenTagsCount == 0) {
1196 this.string = getStringFromBuffer();
1197 return Token.XMLEND;
1198 }
1199 } else {
1200 switch (c) {
1201 case '<':
1202 addToString(c);
1203 c = peekChar();
1204 switch (c) {
1205 case '!':
1206 c = getChar(); // Skip !
1207 addToString(c);
1208 c = peekChar();
1209 switch (c) {
1210 case '-':
1211 c = getChar(); // Skip -
1212 addToString(c);
1213 c = getChar();
1214 if (c == '-') {
1215 addToString(c);
1216 if(!readXmlComment()) return Token.ERROR;
1217 } else {
1218 // throw away the string in progress
1219 stringBufferTop = 0;
1220 this.string = null;
1221 parser.addError("msg.XML.bad.form");
1222 return Token.ERROR;
1223 }
1224 break;
1225 case '[':
1226 c = getChar(); // Skip [
1227 addToString(c);
1228 if (getChar() == 'C' &&
1229 getChar() == 'D' &&
1230 getChar() == 'A' &&
1231 getChar() == 'T' &&
1232 getChar() == 'A' &&
1233 getChar() == '[')
1234 {
1235 addToString('C');
1236 addToString('D');
1237 addToString('A');
1238 addToString('T');
1239 addToString('A');
1240 addToString('[');
1241 if (!readCDATA()) return Token.ERROR;
1242
1243 } else {
1244 // throw away the string in progress
1245 stringBufferTop = 0;
1246 this.string = null;
1247 parser.addError("msg.XML.bad.form");
1248 return Token.ERROR;
1249 }
1250 break;
1251 default:
1252 if(!readEntity()) return Token.ERROR;
1253 break;
1254 }
1255 break;
1256 case '?':
1257 c = getChar(); // Skip ?
1258 addToString(c);
1259 if (!readPI()) return Token.ERROR;
1260 break;
1261 case '/':
1262 // End tag
1263 c = getChar(); // Skip /
1264 addToString(c);
1265 if (xmlOpenTagsCount == 0) {
1266 // throw away the string in progress
1267 stringBufferTop = 0;
1268 this.string = null;
1269 parser.addError("msg.XML.bad.form");
1270 return Token.ERROR;
1271 }
1272 xmlIsTagContent = true;
1273 xmlOpenTagsCount--;
1274 break;
1275 default:
1276 // Start tag
1277 xmlIsTagContent = true;
1278 xmlOpenTagsCount++;
1279 break;
1280 }
1281 break;
1282 case '{':
1283 ungetChar(c);
1284 this.string = getStringFromBuffer();
1285 return Token.XML;
1286 default:
1287 addToString(c);
1288 break;
1289 }
1290 }
1291 }
1292
1293 tokenEnd = cursor;
1294 stringBufferTop = 0; // throw away the string in progress
1295 this.string = null;
1296 parser.addError("msg.XML.bad.form");
1297 return Token.ERROR;
1298 }
1299
1300 /**
1301 *
1302 */
1303 private boolean readQuotedString(int quote) throws IOException
1304 {
1305 for (int c = getChar(); c != EOF_CHAR; c = getChar()) {
1306 addToString(c);
1307 if (c == quote) return true;
1308 }
1309
1310 stringBufferTop = 0; // throw away the string in progress
1311 this.string = null;
1312 parser.addError("msg.XML.bad.form");
1313 return false;
1314 }
1315
1316 /**
1317 *
1318 */
1319 private boolean readXmlComment() throws IOException
1320 {
1321 for (int c = getChar(); c != EOF_CHAR;) {
1322 addToString(c);
1323 if (c == '-' && peekChar() == '-') {
1324 c = getChar();
1325 addToString(c);
1326 if (peekChar() == '>') {
1327 c = getChar(); // Skip >
1328 addToString(c);
1329 return true;
1330 } else {
1331 continue;
1332 }
1333 }
1334 c = getChar();
1335 }
1336
1337 stringBufferTop = 0; // throw away the string in progress
1338 this.string = null;
1339 parser.addError("msg.XML.bad.form");
1340 return false;
1341 }
1342
1343 /**
1344 *
1345 */
1346 private boolean readCDATA() throws IOException
1347 {
1348 for (int c = getChar(); c != EOF_CHAR;) {
1349 addToString(c);
1350 if (c == ']' && peekChar() == ']') {
1351 c = getChar();
1352 addToString(c);
1353 if (peekChar() == '>') {
1354 c = getChar(); // Skip >
1355 addToString(c);
1356 return true;
1357 } else {
1358 continue;
1359 }
1360 }
1361 c = getChar();
1362 }
1363
1364 stringBufferTop = 0; // throw away the string in progress
1365 this.string = null;
1366 parser.addError("msg.XML.bad.form");
1367 return false;
1368 }
1369
1370 /**
1371 *
1372 */
1373 private boolean readEntity() throws IOException
1374 {
1375 int declTags = 1;
1376 for (int c = getChar(); c != EOF_CHAR; c = getChar()) {
1377 addToString(c);
1378 switch (c) {
1379 case '<':
1380 declTags++;
1381 break;
1382 case '>':
1383 declTags--;
1384 if (declTags == 0) return true;
1385 break;
1386 }
1387 }
1388
1389 stringBufferTop = 0; // throw away the string in progress
1390 this.string = null;
1391 parser.addError("msg.XML.bad.form");
1392 return false;
1393 }
1394
1395 /**
1396 *
1397 */
1398 private boolean readPI() throws IOException
1399 {
1400 for (int c = getChar(); c != EOF_CHAR; c = getChar()) {
1401 addToString(c);
1402 if (c == '?' && peekChar() == '>') {
1403 c = getChar(); // Skip >
1404 addToString(c);
1405 return true;
1406 }
1407 }
1408
1409 stringBufferTop = 0; // throw away the string in progress
1410 this.string = null;
1411 parser.addError("msg.XML.bad.form");
1412 return false;
1413 }
1414
1415 private String getStringFromBuffer()
1416 {
1417 tokenEnd = cursor;
1418 return new String(stringBuffer, 0, stringBufferTop);
1419 }
1420
1421 private void addToString(int c)
1422 {
1423 int N = stringBufferTop;
1424 if (N == stringBuffer.length) {
1425 char[] tmp = new char[stringBuffer.length * 2];
1426 System.arraycopy(stringBuffer, 0, tmp, 0, N);
1427 stringBuffer = tmp;
1428 }
1429 stringBuffer[N] = (char)c;
1430 stringBufferTop = N + 1;
1431 }
1432
1433 private boolean canUngetChar() {
1434 return ungetCursor == 0 || ungetBuffer[ungetCursor - 1] != '\n';
1435 }
1436
1437 private void ungetChar(int c)
1438 {
1439 // can not unread past across line boundary
1440 if (ungetCursor != 0 && ungetBuffer[ungetCursor - 1] == '\n')
1441 Kit.codeBug();
1442 ungetBuffer[ungetCursor++] = c;
1443 cursor--;
1444 }
1445
1446 private boolean matchChar(int test) throws IOException
1447 {
1448 int c = getCharIgnoreLineEnd();
1449 if (c == test) {
1450 tokenEnd = cursor;
1451 return true;
1452 } else {
1453 ungetCharIgnoreLineEnd(c);
1454 return false;
1455 }
1456 }
1457
1458 private int peekChar() throws IOException
1459 {
1460 int c = getChar();
1461 ungetChar(c);
1462 return c;
1463 }
1464
1465 private int getChar() throws IOException
1466 {
1467 return getChar(true);
1468 }
1469
1470 private int getChar(boolean skipFormattingChars) throws IOException
1471 {
1472 if (ungetCursor != 0) {
1473 cursor++;
1474 return ungetBuffer[--ungetCursor];
1475 }
1476
1477 for(;;) {
1478 int c;
1479 if (sourceString != null) {
1480 if (sourceCursor == sourceEnd) {
1481 hitEOF = true;
1482 return EOF_CHAR;
1483 }
1484 cursor++;
1485 c = sourceString.charAt(sourceCursor++);
1486 } else {
1487 if (sourceCursor == sourceEnd) {
1488 if (!fillSourceBuffer()) {
1489 hitEOF = true;
1490 return EOF_CHAR;
1491 }
1492 }
1493 cursor++;
1494 c = sourceBuffer[sourceCursor++];
1495 }
1496
1497 if (lineEndChar >= 0) {
1498 if (lineEndChar == '\r' && c == '\n') {
1499 lineEndChar = '\n';
1500 continue;
1501 }
1502 lineEndChar = -1;
1503 lineStart = sourceCursor - 1;
1504 lineno++;
1505 }
1506
1507 if (c <= 127) {
1508 if (c == '\n' || c == '\r') {
1509 lineEndChar = c;
1510 c = '\n';
1511 }
1512 } else {
1513 if (c == BYTE_ORDER_MARK) return c; // BOM is considered whitespace
1514 if (skipFormattingChars && isJSFormatChar(c)) {
1515 continue;
1516 }
1517 if (ScriptRuntime.isJSLineTerminator(c)) {
1518 lineEndChar = c;
1519 c = '\n';
1520 }
1521 }
1522 return c;
1523 }
1524 }
1525
1526 private int getCharIgnoreLineEnd() throws IOException
1527 {
1528 if (ungetCursor != 0) {
1529 cursor++;
1530 return ungetBuffer[--ungetCursor];
1531 }
1532
1533 for(;;) {
1534 int c;
1535 if (sourceString != null) {
1536 if (sourceCursor == sourceEnd) {
1537 hitEOF = true;
1538 return EOF_CHAR;
1539 }
1540 cursor++;
1541 c = sourceString.charAt(sourceCursor++);
1542 } else {
1543 if (sourceCursor == sourceEnd) {
1544 if (!fillSourceBuffer()) {
1545 hitEOF = true;
1546 return EOF_CHAR;
1547 }
1548 }
1549 cursor++;
1550 c = sourceBuffer[sourceCursor++];
1551 }
1552
1553 if (c <= 127) {
1554 if (c == '\n' || c == '\r') {
1555 lineEndChar = c;
1556 c = '\n';
1557 }
1558 } else {
1559 if (c == BYTE_ORDER_MARK) return c; // BOM is considered whitespace
1560 if (isJSFormatChar(c)) {
1561 continue;
1562 }
1563 if (ScriptRuntime.isJSLineTerminator(c)) {
1564 lineEndChar = c;
1565 c = '\n';
1566 }
1567 }
1568 return c;
1569 }
1570 }
1571
1572 private void ungetCharIgnoreLineEnd(int c)
1573 {
1574 ungetBuffer[ungetCursor++] = c;
1575 cursor--;
1576 }
1577
1578 private void skipLine() throws IOException
1579 {
1580 // skip to end of line
1581 int c;
1582 while ((c = getChar()) != EOF_CHAR && c != '\n') { }
1583 ungetChar(c);
1584 tokenEnd = cursor;
1585 }
1586
1587 /**
1588 * Returns the offset into the current line.
1589 */
1590 final int getOffset()
1591 {
1592 int n = sourceCursor - lineStart;
1593 if (lineEndChar >= 0) { --n; }
1594 return n;
1595 }
1596
1597 private final int charAt(int index) {
1598 if (index < 0) {
1599 return EOF_CHAR;
1600 }
1601 if (sourceString != null) {
1602 if (index >= sourceEnd) {
1603 return EOF_CHAR;
1604 }
1605 return sourceString.charAt(index);
1606 } else {
1607 if (index >= sourceEnd) {
1608 int oldSourceCursor = sourceCursor;
1609 try {
1610 if (!fillSourceBuffer()) { return EOF_CHAR; }
1611 } catch (IOException ioe) {
1612 // ignore it, we're already displaying an error...
1613 return EOF_CHAR;
1614 }
1615 // index recalculuation as fillSourceBuffer can move saved
1616 // line buffer and change sourceCursor
1617 index -= (oldSourceCursor - sourceCursor);
1618 }
1619 return sourceBuffer[index];
1620 }
1621 }
1622
1623 private final String substring(int beginIndex, int endIndex) {
1624 if (sourceString != null) {
1625 return sourceString.substring(beginIndex, endIndex);
1626 } else {
1627 int count = endIndex - beginIndex;
1628 return new String(sourceBuffer, beginIndex, count);
1629 }
1630 }
1631
1632 final String getLine() {
1633 int lineEnd = sourceCursor;
1634 if (lineEndChar >= 0) {
1635 // move cursor before newline sequence
1636 lineEnd -= 1;
1637 if (lineEndChar == '\n' && charAt(lineEnd - 1) == '\r') {
1638 lineEnd -= 1;
1639 }
1640 } else {
1641 // Read until the end of line
1642 int lineLength = lineEnd - lineStart;
1643 for (;; ++lineLength) {
1644 int c = charAt(lineStart + lineLength);
1645 if (c == EOF_CHAR || ScriptRuntime.isJSLineTerminator(c)) {
1646 break;
1647 }
1648 }
1649 lineEnd = lineStart + lineLength;
1650 }
1651 return substring(lineStart, lineEnd);
1652 }
1653
1654 final String getLine(int position, int[] linep) {
1655 assert position >= 0 && position <= cursor;
1656 assert linep.length == 2;
1657 int delta = (cursor + ungetCursor) - position;
1658 int cur = sourceCursor;
1659 if (delta > cur) {
1660 // requested line outside of source buffer
1661 return null;
1662 }
1663 // read back until position
1664 int end = 0, lines = 0;
1665 for (; delta > 0; --delta, --cur) {
1666 assert cur > 0;
1667 int c = charAt(cur - 1);
1668 if (ScriptRuntime.isJSLineTerminator(c)) {
1669 if (c == '\n' && charAt(cur - 2) == '\r') {
1670 // \r\n sequence
1671 delta -= 1;
1672 cur -= 1;
1673 }
1674 lines += 1;
1675 end = cur - 1;
1676 }
1677 }
1678 // read back until line start
1679 int start = 0, offset = 0;
1680 for (; cur > 0; --cur, ++offset) {
1681 int c = charAt(cur - 1);
1682 if (ScriptRuntime.isJSLineTerminator(c)) {
1683 start = cur;
1684 break;
1685 }
1686 }
1687 linep[0] = lineno - lines + (lineEndChar >= 0 ? 1 : 0);
1688 linep[1] = offset;
1689 if (lines == 0) {
1690 return getLine();
1691 } else {
1692 return substring(start, end);
1693 }
1694 }
1695
1696 private boolean fillSourceBuffer() throws IOException
1697 {
1698 if (sourceString != null) Kit.codeBug();
1699 if (sourceEnd == sourceBuffer.length) {
1700 if (lineStart != 0 && !isMarkingComment()) {
1701 System.arraycopy(sourceBuffer, lineStart, sourceBuffer, 0,
1702 sourceEnd - lineStart);
1703 sourceEnd -= lineStart;
1704 sourceCursor -= lineStart;
1705 lineStart = 0;
1706 } else {
1707 char[] tmp = new char[sourceBuffer.length * 2];
1708 System.arraycopy(sourceBuffer, 0, tmp, 0, sourceEnd);
1709 sourceBuffer = tmp;
1710 }
1711 }
1712 int n = sourceReader.read(sourceBuffer, sourceEnd,
1713 sourceBuffer.length - sourceEnd);
1714 if (n < 0) {
1715 return false;
1716 }
1717 sourceEnd += n;
1718 return true;
1719 }
1720
1721 /**
1722 * Return the current position of the scanner cursor.
1723 */
1724 public int getCursor() {
1725 return cursor;
1726 }
1727
1728 /**
1729 * Return the absolute source offset of the last scanned token.
1730 */
1731 public int getTokenBeg() {
1732 return tokenBeg;
1733 }
1734
1735 /**
1736 * Return the absolute source end-offset of the last scanned token.
1737 */
1738 public int getTokenEnd() {
1739 return tokenEnd;
1740 }
1741
1742 /**
1743 * Return tokenEnd - tokenBeg
1744 */
1745 public int getTokenLength() {
1746 return tokenEnd - tokenBeg;
1747 }
1748
1749 /**
1750 * Return the type of the last scanned comment.
1751 * @return type of last scanned comment, or 0 if none have been scanned.
1752 */
1753 public Token.CommentType getCommentType() {
1754 return commentType;
1755 }
1756
1757 private void markCommentStart() {
1758 markCommentStart("");
1759 }
1760
1761 private void markCommentStart(String prefix) {
1762 if (parser.compilerEnv.isRecordingComments() && sourceReader != null) {
1763 commentPrefix = prefix;
1764 commentCursor = sourceCursor - 1;
1765 }
1766 }
1767
1768 private boolean isMarkingComment() {
1769 return commentCursor != -1;
1770 }
1771
1772 final String getAndResetCurrentComment() {
1773 if (sourceString != null) {
1774 if (isMarkingComment()) Kit.codeBug();
1775 return sourceString.substring(tokenBeg, tokenEnd);
1776 } else {
1777 if (!isMarkingComment()) Kit.codeBug();
1778 StringBuilder comment = new StringBuilder(commentPrefix);
1779 comment.append(sourceBuffer, commentCursor,
1780 getTokenLength() - commentPrefix.length());
1781 commentCursor = -1;
1782 return comment.toString();
1783 }
1784 }
1785
1786 private String convertLastCharToHex(String str) {
1787 int lastIndex = str.length()-1;
1788 StringBuffer buf = new StringBuffer(
1789 str.substring(0, lastIndex));
1790 buf.append("\\u");
1791 String hexCode = Integer.toHexString(str.charAt(lastIndex));
1792 for (int i = 0; i < 4-hexCode.length(); ++i) {
1793 buf.append('0');
1794 }
1795 buf.append(hexCode);
1796 return buf.toString();
1797 }
1798
1799 // stuff other than whitespace since start of line
1800 private boolean dirtyLine;
1801
1802 String regExpFlags;
1803
1804 // Set this to an initial non-null value so that the Parser has
1805 // something to retrieve even if an error has occurred and no
1806 // string is found. Fosters one class of error, but saves lots of
1807 // code.
1808 private String string = "";
1809 private double number;
1810 private boolean isBinary;
1811 private boolean isOldOctal;
1812 private boolean isOctal;
1813 private boolean isHex;
1814
1815 // delimiter for last string literal scanned
1816 private int quoteChar;
1817
1818 private char[] stringBuffer = new char[128];
1819 private int stringBufferTop;
1820 private ObjToIntMap allStrings = new ObjToIntMap(50);
1821
1822 // Room to backtrace from to < on failed match of the last - in <!--
1823 private final int[] ungetBuffer = new int[3];
1824 private int ungetCursor;
1825
1826 private boolean hitEOF = false;
1827
1828 private int lineStart = 0;
1829 private int lineEndChar = -1;
1830 int lineno;
1831
1832 private String sourceString;
1833 private Reader sourceReader;
1834 private char[] sourceBuffer;
1835 private int sourceEnd;
1836
1837 // sourceCursor is an index into a small buffer that keeps a
1838 // sliding window of the source stream.
1839 int sourceCursor;
1840
1841 // cursor is a monotonically increasing index into the original
1842 // source stream, tracking exactly how far scanning has progressed.
1843 // Its value is the index of the next character to be scanned.
1844 int cursor;
1845
1846 // Record start and end positions of last scanned token.
1847 int tokenBeg;
1848 int tokenEnd;
1849
1850 // Type of last comment scanned.
1851 Token.CommentType commentType;
1852
1853 // for xml tokenizer
1854 private boolean xmlIsAttribute;
1855 private boolean xmlIsTagContent;
1856 private int xmlOpenTagsCount;
1857
1858 private Parser parser;
1859
1860 private String commentPrefix = "";
1861 private int commentCursor = -1;
1862 }