-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scanner.java
executable file
·1471 lines (1404 loc) · 49.8 KB
/
Scanner.java
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
/*
* Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.javac.parser;
import com.sun.tools.javac.code.Source;
import com.sun.tools.javac.file.JavacFileManager;
import com.sun.tools.javac.util.*;
import java.nio.CharBuffer;
import static com.sun.tools.javac.parser.Token.*;
import static com.sun.tools.javac.util.LayoutCharacters.*;
/**
* The lexical analyzer maps an input stream consisting of
* ASCII characters and Unicode escapes into a token sequence.
*
* <p><b>This is NOT part of any supported API.
* If you write code that depends on this, you do so at your own risk.
* This code and its internal interfaces are subject to change or
* deletion without notice.</b>
*/
public class Scanner implements Lexer {
/**
* Are surrogates supported?
*/
final static boolean surrogatesSupported = surrogatesSupported();
/* Output variables; set by nextToken():
*/
private static final boolean hexFloatsWork = hexFloatsWork();
private static boolean scannerDebug = false;
/**
* The log to be used for error reporting.
*/
private final Log log;
/**
* The name table.
*/
private final Names names;
/**
* The keyword table.
*/
private final Keywords keywords;
/**
* Has a @deprecated been encountered in last doc comment?
* this needs to be reset by client.
*/
protected boolean deprecatedFlag = false;
/**
* The token, set by nextToken().
*/
// 调用nextToken()方法生成的Token对象会赋值给一个名称为token的成员变量
private Token token;
/**
* Allow hex floating-point literals.
*/
private boolean allowHexFloats;
/**
* Allow binary literals.
*/
private boolean allowBinaryLiterals;
/**
* Allow underscores in literals.
*/
private boolean allowUnderscoresInLiterals;
/**
* The source language setting.
*/
private Source source;
/**
* The token's position, 0-based offset from beginning of text.
*/
private int pos;
/**
* Character position just after the last character of the token.
*/
private int endPos;
/**
* The last character position of the previous token.
*/
private int prevEndPos;
/**
* The position where a lexical error occurred;
*/
private int errPos = Position.NOPOS;
/**
* The name of an identifier or token:
*/
private Name name;
/**
* the radix of a numeric literal token.
*/
private int radix;
/**
* A character buffer for literals.
*/
// sbuf数组按顺序暂存读入的字符
private char[] sbuf = new char[128];
// sp指示了sbuf中下一个可用的位置
// 每次调用nextToken()方法,sp就会被初始化为0
private int sp;
/**
* The input buffer, index of next chacter to be read,
* index of one past last character in buffer.
*/
// 保存了从Java源文件中读入的所有字符,最后一个数组元素的值为EOI,EOI其实就是一个值为0x1A的常量,表示已经没有可读取的字符
private char[] buf;
// bp保存了buf数组中当前要处理的字符的位置,初始化时将bp设置为-1
private int bp;
// buflen保存了buf数组中可读字符的数量,或者说指向了buf数组中可读取字符的最大下标,不包括下标值为buflen的元素
private int buflen;
private int eofPos;
/**
* The current character.
*/
private char ch;
/**
* The buffer index of the last converted unicode character
*/
private int unicodeConversionBp = -1;
/**
* Common code for constructors.
*/
private Scanner(ScannerFactory fac) {
log = fac.log;
names = fac.names;
keywords = fac.keywords;
source = fac.source;
allowBinaryLiterals = source.allowBinaryLiterals();
allowHexFloats = source.allowHexFloats();
allowUnderscoresInLiterals = source.allowUnderscoresInLiterals();
}
/**
* Create a scanner from the input buffer. buffer must implement
* array() and compact(), and remaining() must be less than limit().
*/
protected Scanner(ScannerFactory fac, CharBuffer buffer) {
// JavacFileManager.toArray(buffer):将buffer转化为数组
this(fac, JavacFileManager.toArray(buffer), buffer.limit());
}
/**
* Create a scanner from the input array. This method might
* modify the array. To avoid copying the input array, ensure
* that {@code inputLength < input.length} or
* {@code input[input.length -1]} is a white space character.
*
* @param fac the factory which created this Scanner
* @param input the input, might be modified
* @param inputLength the size of the input.
* Must be positive and less than or equal to input.length.
*/
protected Scanner(ScannerFactory fac, char[] input, int inputLength) {
this(fac);
eofPos = inputLength;
if (inputLength == input.length) {
if (input.length > 0 && Character.isWhitespace(input[input.length - 1])) {
inputLength--;
} else {
char[] newInput = new char[inputLength + 1];
System.arraycopy(input, 0, newInput, 0, input.length);
input = newInput;
}
}
buf = input;
buflen = inputLength;
buf[buflen] = EOI;
bp = -1;
// 在处理开始时,通常会调用scanChar()方法将bp值更新为下一个要处理字符的下标位置
// bp加1,要处理的ch致为buf的第一个字符
scanChar();
}
private static boolean hexFloatsWork() {
try {
Float.valueOf("0x1.0p1");
return true;
} catch (NumberFormatException ex) {
return false;
}
}
private static boolean surrogatesSupported() {
try {
Character.isHighSurrogate('a');
return true;
} catch (NoSuchMethodError ex) {
return false;
}
}
/**
* Report an error at the given position using the provided arguments.
*/
private void lexError(int pos, String key, Object... args) {
log.error(pos, key, args);
token = ERROR;
errPos = pos;
}
/**
* Report an error at the current token position using the provided
* arguments.
*/
private void lexError(String key, Object... args) {
lexError(pos, key, args);
}
/**
* Convert an ASCII digit from its base (8, 10, or 16)
* to its value.
*/
private int digit(int base) {
char c = ch;
int result = Character.digit(c, base);
if (result >= 0 && c > 0x7f) {
lexError(pos + 1, "illegal.nonascii.digit");
ch = "0123456789abcdef".charAt(result);
}
return result;
}
/**
* Convert unicode escape; bp points to initial '\' character
* (Spec 3.3).
*/
private void convertUnicode() {
if (ch == '\\' && unicodeConversionBp != bp) {
bp++;
ch = buf[bp];
if (ch == 'u') {
do {
bp++;
ch = buf[bp];
} while (ch == 'u');
int limit = bp + 3;
if (limit < buflen) {
int d = digit(16);
int code = d;
while (bp < limit && d >= 0) {
bp++;
ch = buf[bp];
d = digit(16);
code = (code << 4) + d;
}
if (d >= 0) {
ch = (char) code;
unicodeConversionBp = bp;
return;
}
}
lexError(bp, "illegal.unicode.esc");
} else {
bp--;
ch = '\\';
}
}
}
/**
* Read next character.
*/
// bp值加1,将ch更新为buf数组中保存的下一个待处理的字符
private void scanChar() {
ch = buf[++bp];
if (ch == '\\') {
convertUnicode();
}
}
/**
* Read next character in comment, skipping over double '\' characters.
*/
private void scanCommentChar() {
scanChar();
if (ch == '\\') {
if (buf[bp + 1] == '\\' && unicodeConversionBp != bp) {
bp++;
} else {
convertUnicode();
}
}
}
/**
* Append a character to sbuf.
*/
// sbuf扩容
private void putChar(char ch) {
if (sp == sbuf.length) {
char[] newsbuf = new char[sbuf.length * 2];
System.arraycopy(sbuf, 0, newsbuf, 0, sbuf.length);
sbuf = newsbuf;
}
sbuf[sp++] = ch;
}
/**
* Read next character in character or string literal and copy into sbuf.
*/
// 读取下一个字符或string文字,并且复制进sbuf
// 处理字符常量
private void scanLitChar() {
if (ch == '\\') { // 处理转义字符
if (buf[bp + 1] == '\\' && unicodeConversionBp != bp) {
bp++;
putChar('\\');
scanChar();
} else {
scanChar();
switch (ch) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
char leadch = ch;
int oct = digit(8);
scanChar();
if ('0' <= ch && ch <= '7') {
// 将八进制表示的数转换为十进制表示,然后强制转换为char类型后调用putChar()方法
oct = oct * 8 + digit(8);
scanChar();
if (leadch <= '3' && '0' <= ch && ch <= '7') {
oct = oct * 8 + digit(8);
scanChar();
}
}
putChar((char) oct);
break;
case 'b':
putChar('\b');
scanChar();
break;
case 't':
putChar('\t');
scanChar();
break;
case 'n':
putChar('\n');
scanChar();
break;
case 'f':
putChar('\f');
scanChar();
break;
case 'r':
putChar('\r');
scanChar();
break;
case '\'':
putChar('\'');
scanChar();
break;
case '\"':
putChar('\"');
scanChar();
break;
case '\\':
putChar('\\');
scanChar();
break;
default:
lexError(bp, "illegal.esc.char");
}
}
} else if (bp != buflen) {// 处理非转义字符
putChar(ch);
scanChar();
}
}
private void scanDigits(int digitRadix) {
char saveCh;
int savePos;
do {
if (ch != '_') {
putChar(ch);
} else {
if (!allowUnderscoresInLiterals) {
lexError("unsupported.underscore.lit", source.name);
allowUnderscoresInLiterals = true;
}
}
saveCh = ch;
savePos = bp;
scanChar();
} while (digit(digitRadix) >= 0 || ch == '_');
if (saveCh == '_') {
lexError(savePos, "illegal.underscore");
}
}
/**
* Read fractional part of hexadecimal floating point number.
*/
private void scanHexExponentAndSuffix() {
if (ch == 'p' || ch == 'P') {
putChar(ch);
scanChar();
skipIllegalUnderscores();
if (ch == '+' || ch == '-') {
putChar(ch);
scanChar();
}
skipIllegalUnderscores();
if ('0' <= ch && ch <= '9') {
scanDigits(10);
if (!allowHexFloats) {
lexError("unsupported.fp.lit", source.name);
allowHexFloats = true;
} else if (!hexFloatsWork) {
lexError("unsupported.cross.fp.lit");
}
} else {
lexError("malformed.fp.lit");
}
} else {
lexError("malformed.fp.lit");
}
if (ch == 'f' || ch == 'F') {
putChar(ch);
scanChar();
token = FLOATLITERAL;
} else {
if (ch == 'd' || ch == 'D') {
putChar(ch);
scanChar();
}
token = DOUBLELITERAL;
}
}
/**
* Read fractional part of floating point number.
*/
private void scanFraction() {
skipIllegalUnderscores();
if ('0' <= ch && ch <= '9') {
scanDigits(10);
}
int sp1 = sp;
if (ch == 'e' || ch == 'E') {
putChar(ch);
scanChar();
skipIllegalUnderscores();
if (ch == '+' || ch == '-') {
putChar(ch);
scanChar();
}
skipIllegalUnderscores();
if ('0' <= ch && ch <= '9') {
scanDigits(10);
return;
}
lexError("malformed.fp.lit");
sp = sp1;
}
}
/**
* Read fractional part and 'd' or 'f' suffix of floating point number.
*/
private void scanFractionAndSuffix() {
radix = 10;
scanFraction();
if (ch == 'f' || ch == 'F') {
putChar(ch);
scanChar();
token = FLOATLITERAL;
} else {
if (ch == 'd' || ch == 'D') {
putChar(ch);
scanChar();
}
token = DOUBLELITERAL;
}
}
/**
* Read fractional part and 'd' or 'f' suffix of floating point number.
*/
private void scanHexFractionAndSuffix(boolean seendigit) {
radix = 16;
Assert.check(ch == '.');
putChar(ch);
scanChar();
skipIllegalUnderscores();
if (digit(16) >= 0) {
seendigit = true;
scanDigits(16);
}
if (!seendigit) {
lexError("invalid.hex.number");
} else {
scanHexExponentAndSuffix();
}
}
private void skipIllegalUnderscores() {
if (ch == '_') {
lexError(bp, "illegal.underscore");
while (ch == '_') {
scanChar();
}
}
}
/**
* Read a number.
*
* @param radix The radix of the number; one of 2, j8, 10, 16.
*/
private void scanNumber(int radix) {
this.radix = radix;
// for octal, allow base-10 digit in case it's a float literal
int digitRadix = (radix == 8 ? 10 : radix);
boolean seendigit = false;
if (digit(digitRadix) >= 0) {
seendigit = true;
scanDigits(digitRadix);
}
if (radix == 16 && ch == '.') {
scanHexFractionAndSuffix(seendigit);
} else if (seendigit && radix == 16 && (ch == 'p' || ch == 'P')) {
scanHexExponentAndSuffix();
} else if (digitRadix == 10 && ch == '.') {
putChar(ch);
scanChar();
scanFractionAndSuffix();
} else if (digitRadix == 10 &&
(ch == 'e' || ch == 'E' ||
ch == 'f' || ch == 'F' ||
ch == 'd' || ch == 'D')) {
scanFractionAndSuffix();
} else {
if (ch == 'l' || ch == 'L') {
scanChar();
token = LONGLITERAL;
} else {
token = INTLITERAL;
}
}
}
/**
* Read an identifier.
*/
// 将所有组成标识符的字符从buf数组中读取出来按顺序存储到sbuf数组中,
// 最后作为参数调用names.fromChars()方法获取NameImpl对象方法
// 对字母、数字、下划线、美元符号及一些控制字符不做任何处理,
// 直接通过break跳出switch语句后重新执行do-while循环,
// 然后将这些字符存储到sbuf数组中,这都是标识符的一部分
private void scanIdent() {
boolean isJavaIdentifierPart;
char high;
do {
// sbuf数组不能存储更多字符,调用putChar()方法进行扩容
if (sp == sbuf.length) {
putChar(ch);
} else {
sbuf[sp++] = ch;
}
// optimization, was: putChar(ch);
scanChar();
switch (ch) {
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
case 'S':
case 'T':
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':
case '$':
case '_':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '\u0000':
case '\u0001':
case '\u0002':
case '\u0003':
case '\u0004':
case '\u0005':
case '\u0006':
case '\u0007':
case '\u0008':
case '\u000E':
case '\u000F':
case '\u0010':
case '\u0011':
case '\u0012':
case '\u0013':
case '\u0014':
case '\u0015':
case '\u0016':
case '\u0017':
case '\u0018':
case '\u0019':
case '\u001B':
case '\u007F':
break;
case '\u001A': // EOI is also a legal identifier part
if (bp >= buflen) {// 已经没有待处理的字符了
name = names.fromChars(sbuf, 0, sp);
token = keywords.key(name);
return;
}
break;
default:
if (ch < '\u0080') {
// ch是ASCII编码中的一个字符
// 所有合法的ASCII字符已经在上面的case分支中进行了处理
isJavaIdentifierPart = false;
} else {
// 获取高代理项
high = scanSurrogates();
if (high != 0) {
if (sp == sbuf.length) {
putChar(high);
} else {
sbuf[sp++] = high;
}
// 方法会判断通过高代理项和低代理项表示的字符是否为合法
// 标识符的首字符
isJavaIdentifierPart = Character.isJavaIdentifierPart(
Character.toCodePoint(high, ch));
} else {
isJavaIdentifierPart = Character.isJavaIdentifierPart(ch);
}
}
// 生成Name和Token对象
if (!isJavaIdentifierPart) {
// 生成Name对象
name = names.fromChars(sbuf, 0, sp);
// 转化为Token对象
token = keywords.key(name);
return;
}
}
} while (true);
}
/**
* Scan surrogate pairs. If 'ch' is a high surrogate and
* the next character is a low surrogate, then put the low
* surrogate in 'ch', and return the high surrogate.
* otherwise, just return 0.
*/
private char scanSurrogates() {
if (surrogatesSupported && Character.isHighSurrogate(ch)) {
char high = ch;
scanChar();
if (Character.isLowSurrogate(ch)) {
return high;
}
ch = high;
}
return 0;
}
/**
* Return true if ch can be part of an operator.
*/
// 判断是否为标识符号或标识符号的一部分
private boolean isSpecial(char ch) {
switch (ch) {
case '!':
case '%':
case '&':
case '*':
case '?':
case '+':
case '-':
case ':':
case '<':
case '=':
case '>':
case '^':
case '|':
case '~':
case '@':
return true;
default:
return false;
}
}
/**
* Read longest possible sequence of special characters and convert
* to token.
*/
// 扫描出完整的标识符号
private void scanOperator() {
while (true) {
putChar(ch);
Name newname = names.fromChars(sbuf, 0, sp);
if (keywords.key(newname) == IDENTIFIER) {
sp--;
break;
}
name = newname;
token = keywords.key(newname);
scanChar();
if (!isSpecial(ch)) {
break;
}
}
}
/**
* Scan a documention comment; determine if a deprecated tag is present.
* Called once the initial /, * have been skipped, positioned at the second *
* (which is treated as the beginning of the first line).
* Stops positioned at the closing '/'.
*/
@SuppressWarnings("fallthrough")
private void scanDocComment() {
boolean deprecatedPrefix = false;
forEachLine:
while (bp < buflen) {
// Skip optional WhiteSpace at beginning of line
while (bp < buflen && (ch == ' ' || ch == '\t' || ch == FF)) {
scanCommentChar();
}
// Skip optional consecutive Stars
while (bp < buflen && ch == '*') {
scanCommentChar();
if (ch == '/') {
return;
}
}
// Skip optional WhiteSpace after Stars
while (bp < buflen && (ch == ' ' || ch == '\t' || ch == FF)) {
scanCommentChar();
}
deprecatedPrefix = false;
// At beginning of line in the JavaDoc sense.
if (bp < buflen && ch == '@' && !deprecatedFlag) {
scanCommentChar();
if (bp < buflen && ch == 'd') {
scanCommentChar();
if (bp < buflen && ch == 'e') {
scanCommentChar();
if (bp < buflen && ch == 'p') {
scanCommentChar();
if (bp < buflen && ch == 'r') {
scanCommentChar();
if (bp < buflen && ch == 'e') {
scanCommentChar();
if (bp < buflen && ch == 'c') {
scanCommentChar();
if (bp < buflen && ch == 'a') {
scanCommentChar();
if (bp < buflen && ch == 't') {
scanCommentChar();
if (bp < buflen && ch == 'e') {
scanCommentChar();
if (bp < buflen && ch == 'd') {
deprecatedPrefix = true;
scanCommentChar();
}
}
}
}
}
}
}
}
}
}
}
if (deprecatedPrefix && bp < buflen) {
if (Character.isWhitespace(ch)) {
deprecatedFlag = true;
} else if (ch == '*') {
scanCommentChar();
if (ch == '/') {
deprecatedFlag = true;
return;
}
}
}
// Skip rest of line
while (bp < buflen) {
switch (ch) {
case '*':
scanCommentChar();
if (ch == '/') {
return;
}
break;
case CR: // (Spec 3.4)
scanCommentChar();
if (ch != LF) {
continue forEachLine;
}
/* fall through to LF case */
case LF: // (Spec 3.4)
scanCommentChar();
continue forEachLine;
default:
scanCommentChar();
}
} // rest of line
} // forEachLine
return;
}
/**
* The value of a literal token, recorded as a string.
* For integers, leading 0x and 'l' suffixes are suppressed.
*/
@Override
public String stringVal() {
return new String(sbuf, 0, sp);
}
/**
* Read token.
*/
@Override
public void nextToken() {
try {
prevEndPos = endPos;
sp = 0;
while (true) {
pos = bp;
// switch语句会根据首个出现的字符来判断可能生成的Token对象
switch (ch) { // switch语句所有的处理分支可大概分为以下8类
/*
1、特殊字符的处理
2、标识符的处理
3、数字的处理
4、分隔符的处理
5、斜线作为首字符的处理
6、单引号作为首字符的处理
7、双引号作为首字符的处理
8、默认的处理
*/
// -------------------------------特殊字符的处理
// 这些字符都不会生成具体的Token对象,在当前的词法分析阶段调用scanChar()方法直接摒弃这些字符
case ' ': // (Spec 3.6) 空格
case '\t': // (Spec 3.6) 水平制表符
case FF: // (Spec 3.6) 换行换页符
// 将空格、水平制表符与换页符当作空白字符来处理
do {
scanChar();
} while (ch == ' ' || ch == '\t' || ch == FF);
endPos = bp;
processWhiteSpace();
break;
case LF: // (Spec 3.4) 换行符
scanChar();
endPos = bp;
processLineTerminator();
break;
case CR: // (Spec 3.4) 回车
scanChar();
if (ch == LF) { // 换行
scanChar();
}
endPos = bp;
processLineTerminator();
break;
// ----------------------------------特殊字符的处理
// ----------------------------------标识符的处理
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
case 'S':
case 'T':
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':