-
Notifications
You must be signed in to change notification settings - Fork 187
/
Lexer.java
1489 lines (1366 loc) · 33.9 KB
/
Lexer.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) 2007, 2015 Wind River Systems, Inc. and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Markus Schorn - initial API and implementation
* Mike Kucera (IBM) - UTF string literals
* Sergey Prigogin (Google)
* Richard Eames
*******************************************************************************/
package org.eclipse.cdt.internal.core.parser.scanner;
import org.eclipse.cdt.core.parser.IGCCToken;
import org.eclipse.cdt.core.parser.IProblem;
import org.eclipse.cdt.core.parser.IToken;
import org.eclipse.cdt.core.parser.IncludeExportPatterns;
import org.eclipse.cdt.core.parser.OffsetLimitReachedException;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
/**
* In short this class converts line endings (to '\n') and trigraphs
* (to their corresponding character),
* removes line-splices, comments and whitespace other than newline.
* Returns preprocessor tokens.
* <p>
* In addition to the preprocessor tokens the following tokens may also be returned:
* {@link #tBEFORE_INPUT}, {@link IToken#tEND_OF_INPUT}, {@link IToken#tCOMPLETION}.
* <p>
* Number literals are split up into {@link IToken#tINTEGER} and {@link IToken#tFLOATINGPT}.
* No checks are done on the number literals.
* <p>
* UNCs are accepted, however characters from outside of the basic source character set are
* not converted to UNCs. Rather than that they are tested with
* {@link Character#isUnicodeIdentifierPart(char)} and may be accepted as part of an
* identifier.
* <p>
* The characters in string literals and char-literals are left as they are found, no conversion to
* an execution character-set is performed.
*/
final public class Lexer implements ITokenSequence {
public static final int tBEFORE_INPUT = IToken.FIRST_RESERVED_SCANNER;
public static final int tNEWLINE = IToken.FIRST_RESERVED_SCANNER + 1;
public static final int tQUOTE_HEADER_NAME = IToken.FIRST_RESERVED_SCANNER + 2;
public static final int tSYSTEM_HEADER_NAME = IToken.FIRST_RESERVED_SCANNER + 3;
public static final int tOTHER_CHARACTER = IToken.FIRST_RESERVED_SCANNER + 4;
private static final int END_OF_INPUT = -1;
private static final int ORIGIN_LEXER = OffsetLimitReachedException.ORIGIN_LEXER;
public final static class LexerOptions implements Cloneable {
public boolean fSupportDollarInIdentifiers = true;
public boolean fSupportAtSignInIdentifiers = true;
public boolean fSupportMinAndMax = true;
public boolean fCreateImageLocations = true;
public boolean fSupportSlashPercentComments = false;
public boolean fSupportUTFLiterals = true;
public boolean fSupportRawStringLiterals = false;
public boolean fSupportUserDefinedLiterals = false;
public boolean fSupportDigitSeparators = false;
public boolean fSupportThreeWayComparisonOperator = false;
public IncludeExportPatterns fIncludeExportPatterns;
@Override
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
return null;
}
}
}
// configuration
private final LexerOptions fOptions;
private boolean fSupportContentAssist = false;
private final ILexerLog fLog;
private final Object fSource;
// the input to the lexer
private final AbstractCharArray fInput;
private final int fStart;
private int fLimit;
// after phase 3 (newline, trigraph, line-splice)
private int fOffset;
private int fEndOffset;
private int fCharPhase3;
private boolean fInsideIncludeDirective = false;
private Token fToken;
private Token fLastToken;
// For the few cases where we have to lookahead more than one character
private int fMarkPhase3Offset;
private int fMarkPhase3EndOffset;
private int fMarkPhase3PrefetchedChar;
// To store the entire state. Note that we don't reuse the variables
// used for saving the phase3 because calls to markPhase3() and
// restorePhase3() can occur in between calls to saveState() and
// restoreState().
private int fMarkOffset;
private int fMarkEndOffset;
private int fMarkPrefetchedChar;
private boolean fMarkInsideIncludeDirective;
private Token fMarkToken;
private Token fMarkLastToken;
public Lexer(char[] input, LexerOptions options, ILexerLog log, Object source) {
this(new CharArray(input), 0, input.length, options, log, source);
}
public Lexer(AbstractCharArray input, LexerOptions options, ILexerLog log, Object source) {
this(input, 0, input.tryGetLength(), options, log, source);
}
public Lexer(AbstractCharArray input, int start, int end, LexerOptions options, ILexerLog log, Object source) {
fInput = input;
fStart = fOffset = fEndOffset = start;
fLimit = end;
fOptions = options;
fLog = log;
fSource = source;
fLastToken = fToken = new Token(tBEFORE_INPUT, source, start, start);
nextCharPhase3();
}
private boolean isValidOffset(int pos) {
if (fLimit < 0)
return fInput.isValidOffset(pos);
return pos < fLimit;
}
/**
* Returns the source that is attached to the tokens generated by this lexer
*/
public Object getSource() {
return fSource;
}
/**
* Resets the lexer to the first char and prepares for content-assist mode.
*/
public void setContentAssistMode(int offset) {
fSupportContentAssist = true;
if (isValidOffset(offset)) {
fLimit = offset;
}
// re-initialize
fOffset = fEndOffset = fStart;
nextCharPhase3();
}
public boolean isContentAssistMode() {
return fSupportContentAssist;
}
/**
* Call this before consuming the name-token in the include directive. It causes the header-file
* tokens to be created.
*/
public void setInsideIncludeDirective(boolean val) {
fInsideIncludeDirective = val;
}
/**
* Returns the current preprocessor token, does not advance.
*/
@Override
public Token currentToken() {
return fToken;
}
/**
* Returns the endoffset of the token before the current one.
*/
@Override
public int getLastEndOffset() {
return fLastToken.getEndOffset();
}
/**
* Advances to the next token, skipping whitespace other than newline.
* @throws OffsetLimitReachedException when completion is requested in a literal or a header-name.
*/
@Override
public Token nextToken() throws OffsetLimitReachedException {
fLastToken = fToken;
return fToken = fetchToken();
}
public boolean currentTokenIsFirstOnLine() {
final int type = fLastToken.getType();
return type == tNEWLINE || type == tBEFORE_INPUT;
}
/**
* Advances to the next newline or the end of input. The newline will not be consumed. If the
* current token is a newline no action is performed.
* Returns the end offset of the last token before the newline.
* @param origin parameter for the {@link OffsetLimitReachedException} when it has to be thrown.
* @since 5.0
*/
public final int consumeLine(int origin) throws OffsetLimitReachedException {
Token t = fToken;
Token lt = null;
while (true) {
switch (t.getType()) {
case IToken.tCOMPLETION:
if (lt != null) {
fLastToken = lt;
}
fToken = t;
throw new OffsetLimitReachedException(origin, t);
case IToken.tEND_OF_INPUT:
if (fSupportContentAssist) {
t.setType(IToken.tCOMPLETION);
throw new OffsetLimitReachedException(origin, t);
}
//$FALL-THROUGH$
case Lexer.tNEWLINE:
fToken = t;
if (lt != null) {
fLastToken = lt;
}
return getLastEndOffset();
}
lt = t;
t = fetchToken();
}
}
/**
* Advances to the next pound token that starts a preprocessor directive.
* @return pound token of the directive or end-of-input.
* @throws OffsetLimitReachedException when completion is requested in a literal or an header-name.
*/
public Token nextDirective() throws OffsetLimitReachedException {
Token t0;
Token t1 = fToken;
for (;;) {
t0 = t1;
t1 = fetchToken();
final int tt1 = t1.getType();
if (tt1 == IToken.tEND_OF_INPUT || tt1 == IToken.tCOMPLETION)
break;
if (tt1 == IToken.tPOUND) {
final int tt0 = t0.getType();
if (tt0 == tNEWLINE || tt0 == tBEFORE_INPUT)
break;
}
}
fLastToken = t0;
return fToken = t1;
}
/**
* Computes the next token.
*/
private Token fetchToken() throws OffsetLimitReachedException {
while (true) {
final int start = fOffset;
final int c = fCharPhase3;
final int d = nextCharPhase3();
switch (c) {
case END_OF_INPUT:
return newToken(IToken.tEND_OF_INPUT, start);
case '\n':
fInsideIncludeDirective = false;
return newToken(Lexer.tNEWLINE, start);
case ' ':
case '\t':
case 0xb: // vertical tab
case '\f':
case '\r':
continue;
case 'L':
switch (d) {
case 'R':
if (fOptions.fSupportRawStringLiterals) {
markPhase3();
if (nextCharPhase3() == '"') {
nextCharPhase3();
return rawStringLiteral(start, 3, IToken.tLSTRING);
}
restorePhase3();
}
break;
case '"':
nextCharPhase3();
return stringLiteral(start, 2, IToken.tLSTRING);
case '\'':
nextCharPhase3();
return charLiteral(start, IToken.tLCHAR);
}
return identifier(start, 1);
case 'u':
case 'U':
if (fOptions.fSupportUTFLiterals) {
switch (d) {
case 'R':
if (fOptions.fSupportRawStringLiterals) {
markPhase3();
if (nextCharPhase3() == '"') {
nextCharPhase3();
return rawStringLiteral(start, 3, c == 'u' ? IToken.tUTF16STRING : IToken.tUTF32STRING);
}
restorePhase3();
}
break;
case '"':
nextCharPhase3();
return stringLiteral(start, 2, c == 'u' ? IToken.tUTF16STRING : IToken.tUTF32STRING);
case '\'':
nextCharPhase3();
return charLiteral(start, c == 'u' ? IToken.tUTF16CHAR : IToken.tUTF32CHAR);
case '8':
if (c == 'u') {
markPhase3();
switch (nextCharPhase3()) {
case 'R':
if (fOptions.fSupportRawStringLiterals && nextCharPhase3() == '"') {
nextCharPhase3();
return rawStringLiteral(start, 4, IToken.tSTRING);
}
break;
case '"':
nextCharPhase3();
return stringLiteral(start, 3, IToken.tSTRING);
}
restorePhase3();
}
break;
}
}
return identifier(start, 1);
case 'R':
if (fOptions.fSupportRawStringLiterals && d == '"') {
nextCharPhase3();
return rawStringLiteral(start, 2, IToken.tSTRING);
}
return identifier(start, 1);
case '"':
if (fInsideIncludeDirective) {
return headerName(start, true);
}
return stringLiteral(start, 1, IToken.tSTRING);
case '\'':
return charLiteral(start, IToken.tCHAR);
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 '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 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'S':
case 'T':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
case '_':
return identifier(start, 1);
case '$':
if (fOptions.fSupportDollarInIdentifiers) {
return identifier(start, 1);
}
break;
case '@':
if (fOptions.fSupportAtSignInIdentifiers) {
return identifier(start, 1);
}
break;
case '\\':
switch (d) {
case 'u':
case 'U':
nextCharPhase3();
return identifier(start, 2);
}
return newToken(tOTHER_CHARACTER, start, 1);
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return number(start, 1, false);
case '.':
switch (d) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
nextCharPhase3();
return number(start, 2, true);
case '.':
markPhase3();
if (nextCharPhase3() == '.') {
nextCharPhase3();
return newToken(IToken.tELLIPSIS, start);
}
restorePhase3();
break;
case '*':
nextCharPhase3();
return newToken(IToken.tDOTSTAR, start);
}
return newToken(IToken.tDOT, start);
case '#':
if (d == '#') {
nextCharPhase3();
return newToken(IToken.tPOUNDPOUND, start);
}
return newToken(IToken.tPOUND, start);
case '{':
return newToken(IToken.tLBRACE, start);
case '}':
return newToken(IToken.tRBRACE, start);
case '[':
return newToken(IToken.tLBRACKET, start);
case ']':
return newToken(IToken.tRBRACKET, start);
case '(':
return newToken(IToken.tLPAREN, start);
case ')':
return newToken(IToken.tRPAREN, start);
case ';':
return newToken(IToken.tSEMI, start);
case ':':
switch (d) {
case ':':
nextCharPhase3();
return newToken(IToken.tCOLONCOLON, start);
case '>':
nextCharPhase3();
return newDigraphToken(IToken.tRBRACKET, start);
}
return newToken(IToken.tCOLON, start);
case '?':
return newToken(IToken.tQUESTION, start);
case '+':
switch (d) {
case '+':
nextCharPhase3();
return newToken(IToken.tINCR, start);
case '=':
nextCharPhase3();
return newToken(IToken.tPLUSASSIGN, start);
}
return newToken(IToken.tPLUS, start);
case '-':
switch (d) {
case '>':
int e = nextCharPhase3();
if (e == '*') {
nextCharPhase3();
return newToken(IToken.tARROWSTAR, start);
}
return newToken(IToken.tARROW, start);
case '-':
nextCharPhase3();
return newToken(IToken.tDECR, start);
case '=':
nextCharPhase3();
return newToken(IToken.tMINUSASSIGN, start);
}
return newToken(IToken.tMINUS, start);
case '*':
if (d == '=') {
nextCharPhase3();
return newToken(IToken.tSTARASSIGN, start);
}
return newToken(IToken.tSTAR, start);
case '/':
switch (d) {
case '=':
nextCharPhase3();
return newToken(IToken.tDIVASSIGN, start);
case '/':
nextCharPhase3();
lineComment(start);
continue;
case '*':
blockComment(start, '*');
continue;
case '%':
if (fOptions.fSupportSlashPercentComments) {
blockComment(start, '%');
continue;
}
break;
}
return newToken(IToken.tDIV, start);
case '%':
switch (d) {
case '=':
nextCharPhase3();
return newToken(IToken.tMODASSIGN, start);
case '>':
nextCharPhase3();
return newDigraphToken(IToken.tRBRACE, start);
case ':':
final int e = nextCharPhase3();
if (e == '%') {
markPhase3();
if (nextCharPhase3() == ':') {
nextCharPhase3();
return newDigraphToken(IToken.tPOUNDPOUND, start);
}
restorePhase3();
}
return newDigraphToken(IToken.tPOUND, start);
}
return newToken(IToken.tMOD, start);
case '^':
if (d == '=') {
nextCharPhase3();
return newToken(IToken.tXORASSIGN, start);
}
return newToken(IToken.tXOR, start);
case '&':
switch (d) {
case '&':
nextCharPhase3();
return newToken(IToken.tAND, start);
case '=':
nextCharPhase3();
return newToken(IToken.tAMPERASSIGN, start);
}
return newToken(IToken.tAMPER, start);
case '|':
switch (d) {
case '|':
nextCharPhase3();
return newToken(IToken.tOR, start);
case '=':
nextCharPhase3();
return newToken(IToken.tBITORASSIGN, start);
}
return newToken(IToken.tBITOR, start);
case '~':
return newToken(IToken.tBITCOMPLEMENT, start);
case '!':
if (d == '=') {
nextCharPhase3();
return newToken(IToken.tNOTEQUAL, start);
}
return newToken(IToken.tNOT, start);
case '=':
if (d == '=') {
nextCharPhase3();
return newToken(IToken.tEQUAL, start);
}
return newToken(IToken.tASSIGN, start);
case '<':
if (fInsideIncludeDirective) {
return headerName(start, false);
}
switch (d) {
case '=':
final int se = nextCharPhase3();
if (fOptions.fSupportThreeWayComparisonOperator) {
if (se == '>') {
nextCharPhase3();
return newToken(IToken.tTHREEWAYCOMPARISON, start);
}
}
return newToken(IToken.tLTEQUAL, start);
case '<':
final int e = nextCharPhase3();
if (e == '=') {
nextCharPhase3();
return newToken(IToken.tSHIFTLASSIGN, start);
}
return newToken(IToken.tSHIFTL, start);
case '?':
if (fOptions.fSupportMinAndMax) {
nextCharPhase3();
return newToken(IGCCToken.tMIN, start);
}
break;
case ':':
// 2.5-3
markPhase3();
if (nextCharPhase3() != ':') {
return newDigraphToken(IToken.tLBRACKET, start);
}
switch (nextCharPhase3()) {
case ':':
case '>':
restorePhase3();
nextCharPhase3();
return newDigraphToken(IToken.tLBRACKET, start);
}
restorePhase3();
break;
case '%':
nextCharPhase3();
return newDigraphToken(IToken.tLBRACE, start);
}
return newToken(IToken.tLT, start);
case '>':
switch (d) {
case '=':
nextCharPhase3();
return newToken(IToken.tGTEQUAL, start);
case '>':
final int e = nextCharPhase3();
if (e == '=') {
nextCharPhase3();
return newToken(IToken.tSHIFTRASSIGN, start);
}
return newToken(IToken.tSHIFTR, start);
case '?':
if (fOptions.fSupportMinAndMax) {
nextCharPhase3();
return newToken(IGCCToken.tMAX, start);
}
break;
}
return newToken(IToken.tGT, start);
case ',':
return newToken(IToken.tCOMMA, start);
default:
// in case we have some other letter to start an identifier
if (Character.isUnicodeIdentifierStart((char) c)) {
return identifier(start, 1);
}
break;
}
// handles for instance @
return newToken(tOTHER_CHARACTER, start, 1);
}
}
private Token newToken(int kind, int offset) {
return new Token(kind, fSource, offset, fOffset);
}
private Token newDigraphToken(int kind, int offset) {
return new TokenForDigraph(kind, fSource, offset, fOffset);
}
private Token newToken(final int kind, final int offset, final int imageLength) {
final int endOffset = fOffset;
final int sourceLen = endOffset - offset;
char[] image;
if (sourceLen != imageLength) {
image = getCharImage(offset, endOffset, imageLength);
} else {
image = new char[imageLength];
fInput.arraycopy(offset, image, 0, imageLength);
}
return new TokenWithImage(kind, fSource, offset, endOffset, image);
}
private void handleProblem(int problemID, char[] arg, int offset) {
fLog.handleProblem(problemID, arg, offset, fOffset);
}
private Token headerName(final int start, final boolean expectQuotes) throws OffsetLimitReachedException {
int length = 1;
boolean done = false;
int c = fCharPhase3;
loop: while (!done) {
switch (c) {
case END_OF_INPUT:
if (fSupportContentAssist) {
throw new OffsetLimitReachedException(ORIGIN_LEXER,
newToken((expectQuotes ? tQUOTE_HEADER_NAME : tSYSTEM_HEADER_NAME), start, length));
}
//$FALL-THROUGH$
case '\n':
handleProblem(IProblem.SCANNER_UNBOUNDED_STRING, getInputChars(start, fOffset), start);
break loop;
case '"':
done = expectQuotes;
break;
case '>':
done = !expectQuotes;
break;
}
length++;
c = nextCharPhase3();
}
return newToken((expectQuotes ? tQUOTE_HEADER_NAME : tSYSTEM_HEADER_NAME), start, length);
}
private void blockComment(final int start, final char trigger) {
// We can ignore line-splices, trigraphs and windows newlines when searching for the '*'
int pos = fEndOffset;
while (isValidOffset(pos)) {
if (fInput.get(pos++) == trigger) {
fEndOffset = pos;
if (nextCharPhase3() == '/') {
nextCharPhase3();
fLog.handleComment(true, start, fOffset, fInput);
return;
}
}
}
fCharPhase3 = END_OF_INPUT;
fOffset = fEndOffset = pos;
fLog.handleComment(true, start, pos, fInput);
}
private void lineComment(final int start) {
int c = fCharPhase3;
while (true) {
switch (c) {
case END_OF_INPUT:
case '\n':
fLog.handleComment(false, start, fOffset, fInput);
return;
}
c = nextCharPhase3();
}
}
private boolean isIdentifierStart(int c) {
return Character.isLetter((char) c) || Character.isDigit((char) c) || Character.isUnicodeIdentifierPart(c)
|| (fOptions.fSupportDollarInIdentifiers && c == '$')
|| (fOptions.fSupportAtSignInIdentifiers && c == '@') || c == '_';
}
private Token stringLiteral(final int start, int length, int tokenType) throws OffsetLimitReachedException {
boolean escaped = false;
boolean done = false;
int c = fCharPhase3;
loop: while (!done) {
switch (c) {
case END_OF_INPUT:
if (fSupportContentAssist) {
throw new OffsetLimitReachedException(ORIGIN_LEXER, newToken(tokenType, start, length));
}
//$FALL-THROUGH$
case '\n':
handleProblem(IProblem.SCANNER_UNBOUNDED_STRING, getInputChars(start, fOffset), start);
break loop;
case '\\':
escaped = !escaped;
break;
case '"':
if (!escaped) {
done = true;
}
escaped = false;
break;
default:
escaped = false;
break;
}
length++;
c = nextCharPhase3();
}
if (fOptions.fSupportUserDefinedLiterals && isUDLSuffixStart(c)) {
Token t = identifier(start + length, 0);
tokenType = IToken.tUSER_DEFINED_STRING_LITERAL;
length += t.getLength();
}
return newToken(tokenType, start, length);
}
private boolean isUDLSuffixStart(int c) {
// C++11 introduced a backward incompatible change breaking the following code:
// #define __STDC_FORMAT_MACROS
// #include <inttypes.h>
// #include <stdio.h>
//
// void test() {
// int64_t i64 = 123;
// printf("My int64: %"PRId64"\n", i64);
// }
//
// We follow the example of Clang and GCC that are working around this by interpreting literal
// suffixes that don't start with underscores as separate tokens, which allows them to expand
// as macros: http://llvm.org/viewvc/llvm-project?view=rev&revision=152287 and
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52538
return c == '_';
}
private Token rawStringLiteral(final int start, int length, int tokenType) throws OffsetLimitReachedException {
final int delimOffset = fOffset;
int delimEndOffset = delimOffset;
int offset;
for (;; delimEndOffset++) {
if (!fInput.isValidOffset(delimEndOffset)) {
offset = delimEndOffset;
break;
}
if (fInput.get(delimEndOffset) == '(') {
offset = delimEndOffset + 1;
break;
}
}
final int delimLength = delimEndOffset - delimOffset;
for (;; offset++) {
if (!fInput.isValidOffset(offset)) {
handleProblem(IProblem.SCANNER_UNBOUNDED_STRING, getInputChars(start, offset), start);
break;
}
final char c = fInput.get(offset);
if (c == ')') {
final int endingDoubleQuoteOffset = offset + delimLength + 1;
if (fInput.isValidOffset(endingDoubleQuoteOffset) && fInput.get(endingDoubleQuoteOffset) == '"') {
boolean prefixMatches = true;
for (int i = 0; i < delimLength; i++) {
if (fInput.get(offset + i + 1) != fInput.get(delimOffset + i)) {
prefixMatches = false;
break;
}
}
if (prefixMatches) {
offset = endingDoubleQuoteOffset + 1;
break;
}
}
}
}
fOffset = offset - 1;
fEndOffset = offset;
fCharPhase3 = 0;
nextCharPhase3();
if (fOptions.fSupportUserDefinedLiterals && isUDLSuffixStart(fCharPhase3)) {
Token t = identifier(offset, 0);
tokenType = IToken.tUSER_DEFINED_STRING_LITERAL;
offset += t.getLength();
}
return newToken(tokenType, start, offset - start);
}
private Token charLiteral(final int start, int tokenType) throws OffsetLimitReachedException {
boolean escaped = false;
boolean done = false;
int length = tokenType == IToken.tCHAR ? 1 : 2;
int c = fCharPhase3;
loop: while (!done) {
switch (c) {
case END_OF_INPUT:
if (fSupportContentAssist) {
throw new OffsetLimitReachedException(ORIGIN_LEXER, newToken(tokenType, start, length));
}
//$FALL-THROUGH$
case '\n':
handleProblem(IProblem.SCANNER_BAD_CHARACTER, getInputChars(start, fOffset), start);
break loop;
case '\\':
escaped = !escaped;
break;
case '\'':
if (!escaped) {
done = true;
}
escaped = false;
break;
default:
escaped = false;
break;
}
length++;
c = nextCharPhase3();
}
if (fOptions.fSupportUserDefinedLiterals && isIdentifierStart(c)) {
Token t = identifier(start + length, 0);
tokenType = IToken.tUSER_DEFINED_CHAR_LITERAL;
length += t.getLength();
}
return newToken(tokenType, start, length);
}
private Token identifier(int start, int length) {
int tokenKind = IToken.tIDENTIFIER;
boolean isPartOfIdentifier = true;
int c = fCharPhase3;
while (true) {
switch (c) {
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':