-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.c
485 lines (465 loc) · 17.7 KB
/
scanner.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
/**
* Implementace překladače imperativního jazyka IFJ22
*
* @file scanner.c
* @author Josef Kuchař ([email protected])
* @author Matej Sirovatka ([email protected])
* @author Tomáš Běhal ([email protected])
* @author Šimon Benčík ([email protected])
* @brief Lexical analysis
*/
#include "scanner.h"
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include "error.h"
#include "str.h"
/**
* @brief Get keyword token type
*
* @param str Keyword name
* @return Keyword token type or -1 if string is not keyword
*/
int get_keyword_token_type(str_t* str) {
if (strcmp("else", str->val) == 0)
return TOK_ELSE;
if (strcmp("float", str->val) == 0)
return TOK_FLOAT;
if (strcmp("function", str->val) == 0)
return TOK_FUNCTION;
if (strcmp("if", str->val) == 0)
return TOK_IF;
if (strcmp("int", str->val) == 0)
return TOK_INT;
if (strcmp("null", str->val) == 0)
return TOK_NULL;
if (strcmp("return", str->val) == 0)
return TOK_RETURN;
if (strcmp("string", str->val) == 0)
return TOK_STRING;
if (strcmp("void", str->val) == 0)
return TOK_VOID;
if (strcmp("while", str->val) == 0)
return TOK_WHILE;
if (strcmp("for", str->val) == 0)
return TOK_FOR;
if (strcmp("break", str->val) == 0)
return TOK_BREAK;
if (strcmp("continue", str->val) == 0)
return TOK_CONTINUE;
return -1;
}
token_t scanner_get_next(scanner_t* scanner) {
while (true) {
scanner->col_nr++;
const int c = fgetc(scanner->input);
switch (scanner->state) {
case SC_CODE_START: {
// If we encounter EOF in this state it's an error
if (c == EOF) {
error_exit(ERR_LEX);
}
str_add_char(&scanner->buffer, c);
if (scanner->buffer.len == 5) { // <?php
if (strcmp(scanner->buffer.val, "<?php") != 0) {
error_exit(ERR_LEX);
} else {
str_clear(&scanner->buffer);
scanner->state = SC_START;
break;
}
}
break;
}
case SC_START: {
// Skip whitespaces
if (isspace(c)) {
if (c == '\n') {
scanner->col_nr = 0;
scanner->line_nr++;
} else {
}
break;
}
// Single char tokens (that are for sure end states)
switch (c) {
case '+':
return token_new(TOK_PLUS, scanner->line_nr, scanner->col_nr);
case '-':
return token_new(TOK_MINUS, scanner->line_nr, scanner->col_nr);
case '*':
return token_new(TOK_MULTIPLY, scanner->line_nr, scanner->col_nr);
case '(':
return token_new(TOK_LPAREN, scanner->line_nr, scanner->col_nr);
case ')':
return token_new(TOK_RPAREN, scanner->line_nr, scanner->col_nr);
case '{':
return token_new(TOK_LBRACE, scanner->line_nr, scanner->col_nr);
case '}':
return token_new(TOK_RBRACE, scanner->line_nr, scanner->col_nr);
case '.':
return token_new(TOK_DOT, scanner->line_nr, scanner->col_nr);
case ',':
return token_new(TOK_COMMA, scanner->line_nr, scanner->col_nr);
case ';':
return token_new(TOK_SEMICOLON, scanner->line_nr, scanner->col_nr);
case ':':
return token_new(TOK_COLON, scanner->line_nr, scanner->col_nr);
case EOF:
return token_new(TOK_EOF, scanner->line_nr, scanner->col_nr);
}
// Multi char tokens
switch (c) {
case '=':
scanner->state = SC_ASSIGN;
continue;
case '!':
scanner->state = SC_EXCLAMATION;
continue;
case '<':
scanner->state = SC_LESS;
continue;
case '>':
scanner->state = SC_GREATER;
continue;
case '$':
scanner->state = SC_VARIABLE_START;
str_add_char(&scanner->buffer, c);
continue;
case '"':
scanner->state = SC_STRING_LIT;
continue;
case '/':
scanner->state = SC_DIVIDE;
continue;
case '?':
scanner->state = SC_QUESTION_MARK;
continue;
}
// Function names and keywords
if (isalpha(c) || c == '_') {
scanner->state = SC_FUNCTION;
str_add_char(&scanner->buffer, c);
break;
}
// Numbers
if (isdigit(c)) {
scanner->state = SC_NUMBER;
str_add_char(&scanner->buffer, c);
break;
}
error_exit(ERR_LEX);
break;
}
case SC_ASSIGN: {
if (c == '=') {
scanner->state = SC_EQUALS;
} else {
scanner->col_nr--;
ungetc(c, scanner->input);
scanner->state = SC_START;
return token_new(TOK_ASSIGN, scanner->line_nr, scanner->col_nr);
}
break;
}
case SC_EQUALS: {
if (c == '=') {
scanner->state = SC_START;
return token_new(TOK_EQUALS, scanner->line_nr, scanner->col_nr);
} else {
// We don't have == token
error_exit(ERR_LEX);
}
break;
}
case SC_EXCLAMATION: {
if (c == '=') {
scanner->state = SC_NEQUALS;
} else {
// We don't have ! token
error_exit(ERR_LEX);
}
break;
}
case SC_NEQUALS: {
if (c == '=') {
scanner->state = SC_START;
return token_new(TOK_NEQUALS, scanner->line_nr, scanner->col_nr);
} else {
// We don't have != token
error_exit(ERR_LEX);
}
break;
}
case SC_LESS: {
scanner->state = SC_START;
if (c == '=') {
return token_new(TOK_LESS_E, scanner->line_nr, scanner->col_nr);
} else {
scanner->col_nr--;
ungetc(c, scanner->input);
return token_new(TOK_LESS, scanner->line_nr, scanner->col_nr);
}
break;
}
case SC_GREATER: {
scanner->state = SC_START;
if (c == '=') {
return token_new(TOK_GREATER_E, scanner->line_nr, scanner->col_nr);
} else {
scanner->col_nr--;
ungetc(c, scanner->input);
return token_new(TOK_GREATER, scanner->line_nr, scanner->col_nr);
}
break;
}
case SC_VARIABLE_START: {
if (isalpha(c) || c == '_') {
str_add_char(&scanner->buffer, c);
scanner->state = SC_VARIABLE;
} else {
// There has to be valid character after $
error_exit(ERR_LEX);
}
break;
}
case SC_VARIABLE: {
if (isalnum(c) || c == '_') {
str_add_char(&scanner->buffer, c);
} else {
scanner->col_nr--;
ungetc(c, scanner->input);
scanner->state = SC_START;
return token_new_with_string(TOK_VAR, &scanner->buffer, scanner->line_nr,
scanner->col_nr);
}
break;
}
case SC_FUNCTION: {
if (isalnum(c) || c == '_') {
str_add_char(&scanner->buffer, c);
} else {
scanner->col_nr--;
ungetc(c, scanner->input);
scanner->state = SC_START;
// Check if string is keyword
int keyword = get_keyword_token_type(&scanner->buffer);
if (keyword != -1) {
str_clear(&scanner->buffer);
// False means it's not optional type
return token_new_with_bool(keyword, false, scanner->line_nr,
scanner->col_nr);
} else {
return token_new_with_string(TOK_FUN_NAME, &scanner->buffer,
scanner->line_nr, scanner->col_nr);
}
}
break;
}
case SC_STRING_LIT: {
if (c == EOF) {
error_exit(ERR_LEX);
}
// Handle escape sequence
if (c == '\\') {
const int c2 = getc(scanner->input);
if (c2 == '"') {
str_add_char(&scanner->buffer, '"');
break;
} else if (c2 == '\\') {
str_add_char(&scanner->buffer, '\\');
} else {
ungetc(c2, scanner->input);
}
}
if (c == '"') {
scanner->state = SC_START;
return token_new_with_string_literal(TOK_STR_LIT, &scanner->buffer,
scanner->line_nr, scanner->col_nr);
} else if (c >= 32) {
str_add_char(&scanner->buffer, c);
} else {
error_exit(ERR_LEX);
}
break;
}
case SC_DIVIDE: {
if (c == '*') {
scanner->col_nr = 0;
scanner->line_nr++;
scanner->state = SC_MCOMMENT;
} else if (c == '/') {
scanner->state = SC_LCOMMENT;
} else {
scanner->col_nr--;
ungetc(c, scanner->input);
scanner->state = SC_START;
return token_new(TOK_DIVIDE, scanner->line_nr, scanner->col_nr);
}
break;
}
case SC_LCOMMENT: {
if (c == '\n' || c == EOF) {
if (c == '\n') {
scanner->col_nr = 0;
scanner->line_nr++;
}
scanner->state = SC_START;
}
break;
}
case SC_MCOMMENT: {
if (c == EOF) {
error_exit(ERR_LEX); // Unterminated comment
} else if (c == '*') {
scanner->col_nr = 0;
scanner->line_nr++;
const int c2 = fgetc(scanner->input);
if (c2 == '/') {
scanner->state = SC_START;
} else if (c == EOF) {
error_exit(ERR_LEX);
} else {
ungetc(c2, scanner->input);
}
}
break;
}
case SC_QUESTION_MARK: {
if (c == '>') {
scanner->state = SC_END;
} else if (isalpha(c)) {
scanner->col_nr--;
ungetc(c, scanner->input);
scanner->state = SC_TYPE_OPTIONAL;
} else {
error_exit(ERR_LEX);
}
break;
}
case SC_END: {
if (c == EOF) {
return token_new(TOK_EOF, scanner->line_nr, scanner->col_nr);
}
if (c == '\n' || (c == '\r' && getc(scanner->input) == '\n')) {
scanner->state = SC_HARDEND;
break;
} else {
// There can't be anything after ?> except \n and \r\n
error_exit(ERR_SYN);
}
break;
}
case SC_HARDEND: {
// Only EOF is allowed after hard end
if (c == EOF) {
return token_new(TOK_EOF, scanner->line_nr, scanner->col_nr);
}
error_exit(ERR_SYN);
break;
}
case SC_NUMBER: {
if (isdigit(c)) {
str_add_char(&scanner->buffer, c);
} else if (c == '.') {
str_add_char(&scanner->buffer, c);
const int c2 = fgetc(scanner->input);
if (isdigit(c2)) {
ungetc(c2, scanner->input);
scanner->state = SC_FLOAT;
} else {
error_exit(ERR_LEX);
}
} else if (c == 'e' || c == 'E') {
str_add_char(&scanner->buffer, c);
scanner->state = SC_EXPONENT_SIGN;
} else if (isalpha(c)) {
error_exit(ERR_LEX);
} else {
scanner->col_nr--;
ungetc(c, scanner->input);
scanner->state = SC_START;
return token_new_with_int(TOK_INT_LIT, &scanner->buffer, scanner->line_nr,
scanner->col_nr);
}
break;
}
case SC_FLOAT: {
if (isdigit(c)) {
str_add_char(&scanner->buffer, c);
} else if (c == 'e' || c == 'E') {
str_add_char(&scanner->buffer, 'e');
scanner->state = SC_EXPONENT_SIGN;
} else if (isalpha(c)) {
error_exit(ERR_LEX);
} else {
scanner->col_nr--;
ungetc(c, scanner->input);
scanner->state = SC_START;
return token_new_with_float(TOK_FLOAT_LIT, &scanner->buffer, scanner->line_nr,
scanner->col_nr);
}
break;
}
case SC_EXPONENT_SIGN: {
if (c == '+' || c == '-') {
const int c2 = getc(scanner->input);
if (isdigit(c2)) {
ungetc(c2, scanner->input);
str_add_char(&scanner->buffer, c);
scanner->state = SC_EXPONENT;
} else {
error_exit(ERR_LEX);
}
} else if (isdigit(c)) {
scanner->col_nr--;
ungetc(c, scanner->input);
scanner->state = SC_EXPONENT;
} else {
error_exit(ERR_LEX);
}
break;
}
case SC_EXPONENT: {
if (isdigit(c)) {
str_add_char(&scanner->buffer, c);
} else if (isalpha(c)) {
error_exit(ERR_LEX);
} else {
scanner->col_nr--;
ungetc(c, scanner->input);
scanner->state = SC_START;
return token_new_with_float(TOK_FLOAT_LIT, &scanner->buffer, scanner->line_nr,
scanner->col_nr);
}
break;
}
case SC_TYPE_OPTIONAL: {
if (isalpha(c)) {
str_add_char(&scanner->buffer, c);
} else {
scanner->col_nr--;
ungetc(c, scanner->input);
int keyword = get_keyword_token_type(&scanner->buffer);
if (keyword != -1 &&
(keyword != TOK_FLOAT || keyword != TOK_INT || keyword != TOK_STRING)) {
scanner->state = SC_START;
str_clear(&scanner->buffer);
return token_new_with_bool(keyword, true, scanner->line_nr,
scanner->col_nr);
} else {
error_exit(ERR_LEX);
}
}
break;
}
}
}
}
scanner_t scanner_new(FILE* input) {
return (scanner_t){
.buffer = str_new(), .state = SC_CODE_START, .input = input, .line_nr = 1, .col_nr = 0};
}
void scanner_free(scanner_t* scanner) {
str_free(&scanner->buffer);
}