-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCirLex.c
1132 lines (1078 loc) · 31.3 KB
/
CirLex.c
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
#include "cir_internal.h"
#include <stdio.h>
#include <assert.h>
#include <ctype.h>
#define STRING_BUF_SIZE (1024 * 1024 * 1)
static char strbuf[STRING_BUF_SIZE];
static size_t strbufLen;
// The maximum number of tokens we may need to push onto the stack at any time
#define TOK_STACK_SIZE 1
CirToken cirtok;
static CirToken tokenStack[TOK_STACK_SIZE];
static size_t tokenStack_len;
static CirBBuf bbuf;
static size_t idx;
static const CirMachine *CirLex__mach;
void
CirLex__init(const char *path, const CirMachine *mach)
{
CirLex__mach = mach;
CirBBuf_init(&bbuf);
CirBBuf__readFile(&bbuf, path);
idx = 0;
CirName filename = CirName_of(path);
CirLog__setRealLocation(filename, 1);
CirLog__pushLocation(filename, 1);
}
void
CirLex__push(const CirToken *token)
{
assert(tokenStack_len < TOK_STACK_SIZE);
tokenStack[tokenStack_len++] = *token;
}
static bool
CirLex__eof(void)
{
return idx >= bbuf.len;
}
static uint8_t
CirLex__c(void)
{
assert(!CirLex__eof());
return bbuf.items[idx];
}
static void
CirLex__advance(size_t n)
{
idx += n;
}
static bool
CirLex__startsWith(const char *p)
{
for (size_t i = 0; *p; i++, p++) {
if (idx + i >= bbuf.len || bbuf.items[idx + i] != *p)
return false;
}
return true;
}
static bool
CirLex__startsWithI(const char *p)
{
for (size_t i = 0; *p; i++, p++) {
if (idx + i >= bbuf.len || toupper(bbuf.items[idx + i]) != *p)
return false;
}
return true;
}
static bool
isoctal(char c)
{
return '0' <= c && c <= '7';
}
static int
hex(char c)
{
if ('0' <= c && c <= '9')
return c - '0';
if ('a' <= c && c <= 'f')
return c - 'a' + 10;
assert('A' <= c && c <= 'F');
return c - 'A' + 10;
}
// Read a single character in a char or string literal
static char
CirLex__char(const char *what)
{
// Nonescaped
if (CirLex__c() != '\\') {
char c = CirLex__c();
CirLex__advance(1);
return c;
}
CirLex__advance(1); // Consume escape character
if (CirLex__eof()) {
cir_fatal("lexer error: unterminated %s literal at EOF", what);
}
char c = 0;
switch (CirLex__c()) {
// Simple (e.g. '\n' or '\a')
case 'a':
CirLex__advance(1);
return '\a';
case 'b':
CirLex__advance(1);
return '\b';
case 'f':
CirLex__advance(1);
return '\f';
case 'n':
CirLex__advance(1);
return '\n';
case 'r':
CirLex__advance(1);
return '\r';
case 't':
CirLex__advance(1);
return '\t';
case 'v':
CirLex__advance(1);
return '\v';
case 'e': case 'E':
CirLex__advance(1);
return '\033';
// Hexadecimal
case 'x':
CirLex__advance(1);
while (!CirLex__eof() && isxdigit(CirLex__c())) {
c = c * 16 + hex(CirLex__c());
CirLex__advance(1);
}
return c;
// Octal
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7':
c = CirLex__c() - '0';
CirLex__advance(1);
if (!CirLex__eof() && isoctal(CirLex__c())) {
c = c * 8 + (CirLex__c() - '0');
CirLex__advance(1);
}
if (!CirLex__eof() && isoctal(CirLex__c())) {
c = c * 8 + (CirLex__c() - '0');
CirLex__advance(1);
}
return c;
// Default: return as-is
default:
c = CirLex__c();
CirLex__advance(1);
return c;
}
}
enum CirLex__suffix {
CIRLEX_SUFFIX_NONE,
CIRLEX_SUFFIX_L,
CIRLEX_SUFFIX_U,
CIRLEX_SUFFIX_UL,
CIRLEX_SUFFIX_LL,
CIRLEX_SUFFIX_ULL
};
static enum CirLex__suffix
CirLex__intsuffix(void)
{
if (CirLex__startsWithI("ULL") || CirLex__startsWithI("LLU")) {
CirLex__advance(3);
return CIRLEX_SUFFIX_ULL;
} else if (CirLex__startsWithI("LL")) {
CirLex__advance(2);
return CIRLEX_SUFFIX_LL;
} else if (CirLex__startsWithI("UL") || CirLex__startsWithI("LU")) {
CirLex__advance(2);
return CIRLEX_SUFFIX_UL;
} else if (CirLex__startsWithI("L")) {
CirLex__advance(1);
return CIRLEX_SUFFIX_L;
} else if (CirLex__startsWithI("U")) {
CirLex__advance(1);
return CIRLEX_SUFFIX_U;
} else {
return CIRLEX_SUFFIX_NONE;
}
}
static bool
fitsInInt(uint64_t val, uint32_t ikind, const CirMachine *mach)
{
uint32_t sizeInBytes = CirIkind_size(ikind, mach);
if (CirIkind_isSigned(ikind, mach)) {
// Signed trunc
switch (sizeInBytes) {
case 1:
return val <= 127ULL;
case 2:
return val <= 32767ULL;
case 4:
return val <= 2147483647ULL;
case 8:
return val <= 9223372036854775807ULL;
default:
cir_bug("unsupported sizeInBytes");
}
} else {
// Unsigned trunc
switch (sizeInBytes) {
case 1:
return val <= 255ULL;
case 2:
return val <= 65535ULL;
case 4:
return val <= 4294967295ULL;
case 8:
return true;
default:
cir_bug("unsupported sizeInBytes");
}
}
}
static bool
fitVal(uint64_t val, bool decimal, enum CirLex__suffix suffix, const CirMachine *mach)
{
uint32_t totry[6] = {};
switch (suffix) {
case CIRLEX_SUFFIX_ULL:
totry[0] = CIR_IULONGLONG;
break;
case CIRLEX_SUFFIX_LL:
if (!decimal) {
totry[0] = CIR_ILONGLONG;
totry[1] = CIR_IULONGLONG;
} else {
totry[0] = CIR_ILONGLONG;
}
break;
case CIRLEX_SUFFIX_UL:
totry[0] = CIR_IULONG;
totry[1] = CIR_IULONGLONG;
break;
case CIRLEX_SUFFIX_L:
if (!decimal) {
totry[0] = CIR_ILONG;
totry[1] = CIR_IULONG;
totry[2] = CIR_ILONGLONG;
totry[3] = CIR_IULONGLONG;
} else {
totry[0] = CIR_ILONG;
totry[1] = CIR_ILONGLONG;
}
break;
case CIRLEX_SUFFIX_U:
totry[0] = CIR_IUINT;
totry[1] = CIR_IULONG;
totry[2] = CIR_IULONGLONG;
break;
case CIRLEX_SUFFIX_NONE:
if (!decimal) {
totry[0] = CIR_IINT;
totry[1] = CIR_IUINT;
totry[2] = CIR_ILONG;
totry[3] = CIR_IULONG;
totry[4] = CIR_ILONGLONG;
totry[5] = CIR_IULONGLONG;
} else {
totry[0] = CIR_IINT;
totry[1] = CIR_ILONG;
totry[2] = CIR_ILONGLONG;
}
break;
default:
cir_bug("unknown suffix");
}
size_t i;
for (i = 0; i < 7 && totry[i]; i++) {
if (fitsInInt(val, totry[i], mach)) {
cirtok.data.intlit.ikind = totry[i];
if (CirIkind_isSigned(totry[i], mach))
cirtok.data.intlit.val.i64 = val;
else
cirtok.data.intlit.val.u64 = val;
return false; // no overflow
}
}
cirtok.data.intlit.ikind = totry[i-1];
if (CirIkind_isSigned(totry[i-1], mach))
cirtok.data.intlit.val.i64 = val;
else
cirtok.data.intlit.val.u64 = val;
return true; // has overflow
}
static struct {
const char *name;
int ty;
} symbols[] = {
{ "<<=", CIRTOK_INF_INF_EQ },
{ ">>=", CIRTOK_SUP_SUP_EQ },
{ "...", CIRTOK_ELLIPSIS },
{ "+=", CIRTOK_PLUS_EQ },
{ "-=", CIRTOK_MINUS_EQ },
{ "*=", CIRTOK_STAR_EQ },
{ "/=", CIRTOK_SLASH_EQ },
{ "%=", CIRTOK_PERCENT_EQ },
{ "|=", CIRTOK_PIPE_EQ },
{ "&=", CIRTOK_AND_EQ },
{ "^=", CIRTOK_CIRC_EQ },
{ "<<", CIRTOK_INF_INF },
{ ">>", CIRTOK_SUP_SUP },
{ "==", CIRTOK_EQ_EQ },
{ "!=", CIRTOK_EXCLAM_EQ },
{ "<=", CIRTOK_INF_EQ },
{ ">=", CIRTOK_SUP_EQ },
{ "++", CIRTOK_PLUS_PLUS },
{ "--", CIRTOK_MINUS_MINUS },
{ "->", CIRTOK_ARROW },
{ "&&", CIRTOK_AND_AND },
{ "||", CIRTOK_PIPE_PIPE },
{ NULL, 0 }
};
// Unlike isspace(), this returns false on \n
static bool
isBlank(char c)
{
return c == ' ' || c == '\t' || c == '\r';
}
// We are lexing a file hash with format
// # <line number> <file name> <flags>
static void
CirLex__nextFileHash(void)
{
// We enter the function with the hash already seen,
// and we are looking at the line number.
assert(isdigit(CirLex__c()));
// Parse line number
uint32_t lineNumber = 0;
while (!CirLex__eof() && isdigit(CirLex__c())) {
lineNumber = lineNumber * 10 + (CirLex__c() - '0');
CirLex__advance(1);
}
// Subtract 1 since the lineno only starts after this line
lineNumber--;
// Skip whitespace
while (!CirLex__eof() && isBlank(CirLex__c()))
CirLex__advance(1);
// Are we looking at the start of the filename?
if (CirLex__eof() || CirLex__c() != '"') {
// Unclear what we are looking at
while (!CirLex__eof() && CirLex__c() != '\n')
CirLex__advance(1);
return;
}
// Parse filename
CirLex__advance(1); // consume `"`
strbufLen = 0;
while (!CirLex__eof() && CirLex__c() != '"') {
char c = CirLex__char("string");
if (c == '\0')
cir_fatal("lexer error: hash filename cannot contain NUL bytes");
if (strbufLen >= STRING_BUF_SIZE)
cir_fatal("lexer error: string literal is too long");
strbuf[strbufLen++] = c;
}
if (CirLex__eof())
cir_fatal("lexer error: unterminated string at eof");
assert(CirLex__c() == '"');
CirLex__advance(1);
// NUL-terminate filename string
if (strbufLen >= STRING_BUF_SIZE)
cir_fatal("lexer error: string literal is too long");
strbuf[strbufLen++] = '\0';
CirName filename = CirName_of(strbuf);
// Skip whitespace
while (!CirLex__eof() && isBlank(CirLex__c()))
CirLex__advance(1);
// Parse flags
bool shouldPush = false, shouldPop = false;
while (!CirLex__eof() && isdigit(CirLex__c())) {
if (CirLex__c() == '1') {
CirLex__advance(1);
shouldPush = true;
while (!CirLex__eof() && isBlank(CirLex__c()))
CirLex__advance(1);
} else if (CirLex__c() == '2') {
CirLex__advance(1);
shouldPop = true;
while (!CirLex__eof() && isBlank(CirLex__c()))
CirLex__advance(1);
} else {
// Unclear what we are looking at, advance to next digit
while (!CirLex__eof() && !isBlank(CirLex__c()) && CirLex__c() != '\n')
CirLex__advance(1);
while (!CirLex__eof() && !isdigit(CirLex__c()) && CirLex__c() != '\n')
CirLex__advance(1);
}
}
if (shouldPush) {
CirLog__pushLocation(filename, lineNumber);
} else if (shouldPop) {
CirLog__popLocation();
CirLog__setLocation(filename, lineNumber);
} else {
CirLog__setLocation(filename, lineNumber);
}
assert(CirLex__eof() || CirLex__c() == '\n');
return;
}
void
CirLex__next(void)
{
if (tokenStack_len) {
cirtok = tokenStack[--tokenStack_len];
return;
}
loop:
if (CirLex__eof()) {
// EOF
cirtok.type = CIRTOK_EOF;
return;
} else if (CirLex__c() == '\n') {
// Newline
CirLog__announceNewLine();
CirLex__advance(1);
goto loop;
} else if (isBlank(CirLex__c())) {
// Skip whitespace
CirLex__advance(1);
goto loop;
} else if (CirLex__startsWith("//")) {
// Line comment
CirLex__advance(strlen("//"));
while (!CirLex__eof() && CirLex__c() != '\n')
CirLex__advance(1);
goto loop;
} else if (CirLex__startsWith("/*")) {
// Block comment
CirLex__advance(strlen("/*"));
for (;;) {
if (CirLex__eof()) {
cir_fatal("lexer error: unterminated block comment at EOF");
} else if (CirLex__startsWith("*/")) {
CirLex__advance(strlen("*/"));
break;
} else if (CirLex__c() == '\n') {
CirLog__announceNewLine();
CirLex__advance(1);
} else {
CirLex__advance(1);
}
}
goto loop;
} else if (CirLex__c() == '\'') {
// Character literal
CirLex__advance(1);
char val = CirLex__char("character");
if (CirLex__eof() || CirLex__c() != '\'')
cir_fatal("lexer error: unclosed character literal");
CirLex__advance(1);
cirtok.type = CIRTOK_CHARLIT;
cirtok.data.charlit = val;
return;
} else if (CirLex__c() == '"') {
// String literal
CirLex__advance(1);
strbufLen = 0;
while (!CirLex__eof() && CirLex__c() != '"') {
char c = CirLex__char("string");
if (strbufLen >= STRING_BUF_SIZE)
cir_fatal("lexer error: string literal is too long");
strbuf[strbufLen++] = c;
}
if (CirLex__eof())
cir_fatal("lexer error: unterminated string at eof");
assert(CirLex__c() == '"');
CirLex__advance(1);
cirtok.type = CIRTOK_STRINGLIT;
cirtok.data.stringlit.buf = strbuf;
cirtok.data.stringlit.len = strbufLen;
return;
} else if (CirLex__startsWith("R\"")) {
// Raw string literal
char delim[19];
delim[0] = ')';
CirLex__advance(2); // consume R"
size_t i = 1;
for (; !CirLex__eof() && CirLex__c() != '('; CirLex__advance(1), i++) {
if (i >= 17)
cir_fatal("lexer error: raw string delimiter too long");
char c = CirLex__c();
if (c == ')' || c == '\\' || isspace(c))
cir_fatal("lexer error: invalid raw string delimiter character: %c", c);
delim[i] = c;
}
if (CirLex__eof())
cir_fatal("lexer error: unterminated raw string at eof");
delim[i++] = '"';
size_t delimLen = i;
delim[i++] = '\0';
strbufLen = 0;
assert(CirLex__c() == '(');
CirLex__advance(1); // consume (
while (!CirLex__eof() && !CirLex__startsWith(delim)) {
char c = CirLex__c();
if (strbufLen >= STRING_BUF_SIZE)
cir_fatal("lexer error: string literal is too long");
strbuf[strbufLen++] = c;
CirLex__advance(1);
}
if (CirLex__eof())
cir_fatal("lexer error: unterminated raw string at eof");
CirLex__advance(delimLen);
cirtok.type = CIRTOK_STRINGLIT;
cirtok.data.stringlit.buf = strbuf;
cirtok.data.stringlit.len = strbufLen;
return;
} else if (CirLex__startsWith("0x") || CirLex__startsWith("0X")) {
// Hexadecimal number
CirLex__advance(2);
if (CirLex__eof() || !isxdigit(CirLex__c()))
cir_fatal("lexer error: bad hexadecimal number");
uint64_t val = 0;
bool hasOverflow = false;
while (!CirLex__eof() && isxdigit(CirLex__c())) {
if (__builtin_mul_overflow(val, (uint64_t)16, &val))
hasOverflow = true;
uint64_t b = hex(CirLex__c());
CirLex__advance(1);
if (__builtin_add_overflow(val, b, &val))
hasOverflow = true;
}
cirtok.type = CIRTOK_INTLIT;
enum CirLex__suffix suffix = CirLex__intsuffix();
if (fitVal(val, false, suffix, CirLex__mach))
hasOverflow = true;
if (hasOverflow)
cir_warn("hex literal: overflow");
return;
} else if (CirLex__c() == '0') {
// Octal number
uint64_t val = 0;
bool hasOverflow = false;
for (int i = 0; i < 3 && !CirLex__eof() && isoctal(CirLex__c()); i++) {
if (__builtin_mul_overflow(val, 8, &val))
hasOverflow = true;
uint64_t b = CirLex__c() - '0';
CirLex__advance(1);
if (__builtin_add_overflow(val, b, &val))
hasOverflow = true;
}
cirtok.type = CIRTOK_INTLIT;
enum CirLex__suffix suffix = CirLex__intsuffix();
if (fitVal(val, false, suffix, CirLex__mach))
hasOverflow = true;
if (hasOverflow)
cir_warn("octal literal: overflow");
return;
} else if (isdigit(CirLex__c())) {
// Decimal number
uint64_t val = 0;
bool hasOverflow = false;
while (!CirLex__eof() && isdigit(CirLex__c())) {
if (__builtin_mul_overflow(val, 10, &val))
hasOverflow = true;
uint64_t b = CirLex__c() - '0';
CirLex__advance(1);
if (__builtin_add_overflow(val, b, &val))
hasOverflow = true;
}
cirtok.type = CIRTOK_INTLIT;
enum CirLex__suffix suffix = CirLex__intsuffix();
if (fitVal(val, true, suffix, CirLex__mach))
hasOverflow = true;
if (hasOverflow)
cir_warn("decimal literal: overflow");
return;
} else if (CirLex__c() == '#') {
// Pragma or Line info
CirLex__advance(1); // consume #
if (CirLex__eof())
goto loop;
// Skip whitespace
while (!CirLex__eof() && isBlank(CirLex__c()))
CirLex__advance(1);
if (CirLex__eof())
goto loop;
if (isdigit(CirLex__c())) {
// We are looking at a file hash line
CirLex__nextFileHash();
} else {
// Unclear what we are looking at,
// skip until end of line/EOF
while (!CirLex__eof() && CirLex__c() != '\n')
CirLex__advance(1);
}
goto loop;
}
// Is an operator with more than one symbol?
for (size_t i = 0; symbols[i].name; i++) {
if (CirLex__startsWith(symbols[i].name)) {
cirtok.type = symbols[i].ty;
CirLex__advance(strlen(symbols[i].name));
return;
}
}
// Is an operator with a single symbol?
switch (CirLex__c()) {
case '=':
cirtok.type = CIRTOK_EQ;
CirLex__advance(1);
return;
case '<':
cirtok.type = CIRTOK_INF;
CirLex__advance(1);
return;
case '>':
cirtok.type = CIRTOK_SUP;
CirLex__advance(1);
return;
case '+':
cirtok.type = CIRTOK_PLUS;
CirLex__advance(1);
return;
case '-':
cirtok.type = CIRTOK_MINUS;
CirLex__advance(1);
return;
case '*':
cirtok.type = CIRTOK_STAR;
CirLex__advance(1);
return;
case '/':
cirtok.type = CIRTOK_SLASH;
CirLex__advance(1);
return;
case '%':
cirtok.type = CIRTOK_PERCENT;
CirLex__advance(1);
return;
case '!':
cirtok.type = CIRTOK_EXCLAM;
CirLex__advance(1);
return;
case '&':
cirtok.type = CIRTOK_AND;
CirLex__advance(1);
return;
case '|':
cirtok.type = CIRTOK_PIPE;
CirLex__advance(1);
return;
case '^':
cirtok.type = CIRTOK_CIRC;
CirLex__advance(1);
return;
case '?':
cirtok.type = CIRTOK_QUEST;
CirLex__advance(1);
return;
case ':':
cirtok.type = CIRTOK_COLON;
CirLex__advance(1);
return;
case '~':
cirtok.type = CIRTOK_TILDE;
CirLex__advance(1);
return;
case '{':
cirtok.type = CIRTOK_LBRACE;
CirLex__advance(1);
return;
case '}':
cirtok.type = CIRTOK_RBRACE;
CirLex__advance(1);
return;
case '[':
cirtok.type = CIRTOK_LBRACKET;
CirLex__advance(1);
return;
case ']':
cirtok.type = CIRTOK_RBRACKET;
CirLex__advance(1);
return;
case '(':
cirtok.type = CIRTOK_LPAREN;
CirLex__advance(1);
return;
case ')':
cirtok.type = CIRTOK_RPAREN;
CirLex__advance(1);
return;
case ';':
cirtok.type = CIRTOK_SEMICOLON;
CirLex__advance(1);
return;
case ',':
cirtok.type = CIRTOK_COMMA;
CirLex__advance(1);
return;
case '.':
cirtok.type = CIRTOK_DOT;
CirLex__advance(1);
return;
case '@':
cirtok.type = CIRTOK_AT;
CirLex__advance(1);
return;
}
// Is a keyword or identifier?
if (isalpha(CirLex__c()) || CirLex__c() == '_') {
// Read ident into buffer
strbufLen = 0;
while (!CirLex__eof() && (isalpha(CirLex__c()) || isdigit(CirLex__c()) || CirLex__c() == '_')) {
if (strbufLen >= STRING_BUF_SIZE)
cir_fatal("lexer error: ident is too long");
strbuf[strbufLen++] = CirLex__c();
CirLex__advance(1);
}
// NUL-terminate string buffer
if (strbufLen >= STRING_BUF_SIZE)
cir_fatal("lexer error: ident is too long");
strbuf[strbufLen++] = '\0';
// Is this a reserved keyword?
if (!strcmp(strbuf, "auto")) {
cirtok.type = CIRTOK_AUTO;
return;
} else if (!strcmp(strbuf, "const")) {
cirtok.type = CIRTOK_CONST;
return;
} else if (!strcmp(strbuf, "static")) {
cirtok.type = CIRTOK_STATIC;
return;
} else if (!strcmp(strbuf, "extern")) {
cirtok.type = CIRTOK_EXTERN;
return;
} else if (!strcmp(strbuf, "long")) {
cirtok.type = CIRTOK_LONG;
return;
} else if (!strcmp(strbuf, "short")) {
cirtok.type = CIRTOK_SHORT;
return;
} else if (!strcmp(strbuf, "register")) {
cirtok.type = CIRTOK_REGISTER;
return;
} else if (!strcmp(strbuf, "signed")) {
cirtok.type = CIRTOK_SIGNED;
return;
} else if (!strcmp(strbuf, "unsigned")) {
cirtok.type = CIRTOK_UNSIGNED;
return;
} else if (!strcmp(strbuf, "volatile")) {
cirtok.type = CIRTOK_VOLATILE;
return;
} else if (!strcmp(strbuf, "_Bool")) {
cirtok.type = CIRTOK_BOOL;
return;
} else if (!strcmp(strbuf, "char")) {
cirtok.type = CIRTOK_CHAR;
return;
} else if (!strcmp(strbuf, "int")) {
cirtok.type = CIRTOK_INT;
return;
} else if (!strcmp(strbuf, "float")) {
cirtok.type = CIRTOK_FLOAT;
return;
} else if (!strcmp(strbuf, "double")) {
cirtok.type = CIRTOK_DOUBLE;
return;
} else if (!strcmp(strbuf, "void")) {
cirtok.type = CIRTOK_VOID;
return;
} else if (!strcmp(strbuf, "enum")) {
cirtok.type = CIRTOK_ENUM;
return;
} else if (!strcmp(strbuf, "struct")) {
cirtok.type = CIRTOK_STRUCT;
return;
} else if (!strcmp(strbuf, "typedef")) {
cirtok.type = CIRTOK_TYPEDEF;
return;
} else if (!strcmp(strbuf, "union")) {
cirtok.type = CIRTOK_UNION;
return;
} else if (!strcmp(strbuf, "break")) {
cirtok.type = CIRTOK_BREAK;
return;
} else if (!strcmp(strbuf, "continue")) {
cirtok.type = CIRTOK_CONTINUE;
return;
} else if (!strcmp(strbuf, "goto")) {
cirtok.type = CIRTOK_GOTO;
return;
} else if (!strcmp(strbuf, "return")) {
cirtok.type = CIRTOK_RETURN;
return;
} else if (!strcmp(strbuf, "switch")) {
cirtok.type = CIRTOK_SWITCH;
return;
} else if (!strcmp(strbuf, "case")) {
cirtok.type = CIRTOK_CASE;
return;
} else if (!strcmp(strbuf, "default")) {
cirtok.type = CIRTOK_DEFAULT;
return;
} else if (!strcmp(strbuf, "while")) {
cirtok.type = CIRTOK_WHILE;
return;
} else if (!strcmp(strbuf, "do")) {
cirtok.type = CIRTOK_DO;
return;
} else if (!strcmp(strbuf, "for")) {
cirtok.type = CIRTOK_FOR;
return;
} else if (!strcmp(strbuf, "if")) {
cirtok.type = CIRTOK_IF;
return;
} else if (!strcmp(strbuf, "else")) {
cirtok.type = CIRTOK_ELSE;
return;
} else if (!strcmp(strbuf, "__auto_type")) {
cirtok.type = CIRTOK_AUTO_TYPE;
return;
} else if (!strcmp(strbuf, "inline") || !strcmp(strbuf, "__inline__") || !strcmp(strbuf, "__inline")) {
cirtok.type = CIRTOK_INLINE;
return;
} else if (!strcmp(strbuf, "__attribute__")) {
cirtok.type = CIRTOK_ATTRIBUTE;
return;
} else if (!strcmp(strbuf, "__asm__")) {
cirtok.type = CIRTOK_ASM;
return;
} else if (!strcmp(strbuf, "typeof")) {
cirtok.type = CIRTOK_TYPEOF;
return;
} else if (!strcmp(strbuf, "restrict") || !strcmp(strbuf, "__restrict")) {
cirtok.type = CIRTOK_RESTRICT;
return;
} else if (!strcmp(strbuf, "__builtin_va_list")) {
cirtok.type = CIRTOK_BUILTIN_VA_LIST;
return;
} else if (!strcmp(strbuf, "sizeof")) {
cirtok.type = CIRTOK_SIZEOF;
return;
} else if (!strcmp(strbuf, "__extension__")) {
// Ignore. __extension__ is used to indicate that the declaration/expression
// should be handled in GNU-C mode.
// However, we already try to support GNU C extensions whether __extension__ is used or not.
goto loop;
} else if (!strcmp(strbuf, "_Alignof") || !strcmp(strbuf, "__alignof__")) {
cirtok.type = CIRTOK_ALIGNOF;
return;
} else if (!strcmp(strbuf, "__typeval")) {
cirtok.type = CIRTOK_TYPEVAL;
return;
} else if (!strcmp(strbuf, "_Float128")) {
cirtok.type = CIRTOK_FLOAT128;
return;
}
// Convert to name
CirName name = CirName_of(strbuf);
CirVarId vid;
CirTypedefId tid;
CirEnumItemId enumItemId;
CirBuiltinId builtinId;
if ((builtinId = CirBuiltin_ofName(name))) {
// Is a builtin
cirtok.type = CIRTOK_BUILTIN;
cirtok.data.builtinId = builtinId;
} else if (CirEnv__findLocalName(name, &vid, &tid, &enumItemId) == 2) {
// Is a type
cirtok.type = CIRTOK_TYPENAME;
cirtok.data.name = name;
} else {
// Is an ident / enum item
cirtok.type = CIRTOK_IDENT;
cirtok.data.name = name;
}
return;
}
cir_fatal("lexer error: invalid byte: %u", (unsigned int)CirLex__c());
}
const char *
CirLex__str(uint32_t tt)
{
enum CirTokType t = tt;
switch (t) {
case CIRTOK_NONE:
return "NONE";
case CIRTOK_EOF:
return "EOF";
case CIRTOK_IDENT:
return "IDENT";
case CIRTOK_TYPENAME:
return "TYPENAME";
case CIRTOK_BUILTIN:
return "BUILTIN";
case CIRTOK_STRINGLIT:
return "STRINGLIT";
case CIRTOK_CHARLIT:
return "CHARLIT";
case CIRTOK_INTLIT:
return "INTLIT";
case CIRTOK_INF_INF_EQ:
return "`<<=`";
case CIRTOK_SUP_SUP_EQ:
return "`>>=`";
case CIRTOK_ELLIPSIS:
return "`...`";
case CIRTOK_PLUS_EQ:
return "`+=`";
case CIRTOK_MINUS_EQ:
return "`-=`";
case CIRTOK_STAR_EQ:
return "`*=`";
case CIRTOK_SLASH_EQ:
return "`/=`";
case CIRTOK_PERCENT_EQ:
return "`%=`";
case CIRTOK_PIPE_EQ:
return "`|=`";
case CIRTOK_AND_EQ:
return "`&=`";
case CIRTOK_CIRC_EQ:
return "`^=`";
case CIRTOK_INF_INF:
return "`<<`";
case CIRTOK_SUP_SUP:
return "`>>`";
case CIRTOK_EQ_EQ:
return "`==`";
case CIRTOK_EXCLAM_EQ:
return "`!=`";
case CIRTOK_INF_EQ:
return "`<=`";
case CIRTOK_SUP_EQ:
return "`>=`";
case CIRTOK_PLUS_PLUS:
return "`++`";
case CIRTOK_MINUS_MINUS:
return "`--`";
case CIRTOK_ARROW:
return "`->`";
case CIRTOK_AND_AND:
return "`&&`";
case CIRTOK_PIPE_PIPE:
return "`||`";
case CIRTOK_EQ:
return "`=`";
case CIRTOK_INF:
return "`<`";
case CIRTOK_SUP:
return "`>`";
case CIRTOK_PLUS:
return "`+`";