-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.c
660 lines (570 loc) · 18.7 KB
/
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
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/******************************************/
#include "splinter.h"
/******************************************/
//
// Parsebuf is mostly to facilitate keeping the track of line/column
// of parsed text. In case of parsing error a friendly message with
// a context can be returned. The overhead is negligible since parsing
// is a one-time operation.
//
struct __parsebuf_t {
char * n; // 'name' for the parsing
char * s; // pointer to parsed string
int i; // index in buffer of the char to get
int l; // line counter, 1-based
int c; // column counter, 1-based
int _c; // old column counter, 1-based
};
typedef struct __parsebuf_t parsebuf_t;
typedef parsebuf_t * parsebuf_p;
static int __parsebuf_next(parsebuf_p pb) {
int c = pb->s[pb->i++];
if (c == 10) {
pb->_c = pb->c;
pb->c = 1;
pb->l++;
} else {
pb->c++;
}
return c;
}
static int __parsebuf_peek(parsebuf_p pb) {
return pb->s[pb->i];
}
static int __parsebuf_prev(parsebuf_p pb) {
if (pb->i <= 0) return -1;
if (pb->s[--pb->i] == 10) {
pb->c = pb->_c;
pb->l--;
} else {
pb->c--;
}
return 0;
}
static int __parsebuf_snapshot(parsebuf_p cur, parsebuf_p old) {
memcpy(old, cur, sizeof(*cur));
return 0;
}
//
// Helper functions for parsers
//
#define is_symbolchar(c) (('a' <= (c) && (c) <= 'z') \
|| ('A' <= (c) && (c) <= 'Z') \
|| ('0' <= (c) && (c) <= '9') \
|| (c)=='-' || (c)=='_' \
|| (c)=='$' || (c)=='@')
#define is_hexchar(c) (('a' <= (c) && (c) <= 'f') \
|| ('A' <= (c) && (c) <= 'F') \
|| ('0' <= (c) && (c) <= '9'))
#define is_whitespace(c) ((c)==' ' \
|| (c)=='\t' \
|| (c)=='\n' \
|| (c)=='\r')
static int __consume_whitespace(parsebuf_p pb) {
int c;
do {
c = __parsebuf_next(pb);
} while(is_whitespace(c));
return __parsebuf_prev(pb);
}
static int __strings_distance(char *s1, char *stop, char *s2) {
char c1, c2;
int i, distance = -1;
if(s1 && s2) {
for(i = 0;; s1++, s2++, i++) {
c1 = *s1;
c2 = *s2;
if((!c1 || s1 == stop) && !c2) {
distance = i ? 0 : -1; // Perfect match
break;
}
if((!c1 || s1 == stop) || !c2) {
distance = i ? i : -1; // Partial match
break;
}
if('A' <= c1 && c1 <= 'Z')
c1 += 'a' - 'A';
if('A' <= c2 && c2 <= 'Z')
c2 += 'a' - 'A';
if(c1 != c2) {
distance = i ? i : -1; // Partial match
break;
}
}
}
return distance;
}
static int __parse_hex_chunk(int c) {
if ('a' <= c && c <= 'f') return 10 + (c - 'a');
if ('A' <= c && c <= 'F') return 10 + (c - 'A');
if ('0' <= c && c <= '9') return c - '0';
return -1;
}
static int __parse_hex_char(parsebuf_p pb) {
int c1, c2;
if ((c1 = __parsebuf_next(pb)) <= 0) return -1;
if ((c1 = __parse_hex_chunk(c1)) < 0) return -1;
if ((c2 = __parsebuf_next(pb)) <= 0) return -1;
if ((c2 = __parse_hex_chunk(c2)) < 0) return -1;
return (c1 << 4) | c2;
}
static int __parse_quote_char(parsebuf_p pb) {
int c;
DEBUG();
if ((c = __parsebuf_next(pb)) <= 0) return -1;
switch(c) {
case 'n': return 10;
case 'r': return 13;
case 't': return 9;
case 'b': return 8;
case 'x': return __parse_hex_char(pb);
default: return c;
}
}
/******************************************/
static int parse_literal(parsebuf_p pb, char **start, char **stop, int terminator)
{
int c;
DEBUG();
*start = pb->s + pb->i;
for(; (c = __parsebuf_next(pb)) > 0 && is_symbolchar(c) && (c != terminator););
if (c <= 0) return -1;
__parsebuf_prev(pb);
*stop = pb->s + pb->i;
if (*start >= *stop) return -1;
if (!is_whitespace(c) && c != terminator) return -1;
return 0;
}
static atom_p parse_string(parsebuf_p pb, char terminator, int print_mode)
{
int c, err;
atom_p a = NULL;
DEBUG();
if ((a = atom_alloc_string(print_mode ? operator_string : operator_value, NULL, NULL)) == NULL)
return NULL;
while((c = __parsebuf_next(pb)) > 0) {
if (c == terminator) {
DEBUG("parsed string atom [%s]", (char *)atom_data(a));
return a;
} else if (c == '\\') {
if ((err = __parse_quote_char(pb)) <= 0) break;
if (string_append((byte_p *)(&atom_data(a)), err & 0xff)) break;
} else if (c == '%') {
if ((err = __parse_hex_char(pb)) <= 0) break;
if (string_append((byte_p *)(&atom_data(a)), err & 0xff)) break;
} else {
if (string_append((byte_p *)(&atom_data(a)), c)) break;
}
}
return atom_free(a);
}
static atom_p parse_symbol(parsebuf_p pb, char terminator, int print_mode)
{
char * symbol_start;
char * symbol_stop;
int symbol_length;
uint_t symbol_address;
static char symbol_buffer[128];
DEBUG();
if (parse_literal(pb, &symbol_start, &symbol_stop, terminator))
return NULL;
if (symbol_start[0] != '@' ||
!(('a' <= symbol_start[1] && symbol_start[1] <= 'z')
|| ('A' <= symbol_start[1] && symbol_start[1] <= 'Z')
|| ('0' <= symbol_start[1] && symbol_start[1] <= '9')
|| symbol_start[1] == '_')) return NULL;
symbol_length = symbol_stop - (++symbol_start);
if (symbol_length > sizeof(symbol_buffer) - 1)
return NULL;
strncpy(symbol_buffer, symbol_start, symbol_length);
symbol_buffer[symbol_length] = '\0';
symbol_address = splinter_get_symbol(symbol_buffer);
if (!symbol_address)
return NULL;
DEBUG("parsed symbol atom [%s] -> [%lx]", symbol_buffer, symbol_address);
return atom_alloc_plain(print_mode ? operator_hex : operator_value, symbol_address);
}
static atom_p parse_variable(parsebuf_p pb, char terminator, int print_mode)
{
char * var_start;
char * var_stop;
char * var_value;
int var_length;
static char var_buffer[128];
DEBUG();
if (parse_literal(pb, &var_start, &var_stop, terminator))
return NULL;
if (var_start[0] != '$' ||
!(('a' <= var_start[1] && var_start[1] <= 'z')
|| ('A' <= var_start[1] && var_start[1] <= 'Z')
|| ('0' <= var_start[1] && var_start[1] <= '9')
|| var_start[1] == '_')) return NULL;
var_length = var_stop - (++var_start);
if (var_length > sizeof(var_buffer) - 1)
return NULL;
strncpy(var_buffer, var_start, var_length);
var_buffer[var_length] = '\0';
var_value = splinter_find_variable(var_buffer);
if (!var_value)
return NULL;
var_length = strlen(var_value);
DEBUG("parsed variable atom [%s] -> [%s]", var_buffer, var_value);
return atom_alloc_string(print_mode ? operator_string : operator_value, var_value, var_value + var_length);
}
static atom_p parse_oct(parsebuf_p pb, int print_mode)
{
uint_t n, o;
atom_p a = NULL;
int c, valid = 1;
DEBUG();
for(n = o = 0; '0' <= (c = __parsebuf_next(pb)) && c <= '7';) {
if(valid) {
n = n * 8 + (c - '0');
if(n < o)
valid = 0;
else
o = n;
}
}
__parsebuf_prev(pb);
if(valid) {
DEBUG("parsed octal number [%lu]", n);
a = atom_alloc_plain(print_mode ? operator_uint : operator_value, n);
}
return a;
}
static atom_p parse_int(parsebuf_p pb, int print_mode)
{
int_t n, o;
atom_p a = NULL;
int c, valid = 1;
DEBUG();
if((c = __parsebuf_next(pb)) != '-') return a;
for(n = o = 0; '0' <= (c = __parsebuf_next(pb)) && c <= '9';) {
if(valid) {
n = n * 10 + (c - '0');
if(n < o)
valid = 0;
else
o = n;
}
}
__parsebuf_prev(pb);
if(valid) {
DEBUG("parsed int number [%li]", -n);
a = atom_alloc_plain(print_mode ? operator_int : operator_value, (uint_t) (-n));
}
return a;
}
static atom_p parse_uint(parsebuf_p pb, int print_mode)
{
uint_t n, o;
atom_p a = NULL;
int c, valid = 1;
DEBUG();
for(n = o = 0; '0' <= (c = __parsebuf_next(pb)) && c <= '9';) {
if(valid) {
n = n * 10 + (c - '0');
if(n < o)
valid = 0;
else
o = n;
}
}
__parsebuf_prev(pb);
if(valid) {
DEBUG("parsed uint number [%lu]", n);
a = atom_alloc_plain(print_mode ? operator_uint : operator_value, n);
}
return a;
}
static atom_p parse_hex(parsebuf_p pb, int print_mode)
{
uint_t n, o, i;
atom_p a = NULL;
int c, valid = 1;
DEBUG();
if (__parsebuf_next(pb) != '0') return NULL;
if ((c = __parsebuf_next(pb)) != 'x'
&& c != 'X') return NULL;
for(n = o = 0;;) {
c = __parsebuf_next(pb);
if('0' <= c && c <= '9') {
i = c - '0';
} else if('a' <= c && c <= 'f') {
i = c - 'a' + 10;
} else if('A' <= c && c <= 'F') {
i = c - 'A' + 10;
} else {
break;
}
if(valid) {
n = (n << 4) + i;
if(n < o)
valid = 0;
else
o = n;
}
}
__parsebuf_prev(pb);
if(valid) {
DEBUG("parsed hex number [%lx]", n);
a = atom_alloc_plain(print_mode ? operator_hex : operator_value, n);
}
return a;
}
struct __operator
{
char *token;
atom_data_callback_t exec_call;
int mode;
} operators[] =
{
//
// WARNING!!!
//
// The order of the below definitions matters!
// As the parser picks the first of equally matching
// strings it can lead to unwanted results.
//
// Example:
// "char" and "call" as starting with c letter colide,
// that is, "(c 10)" means "print LF" if "char" is declared
// first. But if "call" is first it will be interpreted as
// "call function @ address 10" which is not necesarily what
// we want.
//
{ "exec", operator_exec, 0},
{ "print", operator_exec, 1},
{ "memory", operator_memory, 0},
{ "register", operator_register, 0},
{ "argument", operator_argument, 0},
{ "variable", operator_variable, 0},
{ "store", operator_store, 0},
{ "return", operator_return, 0},
{ "print-string", operator_print_string, 0},
{ "print-char", operator_print_char, 0},
{ "print-byte", operator_print_byte, 0},
{ "print-int", operator_print_int, 0},
{ "print-uint", operator_print_uint, 0},
{ "print-hex", operator_print_hex, 0},
{ "print-hex0", operator_print_hex0, 0},
{ "print-chars", operator_print_chars, 0},
{ "print-bytes", operator_print_bytes, 0},
{ "print-uints", operator_print_uints, 0},
{ "print-argv", operator_print_argv, 0},
{ "print-ipv4", operator_print_ipv4, 0},
{ "buff-flush", operator_buffer_flush, 0},
{ "buff-clear", operator_buffer_clear, 0},
{ "add", operator_add, 0},
{ "sub", operator_sub, 0},
{ "mul", operator_mul, 0},
{ "div", operator_div, 0},
{ "and", operator_and, 0},
{ "or", operator_or, 0},
{ "not", operator_not, 0},
{ "bit-shl", operator_bit_shl, 0},
{ "bit-shr", operator_bit_shr, 0},
{ "bit-and", operator_bit_and, 0},
{ "bit-or", operator_bit_or, 0},
{ "bit-xor", operator_bit_xor, 0},
{ "bit-not", operator_bit_not, 0},
{ "if", operator_if, 0},
{ "not-if", operator_not_if, 0},
{ "is-eq", operator_is_eq, 0},
{ "is-lt", operator_is_lt, 0},
{ "is-le", operator_is_le, 0},
{ "is-gt", operator_is_gt, 0},
{ "is-ge", operator_is_ge, 0},
{ "is-null", operator_is_null, 0},
{ "is-error", operator_is_err, 0},
{ "str-equal", operator_str_equal, 0},
{ "str-length", operator_str_length, 0},
{ "str-starts", operator_str_starts, 0},
{ "str-ends", operator_str_ends, 0},
{ "str-find", operator_str_find, 0},
{ "str-contains", operator_str_contains, 0},
{ "flip", operator_flip, 0},
{ "repeat", operator_repeat, 0},
{ "while", operator_while, 0},
{ "not-while", operator_not_while, 0},
{ "break", operator_break, 0},
{ "quit", operator_quit, 0},
{ "call", operator_call, 0},
// #ifdef __KERNEL__
{ "pid", operator_pid, 0},
{ "uid", operator_uid, 0},
{ "task", operator_task, 0},
{ "time", operator_time, 0},
{ "signature", operator_signature, 0},
{ "current", operator_current, 0},
{ "is-pid", operator_is_pid, 0},
// #endif
{ "timestamp", operator_timestamp, 0},
{ NULL, NULL, 0} // Terminating NULLs
};
static struct __operator * __find_closest_token(parsebuf_p pb, char terminator)
{
char * start, * stop;
struct __operator *closest_operator = NULL;
struct __operator *current_operator = operators;
int closest_distance = -1, current_distance;
parsebuf_t buf;
DEBUG();
__parsebuf_snapshot(pb, &buf);
if(parse_literal(pb, &start, &stop, terminator))
return NULL;
for(; current_operator->token; current_operator++)
{
if((current_distance = __strings_distance(start, stop, current_operator->token)) == 0) {
// A perfect match, return it
DEBUG("found perfect match for [%s]", current_operator->token);
closest_distance = current_distance;
closest_operator = current_operator;
break;
}
if(current_distance > 0) {
if(closest_distance < 0 || current_distance > closest_distance) {
closest_distance = current_distance;
closest_operator = current_operator;
}
}
}
if (closest_distance > 0 && closest_distance < 3) {
splinter_error_return(NULL, "%s ambiguous keyword @ %d:%d - %d:%d", buf.n, buf.l, buf.c, pb->l, pb->c);
}
if(closest_operator) {
DEBUG("parsed keyword [%s] distance = %d", closest_operator->token, closest_distance);
return closest_operator;
}
splinter_error_return(NULL, "%s invalid keyword @ %d:%d - %d:%d", buf.n, buf.l, buf.c, pb->l, pb->c);
return NULL;
}
static void __parse_out_char(expression_callback_t out, char c) {
if (out == NULL) return;
out(c);
}
static void __parse_out_string(expression_callback_t out, char * s, int len) {
if (out == NULL) return;
while(len-- > 0) out(*s++);
}
static void __parse_out_indentation(expression_callback_t out, int len) {
if (out == NULL) return;
while(len-- > 0) out(' ');
}
static void __parse_out_line(expression_callback_t out, char * s, int len, int indentation) {
__parse_out_indentation(out, indentation);
__parse_out_string(out, s, len);
}
static atom_p parse_list(parsebuf_p pb, expression_callback_t out, int indentation)
{
int c, i, terminator;
atom_p first, prev, curr;
struct __operator * operator;
parsebuf_t buf;
DEBUG();
// Grab the opening parenthesis, or whatever it is
__consume_whitespace(pb);
__parsebuf_snapshot(pb, &buf);
if((c = __parsebuf_next(pb)) == '(') {
terminator = ')';
} else if(c == '[') {
terminator = ']';
} else if(c == '<') {
terminator = '>';
} else if(c == '{') {
terminator = '}';
} else {
splinter_error_return(NULL, "%s invalid list opening @ %d:%d", buf.n, buf.l, buf.c);
}
__consume_whitespace(pb);
if ((operator = __find_closest_token(pb, terminator)) == NULL)
return NULL;
__parse_out_indentation(out, indentation);
__parse_out_char(out, c);
__parse_out_string(out, operator->token, (int)strlen(operator->token));
for(first = prev = curr = NULL; 1;) {
__consume_whitespace(pb);
i = pb->i;
__parse_out_char(out, '\n');
if((c = __parsebuf_next(pb)) <= 0) {
splinter_error_set("%s missing list closing @ %d:%d", buf.n, pb->l, pb->c);
return atom_free(first);
} else if(c == terminator) {
if ((curr = atom_alloc_list(operator->exec_call, first)) == NULL) {
return atom_free(first);
}
__parse_out_indentation(out, indentation);
__parse_out_char(out, terminator);
return curr;
} else if(c == '0' && (__parsebuf_peek(pb) == 'x' || __parsebuf_peek(pb) == 'X')) {
__parsebuf_prev(pb);
curr = parse_hex(pb, operator->mode);
if (curr != NULL) __parse_out_line(out, pb->s + i, pb->i - i, indentation + 2);
} else if('0' <= c && c <= '9') {
__parsebuf_prev(pb);
curr = (c == '0') ? parse_oct(pb, operator->mode) : parse_uint(pb, operator->mode);
if (curr != NULL) __parse_out_line(out, pb->s + i, pb->i - i, indentation + 2);
} else if(c == '-' && ('0' <= __parsebuf_peek(pb) && __parsebuf_peek(pb) <= '9')) {
__parsebuf_prev(pb);
curr = parse_int(pb, operator->mode);
if (curr != NULL) __parse_out_line(out, pb->s + i, pb->i - i, indentation + 2);
} else if(c == '(' || c == '[' || c == '<' || c == '{') {
__parsebuf_prev(pb);
curr = parse_list(pb, out, indentation + 2);
} else if(c == '\'') {
curr = parse_string(pb, '\'', operator->mode);
if (curr != NULL) __parse_out_line(out, pb->s + i, pb->i - i, indentation + 2);
} else if(c == '"') {
curr = parse_string(pb, '"', operator->mode);
if (curr != NULL) __parse_out_line(out, pb->s + i, pb->i - i, indentation + 2);
} else if (c == '$') {
__parsebuf_prev(pb);
curr = parse_variable(pb, terminator, operator->mode);
if (curr != NULL) __parse_out_line(out, (char *)curr->data, strlen((char *)curr->data), indentation + 2);
} else if (c == '@') {
__parsebuf_prev(pb);
curr = parse_symbol(pb, terminator, operator->mode);
if (curr != NULL) __parse_out_line(out, pb->s + i, pb->i - i, indentation + 2);
} else {
curr = NULL;
}
if(curr == NULL) {
return atom_free(first);
}
if(first == NULL) {
first = prev = curr;
} else {
prev->next = curr;
prev = curr;
}
}
}
atom_p parse_expression(char * n, char * s) {
parsebuf_t pb;
DEBUG();
pb.n = n;
pb.s = s;
pb.i = 0;
pb.l = 1;
pb.c = 1;
pb._c = -1;
return parse_list(&pb, NULL, 0);
}
int validate_expression(char * n, char * s, expression_callback_t out) {
parsebuf_t pb;
atom_p a;
DEBUG();
pb.n = n;
pb.s = s;
pb.i = 0;
pb.l = 1;
pb.c = 1;
pb._c = -1;
a = parse_list(&pb, out, 0);
if (a != NULL) {
atom_free(a);
return 0;
}
return -1;
}