-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlexical_parser.c
441 lines (379 loc) · 13.6 KB
/
lexical_parser.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
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <ctype.h>
#include "util.h"
#include "token.h"
#include "lexical_parser.h"
static char * number(char *, char *);
static char * real_number(char *, char *);
static char * operator(char *, char *);
static char * error(char *);
static char * single_line_comment(char *, char*);
static char * multi_line_comment(char *, char*);
static char * string_single_quote(char * cur, char * start);
static char * string_double_quote(char * cur, char * start);
static char * identifier(char * cur, char * start);
static int is_valid_symbol(char c);
static char * regex(char * cur, char * start);
static char * regex_flag(char * cur, char * start);
static char * escape_sequence(char * cur, char * start);
static const char * literals[] = {
"true", "false", "{}", "Infinity", "NaN", "null", "undefined"
};
#define RESERVED_WORDS_LENGTH 32
static const char* reserved_words[] = {
"this", "new", "function", "var",
"for", "if", "else", "true",
"false", "return", "undefined","null",
"instanceof", "in", "do", "default",
"void", "while", "break", "case",
"typeof", "continue", "Infinity", "NaN",
"delete", "switch", "try", "catch",
"throw", "finally", "with", "debugger",
};
#define FUTURE_RESERVED_WORDS_LENGTH 7
static const char * future_reserved_words[] = {
"class", "const", "enum", "export",
"extends", "import", "super"
};
static const char * reserved_symbols = "{}.+!=<>;-~(),*&|%&[]?^/:\\";
#define PUNCTUATORS_LENGTH 49
static const char * punctuators[] = {
"{", "}", ".", ">=", "+",
"<<", "!", "=", ">>=", ";", "/",
"==", "-", ">>", "~", "+=", "/=",
">>>=", "(", ",", "!=", "*",
">>>", "&&", "-=", "&=", ")",
"<", "===", "%", "&", "||",
"*=", "|=", "[", ">", "!==",
"++", "|", "?", "%=", "^=", "\\",
"--", "^", ":", "<<=", "]", "<="
};
#define ML_OFF 0
#define ML_COMMENT 1
#define ML_STRING 2
static int multiline_switch = ML_OFF;
static int current_line = (int) NULL;
//static int token_list_exist = 0;
static TokenList * tklst;
/**
This method takes a line, iterates over its chars
and extracts it's tokens determining whether it has lexical errors.
*/
int lexical_analisys(char * line, TokenList * tokenList, int line_number) {
char * c;
c = line; //initial position
tklst = tokenList;
current_line = line_number;
while(1) {
if(multiline_switch) { //multiline_switch is ON
switch(multiline_switch){
case ML_COMMENT:
if(LOG) printf("Line numbr: %d\n", current_line);
c = multi_line_comment(c+1, c);
break;
case ML_STRING:
//not implemented yet
error(c);
return 1;
default:
//not implemented yet
error(c);
return 1;
}
}
if(isdigit(*c)){ //digito
c = number(c+1, c);
if(c == NULL) {
return 1;
}
} else if (isalpha(*c) || *c == '$' || *c == '_') { //alpha
c = identifier(c+1, c);
} else if(is_valid_symbol(*c)){ //symbol
c = operator(c+1, c);
//printf("%c, %d - %x: en symbol\n", *c, *c, (unsigned int)*c);
if(c == NULL){ //TODO: why is this case here? check!
return 1;
} else if (*c == '\n' || *c == '\r') {
return 0;
}
} else {
switch(*c){
case '\"':
c = string_double_quote(c+1, c);
break;
case '\'':
c = string_single_quote(c+1, c);
break;
case ' ': case '\t':
c = c+1;
break;
case '\n': case '\r':
if(LOG) printf("== New line character ==\n");
if(LOG) printf("Finished parsing line\n\n");
Token * token = createToken(c, c, 0, 0, NEW_LINE);
appendToken(tklst, token);
return 0;
case '\0': //accounts for EOF, when a new line is not provided.
if(LOG) printf("Finished parsing line\n\n");
return 0;
default:
printf("unrecognized char! %c\n", *c);
error(c);
return 1;
}
}
}
}
static char * escape_sequence(char * cur, char * start) {
//hex escape sequence: '\x64' (hex char(2 digits))
//unicode escape: '\u1234' (unicode char (4 digits))
switch(*cur) {
case '\'':
cur++;
//create token for escaped single quote
break;
case '\"':
cur++;
//create token for escaped double quote
break;
case '\\':
cur++;
//create token for escaped backslash
break;
case 'b':
cur++;
//TODO: which is this escaped char for?
break;
case 'f': //form feed
cur++;
//create token for escaped form feed
break;
case 'n': //line feed
cur++;
//create token for escaped line feed
break;
case 'r': //carriage return
cur++;
//create token for escaped car. return
break;
case 't': //horizontal tab
cur++;
//create token for escaped horiz tab
break;
case 'v': //vertical tab
cur++;
//create token for escaped vertical tab
break;
case 'x':
//hex_character(*cur);
cur = cur+3; //temporary
break;
case 'u':
//unicode_chracter(*cur); TODO
cur = cur+5; //temp jumping to next characters
break;
case '/': //this slash is escaped when on regex. does this belong here? Temp fix
cur++;
if(LOG) printf("Escaped Bar");
break;
default:
break;
}
return cur;
}
static char * regex(char * cur, char * start) {
if(*cur == '\\') {
cur = escape_sequence(cur+1, cur); //jump through escaped parts
}
if( *cur == '/') {
return regex_flag(++cur, start);
} else { //TODO: here I should check the standard for not allowed characters instead of else)
return regex(++cur, start);
}
}
static char * regex_flag(char * cur, char * start) {
if(isalpha(*cur)) { //TODO: are only letters valid? do numbers and punct work too?
return regex_flag(++cur, start);
} else {
Token * token = createToken(start, cur-1, 0, 0, REGEX);
appendToken(tklst, token);
if(LOG) printf("regex: %s\n", get_string(start, cur-1));
return cur;
}
}
static char * operator(char * cur, char * start){
if(*start == '/' && isgraph(*cur)) {
if(*cur == '/'){
return single_line_comment(cur+1, start);
}
if(*cur == '*'){
return multi_line_comment(cur+1, start);
}
else {
//printf("REGEXP! %c, %c \n", *cur-1, *cur);
return regex(cur, start);
}
}else if(is_valid_symbol(*cur) ) {
//continues finding an operator
return operator(cur+1, start);
} else {
int string_index;
if((string_index = is_string_in_array(get_string(start, cur-1), punctuators, PUNCTUATORS_LENGTH))) {
if(LOG) printf("punctuator: %s - punctuator index: %d, string_index: %d\n", get_string(start, cur-1), PUNCTUATOR, string_index);
Token * token = createToken(start, cur-1, 0, 0, PUNCTUATOR + string_index);
appendToken(tklst, token);
return cur;
} else {
/* This else branch means that some concatenated punctuators were identified but not recognized
They need to be separated on their own. e.g. "(){" would be needed to expand to tokens "(", ")" and "{" respectively
*/
if(LOG) printf("possible error on punctuator: %s\n", get_string(start, cur-1));
char * c = cur-1; //last punctuator found
//decrementing c until a valid punctuator is found. next char of c is returned
while(!(string_index = is_string_in_array(get_string(start, --c), punctuators, PUNCTUATORS_LENGTH))); //&& cur >= start){
if(LOG) printf("trying to fix punctuator: %s\n", get_string(start, c));
Token * token = createToken(start, c, 0, 0, PUNCTUATOR + string_index);
appendToken(tklst, token);
return c+1;
}
}
}
//TODO: Add support for hex (ie: 0x123)
// and positive and negative numbers (at sintax?), and exponents(ie: 234e10, 45E100)
static char * number(char * cur, char * start) {
switch(*cur) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
//printf("%c - %x\n",*cur, (unsigned int)cur );
return number(cur+1, start);
//case '+': case '-': case '*': case '/':
//case ' ': case '\t': case '\0': case '\n':
case '.':
return real_number(cur+1, start);
case 'e': case 'E':
//-return exponential_number(cur+1, start);
default:
if(LOG) printf("integer: %s\n", get_string(start, cur-1));
Token * token = createToken(start, cur-1, 0, 0, LITERAL_NUMBER);
appendToken(tklst, token);
return cur;
}
}
static char * identifier(char * cur, char * start) {
if(isalnum(*cur) || *cur == '_' || *cur == '$') {
return identifier(++cur, start);
}
char * str = get_string(start, cur-1);
int string_index;
if((string_index = is_string_in_array(str, reserved_words, RESERVED_WORDS_LENGTH))) {
if(LOG) printf("reserved word: %s - kw_index = %d - token index: %d\n", str, string_index, KEYWORD + string_index );
Token * token = createToken(start, cur-1, 0, 0, KEYWORD + string_index);
appendToken(tklst, token);
} else if ((string_index = is_string_in_array(str, future_reserved_words, FUTURE_RESERVED_WORDS_LENGTH))) {
if(LOG) printf("future reserved word: %s - kw_index = %d - token index: %d\n", str, string_index, KEYWORD + string_index );
Token * token = createToken(start, cur-1, 0, 0, KEYWORD + string_index);
appendToken(tklst, token);
} else {
if(LOG) printf("identifier: %s\n", str);
Token * token = createToken(start, cur-1, 0, 0, IDENTIFIER);
appendToken(tklst, token);
}
return cur;
}
/*
TODO I Need to account for zero at left. 0005 should not be a valid number.
0 is valid, 30 is valid, 0.3 is valid, 03 is not
*/
static char * real_number(char * cur, char * start) {
switch(*cur) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
return real_number(cur+1, start);
default:
if(LOG) printf("float: %s\n", get_string(start, cur-1));
Token * token = createToken(start, cur-1, 0, 0, LITERAL_NUMBER);
appendToken(tklst, token);
return cur;
}
}
/*
* Parses the double-quote strings. i.e: "abcdefg"
*/
static char * string_double_quote(char * cur, char * start) {
switch(*cur){
case '\"':
if(*(cur-1) != '\\') {
if(LOG) printf("string: %s\n", get_string(start, cur));
Token * token = createToken(start, cur, 0, 0, LITERAL_STRING);
appendToken(tklst, token);
return ++cur;
}
return string_double_quote(cur+1, start);
default:
//printf(" %c, %x", *cur, (unsigned int) cur);
return string_double_quote(cur+1, start);
}
}
/*
* Parses the single-quote strings. i.e: 'abcdefg'
*/
static char * string_single_quote(char * cur, char * start) {
switch(*cur){
case '\'':
if(*(cur-1) != '\\') {
if(LOG) printf("string: %s\n", get_string(start, cur));
Token * token = createToken(start, cur, 0, 0, LITERAL_STRING);
appendToken(tklst, token);
return ++cur;
}
return string_single_quote(cur+1, start);
default:
return string_single_quote(cur+1, start);
}
}
/*
* Parses single-line comments. i.e: //some comment till end of line
*/
static char * single_line_comment(char * cur, char * start) {
switch(*cur){
case '\n': case '\r':
if(LOG) printf("SLC: %s\n", get_string(start, cur-1));
Token * token = createToken(start, cur-1, 0, 0, SINGLE_LINE_COMMENT);
appendToken(tklst, token);
return cur;
default:
//printf("in comm: %c (%d) - %x\n", *cur, *cur, (unsigned int) cur);
return single_line_comment(cur+1, start);
}
}
static char * multi_line_comment(char * cur, char * start) {
if(LOG) printf("%c, %d - %x: detecto comment ml!\n", *cur, *cur, (unsigned int)*cur);
switch(*cur){
case '\n': case '\r':
multiline_switch = ML_COMMENT;
return cur;
case '/':
//posible segfault when first element of the line buffer
if(*(cur-1) == '*'){
if(LOG) printf("MLC: %s\n", get_string(start, cur));
Token * token = createToken(start, cur, 0, 0, MULTI_LINE_COMMENT);
appendToken(tklst, token);
//turn off ml switch
if(multiline_switch) multiline_switch = ML_OFF;
return cur+1;
}
//do not break case as it should enter default
default:
return multi_line_comment(cur+1, start);
}
}
static char * error(char * cur) {
printf("Error found near %c. Stopping parser\n", *cur);
return cur;
}
static int is_valid_symbol(char c){
if(c == '\0') return 0;
return is_char_in_array(c, reserved_symbols);
}