-
Notifications
You must be signed in to change notification settings - Fork 749
/
lex.c
610 lines (560 loc) · 17.4 KB
/
lex.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
// Copyright 2012 Rui Ueyama. Released under the MIT license.
/*
* Tokenizer
*
* This is a translation phase after the phase 1 and 2 in file.c.
* In this phase, the source code is decomposed into preprocessing tokens.
*
* Each comment is treated as if it were a space character.
* Space characters are removed, but the presence of the characters is
* recorded to the token that immediately follows the spaces as a boolean flag.
* Newlines are converted to newline tokens.
*
* Note that the pp-token is different from the regular token.
* A keyword, such as "if", is just an identifier at this stage.
* The definition of the pp-token is usually more relaxed than
* the regular one. For example, ".32e." is a valid pp-number.
* Pp-tokens are converted to regular tokens by the C preprocesor
* (and invalid tokens are rejected by that).
* Some tokens are removed by the preprocessor (e.g. newline).
* For more information about pp-tokens, see C11 6.4 "Lexical Elements".
*/
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "8cc.h"
static Vector *buffers = &EMPTY_VECTOR;
static Token *space_token = &(Token){ TSPACE };
static Token *newline_token = &(Token){ TNEWLINE };
static Token *eof_token = &(Token){ TEOF };
typedef struct {
int line;
int column;
} Pos;
static Pos pos;
static char *pos_string(Pos *p) {
File *f = current_file();
return format("%s:%d:%d", f ? f->name : "(unknown)", p->line, p->column);
}
#define errorp(p, ...) errorf(__FILE__ ":" STR(__LINE__), pos_string(&p), __VA_ARGS__)
#define warnp(p, ...) warnf(__FILE__ ":" STR(__LINE__), pos_string(&p), __VA_ARGS__)
static void skip_block_comment(void);
void lex_init(char *filename) {
vec_push(buffers, make_vector());
if (!strcmp(filename, "-")) {
stream_push(make_file(stdin, "-"));
return;
}
FILE *fp = fopen(filename, "r");
if (!fp)
error("Cannot open %s: %s", filename, strerror(errno));
stream_push(make_file(fp, filename));
}
static Pos get_pos(int delta) {
File *f = current_file();
return (Pos){ f->line, f->column + delta };
}
static void mark() {
pos = get_pos(0);
}
static Token *make_token(Token *tmpl) {
Token *r = malloc(sizeof(Token));
*r = *tmpl;
r->hideset = NULL;
File *f = current_file();
r->file = f;
r->line = pos.line;
r->column = pos.column;
r->count = f->ntok++;
return r;
}
static Token *make_ident(char *p) {
return make_token(&(Token){ TIDENT, .sval = p });
}
static Token *make_strtok(char *s, int len, int enc) {
return make_token(&(Token){ TSTRING, .sval = s, .slen = len, .enc = enc });
}
static Token *make_keyword(int id) {
return make_token(&(Token){ TKEYWORD, .id = id });
}
static Token *make_number(char *s) {
return make_token(&(Token){ TNUMBER, .sval = s });
}
static Token *make_invalid(char c) {
return make_token(&(Token){ TINVALID, .c = c });
}
static Token *make_char(int c, int enc) {
return make_token(&(Token){ TCHAR, .c = c, .enc = enc });
}
static bool iswhitespace(int c) {
return c == ' ' || c == '\t' || c == '\f' || c == '\v';
}
static int peek() {
int r = readc();
unreadc(r);
return r;
}
static bool next(int expect) {
int c = readc();
if (c == expect)
return true;
unreadc(c);
return false;
}
static void skip_line() {
for (;;) {
int c = readc();
if (c == EOF)
return;
if (c == '\n') {
unreadc(c);
return;
}
}
}
static bool do_skip_space() {
int c = readc();
if (c == EOF)
return false;
if (iswhitespace(c))
return true;
if (c == '/') {
if (next('*')) {
skip_block_comment();
return true;
}
if (next('/')) {
skip_line();
return true;
}
}
unreadc(c);
return false;
}
// Skips spaces including comments.
// Returns true if at least one space is skipped.
static bool skip_space() {
if (!do_skip_space())
return false;
while (do_skip_space());
return true;
}
static void skip_char() {
if (readc() == '\\')
readc();
int c = readc();
while (c != EOF && c != '\'')
c = readc();
}
static void skip_string() {
for (int c = readc(); c != EOF && c != '"'; c = readc())
if (c == '\\')
readc();
}
// Skips a block of code excluded from input by #if, #ifdef and the like.
// C11 6.10 says that code within #if and #endif needs to be a sequence of
// valid tokens even if skipped. However, in reality, most compilers don't
// tokenize nor validate contents. We don't do that, too.
// This function is to skip code until matching #endif as fast as we can.
void skip_cond_incl() {
int nest = 0;
for (;;) {
bool bol = (current_file()->column == 1);
skip_space();
int c = readc();
if (c == EOF)
return;
if (c == '\'') {
skip_char();
continue;
}
if (c == '\"') {
skip_string();
continue;
}
if (c != '#' || !bol)
continue;
int column = current_file()->column - 1;
Token *tok = lex();
if (tok->kind != TIDENT)
continue;
if (!nest && (is_ident(tok, "else") || is_ident(tok, "elif") || is_ident(tok, "endif"))) {
unget_token(tok);
Token *hash = make_keyword('#');
hash->bol = true;
hash->column = column;
unget_token(hash);
return;
}
if (is_ident(tok, "if") || is_ident(tok, "ifdef") || is_ident(tok, "ifndef"))
nest++;
else if (nest && is_ident(tok, "endif"))
nest--;
skip_line();
}
}
// Reads a number literal. Lexer's grammar on numbers is not strict.
// Integers and floating point numbers and different base numbers are not distinguished.
static Token *read_number(char c) {
Buffer *b = make_buffer();
buf_write(b, c);
char last = c;
for (;;) {
int c = readc();
bool flonum = strchr("eEpP", last) && strchr("+-", c);
if (!isdigit(c) && !isalpha(c) && c != '.' && !flonum) {
unreadc(c);
buf_write(b, '\0');
return make_number(buf_body(b));
}
buf_write(b, c);
last = c;
}
}
static bool nextoct() {
int c = peek();
return '0' <= c && c <= '7';
}
// Reads an octal escape sequence.
static int read_octal_char(int c) {
int r = c - '0';
if (!nextoct())
return r;
r = (r << 3) | (readc() - '0');
if (!nextoct())
return r;
return (r << 3) | (readc() - '0');
}
// Reads a \x escape sequence.
static int read_hex_char() {
Pos p = get_pos(-2);
int c = readc();
if (!isxdigit(c))
errorp(p, "\\x is not followed by a hexadecimal character: %c", c);
int r = 0;
for (;; c = readc()) {
switch (c) {
case '0' ... '9': r = (r << 4) | (c - '0'); continue;
case 'a' ... 'f': r = (r << 4) | (c - 'a' + 10); continue;
case 'A' ... 'F': r = (r << 4) | (c - 'A' + 10); continue;
default: unreadc(c); return r;
}
}
}
static bool is_valid_ucn(unsigned int c) {
// C11 6.4.3p2: U+D800 to U+DFFF are reserved for surrogate pairs.
// A codepoint within the range cannot be a valid character.
if (0xD800 <= c && c <= 0xDFFF)
return false;
// It's not allowed to encode ASCII characters using \U or \u.
// Some characters not in the basic character set (C11 5.2.1p3)
// are allowed as exceptions.
return 0xA0 <= c || c == '$' || c == '@' || c == '`';
}
// Reads \u or \U escape sequences. len is 4 or 8, respecitvely.
static int read_universal_char(int len) {
Pos p = get_pos(-2);
unsigned int r = 0;
for (int i = 0; i < len; i++) {
char c = readc();
switch (c) {
case '0' ... '9': r = (r << 4) | (c - '0'); continue;
case 'a' ... 'f': r = (r << 4) | (c - 'a' + 10); continue;
case 'A' ... 'F': r = (r << 4) | (c - 'A' + 10); continue;
default: errorp(p, "invalid universal character: %c", c);
}
}
if (!is_valid_ucn(r))
errorp(p, "invalid universal character: \\%c%0*x", (len == 4) ? 'u' : 'U', len, r);
return r;
}
static int read_escaped_char() {
Pos p = get_pos(-1);
int c = readc();
// This switch-cases is an interesting example of magical aspects
// of self-hosting compilers. Here, we teach the compiler about
// escaped sequences using escaped sequences themselves.
// This is a tautology. The information about their real character
// codes is not present in the source code but propagated from
// a compiler compiling the source code.
// See "Reflections on Trusting Trust" by Ken Thompson for more info.
// http://cm.bell-labs.com/who/ken/trust.html
switch (c) {
case '\'': case '"': case '?': case '\\':
return c;
case 'a': return '\a';
case 'b': return '\b';
case 'f': return '\f';
case 'n': return '\n';
case 'r': return '\r';
case 't': return '\t';
case 'v': return '\v';
case 'e': return '\033'; // '\e' is GNU extension
case 'x': return read_hex_char();
case 'u': return read_universal_char(4);
case 'U': return read_universal_char(8);
case '0' ... '7': return read_octal_char(c);
}
warnp(p, "unknown escape character: \\%c", c);
return c;
}
static Token *read_char(int enc) {
int c = readc();
int r = (c == '\\') ? read_escaped_char() : c;
c = readc();
if (c != '\'')
errorp(pos, "unterminated char");
if (enc == ENC_NONE)
return make_char((char)r, enc);
return make_char(r, enc);
}
// Reads a string literal.
static Token *read_string(int enc) {
Buffer *b = make_buffer();
for (;;) {
int c = readc();
if (c == EOF)
errorp(pos, "unterminated string");
if (c == '"')
break;
if (c != '\\') {
buf_write(b, c);
continue;
}
bool isucs = (peek() == 'u' || peek() == 'U');
c = read_escaped_char();
if (isucs) {
write_utf8(b, c);
continue;
}
buf_write(b, c);
}
buf_write(b, '\0');
return make_strtok(buf_body(b), buf_len(b), enc);
}
static Token *read_ident(char c) {
Buffer *b = make_buffer();
buf_write(b, c);
for (;;) {
c = readc();
if (isalnum(c) || (c & 0x80) || c == '_' || c == '$') {
buf_write(b, c);
continue;
}
// C11 6.4.2.1: \u or \U characters (universal-character-name)
// are allowed to be part of identifiers.
if (c == '\\' && (peek() == 'u' || peek() == 'U')) {
write_utf8(b, read_escaped_char());
continue;
}
unreadc(c);
buf_write(b, '\0');
return make_ident(buf_body(b));
}
}
static void skip_block_comment() {
Pos p = get_pos(-2);
bool maybe_end = false;
for (;;) {
int c = readc();
if (c == EOF)
errorp(p, "premature end of block comment");
if (c == '/' && maybe_end)
return;
maybe_end = (c == '*');
}
}
// Reads a digraph starting with '%'. Digraphs are alternative spellings
// for some punctuation characters. They are useless in ASCII.
// We implement this just for the standard compliance.
// See C11 6.4.6p3 for the spec.
static Token *read_hash_digraph() {
if (next('>'))
return make_keyword('}');
if (next(':')) {
if (next('%')) {
if (next(':'))
return make_keyword(KHASHHASH);
unreadc('%');
}
return make_keyword('#');
}
return NULL;
}
static Token *read_rep(char expect, int t1, int els) {
return make_keyword(next(expect) ? t1 : els);
}
static Token *read_rep2(char expect1, int t1, char expect2, int t2, char els) {
if (next(expect1))
return make_keyword(t1);
return make_keyword(next(expect2) ? t2 : els);
}
static Token *do_read_token() {
if (skip_space())
return space_token;
mark();
int c = readc();
switch (c) {
case '\n': return newline_token;
case ':': return make_keyword(next('>') ? ']' : ':');
case '#': return make_keyword(next('#') ? KHASHHASH : '#');
case '+': return read_rep2('+', OP_INC, '=', OP_A_ADD, '+');
case '*': return read_rep('=', OP_A_MUL, '*');
case '=': return read_rep('=', OP_EQ, '=');
case '!': return read_rep('=', OP_NE, '!');
case '&': return read_rep2('&', OP_LOGAND, '=', OP_A_AND, '&');
case '|': return read_rep2('|', OP_LOGOR, '=', OP_A_OR, '|');
case '^': return read_rep('=', OP_A_XOR, '^');
case '"': return read_string(ENC_NONE);
case '\'': return read_char(ENC_NONE);
case '/': return make_keyword(next('=') ? OP_A_DIV : '/');
case 'a' ... 't': case 'v' ... 'z': case 'A' ... 'K':
case 'M' ... 'T': case 'V' ... 'Z': case '_': case '$':
case 0x80 ... 0xFD:
return read_ident(c);
case '0' ... '9':
return read_number(c);
case 'L': case 'U': {
// Wide/char32_t character/string literal
int enc = (c == 'L') ? ENC_WCHAR : ENC_CHAR32;
if (next('"')) return read_string(enc);
if (next('\'')) return read_char(enc);
return read_ident(c);
}
case 'u':
if (next('"')) return read_string(ENC_CHAR16);
if (next('\'')) return read_char(ENC_CHAR16);
// C11 6.4.5: UTF-8 string literal
if (next('8')) {
if (next('"'))
return read_string(ENC_UTF8);
unreadc('8');
}
return read_ident(c);
case '.':
if (isdigit(peek()))
return read_number(c);
if (next('.')) {
if (next('.'))
return make_keyword(KELLIPSIS);
return make_ident("..");
}
return make_keyword('.');
case '(': case ')': case ',': case ';': case '[': case ']': case '{':
case '}': case '?': case '~':
return make_keyword(c);
case '-':
if (next('-')) return make_keyword(OP_DEC);
if (next('>')) return make_keyword(OP_ARROW);
if (next('=')) return make_keyword(OP_A_SUB);
return make_keyword('-');
case '<':
if (next('<')) return read_rep('=', OP_A_SAL, OP_SAL);
if (next('=')) return make_keyword(OP_LE);
if (next(':')) return make_keyword('[');
if (next('%')) return make_keyword('{');
return make_keyword('<');
case '>':
if (next('=')) return make_keyword(OP_GE);
if (next('>')) return read_rep('=', OP_A_SAR, OP_SAR);
return make_keyword('>');
case '%': {
Token *tok = read_hash_digraph();
if (tok)
return tok;
return read_rep('=', OP_A_MOD, '%');
}
case EOF:
return eof_token;
default: return make_invalid(c);
}
}
static bool buffer_empty() {
return vec_len(buffers) == 1 && vec_len(vec_head(buffers)) == 0;
}
// Reads a header file name for #include.
//
// Filenames after #include need a special tokenization treatment.
// A filename string may be quoted by < and > instead of "".
// Even if it's quoted by "", it's still different from a regular string token.
// For example, \ in this context is not interpreted as a quote.
// Thus, we cannot use lex() to read a filename.
//
// That the C preprocessor requires a special lexer behavior only for
// #include is a violation of layering. Ideally, the lexer should be
// agnostic about higher layers status. But we need this for the C grammar.
char *read_header_file_name(bool *std) {
if (!buffer_empty())
return NULL;
skip_space();
Pos p = get_pos(0);
char close;
if (next('"')) {
*std = false;
close = '"';
} else if (next('<')) {
*std = true;
close = '>';
} else {
return NULL;
}
Buffer *b = make_buffer();
while (!next(close)) {
int c = readc();
if (c == EOF || c == '\n')
errorp(p, "premature end of header name");
buf_write(b, c);
}
if (buf_len(b) == 0)
errorp(p, "header name should not be empty");
buf_write(b, '\0');
return buf_body(b);
}
bool is_keyword(Token *tok, int c) {
return (tok->kind == TKEYWORD) && (tok->id == c);
}
// Temporarily switches the input token stream to given list of tokens,
// so that you can get the tokens as return values of lex() again.
// After the tokens are exhausted, EOF is returned from lex() until
// "unstash" is called to restore the original state.
void token_buffer_stash(Vector *buf) {
vec_push(buffers, buf);
}
void token_buffer_unstash() {
vec_pop(buffers);
}
void unget_token(Token *tok) {
if (tok->kind == TEOF)
return;
Vector *buf = vec_tail(buffers);
vec_push(buf, tok);
}
// Reads a token from a given string.
// This function temporarily switches the main input stream to
// a given string and reads one token.
Token *lex_string(char *s) {
stream_stash(make_file_string(s));
Token *r = do_read_token();
next('\n');
Pos p = get_pos(0);
if (peek() != EOF)
errorp(p, "unconsumed input: %s", s);
stream_unstash();
return r;
}
Token *lex() {
Vector *buf = vec_tail(buffers);
if (vec_len(buf) > 0)
return vec_pop(buf);
if (vec_len(buffers) > 1)
return eof_token;
bool bol = (current_file()->column == 1);
Token *tok = do_read_token();
while (tok->kind == TSPACE) {
tok = do_read_token();
tok->space = true;
}
tok->bol = bol;
return tok;
}