-
Notifications
You must be signed in to change notification settings - Fork 9
/
lex.cpp
584 lines (543 loc) · 14.6 KB
/
lex.cpp
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
#include <cctype>
#include <cstdint>
#include <cstdio> // printf
#include <cstring> // strlen()
#include <limits>
#include "common.h"
#include "error.h" // log()
#include "str_intern.h" // str_intern_range()
#include "tokens.h" // TOK, token_t
// Globals
location_t loc;
token_t token;
static const char *input;
static constexpr const char *print_stmt_key = "System.out.println";
static int print_stmt_key_len = strlen(print_stmt_key);
// TODO: That can be moved to a loca in next_token()
static const char *tok_start;
enum { BLANK = 0b1, MIDCHAR = 0b10, DEC = 0b100,
HEX = 0b1000, OCT = 0b10000, OTHER = 0b100000 };
static uint8_t charmap[256];
static uint16_t digit_from_char[256];
void scan_int() {
token.kind = TOK::INTLIT;
// Find base
int base = 10;
if (*input == '0') {
++input;
if (tolower(*input) == 'x') {
base = 16;
++input;
if (!(charmap[*input]&HEX)) {
syntax_error("Invalid hex integer literal.");
return;
}
} else if (charmap[*input]&OCT) {
base = 8;
} else if (charmap[*input]&(DEC|HEX)) {
syntax_error("Invalid octal integer literal.");
token.kind = TOK::INTLIT;
token.val = 0;
return;
}
}
// Compute value.
int val = 0;
while (true) {
int digit = digit_from_char[tolower(*input)];
if (digit == 0 && *input != '0') {
break;
}
if (digit >= base) {
syntax_error("Digit `", *input, "` out of range for base ", base);
break;
}
// You can choose to either continue on overflow
// or eat the remaining digits and set a standard value
// like 0.
constexpr int int_max = std::numeric_limits<int>::max();
if (val > (int_max - digit) / 10) {
warning("Integer literal overflow.");
// eat remaining digits
digit = digit_from_char[*input];
for(;;) {
if(charmap[*input]&DEC) {
++input;
} else {
digit = digit_from_char[*input];
if(digit != 0 && digit < base) {
++input;
} else {
break;
}
}
}
val = int_max;
break;
}
val = val * base + digit;
++input;
}
token.val = val;
}
void encountered_newline() {
++loc.ln;
}
void next_token() {
tok_start = input;
lex_again:
if (!input[0]) {
token.kind = TOK::EOI;
return;
}
// skip whitespace
while (*input && charmap[*input] & BLANK) {
if (*input == '\n') {
encountered_newline();
}
input++;
}
if (!input[0]) goto lex_again;
tok_start = input;
switch (*input) {
case '.':
{
token.kind = TOK::DOT;
if (charmap[input[1]]&DEC) {
warning("Floating numbers are not supported.");
}
++input;
} break;
case '0' ... '9':
{
while (charmap[*input]&DEC) {
++input;
}
const char *save = input;
if (*input == '.' || *input == 'e') {
warning("Floating numbers are not supported.");
++save;
}
input = tok_start; // reset input
scan_int();
input = save;
} break;
// TODO: Maybe an automata-like lexer is not that good these days..
// Think of making a modified version of memcmp (one that can
// intelligently be inlined) optimized for small lengths.
// Explicitly handle letters that start keywords.
// If we don't match a keyword, we fall-through to the
// identifier case.
case 'b':
{
if (input[1] == 'o'
&& input[2] == 'o'
&& input[3] == 'l'
&& input[4] == 'e'
&& input[5] == 'a'
&& input[6] == 'n'
&& !(charmap[input[7]]&(DEC|MIDCHAR))) {
input += 7;
token.kind = TOK::BOOLEAN;
return;
}
goto Lid;
} break;
case 'c':
{
if (input[1] == 'l'
&& input[2] == 'a'
&& input[3] == 's'
&& input[4] == 's'
&& !(charmap[input[5]]&(DEC|MIDCHAR))) {
input += 5;
token.kind = TOK::CLASS;
return;
}
goto Lid;
} break;
case 'e':
{
if (input[1] == 'l'
&& input[2] == 's'
&& input[3] == 'e'
&& !(charmap[input[4]]&(DEC|MIDCHAR))) {
input += 4;
token.kind = TOK::ELSE;
return;
}
if (input[1] == 'x'
&& input[2] == 't'
&& input[3] == 'e'
&& input[4] == 'n'
&& input[5] == 'd'
&& input[6] == 's'
&& !(charmap[input[7]]&(DEC|MIDCHAR))) {
input += 7;
token.kind = TOK::EXTENDS;
return;
}
goto Lid;
}
case 'f':
{
if (input[1] == 'a'
&& input[2] == 'l'
&& input[3] == 's'
&& input[4] == 'e'
&& !(charmap[input[5]]&(DEC|MIDCHAR))) {
input += 5;
token.kind = TOK::FALSE;
return;
}
goto Lid;
}
case 'i':
{
if (input[1] == 'f'
&& !(charmap[input[2]]&(DEC|MIDCHAR)))
{
input += 2;
token.kind = TOK::IF;
return;
}
if (input[1] == 'n'
&& input[2] == 't'
&& !(charmap[input[3]]&(DEC|MIDCHAR)))
{
input += 3;
token.kind = TOK::INT;
return;
}
goto Lid;
}
case 'n':
{
if (input[1] == 'e'
&& input[2] == 'w'
&& !(charmap[input[3]]&(DEC|MIDCHAR))) {
input += 3;
token.kind = TOK::NEW;
return;
}
goto Lid;
} break;
case 'p':
{
if (input[1] == 'u'
&& input[2] == 'b'
&& input[3] == 'l'
&& input[4] == 'i'
&& input[5] == 'c'
&& !(charmap[input[6]]&(DEC|MIDCHAR))) {
input += 6;
token.kind = TOK::PUBLIC;
return;
}
goto Lid;
} break;
case 'r':
{
if (input[1] == 'e'
&& input[2] == 't'
&& input[3] == 'u'
&& input[4] == 'r'
&& input[5] == 'n'
&& !(charmap[input[6]]&(DEC|MIDCHAR))) {
input += 6;
token.kind = TOK::RETURN;
return;
}
goto Lid;
}
case 's':
{
if (input[1] == 't'
&& input[2] == 'a'
&& input[3] == 't'
&& input[4] == 'i'
&& input[5] == 'c'
&& !(charmap[input[6]]&(DEC|MIDCHAR))) {
input += 6;
token.kind = TOK::STATIC;
return;
}
goto Lid;
}
case 't':
{
if (input[1] == 'r'
&& input[2] == 'u'
&& input[3] == 'e'
&& !(charmap[input[4]]&(DEC|MIDCHAR))) {
input += 4;
token.kind = TOK::TRUE;
return;
}
if (input[1] == 'h'
&& input[2] == 'i'
&& input[3] == 's'
&& !(charmap[input[4]]&(DEC|MIDCHAR))) {
input += 4;
token.kind = TOK::THIS;
return;
}
goto Lid;
}
case 'w':
{
if (input[1] == 'h'
&& input[2] == 'i'
&& input[3] == 'l'
&& input[4] == 'e'
&& !(charmap[input[5]]&(DEC|MIDCHAR))) {
input += 5;
token.kind = TOK::WHILE;
return;
}
goto Lid;
}
case 'v':
{
if (input[1] == 'o'
&& input[2] == 'i'
&& input[3] == 'd'
&& !(charmap[input[4]]&(DEC|MIDCHAR))) {
input += 4;
token.kind = TOK::VOID;
return;
}
goto Lid;
}
case 'S':
{
if (input[1] == 't'
&& input[2] == 'r'
&& input[3] == 'i'
&& input[4] == 'n'
&& input[5] == 'g'
&& !(charmap[input[6]]&(DEC|MIDCHAR))) {
input += 6;
token.kind = TOK::STRING;
return;
}
if (!memcmp(input, print_stmt_key, print_stmt_key_len)) {
input += print_stmt_key_len;
token.kind = TOK::PRINT;
return;
}
goto Lid;
} break;
// The rest of letters and _ (which correspond to an identifier)
case 'a': case 'd': case 'g': case 'h': case 'j':
case 'k': case 'l': case 'm': case 'o':
case 'q': case 'u':
case 'x': case 'y': case 'z':
case 'A' ... 'R': case 'T' ... 'Z':
case '_':
{
Lid:
while (charmap[*input]&(MIDCHAR|DEC)) {
++input;
}
token.id = str_intern_range(tok_start, input);
token.kind = TOK::ID;
} break;
case '&':
{
if (input[1] == '&') {
token.kind = TOK::AND_AND;
input += 2;
} else {
warning("There's no `&` token. Maybe you meant to write `&&`.");
++input;
}
} break;
case '<': case '\0': case '(': case ')': case '{': case '}':
case '[': case ']': case ',': case ';': case '!': case '=':
case '-': case '+': case '*':
{
switch (*input)
{
case '<': token.kind = TOK::LT; break;
case '\0': token.kind = TOK::EOI; break;
case '(': token.kind = TOK::LPAR; break;
case ')': token.kind = TOK::RPAR; break;
case '{': token.kind = TOK::LBRACE; break;
case '}': token.kind = TOK::RBRACE; break;
case '[': token.kind = TOK::LBRACKET; break;
case ']': token.kind = TOK::RBRACKET; break;
case ',': token.kind = TOK::COMMA; break;
case ';': token.kind = TOK::SEMI; break;
case '!': token.kind = TOK::NOT; break;
case '=': token.kind = TOK::ASGN; break;
case '-': token.kind = TOK::MINUS; break;
case '+': token.kind = TOK::PLUS; break;
case '*': token.kind = TOK::STAR; break;
default: printf("%c\n", *input); assert(0);
}
++input;
} break;
case '/':
{
++input;
if (*input != '/') {
syntax_error("Expected `/` after `/` so that comment starts;"
" skipping the rest of the line");
}
while (*input != 0 && *input != '\n') {
++input;
}
goto lex_again;
} break;
default:
fatal_error("Unrecognized character: ", *input, " - ln: ", loc.ln);
}
}
token_t peek_token() {
// Save
token_t save_token = token;
location_t save_loc = loc;
const char *save_input = input;
next_token();
// Save the new token to return it.
token_t new_token = token;
// Restore
input = save_input;
loc = save_loc;
token = save_token;
return new_token;
}
void assert_token(TOK kind) {
//printf("tokens: ");
//log(kind);
//printf(" ");
//log(token.kind);
//printf("\n");
assert(token.kind == kind);
next_token();
}
void assert_token_int(int i) {
//printf("%d %d\n", token.val, i);
assert(token.kind == TOK::INTLIT && token.val == i);
next_token();
}
void assert_token_id(const char *s) {
assert(token.kind == TOK::ID && token.id == str_intern(s));
next_token();
}
void assert_token_eof() {
assert(token.kind == TOK::EOI);
}
void init_input(const char *s) {
input = s;
loc.ln = 1;
next_token();
}
void lexer_init(const char *input, const char *file) {
int c;
// Initialize charmap
for (c = 0; c < 256; ++c) {
if ('0' <= c && c <= '7') {
charmap[c] |= OCT;
}
if (('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')) {
charmap[c] |= HEX;
}
if ('0' <= c && c <= '9') {
charmap[c] |= DEC;
charmap[c] |= HEX;
}
switch(c) {
case 0x9 ... 0xc:
case 0x20:
charmap[c] |= BLANK;
break;
case 0x21 ... 0x2f:
case 0x3a ... 0x40:
case 0x5b ... 0x5e:
case 0x7b ... 0x7e:
charmap[c] |= OTHER;
break;
case 'A' ... 'Z':
case 'a' ... 'z':
case '_':
charmap[c] |= MIDCHAR;
break;
}
}
// Initialize digit_from_char
for (c = '0'; c <= '9'; ++c) {
digit_from_char[c] = c - '0';
}
for (c = 'a'; c <= 'f'; ++c) {
digit_from_char[c] = c - 'a' + 10;
}
// Initialize location info
loc.ln = 1;
// Initialize the input
init_input(input);
}
void lexer_test() {
lexer_init("123 456 0x234", "test.java");
assert_token_int(123);
assert_token_int(456);
assert_token_int(0x234);
assert_token(TOK::EOI);
assert_token(TOK::EOI);
lexer_init("stefanos 456", "test.java");
assert_token_id("stefanos");
assert_token_int(456);
assert_token(TOK::EOI);
lexer_init("boolean class else if int return while System.out.println", "test.java");
assert_token(TOK::BOOLEAN);
assert_token(TOK::CLASS);
assert_token(TOK::ELSE);
assert_token(TOK::IF);
assert_token(TOK::INT);
assert_token(TOK::RETURN);
assert_token(TOK::WHILE);
assert_token(TOK::PRINT);
assert_token(TOK::EOI);
lexer_init("&& \0 another", "test.java");
assert_token(TOK::AND_AND);
assert_token(TOK::EOI);
assert_token(TOK::EOI);
lexer_init("&& ( ) { } [ ] , ; ! = - + *", "test.java");
assert_token(TOK::AND_AND);
assert_token(TOK::LPAR);
assert_token(TOK::RPAR);
assert_token(TOK::LBRACE);
assert_token(TOK::RBRACE);
assert_token(TOK::LBRACKET);
assert_token(TOK::RBRACKET);
assert_token(TOK::COMMA);
assert_token(TOK::SEMI);
assert_token(TOK::NOT);
assert_token(TOK::ASGN);
assert_token(TOK::MINUS);
assert_token(TOK::PLUS);
assert_token(TOK::STAR);
assert_token(TOK::EOI);
// Note: No error!
lexer_init("0r8", "test.java");
assert_token_int(0);
assert_token_id("r8");
assert_token(TOK::EOI);
// ** Warnings / Errors ** //
// Warning: floatings are not supported
lexer_init(".45", "test.java");
assert_token(TOK::DOT);
assert_token_int(45);
assert_token(TOK::EOI);
// Syntax Error: Invalid hex constant
lexer_init("0xw", "test.java");
// Warning: Integer literal overflow
lexer_init("0xffffffff", "test.java");
// Warning: There's no `&` token.
lexer_init("& +", "test.java");
// Syntax Error: Invalid octal integer literal.
lexer_init("085", "test.java");
// Syntax Error: Digit `8` is out of range for base 8.
lexer_init("058", "test.java");
}