-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathblex.c
437 lines (401 loc) · 11.5 KB
/
blex.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
#include "blex.h"
#include "berror.h"
#include "bstring.h"
#include <limits.h>
/* ORDER RESERVED */
const char *const beanX_tokens [] = {
"and", "break", "else", "elsif",
"+", "-", "*", "/",
"&", "|", "^", "%",
":", ",", ";", "!",
"{", "}", "(", ")", "[", "]", ".",
"false", "for", "fn", "if", "self", "in",
"typeof", "var", "nil", "not", "or",
"return", "true", "do", "while",
"==", "=", ">=", ">", "<=", "<", "!=",
"<<", ">>", "<eof>",
"<number>", "<name>", "<string>"
};
#define save_and_next(ls) (save(ls, ls->current), next(ls))
static void save(LexState * ls, int c) {
Mbuffer * b = ls -> buff;
if (beanZ_bufflen(b) + 1 > beanZ_sizebuffer(b)) {
size_t newsize;
if (beanZ_sizebuffer(b) >= BUFFER_MAX/2) lex_error(ls, "lexical element too long", 0);
newsize = beanZ_sizebuffer(b) * 2;
beanZ_resizebuffer(ls -> B, b, newsize);
}
b -> buffer[beanZ_bufflen(b)++] = cast_char(c);
}
void beanX_init(bean_State * B) {
TString * e = beanS_newliteral(B, BEAN_ENV);
/* printf("BeanX initialize in %s.\n", getstr(e)); */
/* luaC_fix(L, obj2gco(e)); /\* never collect this name *\/ */
for (int i = 0; i < NUM_RESERVED; i++) {
TString * ts = beanS_newlstr(B, beanX_tokens[i], strlen(beanX_tokens[i]));
ts -> reserved = cast_byte(i + 1);
}
}
const char *txtToken (LexState *ls, int token) {
switch (token) {
case TK_NAME: case TK_STRING:
case TK_NUM:
save(ls, '\0');
return beanO_pushfstring(ls->B, "'%s'", beanZ_buffer(ls->buff));
default:
return beanX_token2str(ls, token);
}
}
const char *beanX_token2str (LexState *ls, int token) {
const char *s = beanX_tokens[token];
if (token < TK_EOS) /* fixed format (symbols and reserved words)? */
return beanO_pushfstring(ls->B, "'%s'", s);
else /* names, strings, and numerals */
return s;
}
void beanX_setinput (bean_State *B, char * inputStream, TString *source, int firstchar) {
LexState * ls = B->ls;
ls -> current = firstchar;
ls -> linenumber = 1;
ls -> lastline = 1;
ls -> t.type = TK_EOS;
ls -> pre.type = 0;
ls -> source = source;
ls -> fs = NULL;
ls -> inputStream = inputStream;
ls -> envn = beanS_newliteral(B, BEAN_ENV);
ls -> buff = malloc(sizeof(Mbuffer));
beanZ_initbuffer(ls -> buff);
}
/*
** creates a new string and anchors it in scanner's table so that
** it will not be collected until the end of the compilation
** (by that time it should be anchored somewhere)
*/
TString *beanX_newstring (LexState *ls, const char *str, size_t l) {
bean_State *B = ls->B;
TString *ts = beanS_newlstr(B, str, l); /* create new string */
// TODO: The VM logic
/* TValue *o; /\* entry for 'str' *\/ */
/* setsvalue2s(L, L->top++, ts); /\* temporarily anchor it in stack *\/ */
/* o = luaH_set(L, ls->h, s2v(L->top - 1)); */
/* if (isempty(o)) { /\* not in use yet? *\/ */
/* /\* boolean value does not need GC barrier; */
/* table is not a metatable, so it does not need to invalidate cache *\/ */
/* setbvalue(o, 1); /\* t[string] = true *\/ */
/* luaC_checkGC(L); */
/* } */
/* else { /\* string already present *\/ */
/* ts = keystrval(nodefromval(o)); /\* re-use value previously stored *\/ */
/* } */
/* L->top--; /\* remove string from stack *\/ */
return ts;
}
static void inclinenumber(LexState * ls) {
int old = ls -> current;
bean_assert(currIsNewline(ls));
next(ls); // skip '\n' and '\r'
if (currIsNewline(ls) && ls->current != old) next(ls); // skip '\n\r' or '\r\n'
if (++ls -> linenumber >= INT_MAX) lex_error(ls, "chunk has too many lines", 0);
}
static void read_string(LexState *ls, int del, SemInfo *seminfo) {
save_and_next(ls);
while(ls -> current != del) {
switch(ls->current) {
case EOZ:
lex_error(ls, "unfinished string for end of file", EOZ);
break; /* to avoid warnings */
case '\n':
case '\r':
lex_error(ls, "unfinished string for line break", TK_STRING);
break; /* to avoid warnings */
case '\\': {
int c; /* final character to be saved */
save_and_next(ls); /* keep '\\' for error messages */
switch(ls -> current) {
case 'a': c = '\a'; goto read_save;
case 'b': c = '\b'; goto read_save;
case 'f': c = '\f'; goto read_save;
case 'n': c = '\n'; goto read_save;
case 'r': c = '\r'; goto read_save;
case 't': c = '\t'; goto read_save;
case 'v': c = '\v'; goto read_save;
case '\n': case '\r':
inclinenumber(ls); c = '\n'; goto only_save;
case '\\': case '\"': case '\'':
c = ls->current; goto read_save;
case EOZ: goto no_save; /* will raise an error next loop */
case 'z': { /* zap following span of spaces */
beanZ_buffremove(ls->buff, 1); /* remove '\\' */
next(ls); /* skip the 'z' */
while (bisspace(ls->current)) {
if (currIsNewline(ls)) inclinenumber(ls);
else next(ls);
}
goto no_save;
}
case 'S':
case 'D':
case 'W':
case 's':
case 'd':
case 'w': { // Just for regex expression `\s`, `\w`, `\d`, `\S`, `\W`, `\D`
c = ls->current;
save_and_next(ls);
goto no_save;
}
}
read_save:
next(ls);
only_save:
beanZ_buffremove(ls -> buff, 1);
save(ls, c);
no_save: break;
}
default:
save_and_next(ls);
}
}
save_and_next(ls); /* skip delimiter */
seminfo -> ts = beanX_newstring(ls, beanZ_buffer(ls -> buff) + 1, beanZ_bufflen(ls -> buff) - 2);
}
static int check_next1(LexState * ls, int c) {
if (ls -> current == c) {
next(ls);
return true;
}
return false;
}
static int check_next2(LexState * ls, const char *set) {
bean_assert(set[2] == '\0');
if (ls->current == set[0] || ls->current == set[1]) {
save_and_next(ls);
return true;
}
else return false;
}
static const char *b_str2int (const char *s, bean_Number *result) {
const char * p = s;
int base = 10;
char * ptr;
errno = 0;
while (bisspace(cast_uchar(*p))) p++; /* skip initial spaces */
if (*p == '-' || * p == '+') p++;
if (*p == '0' && (*(p + 1) == 'x' || *(p + 1) == 'X')) base = 16;
*result = strtol(s, &ptr, base);
if (errno) return NULL; // parse error
if (*ptr == '.') return NULL; // with pointer
return ptr;
}
static const char *b_str2d (const char *s, bean_Number *result) {
char * ptr;
errno = 0;
*result = strtod(s, &ptr);
if (errno) return NULL; // parse error
return ptr;
}
int beanO_str2num(bean_State * B, const char * s, TValue *o) {
bean_Number n;
const char *e;
if ((e = b_str2int(s, &n)) != NULL) { /* try as an integer */
if (e[0] == 'e' || e[0] == 'E') {
if ((e = b_str2d(s, &n)) != NULL) {
setnvalue(o, n);
} else {
return 0;
}
} else {
setnvalue(o, n);
}
} else if ((e = b_str2d(s, &n)) != NULL) {
setnvalue(o, n);
} else {
return 0;
}
return e - s;
}
static int read_numeral(LexState * ls, SemInfo * seminfo) {
TValue obj;
const char *expo = "Ee";
int first = ls -> current;
bean_assert(bisdigit(ls->current));
save_and_next(ls);
if (first == '0' && check_next2(ls, "xX")) /* hexadecimal? */
expo = "Pp";
while (true) {
if (check_next2(ls, expo)) check_next2(ls, "+-");
else if (bisxdigit(ls->current) || ls->current == '.') /* '%x|%.' */
save_and_next(ls);
else break;
}
if (bislalpha(ls->current)) /* is numeral touching a letter? */
save_and_next(ls); /* force an error */
save(ls, '\0');
if (beanO_str2num(ls->B, beanZ_buffer(ls -> buff), &obj) == 0) lex_error(ls, "malformed number", TK_NUM);
seminfo->n = nvalue(&obj);
return TK_NUM;
}
static int llex(LexState * ls, SemInfo * seminfo) {
beanZ_resetbuffer(ls -> buff);
while (true) {
switch(ls -> current) {
case '\n':
case '\r': {
ls->lastline = ls->linenumber;
do {
inclinenumber(ls);
} while(ls->current == '\n' || ls->current == '\r');
break;
}
case ' ': case '\f': case '\t': case '\v': {
next(ls);
break;
}
case '(': {
next(ls);
return TK_LEFT_PAREN;
}
case '&': {
next(ls);
return TK_LOGIC_AND;
}
case '|': {
next(ls);
return TK_LOGIC_OR;
}
case '^': {
next(ls);
return TK_LOGIC_XOR;
}
case '%': {
next(ls);
return TK_MOD;
}
case ')': {
next(ls);
return TK_RIGHT_PAREN;
}
case '[': {
next(ls);
return TK_LEFT_BRACKET;
}
case ']': {
next(ls);
return TK_RIGHT_BRACKET;
}
case '{': {
next(ls);
return TK_LEFT_BRACE;
}
case '}': {
next(ls);
return TK_RIGHT_BRACE;
}
case '+': {
next(ls);
return TK_ADD;
}
case ':': {
next(ls);
return TK_COLON;
}
case ',': {
next(ls);
return TK_COMMA;
}
case ';': {
next(ls);
return TK_SEMI;
}
case '.': {
next(ls);
return TK_DOT;
}
case '-': {
next(ls);
return TK_SUB;
}
case '*': {
next(ls);
return TK_MUL;
}
case '=': {
next(ls);
if (check_next1(ls, '=')) return TK_EQ;
return TK_ASSIGN;
}
case '>': {
next(ls);
if (check_next1(ls, '=')) return TK_GE;
else if (check_next1(ls, '>')) return TK_SHR;
return TK_GT;
}
case '<': {
next(ls);
if (check_next1(ls, '=')) return TK_LE;
else if (check_next1(ls, '<')) return TK_SHL;
return TK_LT;
}
case '/': {
next(ls);
if (check_next1(ls, '/')) {
// short comment
while (!currIsNewline(ls) && ls->current != EOZ) next(ls);
} else if (check_next1(ls, '*')){
// long comment
while(ls -> current != EOZ) {
int pre = ls -> current;
next(ls);
if (pre == '\n') inclinenumber(ls);
if (pre == '*' && check_next1(ls, '/')) break;
}
} else {
return TK_DIV;
}
break;
}
case '"':
case '\'': {
read_string(ls, ls -> current, seminfo);
return TK_STRING;
}
case '!': {
next(ls);
if (check_next1(ls, '=')) return TK_NE;
else return TK_BANG;
}
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9': {
return read_numeral(ls, seminfo);
}
case EOZ: {
return TK_EOS;
}
default: {
if (bislalpha(ls -> current)) {
TString * ts;
do {
save_and_next(ls);
} while(bislalnum(ls -> current) || ls -> current == '?'); // The name of variable or function can include '?'
ts = beanX_newstring(ls,
beanZ_buffer(ls -> buff),
beanZ_bufflen(ls -> buff));
seminfo->ts = ts;
if (isreserved(ts)) {
return ts -> reserved - 1;
} else {
return TK_NAME;
}
} else {
int c = ls->current;
next(ls);
return c;
}
}
}
}
}
void beanX_next (LexState *ls) {
ls -> pre = ls -> t;
ls -> t.type = llex(ls, &ls -> t.seminfo);
}