forked from mozilla/rhino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenStream.java
More file actions
1404 lines (1297 loc) · 44.3 KB
/
Copy pathTokenStream.java
File metadata and controls
1404 lines (1297 loc) · 44.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Rhino code, released
* May 6, 1999.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1997-1999 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* Roger Lawrence
* Mike McCabe
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU Public License (the "GPL"), in which case the
* provisions of the GPL are applicable instead of those above.
* If you wish to allow use of your version of this file only
* under the terms of the GPL and not to allow others to use your
* version of this file under the NPL, indicate your decision by
* deleting the provisions above and replace them with the notice
* and other provisions required by the GPL. If you do not delete
* the provisions above, a recipient may use your version of this
* file under either the NPL or the GPL.
*/
package org.mozilla.javascript;
import java.io.*;
/**
* This class implements the JavaScript scanner.
*
* It is based on the C source files jsscan.c and jsscan.h
* in the jsref package.
*
* @see org.mozilla.javascript.Parser
*
* @author Mike McCabe
* @author Brendan Eich
*/
public class TokenStream {
/*
* JSTokenStream flags, mirroring those in jsscan.h. These are used
* by the parser to change/check the state of the scanner.
*/
public final static int
TSF_NEWLINES = 0x0001, // tokenize newlines
TSF_FUNCTION = 0x0002, // scanning inside function body
TSF_RETURN_EXPR = 0x0004, // function has 'return expr;'
TSF_RETURN_VOID = 0x0008, // function has 'return;'
TSF_REGEXP = 0x0010; // looking for a regular expression
/*
* For chars - because we need something out-of-range
* to check. (And checking EOF by exception is annoying.)
* Note distinction from EOF token type!
*/
private final static int
EOF_CHAR = -1;
/**
* Token types. These values correspond to JSTokenType values in
* jsscan.c.
*/
public final static int
// start enum
ERROR = -1, // well-known as the only code < EOF
EOF = 0, // end of file token - (not EOF_CHAR)
EOL = 1, // end of line
// Beginning here are interpreter bytecodes. Their values
// must not exceed 127.
POPV = 2,
ENTERWITH = 3,
LEAVEWITH = 4,
RETURN = 5,
GOTO = 6,
IFEQ = 7,
IFNE = 8,
DUP = 9,
SETNAME = 10,
BITOR = 11,
BITXOR = 12,
BITAND = 13,
EQ = 14,
NE = 15,
LT = 16,
LE = 17,
GT = 18,
GE = 19,
LSH = 20,
RSH = 21,
URSH = 22,
ADD = 23,
SUB = 24,
MUL = 25,
DIV = 26,
MOD = 27,
BITNOT = 28,
NEG = 29,
NEW = 30,
DELPROP = 31,
TYPEOF = 32,
NAMEINC = 33,
PROPINC = 34,
ELEMINC = 35,
NAMEDEC = 36,
PROPDEC = 37,
ELEMDEC = 38,
GETPROP = 39,
SETPROP = 40,
GETELEM = 41,
SETELEM = 42,
CALL = 43,
NAME = 44,
NUMBER = 45,
STRING = 46,
ZERO = 47,
ONE = 48,
NULL = 49,
THIS = 50,
FALSE = 51,
TRUE = 52,
SHEQ = 53, // shallow equality (===)
SHNE = 54, // shallow inequality (!==)
CLOSURE = 55,
OBJECT = 56,
POP = 57,
POS = 58,
VARINC = 59,
VARDEC = 60,
BINDNAME = 61,
THROW = 62,
IN = 63,
INSTANCEOF = 64,
GOSUB = 65,
RETSUB = 66,
CALLSPECIAL = 67,
GETTHIS = 68,
NEWTEMP = 69,
USETEMP = 70,
GETBASE = 71,
GETVAR = 72,
SETVAR = 73,
UNDEFINED = 74,
TRY = 75,
ENDTRY = 76,
NEWSCOPE = 77,
TYPEOFNAME = 78,
ENUMINIT = 79,
ENUMNEXT = 80,
GETPROTO = 81,
GETPARENT = 82,
SETPROTO = 83,
SETPARENT = 84,
SCOPE = 85,
GETSCOPEPARENT = 86,
JTHROW = 87,
// End of interpreter bytecodes
SEMI = 88, // semicolon
LB = 89, // left and right brackets
RB = 90,
LC = 91, // left and right curlies (braces)
RC = 92,
LP = 93, // left and right parentheses
RP = 94,
COMMA = 95, // comma operator
ASSIGN = 96, // assignment ops (= += -= etc.)
HOOK = 97, // conditional (?:)
COLON = 98,
OR = 99, // logical or (||)
AND = 100, // logical and (&&)
EQOP = 101, // equality ops (== !=)
RELOP = 102, // relational ops (< <= > >=)
SHOP = 103, // shift ops (<< >> >>>)
UNARYOP = 104, // unary prefix operator
INC = 105, // increment/decrement (++ --)
DEC = 106,
DOT = 107, // member operator (.)
PRIMARY = 108, // true, false, null, this
FUNCTION = 109, // function keyword
EXPORT = 110, // export keyword
IMPORT = 111, // import keyword
IF = 112, // if keyword
ELSE = 113, // else keyword
SWITCH = 114, // switch keyword
CASE = 115, // case keyword
DEFAULT = 116, // default keyword
WHILE = 117, // while keyword
DO = 118, // do keyword
FOR = 119, // for keyword
BREAK = 120, // break keyword
CONTINUE = 121, // continue keyword
VAR = 122, // var keyword
WITH = 123, // with keyword
CATCH = 124, // catch keyword
FINALLY = 125, // finally keyword
RESERVED = 126, // reserved keywords
/** Added by Mike - these are JSOPs in the jsref, but I
* don't have them yet in the java implementation...
* so they go here. Also whatever I needed.
* Most of these go in the 'op' field when returning
* more general token types, eg. 'DIV' as the op of 'ASSIGN'.
*/
NOP = 127, // NOP
NOT = 128, // etc.
PRE = 129, // for INC, DEC nodes.
POST = 130,
/**
* For JSOPs associated with keywords...
* eg. op = THIS; token = PRIMARY
*/
VOID = 131,
/* types used for the parse tree - these never get returned
* by the scanner.
*/
BLOCK = 132, // statement block
ARRAYLIT = 133, // array literal
OBJLIT = 134, // object literal
LABEL = 135, // label
TARGET = 136,
LOOP = 137,
ENUMDONE = 138,
EXPRSTMT = 139,
PARENT = 140,
CONVERT = 141,
JSR = 142,
NEWLOCAL = 143,
USELOCAL = 144,
SCRIPT = 145, // top-level node for entire script
/**
* For the interpreted mode indicating a line number change in icodes.
*/
LINE = 146,
SOURCEFILE = 147,
// For debugger
BREAKPOINT = 148;
// end enum
/* for mapping int token types to printable strings.
* make sure to add 1 to index before using these!
*/
private static String names[];
private static void checkNames() {
if (Context.printTrees && names == null) {
String[] a = {
"error",
"eof",
"eol",
"popv",
"enterwith",
"leavewith",
"return",
"goto",
"ifeq",
"ifne",
"dup",
"setname",
"bitor",
"bitxor",
"bitand",
"eq",
"ne",
"lt",
"le",
"gt",
"ge",
"lsh",
"rsh",
"ursh",
"add",
"sub",
"mul",
"div",
"mod",
"bitnot",
"neg",
"new",
"delprop",
"typeof",
"nameinc",
"propinc",
"eleminc",
"namedec",
"propdec",
"elemdec",
"getprop",
"setprop",
"getelem",
"setelem",
"call",
"name",
"number",
"string",
"zero",
"one",
"null",
"this",
"false",
"true",
"sheq",
"shne",
"closure",
"object",
"pop",
"pos",
"varinc",
"vardec",
"bindname",
"throw",
"in",
"instanceof",
"gosub",
"retsub",
"callspecial",
"getthis",
"newtemp",
"usetemp",
"getbase",
"getvar",
"setvar",
"undefined",
"try",
"endtry",
"newscope",
"typeofname",
"enuminit",
"enumnext",
"getproto",
"getparent",
"setproto",
"setparent",
"scope",
"getscopeparent",
"jthrow",
"semi",
"lb",
"rb",
"lc",
"rc",
"lp",
"rp",
"comma",
"assign",
"hook",
"colon",
"or",
"and",
"eqop",
"relop",
"shop",
"unaryop",
"inc",
"dec",
"dot",
"primary",
"function",
"export",
"import",
"if",
"else",
"switch",
"case",
"default",
"while",
"do",
"for",
"break",
"continue",
"var",
"with",
"catch",
"finally",
"reserved",
"nop",
"not",
"pre",
"post",
"void",
"block",
"arraylit",
"objlit",
"label",
"target",
"loop",
"enumdone",
"exprstmt",
"parent",
"convert",
"jsr",
"newlocal",
"uselocal",
"script",
"line",
"sourcefile",
};
names = a;
}
}
/* This function uses the cached op, string and number fields in
* TokenStream; if getToken has been called since the passed token
* was scanned, the op or string printed may be incorrect.
*/
public String tokenToString(int token) {
if (Context.printTrees) {
checkNames();
if (token + 1 >= names.length)
return null;
if (token == UNARYOP ||
token == ASSIGN ||
token == PRIMARY ||
token == EQOP ||
token == SHOP ||
token == RELOP) {
return names[token + 1] + " " + names[this.op + 1];
}
if (token == STRING || token == OBJECT || token == NAME)
return names[token + 1] + " `" + this.string + "'";
if (token == NUMBER)
return "NUMBER " + this.number;
return names[token + 1];
}
return "";
}
public static String tokenToName(int type) {
checkNames();
return names == null ? "" : names[type + 1];
}
private static java.util.Hashtable keywords;
static {
String[] strings = {
"break",
"case",
"continue",
"default",
"delete",
"do",
"else",
"export",
"false",
"for",
"function",
"if",
"in",
"new",
"null",
"return",
"switch",
"this",
"true",
"typeof",
"var",
"void",
"while",
"with",
// the following are #ifdef RESERVE_JAVA_KEYWORDS in jsscan.c
"abstract",
"boolean",
"byte",
"catch",
"char",
"class",
"const",
"debugger",
"double",
"enum",
"extends",
"final",
"finally",
"float",
"goto",
"implements",
"import",
"instanceof",
"int",
"interface",
"long",
"native",
"package",
"private",
"protected",
"public",
"short",
"static",
"super",
"synchronized",
"throw",
"throws",
"transient",
"try",
"volatile"
};
int[] values = {
BREAK, // break
CASE, // case
CONTINUE, // continue
DEFAULT, // default
DELPROP, // delete
DO, // do
ELSE, // else
EXPORT, // export
PRIMARY | (FALSE << 8), // false
FOR, // for
FUNCTION, // function
IF, // if
RELOP | (IN << 8), // in
NEW, // new
PRIMARY | (NULL << 8), // null
RETURN, // return
SWITCH, // switch
PRIMARY | (THIS << 8), // this
PRIMARY | (TRUE << 8), // true
UNARYOP | (TYPEOF << 8), // typeof
VAR, // var
UNARYOP | (VOID << 8), // void
WHILE, // while
WITH, // with
RESERVED, // abstract
RESERVED, // boolean
RESERVED, // byte
CATCH, // catch
RESERVED, // char
RESERVED, // class
RESERVED, // const
RESERVED, // debugger
RESERVED, // double
RESERVED, // enum
RESERVED, // extends
RESERVED, // final
FINALLY, // finally
RESERVED, // float
RESERVED, // goto
RESERVED, // implements
IMPORT, // import
RELOP | (INSTANCEOF << 8), // instanceof
RESERVED, // int
RESERVED, // interface
RESERVED, // long
RESERVED, // native
RESERVED, // package
RESERVED, // private
RESERVED, // protected
RESERVED, // public
RESERVED, // short
RESERVED, // static
RESERVED, // super
RESERVED, // synchronized
THROW, // throw
RESERVED, // throws
RESERVED, // transient
TRY, // try
RESERVED // volatile
};
keywords = new java.util.Hashtable(strings.length);
Integer res = new Integer(RESERVED);
for (int i=0; i < strings.length; i++)
keywords.put(strings[i], values[i] == RESERVED
? res
: new Integer(values[i]));
}
private int stringToKeyword(String name) {
Integer result = (Integer) keywords.get(name);
if (result == null)
return EOF;
int x = result.intValue();
this.op = x >> 8;
return x & 0xff;
}
public TokenStream(Reader in, Scriptable scope,
String sourceName, int lineno)
{
this.in = new LineBuffer(in, lineno);
this.scope = scope;
this.pushbackToken = EOF;
this.sourceName = sourceName;
flags = 0;
}
public Scriptable getScope() {
return scope;
}
/* return and pop the token from the stream if it matches...
* otherwise return null
*/
public boolean matchToken(int toMatch) throws IOException {
int token = getToken();
if (token == toMatch)
return true;
// didn't match, push back token
tokenno--;
this.pushbackToken = token;
return false;
}
public void clearPushback() {
this.pushbackToken = EOF;
}
public void ungetToken(int tt) {
if (this.pushbackToken != EOF && tt != ERROR) {
Object[] errArgs = { tokenToString(tt),
tokenToString(this.pushbackToken) };
String message = Context.getMessage("msg.token.replaces.pushback",
errArgs);
throw new RuntimeException(message);
}
this.pushbackToken = tt;
tokenno--;
}
public int peekToken() throws IOException {
int result = getToken();
this.pushbackToken = result;
tokenno--;
return result;
}
public int peekTokenSameLine() throws IOException {
int result;
flags |= TSF_NEWLINES; // SCAN_NEWLINES from jsscan.h
result = peekToken();
flags &= ~TSF_NEWLINES; // HIDE_NEWLINES from jsscan.h
if (this.pushbackToken == EOL)
this.pushbackToken = EOF;
return result;
}
protected static boolean isJSIdentifier(String s) {
int length = s.length();
if (length == 0 || !Character.isJavaIdentifierStart(s.charAt(0)))
return false;
for (int i=1; i<length; i++) {
char c = s.charAt(i);
if (!Character.isJavaIdentifierPart(c))
if (c == '\\')
if (! ((i + 5) < length)
&& (s.charAt(i + 1) == 'u')
&& isXDigit(s.charAt(i + 2))
&& isXDigit(s.charAt(i + 3))
&& isXDigit(s.charAt(i + 4))
&& isXDigit(s.charAt(i + 5)))
return false;
}
return true;
}
private static boolean isAlpha(int c) {
return ((c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z'));
}
static boolean isDigit(int c) {
return (c >= '0' && c <= '9');
}
static boolean isXDigit(int c) {
return ((c >= '0' && c <= '9')
|| (c >= 'a' && c <= 'f')
|| (c >= 'A' && c <= 'F'));
}
/* As defined in ECMA. jsscan.c uses C isspace() (which allows
* \v, I think.) note that code in in.read() implicitly accepts
* '\r' == \u000D as well.
*/
public static boolean isJSSpace(int c) {
return (c == '\u0020' || c == '\u0009'
|| c == '\u000C' || c == '\u000B'
|| c == '\u00A0'
|| Character.getType((char)c) == Character.SPACE_SEPARATOR);
}
public static boolean isJSLineTerminator(int c) {
return (c == '\n' || c == '\r'
|| c == 0x2028 || c == 0x2029);
}
public int getToken() throws IOException {
int c;
tokenno++;
// Check for pushed-back token
if (this.pushbackToken != EOF) {
int result = this.pushbackToken;
this.pushbackToken = EOF;
return result;
}
// Eat whitespace, possibly sensitive to newlines.
do {
c = in.read();
if (c == '\n')
if ((flags & TSF_NEWLINES) != 0)
break;
} while (isJSSpace(c) || c == '\n');
if (c == EOF_CHAR)
return EOF;
// identifier/keyword/instanceof?
// watch out for starting with a <backslash>
boolean isUnicodeEscapeStart = false;
if (c == '\\') {
c = in.read();
if (c == 'u')
isUnicodeEscapeStart = true;
else
c = '\\';
// always unread the 'u' or whatever, we need
// to start the string below at the <backslash>.
in.unread();
}
if (isUnicodeEscapeStart ||
Character.isJavaIdentifierStart((char)c)) {
in.startString();
boolean containsEscape = isUnicodeEscapeStart;
do {
c = in.read();
if (c == '\\') {
c = in.read();
containsEscape = (c == 'u');
}
} while (Character.isJavaIdentifierPart((char)c));
in.unread();
int result;
String str = in.getString();
// OPT we shouldn't have to make a string (object!) to
// check if it's a keyword.
// strictly speaking we should probably push-back
// all the bad characters if the <backslash>uXXXX
// sequence is malformed. But since there isn't a
// correct context(is there?) for a bad Unicode
// escape sequence after an identifier, we can report
// an error here.
if (containsEscape) {
char ca[] = str.toCharArray();
StringBuffer x = new StringBuffer();
int escStart = str.indexOf("\\u");
int start = 0;
while (escStart != -1) {
x.append(ca, start, escStart);
boolean goodEscape = false;
if ((escStart + 5) < ca.length) {
if (isXDigit(ca[escStart + 2])) {
int val = Character.digit(ca[escStart + 2], 16);
if (isXDigit(ca[escStart + 3])) {
val = (val << 4) | Character.digit(ca[escStart + 3], 16);
if (isXDigit(ca[escStart + 4])) {
val = (val << 4) | Character.digit(ca[escStart + 4], 16);
if (isXDigit(ca[escStart + 5])) {
val = (val << 4) | Character.digit(ca[escStart + 5], 16);
x.append((char)val);
start = escStart + 6;
goodEscape = true;
}
}
}
}
}
if (!goodEscape) {
reportSyntaxError("msg.invalid.escape", null);
return ERROR;
}
escStart = str.indexOf("\\u", start);
}
x.append(ca, start, ca.length - start);
str = x.toString();
}
else
// Return the corresponding token if it's a keyword
if ((result = stringToKeyword(str)) != EOF) {
return result;
}
this.string = str;
return NAME;
}
// is it a number?
if (isDigit(c) || (c == '.' && isDigit(in.peek()))) {
int base = 10;
in.startString();
double dval = ScriptRuntime.NaN;
long longval = 0;
boolean isInteger = true;
if (c == '0') {
c = in.read();
if (c == 'x' || c == 'X') {
c = in.read();
base = 16;
// restart the string, losing leading 0x
in.startString();
} else if (isDigit(c)) {
if (c < '8') {
base = 8;
// Restart the string, losing the leading 0
in.startString();
} else {
/* Checking against c < '8' is non-ECMA, but
* is required to support legacy code; we've
* supported it in the past, and it's likely
* that "08" and "09" are in use in code
* having to do with dates. So we need to
* support it, which makes our behavior a
* superset of ECMA in this area. We raise a
* warning if a non-octal digit is
* encountered, then proceed as if it were
* decimal.
*/
Object[] errArgs = { String.valueOf((char)c) };
Context.reportWarning
(Context.getMessage("msg.bad.octal.literal",
errArgs),
getSourceName(), in.getLineno(),
getLine(), getOffset());
// implicitly retain the leading 0
}
} else {
// implicitly retain the leading 0
}
}
while (isXDigit(c)) {
if (base < 16 && (isAlpha(c)
|| (base == 8 && c >= '8'))) {
break;
}
c = in.read();
}
if (base == 10 && (c == '.' || c == 'e' || c == 'E')) {
isInteger = false;
if (c == '.') {
do {
c = in.read();
} while (isDigit(c));
}
if (c == 'e' || c == 'E') {
c = in.read();
if (c == '+' || c == '-') {
c = in.read();
}
if (!isDigit(c)) {
in.getString(); // throw away string in progress
reportSyntaxError("msg.missing.exponent", null);
return ERROR;
}
do {
c = in.read();
} while (isDigit(c));
}
}
in.unread();
String numString = in.getString();
if (base == 10 && !isInteger) {
try {
// Use Java conversion to number from string...
dval = (Double.valueOf(numString)).doubleValue();
}
catch (NumberFormatException ex) {
Object[] errArgs = { ex.getMessage() };
reportSyntaxError("msg.caught.nfe", errArgs);
return ERROR;
}
} else {
dval = ScriptRuntime.stringToNumber(numString, 0, base);
longval = (long) dval;
// is it an integral fits-in-a-long value?
if (longval != dval)
isInteger = false;
}
if (!isInteger) {
/* Can't handle floats right now, because postfix INC/DEC
generate Doubles, but I would generate a Float through this
path, and it causes a stack mismatch. FIXME (MS)
if (Float.MIN_VALUE <= dval && dval <= Float.MAX_VALUE)
this.number = new Xloat((float) dval);
else
*/
this.number = new Double(dval);
} else {
// We generate the smallest possible type here
if (Byte.MIN_VALUE <= longval && longval <= Byte.MAX_VALUE)
this.number = new Byte((byte)longval);
else if (Short.MIN_VALUE <= longval &&
longval <= Short.MAX_VALUE)
this.number = new Short((short)longval);
else if (Integer.MIN_VALUE <= longval &&
longval <= Integer.MAX_VALUE)
this.number = new Integer((int)longval);
else {
// May lose some precision here, but that's the
// appropriate semantics.
this.number = new Double(longval);
}
}
return NUMBER;
}
// is it a string?
if (c == '"' || c == '\'') {
// We attempt to accumulate a string the fast way, by
// building it directly out of the reader. But if there
// are any escaped characters in the string, we revert to
// building it out of a StringBuffer.
StringBuffer stringBuf = null;
int quoteChar = c;
int val = 0;
c = in.read();
in.startString(); // start after the first "
while(c != quoteChar) {
if (c == '\n' || c == EOF_CHAR) {
in.unread();
in.getString(); // throw away the string in progress
reportSyntaxError("msg.unterminated.string.lit", null);
return ERROR;
}
if (c == '\\') {
// We've hit an escaped character; revert to the
// slow method of building a string.
if (stringBuf == null) {
// Don't include the backslash
in.unread();
stringBuf = new StringBuffer(in.getString());
in.read();
}
switch (c = in.read()) {
case 'b': c = '\b'; break;
case 'f': c = '\f'; break;
case 'n': c = '\n'; break;
case 'r': c = '\r'; break;
case 't': c = '\t'; break;
case 'v': c = '\u000B'; break;
// \v a late addition to the ECMA spec.
// '\v' doesn't seem to be valid Java.
default:
if (isDigit(c) && c < '8') {
val = c - '0';
c = in.read();
if (isDigit(c) && c < '8') {
val = 8 * val + c - '0';
c = in.read();
if (isDigit(c) && c < '8') {