forked from xtravar/CppNet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
LexerSource.cs
827 lines (744 loc) · 18 KB
/
LexerSource.cs
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
/*
* Anarres C Preprocessor
* Copyright (c) 2007-2008, Shevek
*
* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
using System;
using System.IO;
using System.Text;
using System.Globalization;
using System.Diagnostics;
namespace CppNet {
/** Does not handle digraphs. */
public class LexerSource : Source {
static bool isJavaIdentifierStart(int c) {
return char.IsLetter((char)c) || c == '$' || c == '_';
}
static bool isJavaIdentifierPart(int c)
{
return char.IsLetter((char)c) || c == '$' || c == '_' || char.IsDigit((char)c);
}
static bool isIdentifierIgnorable(int c) {
return c >= 0 && c <= 8 ||
c >= 0xE && c <= 0x1B ||
c >= 0x7F && c <= 0x9F ||
CharUnicodeInfo.GetUnicodeCategory((char)c) == UnicodeCategory.Format;
}
static int digit(char ch, int radix)
{
string alphabet;
switch(radix) {
case 8:
alphabet = "012345678";
break;
case 10:
alphabet = "0123456789";
break;
case 16:
ch = char.ToLower(ch);
alphabet = "0123456789abcdef";
break;
default:
throw new NotSupportedException();
}
return alphabet.IndexOf(ch);
}
private static readonly bool DEBUG = false;
private JoinReader reader;
private bool ppvalid;
private bool bol;
private bool include;
private bool digraphs;
/* Unread. */
private int u0, u1;
private int ucount;
private int line;
private int column;
private int lastcolumn;
private bool cr;
/* ppvalid is:
* false in StringLexerSource,
* true in FileLexerSource */
public LexerSource(TextReader r, bool ppvalid) {
this.reader = new JoinReader(r);
this.ppvalid = ppvalid;
this.bol = true;
this.include = false;
this.digraphs = true;
this.ucount = 0;
this.line = 1;
this.column = 0;
this.lastcolumn = -1;
this.cr = false;
}
override internal void init(Preprocessor pp) {
base.init(pp);
this.digraphs = pp.getFeature(Feature.DIGRAPHS);
this.reader.init(pp, this);
}
override public int getLine() {
return line;
}
override public int getColumn() {
return column;
}
override internal bool isNumbered() {
return true;
}
/* Error handling. */
private void _error(String msg, bool error) {
int _l = line;
int _c = column;
if (_c == 0) {
_c = lastcolumn;
_l--;
}
else {
_c--;
}
if (error)
base.error(_l, _c, msg);
else
base.warning(_l, _c, msg);
}
/* Allow JoinReader to call this. */
internal void error(String msg)
{
_error(msg, true);
}
/* Allow JoinReader to call this. */
internal void warning(String msg) {
_error(msg, false);
}
/* A flag for string handling. */
internal void setInclude(bool b)
{
this.include = b;
}
/*
private bool _isLineSeparator(int c) {
return Character.getType(c) == Character.LINE_SEPARATOR
|| c == -1;
}
*/
/* XXX Move to JoinReader and canonicalise newlines. */
private static bool isLineSeparator(int c) {
switch ((char)c) {
case '\r':
case '\n':
case '\u2028':
case '\u2029':
case '\u000B':
case '\u000C':
case '\u0085':
return true;
default:
return (c == -1);
}
}
private int read() {
System.Diagnostics.Debug.Assert(ucount <= 2, "Illegal ucount: " + ucount);
switch (ucount) {
case 2:
ucount = 1;
return u1;
case 1:
ucount = 0;
return u0;
}
if (reader == null)
return -1;
int c = reader.read();
switch (c) {
case '\r':
cr = true;
line++;
lastcolumn = column;
column = 0;
break;
case '\n':
if (cr) {
cr = false;
break;
}
goto case '\u2028';
/* fallthrough */
case '\u2028':
case '\u2029':
case '\u000B':
case '\u000C':
case '\u0085':
cr = false;
line++;
lastcolumn = column;
column = 0;
break;
default:
cr = false;
column++;
break;
}
/*
if (isLineSeparator(c)) {
line++;
lastcolumn = column;
column = 0;
}
else {
column++;
}
*/
return c;
}
/* You can unget AT MOST one newline. */
private void unread(int c) {
/* XXX Must unread newlines. */
if (c != -1) {
if (isLineSeparator(c)) {
line--;
column = lastcolumn;
cr = false;
}
else {
column--;
}
switch (ucount) {
case 0:
u0 = c;
ucount = 1;
break;
case 1:
u1 = c;
ucount = 2;
break;
default:
throw new InvalidOperationException(
"Cannot unget another character!"
);
}
// reader.unread(c);
}
}
/* Consumes the rest of the current line into an invalid. */
private Token invalid(StringBuilder text, String reason) {
int d = read();
while (!isLineSeparator(d)) {
text.Append((char)d);
d = read();
}
unread(d);
return new Token(Token.INVALID, text.ToString(), reason);
}
private Token ccomment() {
StringBuilder text = new StringBuilder("/*");
int d;
do {
do {
d = read();
text.Append((char)d);
} while (d != '*');
do {
d = read();
text.Append((char)d);
} while (d == '*');
} while (d != '/');
return new Token(Token.CCOMMENT, text.ToString());
}
private Token cppcomment() {
StringBuilder text = new StringBuilder("//");
int d = read();
while (!isLineSeparator(d)) {
text.Append((char)d);
d = read();
}
unread(d);
return new Token(Token.CPPCOMMENT, text.ToString());
}
private int escape(StringBuilder text) {
int d = read();
switch (d) {
case 'a': text.Append('a'); return 0x07;
case 'b': text.Append('b'); return '\b';
case 'f': text.Append('f'); return '\f';
case 'n': text.Append('n'); return '\n';
case 'r': text.Append('r'); return '\r';
case 't': text.Append('t'); return '\t';
case 'v': text.Append('v'); return 0x0b;
case '\\': text.Append('\\'); return '\\';
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
int len = 0;
int val = 0;
do {
val = (val << 3) + digit((char)d, 8);
text.Append((char)d);
d = read();
} while(++len < 3 && digit((char)d, 8) != -1);
unread(d);
return val;
case 'x':
len = 0;
val = 0;
do {
val = (val << 4) + digit((char)d, 16);
text.Append((char)d);
d = read();
} while(++len < 2 && digit((char)d, 16) != -1);
unread(d);
return val;
/* Exclude two cases from the warning. */
case '"': text.Append('"'); return '"';
case '\'': text.Append('\''); return '\'';
default:
warning("Unnecessary escape character " + (char)d);
text.Append((char)d);
return d;
}
}
private Token character() {
StringBuilder text = new StringBuilder("'");
int d = read();
if (d == '\\') {
text.Append('\\');
d = escape(text);
}
else if (isLineSeparator(d)) {
unread(d);
return new Token(Token.INVALID, text.ToString(),
"Unterminated character literal");
}
else if (d == '\'') {
text.Append('\'');
return new Token(Token.INVALID, text.ToString(),
"Empty character literal");
}
else if (char.IsControl((char)d)) {
text.Append('?');
return invalid(text, "Illegal unicode character literal");
}
else {
text.Append((char)d);
}
int e = read();
if (e != '\'') {
// error("Illegal character constant");
/* We consume up to the next ' or the rest of the line. */
for (;;) {
if (isLineSeparator(e)) {
unread(e);
break;
}
text.Append((char)e);
if (e == '\'')
break;
e = read();
}
return new Token(Token.INVALID, text.ToString(),
"Illegal character constant " + text);
}
text.Append('\'');
/* XXX It this a bad cast? */
return new Token(Token.CHARACTER,
text.ToString(), (char)d);
}
private Token String(char open, char close) {
StringBuilder text = new StringBuilder();
text.Append(open);
StringBuilder buf = new StringBuilder();
for (;;) {
int c = read();
if (c == close) {
break;
}
else if (c == '\\') {
text.Append('\\');
if (!include) {
char d = (char)escape(text);
buf.Append(d);
}
}
else if (c == -1) {
unread(c);
// error("End of file in string literal after " + buf);
return new Token(Token.INVALID, text.ToString(),
"End of file in string literal after " + buf);
}
else if (isLineSeparator(c)) {
unread(c);
// error("Unterminated string literal after " + buf);
return new Token(Token.INVALID, text.ToString(),
"Unterminated string literal after " + buf);
}
else {
text.Append((char)c);
buf.Append((char)c);
}
}
text.Append(close);
return new Token(close == '>' ? Token.HEADER : Token.STRING,
text.ToString(), buf.ToString());
}
private Token _number(StringBuilder text, long val, int d) {
int bits = 0;
for (;;) {
/* XXX Error check duplicate bits. */
if (d == 'U' || d == 'u') {
bits |= 1;
text.Append((char)d);
d = read();
}
else if (d == 'L' || d == 'l') {
if ((bits & 4) != 0)
/* XXX warn */ ;
bits |= 2;
text.Append((char)d);
d = read();
}
else if (d == 'I' || d == 'i') {
if ((bits & 2) != 0)
/* XXX warn */ ;
bits |= 4;
text.Append((char)d);
d = read();
}
else if (char.IsLetter((char)d)) {
unread(d);
return new Token(Token.INVALID, text.ToString(),
"Invalid suffix \"" + (char)d +
"\" on numeric constant");
}
else {
unread(d);
return new Token(Token.INTEGER,
text.ToString(), (long)val);
}
}
}
/* We already chewed a zero, so empty is fine. */
private Token number_octal() {
StringBuilder text = new StringBuilder("0");
int d = read();
long val = 0;
while (digit((char)d, 8) != -1) {
val = (val << 3) + digit((char)d, 8);
text.Append((char)d);
d = read();
}
return _number(text, val, d);
}
/* We do not know whether know the first digit is valid. */
private Token number_hex(char x) {
StringBuilder text = new StringBuilder("0");
text.Append(x);
int d = read();
if (digit((char)d, 16) == -1) {
unread(d);
// error("Illegal hexadecimal constant " + (char)d);
return new Token(Token.INVALID, text.ToString(),
"Illegal hexadecimal digit " + (char)d +
" after "+ text);
}
long val = 0;
do {
val = (val << 4) + digit((char)d, 16);
text.Append((char)d);
d = read();
} while (digit((char)d, 16) != -1);
return _number(text, val, d);
}
/* We know we have at least one valid digit, but empty is not
* fine. */
/* XXX This needs a complete rewrite. */
private Token number_decimal(int c) {
StringBuilder text = new StringBuilder((char)c);
int d = c;
long val = 0;
do {
val = val * 10 + digit((char)d, 10);
text.Append((char)d);
d = read();
} while (digit((char)d, 10) != -1);
return _number(text, val, d);
}
private Token identifier(int c) {
StringBuilder text = new StringBuilder();
int d;
text.Append((char)c);
for (;;) {
d = read();
if (isIdentifierIgnorable(d))
;
else if (isJavaIdentifierPart(d))
text.Append((char)d);
else
break;
}
unread(d);
return new Token(Token.IDENTIFIER, text.ToString());
}
private Token whitespace(int c) {
StringBuilder text = new StringBuilder();
int d;
text.Append((char)c);
for (;;) {
d = read();
if (ppvalid && isLineSeparator(d)) /* XXX Ugly. */
break;
if (char.IsWhiteSpace((char)d))
text.Append((char)d);
else
break;
}
unread(d);
return new Token(Token.WHITESPACE, text.ToString());
}
/* No token processed by cond() contains a newline. */
private Token cond(char c, int yes, int no) {
int d = read();
if (c == d)
return new Token(yes);
unread(d);
return new Token(no);
}
public override Token token() {
Token tok = null;
int _l = line;
int _c = column;
int c = read();
int d;
switch (c) {
case '\n':
if (ppvalid) {
bol = true;
if (include) {
tok = new Token(Token.NL, _l, _c, "\n");
}
else {
int nls = 0;
do {
nls++;
d = read();
} while (d == '\n');
unread(d);
char[] text = new char[nls];
for (int i = 0; i < text.Length; i++)
text[i] = '\n';
// Skip the bol = false below.
tok = new Token(Token.NL, _l, _c, new String(text));
}
if (DEBUG)
Debug.WriteLine("lx: Returning NL: " + tok);
return tok;
}
/* Let it be handled as whitespace. */
break;
case '!':
tok = cond('=', Token.NE, '!');
break;
case '#':
if (bol)
tok = new Token(Token.HASH);
else
tok = cond('#', Token.PASTE, '#');
break;
case '+':
d = read();
if (d == '+')
tok = new Token(Token.INC);
else if (d == '=')
tok = new Token(Token.PLUS_EQ);
else
unread(d);
break;
case '-':
d = read();
if (d == '-')
tok = new Token(Token.DEC);
else if (d == '=')
tok = new Token(Token.SUB_EQ);
else if (d == '>')
tok = new Token(Token.ARROW);
else
unread(d);
break;
case '*':
tok = cond('=', Token.MULT_EQ, '*');
break;
case '/':
d = read();
if (d == '*')
tok = ccomment();
else if (d == '/')
tok = cppcomment();
else if (d == '=')
tok = new Token(Token.DIV_EQ);
else
unread(d);
break;
case '%':
d = read();
if (d == '=')
tok = new Token(Token.MOD_EQ);
else if (digraphs && d == '>')
tok = new Token('}'); // digraph
else if (digraphs && d == ':') {
bool paste = true;
d = read();
if (d != '%') {
unread(d);
tok = new Token('#'); // digraph
paste = false;
}
d = read();
if (d != ':') {
unread(d); // Unread 2 chars here.
unread('%');
tok = new Token('#'); // digraph
paste = false;
}
if(paste) {
tok = new Token(Token.PASTE); // digraph
}
}
else
unread(d);
break;
case ':':
/* :: */
d = read();
if (digraphs && d == '>')
tok = new Token(']'); // digraph
else
unread(d);
break;
case '<':
if (include) {
tok = String('<', '>');
}
else {
d = read();
if (d == '=')
tok = new Token(Token.LE);
else if (d == '<')
tok = cond('=', Token.LSH_EQ, Token.LSH);
else if (digraphs && d == ':')
tok = new Token('['); // digraph
else if (digraphs && d == '%')
tok = new Token('{'); // digraph
else
unread(d);
}
break;
case '=':
tok = cond('=', Token.EQ, '=');
break;
case '>':
d = read();
if (d == '=')
tok = new Token(Token.GE);
else if (d == '>')
tok = cond('=', Token.RSH_EQ, Token.RSH);
else
unread(d);
break;
case '^':
tok = cond('=', Token.XOR_EQ, '^');
break;
case '|':
d = read();
if (d == '=')
tok = new Token(Token.OR_EQ);
else if (d == '|')
tok = cond('=', Token.LOR_EQ, Token.LOR);
else
unread(d);
break;
case '&':
d = read();
if (d == '&')
tok = cond('=', Token.LAND_EQ, Token.LAND);
else if (d == '=')
tok = new Token(Token.AND_EQ);
else
unread(d);
break;
case '.':
d = read();
if (d == '.')
tok = cond('.', Token.ELLIPSIS, Token.RANGE);
else
unread(d);
/* XXX decimal fraction */
break;
case '0':
/* octal or hex */
d = read();
if (d == 'x' || d == 'X')
tok = number_hex((char)d);
else {
unread(d);
tok = number_octal();
}
break;
case '\'':
tok = character();
break;
case '"':
tok = String('"', '"');
break;
case -1:
close();
tok = new Token(Token.EOF, _l, _c, "<eof>");
break;
}
if (tok == null) {
if (char.IsWhiteSpace((char)c)) {
tok = whitespace(c);
}
else if (char.IsDigit((char)c)) {
tok = number_decimal(c);
}
else if (isJavaIdentifierStart(c)) {
tok = identifier(c);
}
else {
tok = new Token(c);
}
}
if (bol) {
switch (tok.getType()) {
case Token.WHITESPACE:
case Token.CCOMMENT:
break;
default:
bol = false;
break;
}
}
tok.setLocation(_l, _c);
if (DEBUG)
Debug.WriteLine("lx: Returning " + tok);
// (new Exception("here")).printStackTrace(System.out);
return tok;
}
public override void close()
{
if(reader != null) {
reader.close();
reader = null;
}
base.close();
}
}
}