-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.c
449 lines (403 loc) · 11.2 KB
/
lexer.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
#include "util.h"
#include "lib/array.h"
#include "lexer.h"
#define ALPHA "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define NUM "0123456789"
#define WHITESPACE " \t\n"
#define IDENTIFIER_HEAD (ALPHA "_")
#define IDENTIFIER_TAIL (ALPHA NUM "_")
/*
* Do not use this function if you want to include '\0' in `list`. As soon as
* it finds '\0', it terminates the search unsuccessfully.
*/
static int char_in(char c, const char *list)
{
for (; *list; list++)
if (c == *list)
return 1;
return 0;
}
/*
* Stores in `skipped` a pointer to the first char at `str` that is not in
* `list`. Returns 1 iff skipped was moved forward in `str`.
*/
static int skip_chars(const char *str, const char *list, const char **skipped)
{
for (*skipped = str; **skipped && char_in(**skipped, list); (*skipped)++)
;
return *skipped != str;
}
static int skip_one_char(const char *str, const char *list, const char **skipped)
{
*skipped = str;
if (**skipped && char_in(**skipped, list))
(*skipped)++;
return *skipped != str;
}
static char *show_on_source_line(const char *line, size_t line_no, size_t col_no)
{
/*
* TODO: could probably use Git's strbuf API for this.
* And a static buffer, so that the caller doesn't have to free it.
*/
char *prefix = xmkstr("On line %zu: ", line_no);
char *ret = xmkstr("%s%s\n%*c^", prefix, line,
col_no + strlen(prefix), ' ');
free(prefix);
return ret;
}
char *show_token_on_source_line(struct token *tok)
{
return show_on_source_line(tok->line, tok->line_no, tok->col_no);
}
const char *tt2str(enum token_type tt)
{
switch(tt) {
case TOK_NONE: return "[none]";
case TOK_OPEN_BRACE: return "{";
case TOK_CLOSE_BRACE: return "}";
case TOK_OPEN_PAR: return "(";
case TOK_CLOSE_PAR: return ")";
case TOK_SEMICOLON: return ";";
case TOK_COLON: return ":";
case TOK_QUESTION_MARK: return "?";
case TOK_COMMA: return ",";
case TOK_INT_KW: return "<int> keyword";
case TOK_VOID_KW: return "<void> keyword";
case TOK_RETURN_KW: return "<return> keyword";
case TOK_IF_KW: return "<if> keyword";
case TOK_ELSE_KW: return "<else> keyword";
case TOK_FOR_KW: return "<for> keyword";
case TOK_WHILE_KW: return "<while> keyword";
case TOK_DO_KW: return "<do> keyword";
case TOK_BREAK_KW: return "<break> keyword";
case TOK_CONTINUE_KW: return "<continue> keyword";
case TOK_GOTO_KW: return "<goto> keyword";
case TOK_IDENTIFIER: return "<identifier>";
case TOK_INTEGER: return "<integer>";
case TOK_MINUS: return "-";
case TOK_TILDE: return "~";
case TOK_PLUS: return "+";
case TOK_STAR: return "*";
case TOK_F_SLASH: return "/";
case TOK_MODULO: return "%";
case TOK_BITWISE_AND: return "&";
case TOK_BITWISE_OR: return "|";
case TOK_BITWISE_XOR: return "^";
case TOK_BITWISE_LEFT_SHIFT: return "<<";
case TOK_BITWISE_RIGHT_SHIFT: return ">>";
case TOK_ASSIGNMENT: return "=";
case TOK_LOGIC_NOT: return "!";
case TOK_LOGIC_AND: return "&&";
case TOK_LOGIC_OR: return "||";
case TOK_EQUAL: return "==";
case TOK_NOT_EQUAL: return "!=";
case TOK_LT: return "<";
case TOK_LE: return "<=";
case TOK_GT: return ">";
case TOK_GE: return ">=";
case TOK_PLUS_ASSIGNMENT: return "+=";
case TOK_MINUS_ASSIGNMENT: return "-=";
case TOK_SLASH_ASSIGNMENT: return "/=";
case TOK_STAR_ASSIGNMENT: return "*=";
case TOK_MODULO_ASSIGNMENT: return "%=";
case TOK_BITWISE_AND_ASSIGNMENT: return "&=";
case TOK_BITWISE_OR_ASSIGNMENT: return "|=";
case TOK_BITWISE_XOR_ASSIGNMENT: return "^=";
case TOK_BITWISE_LEFT_SHIFT_ASSIGNMENT: return "<<=";
case TOK_BITWISE_RIGHT_SHIFT_ASSIGNMENT: return ">>=";
case TOK_PLUS_PLUS: return "++";
case TOK_MINUS_MINUS: return "--";
default:
BUG("tt2str: unknown token type: %d\n", tt);
}
}
char *tok2str(struct token *t)
{
const char *type_str = tt2str(t->type);
switch(t->type) {
case TOK_IDENTIFIER:
return xmkstr("%s '%s'", type_str, (char *)t->value);
case TOK_INTEGER:
return xmkstr("%s '%d'", type_str, *(int *)(t->value));
default:
return xstrdup(type_str);
}
}
void print_token(struct token *t)
{
char *tok_str = tok2str(t);
printf("%s\n", tok_str);
free(tok_str);
}
void free_token(struct token *t)
{
t->type = TOK_NONE;
FREE_AND_NULL(t->value);
free((char *)t->line);
}
struct lex_ctx {
struct token *tokens;
size_t alloc, nr;
const char *buf, *line_start;
size_t line_no, col_no;
};
#define LEX_CTX_INIT(buffer) \
{ .line_no = 1, .buf = (buffer), .line_start = (buffer) }
static void add_token_with_value(struct lex_ctx *ctx, enum token_type type,
void *value)
{
ALLOC_GROW(ctx->tokens, ctx->nr + 1, ctx->alloc);
struct token *tok = &ctx->tokens[ctx->nr++];
tok->type = type;
tok->value = value;
/* TODO: it's wasteful to dup the line for evert token in it. */
tok->line = tab2sp(getline_dup(ctx->line_start), 1);
tok->line_no = ctx->line_no;
tok->col_no = ctx->col_no;
}
#define add_token(ctx, type) add_token_with_value(ctx, type, NULL)
static int consume_str(struct lex_ctx *ctx, const char *needle, enum token_type type)
{
const char *aux;
if (skip_prefix(ctx->buf, needle, &aux)) {
add_token(ctx, type);
ctx->col_no += aux - ctx->buf;
ctx->buf = aux;
return 1;
}
return 0;
}
static int consume_char(struct lex_ctx *ctx, char needle, enum token_type type)
{
if (*ctx->buf == needle) {
add_token(ctx, type);
(ctx->col_no)++;
(ctx->buf)++;
return 1;
}
return 0;
}
static int consume_keyword(struct lex_ctx *ctx, char *needle, enum token_type type)
{
const char *aux;
if (skip_prefix(ctx->buf, needle, &aux) && !char_in(*aux, IDENTIFIER_TAIL)) {
add_token(ctx, type);
ctx->col_no += aux - ctx->buf;
ctx->buf = aux;
return 1;
}
return 0;
}
static int consume_newline(struct lex_ctx *ctx)
{
if (*ctx->buf == '\n') {
ctx->line_start = ++(ctx->buf);
ctx->line_no++;
ctx->col_no = 0;
return 1;
}
return 0;
}
static int consume_whitespaces(struct lex_ctx *ctx)
{
int ret = 0;
while (char_in(*ctx->buf, WHITESPACE)) {
ret = 1;
if (consume_newline(ctx)) {
;
} else {
ctx->col_no++;
ctx->buf++;
}
}
return ret;
}
static int consume_comments(struct lex_ctx *ctx)
{
/* Single-line comments. */
if (*ctx->buf == '/' && *(ctx->buf + 1) == '/') {
ctx->buf += 2;
ctx->col_no += 2;
while (*ctx->buf && *ctx->buf != '\n') {
ctx->buf++;
ctx->col_no++;
}
return 1;
}
/* Multi-line comments. */
if (*ctx->buf == '/' && *(ctx->buf + 1) == '*') {
const char *comment_line_start = ctx->line_start;
size_t comment_line_no = ctx->line_no,
comment_col_no = ctx->col_no;
ctx->buf += 2;
ctx->col_no += 2;
while (1) {
if (*ctx->buf == '*' && *(ctx->buf + 1) == '/') {
ctx->buf += 2;
ctx->col_no += 2;
break;
} else if (consume_newline(ctx)) {
continue;
} else if (!*ctx->buf) {
die("lexer error: runaway comment block.\n%s",
show_on_source_line(
tab2sp(getline_dup(comment_line_start), 1),
comment_line_no, comment_col_no));
} else {
ctx->buf++;
ctx->col_no++;
}
}
return 1;
}
return 0;
}
struct token *lex(const char *str)
{
struct lex_ctx ctx = LEX_CTX_INIT(str);
while (*ctx.buf) {
const char *aux;
if (consume_whitespaces(&ctx))
;
else if (consume_comments(&ctx))
;
else if (consume_char(&ctx, '{', TOK_OPEN_BRACE))
;
else if (consume_char(&ctx, '}', TOK_CLOSE_BRACE))
;
else if (consume_char(&ctx, '(', TOK_OPEN_PAR))
;
else if (consume_char(&ctx, ')', TOK_CLOSE_PAR))
;
else if (consume_char(&ctx, ';', TOK_SEMICOLON))
;
else if (consume_char(&ctx, ':', TOK_COLON))
;
else if (consume_char(&ctx, '?', TOK_QUESTION_MARK))
;
else if (consume_char(&ctx, ',', TOK_COMMA))
;
else if (consume_str(&ctx, "+=", TOK_PLUS_ASSIGNMENT))
;
else if (consume_str(&ctx, "-=", TOK_MINUS_ASSIGNMENT))
;
else if (consume_str(&ctx, "/=", TOK_SLASH_ASSIGNMENT))
;
else if (consume_str(&ctx, "*=", TOK_STAR_ASSIGNMENT))
;
else if (consume_str(&ctx, "%=", TOK_MODULO_ASSIGNMENT))
;
else if (consume_str(&ctx, "&=", TOK_BITWISE_AND_ASSIGNMENT))
;
else if (consume_str(&ctx, "|=", TOK_BITWISE_OR_ASSIGNMENT))
;
else if (consume_str(&ctx, "^=", TOK_BITWISE_XOR_ASSIGNMENT))
;
else if (consume_str(&ctx, "<<=", TOK_BITWISE_LEFT_SHIFT_ASSIGNMENT))
;
else if (consume_str(&ctx, ">>=", TOK_BITWISE_RIGHT_SHIFT_ASSIGNMENT))
;
else if (consume_str(&ctx, "++", TOK_PLUS_PLUS))
;
else if (consume_str(&ctx, "--", TOK_MINUS_MINUS))
;
else if (consume_char(&ctx, '-', TOK_MINUS))
;
else if (consume_char(&ctx, '~', TOK_TILDE))
;
else if (consume_char(&ctx, '+', TOK_PLUS))
;
else if (consume_char(&ctx, '*', TOK_STAR))
;
else if (consume_char(&ctx, '/', TOK_F_SLASH))
;
else if (consume_char(&ctx, '%', TOK_MODULO))
;
else if (consume_char(&ctx, '^', TOK_BITWISE_XOR))
;
else if (consume_str(&ctx, "&&", TOK_LOGIC_AND))
;
else if (consume_str(&ctx, "||", TOK_LOGIC_OR))
;
else if (consume_str(&ctx, "==", TOK_EQUAL))
;
else if (consume_str(&ctx, "!=", TOK_NOT_EQUAL))
;
else if (consume_str(&ctx, "<=", TOK_LE))
;
else if (consume_str(&ctx, ">=", TOK_GE))
;
else if (consume_str(&ctx, "<<", TOK_BITWISE_LEFT_SHIFT))
;
else if (consume_str(&ctx, ">>", TOK_BITWISE_RIGHT_SHIFT))
;
/* These must come after ">*", "<*", and "*=*" tokens. */
else if (consume_char(&ctx, '>', TOK_GT))
;
else if (consume_char(&ctx, '<', TOK_LT))
;
else if (consume_char(&ctx, '!', TOK_LOGIC_NOT))
;
else if (consume_char(&ctx, '=', TOK_ASSIGNMENT))
;
/* These must come after "&*" and "|*" tokens. */
else if (consume_char(&ctx, '&', TOK_BITWISE_AND))
;
else if (consume_char(&ctx, '|', TOK_BITWISE_OR))
;
else if (consume_keyword(&ctx, "int", TOK_INT_KW))
;
else if (consume_keyword(&ctx, "void", TOK_VOID_KW))
;
else if (consume_keyword(&ctx, "return", TOK_RETURN_KW))
;
else if (consume_keyword(&ctx, "if", TOK_IF_KW))
;
else if (consume_keyword(&ctx, "else", TOK_ELSE_KW))
;
else if (consume_keyword(&ctx, "for", TOK_FOR_KW))
;
else if (consume_keyword(&ctx, "while", TOK_WHILE_KW))
;
else if (consume_keyword(&ctx, "do", TOK_DO_KW))
;
else if (consume_keyword(&ctx, "break", TOK_BREAK_KW))
;
else if (consume_keyword(&ctx, "continue", TOK_CONTINUE_KW))
;
else if (consume_keyword(&ctx, "goto", TOK_GOTO_KW))
;
else if (skip_one_char(ctx.buf, IDENTIFIER_HEAD, &aux) &&
skip_chars(ctx.buf, IDENTIFIER_TAIL, &aux) &&
!char_in(*aux, IDENTIFIER_TAIL)) {
add_token_with_value(&ctx, TOK_IDENTIFIER, xstrndup(ctx.buf, aux - ctx.buf));
ctx.col_no += aux - ctx.buf;
ctx.buf = aux;
}
else if (skip_chars(ctx.buf, NUM, &aux) && !char_in(*aux, IDENTIFIER_TAIL)) {
int *val = xmalloc(sizeof(*val));
*val = strtol(ctx.buf, NULL, 10);
add_token_with_value(&ctx, TOK_INTEGER, val);
ctx.col_no += aux - ctx.buf;
ctx.buf = aux;
}
else {
size_t i = 0;
while (ctx.buf[i] && !char_in(ctx.buf[i], WHITESPACE))
i++;
die("lex error: unknown token '%s'\n%s", xstrndup(ctx.buf, i),
show_on_source_line(tab2sp(getline_dup(ctx.line_start), 1),
ctx.line_no, ctx.col_no));
}
}
add_token(&ctx, TOK_NONE); /* sentinel */
REALLOC_ARRAY(ctx.tokens, ctx.nr); /* trim excess. */
return ctx.tokens;
}
void free_tokens(struct token *toks)
{
for (struct token *tok = toks; !end_token(tok); tok++)
free_token(tok);
free(toks);
}