-
Notifications
You must be signed in to change notification settings - Fork 0
/
bcLexer.re
397 lines (329 loc) · 8.45 KB
/
bcLexer.re
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
/*!re2c re2c:flags:i = 0; */
/*!max:re2c*/
/*!re2c
id = [a-zA-Z_][a-zA-Z_0-9]*;
string = ["][^"]*["];
digit = [0-9];
integer = digit+;
spaces = [\t ]+;
add = '+';
sub = '-';
mul = '*';
div = '/';
mod = '%';
eq = '=';
neq = '!=';
gr = '>';
ls = '<';
gre = '>=';
lse = '<=';
lnd = '&&';
lor = '||';
bnd = '&';
bor = '|';
xor = '^';
bls = '<<';
brs = '>>';
openbr = '(';
closebr = ')';
lnot = '!';
bnot = '~';
frac = [0-9]* "." [0-9]+ | [0-9]+ ".";
exp = 'e' [+-]? [0-9]+;
number = (frac exp? | [0-9]+ exp);
int = 'int';
num = 'num';
str = 'str';
set = '<-';
*/
#include <bcParser.h>
#include <bcPrivate.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
const char* AfterSpaces(const char* head)
{
assert(head != NULL);
while((*head == ' ') || (*head == '\t'))
{
++head;
}
return head;
}
int bcGetToken(const char* head, const char** tail, BC_VALUE* pData, bcParseContext_t* parseContext)
{
// head - first character of new token
// YYCURSOR - last character of new token
const uint8_t* YYMARKER; // inner lexer variable is used when there can be longer string to match
const uint8_t* YYCURSOR = (const uint8_t*) head; // initialize cursor to first character position
if (parseContext->newline != 0)
{
const char* afterSpaces = AfterSpaces(head);
int newIndent = (int)(afterSpaces - head);
if (newIndent > *parseContext->indentTop)
{
++parseContext->indentTop;
*parseContext->indentTop = newIndent;
*pData = NULL;
return TOK_INDENT;
}
if (newIndent < *parseContext->indentTop)
{
--parseContext->indentTop;
if (newIndent > *parseContext->indentTop)
{ // tab error!
*pData = NULL;
return 0;
}
*pData = NULL;
return TOK_DEDENT;
}
parseContext->newline = 0;
head = afterSpaces;
}
GET_NEXT_TOKEN: // jump to this label, if processed token is skipped (like spaces)
/*!re2c
re2c:define:YYCTYPE = uint8_t;
re2c:yyfill:enable = 0; // this disable interactive character read
* {
// Exit with error on any unknown symbols.
fprintf(stderr, "Unknown Symbol: '%c' (0x%02x)\n", *head, *head);
*tail = head;
return 0;
}
'\n' {
*tail = (const char*) YYCURSOR;
*pData = NULL;
parseContext->newline = 1;
return TOK_EXPR_END;
}
'\\' [ \t]* '\n'? '\x00' {
// code continues on next line
*tail = (const char*) (YYCURSOR - 1); // leave \0 at tail for safety
*pData = NULL;
parseContext->newline = 0; // do not count for indentation
return -1; // special token, tells that more data expected
}
'\\' [ \t]* '\n' {
// Skips any amount of spaces, tabs and newlines.
head = (const char*) YYCURSOR;
parseContext->newline = 0; // do not count for indentation
goto GET_NEXT_TOKEN;
}
':' [ \t]* '\n' {
*tail = (const char*) YYCURSOR;
*pData = NULL;
parseContext->newline = 1;
return TOK_BLOCK;
}
set {
*tail = (const char*) YYCURSOR;
*pData = NULL;
return TOK_SET;
}
str {
*tail = (const char*) YYCURSOR;
*pData = NULL;
return TOK_STR;
}
int {
*tail = (const char*) YYCURSOR;
*pData = NULL;
return TOK_INT;
}
num {
*tail = (const char*) YYCURSOR;
*pData = NULL;
return TOK_NUM;
}
lnot {
*tail = (const char*) YYCURSOR;
*pData = NULL;
return TOK_LNOT;
}
bnot {
*tail = (const char*) YYCURSOR;
*pData = NULL;
return TOK_BNOT;
}
openbr {
*tail = (const char*) YYCURSOR;
*pData = NULL;
return TOK_OPENBR;
}
closebr {
*tail = (const char*) YYCURSOR;
*pData = NULL;
return TOK_CLOSEBR;
}
add {
// '+'
*tail = (const char*) YYCURSOR;
*pData = NULL;
return TOK_ADD;
}
sub {
// '-'
*tail = (const char*) YYCURSOR;
*pData = NULL;
return TOK_SUB;
}
mul {
// '*'
*tail = (const char*) YYCURSOR;
*pData = NULL;
return TOK_MUL;
}
div {
// '/'
*tail = (const char*) YYCURSOR;
*pData = NULL;
return TOK_DIV;
}
mod {
*tail = (const char*) YYCURSOR;
*pData = NULL;
return TOK_MOD;
}
eq {
*tail = (const char*) YYCURSOR;
*pData = NULL;
return TOK_EQ;
}
neq {
*tail = (const char*) YYCURSOR;
*pData = NULL;
return TOK_NEQ;
}
gr {
*tail = (const char*) YYCURSOR;
*pData = NULL;
return TOK_GR;
}
ls {
*tail = (const char*) YYCURSOR;
*pData = NULL;
return TOK_LS;
}
gre {
*tail = (const char*) YYCURSOR;
*pData = NULL;
return TOK_GRE;
}
lse {
*tail = (const char*) YYCURSOR;
*pData = NULL;
return TOK_LSE;
}
lnd {
*tail = (const char*) YYCURSOR;
*pData = NULL;
return TOK_LND;
}
lor {
*tail = (const char*) YYCURSOR;
*pData = NULL;
return TOK_LOR;
}
bnd {
*tail = (const char*) YYCURSOR;
*pData = NULL;
return TOK_BND;
}
bor {
*tail = (const char*) YYCURSOR;
*pData = NULL;
return TOK_BOR;
}
xor {
*tail = (const char*) YYCURSOR;
*pData = NULL;
return TOK_XOR;
}
bls {
*tail = (const char*) YYCURSOR;
*pData = NULL;
return TOK_BLS;
}
brs {
*tail = (const char*) YYCURSOR;
*pData = NULL;
return TOK_BRS;
}
spaces {
// Skips any amount of spaces, tabs and newlines.
head = (const char*) YYCURSOR;
goto GET_NEXT_TOKEN;
}
'if' {
*tail = (const char*) YYCURSOR;
*pData = NULL;
return TOK_IF;
}
integer {
// Simple C integer.
char* tmpInteger = (char*) malloc((size_t)((YYCURSOR - (const uint8_t*) head) + 1));
if (tmpInteger == NULL)
{ // no memory error, we need better error checking!
assert(0);
*tail = (const char*) YYCURSOR;
return TOK_EXPR_END;
}
memcpy(tmpInteger, head, (size_t)(YYCURSOR - (const uint8_t*) head)); // copy token symbols to temp buffer
tmpInteger[YYCURSOR - (const uint8_t*) head] = 0;
*pData = bcValueInteger(strtoll(tmpInteger, NULL, 10));
// currently there are no checks for strtoll produced a valid integer.
free(tmpInteger);
*tail = (const char*) YYCURSOR;
return TOK_CONSTANT;
}
number {
// Simple C float
char* tmpNumber = (char*) malloc((size_t)((YYCURSOR - (const uint8_t*) head) + 1));
if (tmpNumber == NULL)
{ // no memory error, we need better error checking!
assert(0);
*tail = (const char*) YYCURSOR;
return TOK_EXPR_END;
}
memcpy(tmpNumber, head, (size_t)(YYCURSOR - (const uint8_t*) head)); // copy token symbols to temp buffer
tmpNumber[YYCURSOR - (const uint8_t*) head] = 0;
*pData = bcValueNumber(strtod(tmpNumber, NULL));
// currently there are no checks for strtod produced a valid number
free(tmpNumber);
*tail = (const char*) YYCURSOR;
return TOK_CONSTANT;
}
string {
char* tmpString = (char*) malloc((size_t)((YYCURSOR - (const uint8_t*)(head+1))));
if (tmpString == NULL)
{ // no memory error, we need better error checking!
assert(0);
*tail = (const char*) YYCURSOR;
return TOK_EXPR_END;
}
memcpy(tmpString, head+1, (size_t)(YYCURSOR - (const uint8_t*)(head+1) - 1)); // copy token symbols to temp buffer
tmpString[YYCURSOR - (const uint8_t*)(head+1)-1] = 0;
*pData = bcValueString(tmpString);
free(tmpString);
*tail = (const char*) YYCURSOR;
return TOK_CONSTANT;
}
id {
char* tmpString = (char*) malloc((size_t)((YYCURSOR - (const uint8_t*)head) + 1));
if (tmpString == NULL)
{ // no memory error, we need better error checking!
assert(0);
*tail = (const char*) YYCURSOR;
return TOK_EXPR_END;
}
memcpy(tmpString, head, (size_t)(YYCURSOR - (const uint8_t*)head)); // copy token symbols to temp buffer
tmpString[YYCURSOR - (const uint8_t*)head] = 0;
*pData = bcValueString(tmpString);
free(tmpString);
*tail = (const char*) YYCURSOR;
return TOK_ID;
}
*/
}