-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtokenizer.c
525 lines (442 loc) · 10.7 KB
/
tokenizer.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
#include "gc.h"
/*
* Tokenization code based on wannabe-regex.c,
* which I wrote in Summer 2012
*/
/*
* TODO: cleanup some of the more mysterious
* parts of the code...
*
* Take note, there is a cleaner smarter version of this same
* code which can be found here, but it is being used for a
* C compiler:
*
* https://raw.githubusercontent.com/bl0ckeduser/new-bpf-tools/master/compiler/tokenizer.c
*
*/
#include "tokenizer.h"
#include "tokens.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *code_lines[1024];
extern void fail(char* mesg);
extern void sanity_requires(int exp);
typedef struct match_struct {
/* accept number, if any */
int token;
/* pointer to last character matched */
char* pos;
} match_t;
typedef struct trie_struct {
/* character edges */
struct trie_struct* map[256];
/* epsilon edges */
struct trie_struct** link;
int links;
int link_alloc;
/* accept state, if any */
int valid_token;
} trie;
/* ============================================ */
/* Trie routines */
void add_link(trie* from, trie* to)
{
if (++from->links > from->link_alloc)
from->link = realloc(from->link,
(from->link_alloc += 5) * sizeof(trie *));
if (!from->link)
fail("link array allocation");
from->link[from->links - 1] = to;
}
trie* new_trie()
{
trie* t = cgc_malloc(sizeof(trie));
int i;
if (!t)
fail("trie allocation");
t->link = cgc_malloc(10 * sizeof(trie*));
if (!t->link)
fail("trie links");
t->links = 0;
t->link_alloc = 10;
for (i = 0; i < 10; i++)
t->link[i] = NULL;
t->valid_token = 0;
for(i = 0; i < 256; i++)
t->map[i] = NULL;
return t;
}
/* ============================================ */
/* Pattern compiler routine */
void add_token(trie* t, char* tok, int key)
{
int i, j;
int c;
trie *hist[1024];
trie *next;
int len = strlen(tok);
char* new_tok;
int n;
new_tok = cgc_malloc(len + 32);
if (!new_tok)
fail("buffer allocation");
strcpy(new_tok, tok);
for (i = 0, n = 0; c = new_tok[i], i < len && t; ++i)
{
hist[n] = t;
switch (c) {
case 'A': /* special: set of all letters */
next = new_trie();
for(j = 'a'; j <= 'z'; j++)
t->map[j] = next;
for(j = 'A'; j <= 'Z'; j++)
t->map[j] = next;
t->map['_'] = next;
t = next;
++n;
break;
case 'B': /* special: set of all letters
+ all digits */
next = new_trie();
for(j = 'a'; j <= 'z'; j++)
t->map[j] = next;
for(j = 'A'; j <= 'Z'; j++)
t->map[j] = next;
for(j = '0'; j <= '9'; j++)
t->map[j] = next;
t->map['_'] = next;
t = next;
++n;
break;
case 'D': /* special: set of all digits */
next = new_trie();
for(j = '0'; j <= '9'; j++)
t->map[j] = next;
t = next;
++n;
break;
case 'W': /* special: whitespace */
next = new_trie();
t->map[' '] = next;
t->map['\t'] = next;
t = next;
++n;
break;
case '?': /* optional character */
sanity_requires(n > 0);
/*
* The previous node is allowed to
* go straight to this node, via
* an epsilon link.
*/
add_link(hist[n - 1], t);
break;
case '+': /* character can repeat
any number of times */
sanity_requires(n > 0);
/*
* Make a "buffer" node to keep things
* cleanly separated and avoid
* surprises when matching e.g.
* 'a+b+'. Epsilon-link to it.
*/
add_link(t, hist[++n] = new_trie());
t = hist[n];
/*
* Next, we epsilon-link the buffer node
* all the way back to the node '+' compilation
* started with, to allow repetition.
*/
add_link(t, hist[n - 2]);
/*
* And finally we make a clean
* new node for further work,
* and epsilon-link to it.
*/
add_link(t, hist[++n] = new_trie());
t = hist[n];
break;
case '*': /* optional character
with repetition allowed */
sanity_requires(n > 0);
/* same as +, except that the
* second part makes a mutual link */
add_link(t, hist[++n] = new_trie());
t = hist[n];
/* mutual link */
add_link(t, hist[n - 2]);
add_link(hist[n - 2], t);
add_link(t, hist[++n] = new_trie());
t = hist[n];
break;
case '\\': /* escape to normal text */
if (++i == len)
fail("backslash expected char");
c = new_tok[i];
default: /* normal text */
if (!t->map[c])
t->map[c] = new_trie();
t = t->map[c];
++n;
}
}
/*
* We have reached the accept-state node;
* mark it as such.
*/
t->valid_token = key;
}
/*
* NFA trie evaluator (uses backtracking)
* Has implementation-time hacks, parameters and all
* (aka bloat)
*/
match_t match(trie* automaton, char* full_text, char* where)
{
extern match_t match_algo(trie* t, char* full_tok, char* tok,
int frk, trie* led, int ledi);
return match_algo(automaton, full_text, where, 0, NULL, 0);
}
/* trie* t : matching automaton / current node
* char* full_tok : full text
* char* tok : where we are now
* int frk : internal. initially, give 0
* trie* led : internal. start with NULL
* int ledi : internal. start with 0
*/
match_t match_algo(trie* t, char* full_tok, char* tok, int frk,
trie* led, int ledi)
{
int i = 0;
int j;
char c;
/* greedy choice registers */
int record; /* longest match */
match_t m; /* match register */
match_t ml[10]; /* match list */
int mc = 0; /* match count */
match_t* choice; /* final choice */
int len = strlen(tok) + 1;
int last_inc;
i = 0; do
{
last_inc = 0;
c = tok[i];
/*
* Check if multiple epsilon edges exist
* and if we are not already in a fork
*/
if (t->links && !frk)
{
/*
* Fork for each possibility
* and collect results in an array
*/
/* character edge: frk = 1 */
if (t->map[c])
if ((m = match_algo(t, full_tok, tok + i, 1,
led, ledi)).token)
ml[mc++] = m;
/* epsilon edge(s): frk = 2 + link number */
for (j = 0; j < t->links; j++)
if ((m = match_algo(t, full_tok, tok + i,
j + 2, led, ledi)).token)
ml[mc++] = m;
/*
* If there are multiple epsilon matches,
* be "greedy", i.e. choose longest match.
*/
if (mc > 1) {
record = 0;
choice = NULL;
for (j = 0; j < mc; j++)
if (ml[j].pos - full_tok >= record) {
choice = &ml[j];
record = ml[j].pos - full_tok;
}
if (!choice) fail("greedy choice");
return *choice;
}
else if (mc == 1) /* only one match */
return *ml;
else /* no match, return blank as-is */
return m;
}
/*
* If we are not in an epsilon fork
* and the character edge exists,
* follow it.
*/
if (frk <= 1 && t->map[c]) {
t = t->map[c];
last_inc = 1;
/* break loop if in matching state */
if (t->valid_token)
break;
++i;
goto valid;
}
else if (frk > 1) {
/*
* Prevents infinite loops: if the
* link chosen in this fork brings us back
* to the last link followed and the character
* index has not increased since we followed
* this last link, do not follow it
* (magic mystery code from 2012 ?!)
*/
if (!(led && t->link[frk - 2] == led
&& (int)(&tok[i] - full_tok) == ledi)) {
/* t->links == 1 is special (???) */
if (t->links > 1) {
led = t; /* Last Epsilon departure
node */
ledi = (int)(&tok[i] - full_tok);
}
t = t->link[frk - 2];
goto valid;
}
}
break;
/*
* Clear fork path number after the first
* iteration of the loop, it is not relevant
* afterwards
*/
valid: if (frk)
frk = 0;
} while (i < len && t);
/* set up the returned structure */
if (t && t->valid_token) {
/* match */
m.token = t->valid_token;
m.pos = tok + i + last_inc;
} else {
/* no match */
m.token = 0;
}
return m;
}
trie *t[100];
int tc = 0;
token_t* tokenize(char *in_buf)
{
match_t m;
match_t c;
char buf2[1024];
int max;
int i;
int line = 1;
char *line_start;
token_t *toks = cgc_malloc(64 * sizeof(token_t));
int tok_alloc = 64;
int tok_count = 0;
char *buf = cgc_malloc(strlen(in_buf) + 1);
char *p;
strcpy(buf, in_buf);
buf[strlen(in_buf)] = 0;
*code_lines = buf;
line_start = buf;
for(p = buf; *p; ) {
max = -1;
/*
* Choose the matching pattern
* that gives the longest match.
* Shit is "meta-greedy".
*/
for (i = 0; i < tc; i++) {
m = match(t[i], buf, p);
if (m.token) { /* match has succeeded */
if (m.pos - p > max) {
c = m;
max = m.pos - p;
}
}
}
if (max == -1) {
/* matching from this offset failed */
fail("tokenization failed");
} else {
/* add token to the tokens array */
if (c.token != TOK_WHITESPACE
&& c.token != TOK_NEWLINE) {
if (++tok_count > tok_alloc) {
tok_alloc = tok_count + 64;
toks = realloc(toks,
tok_alloc * sizeof(token_t));
if (!toks)
fail("resize tokens array");
}
toks[tok_count - 1].type = c.token;
toks[tok_count - 1].start = p;
toks[tok_count - 1].len = max;
toks[tok_count - 1].from_line = line;
toks[tok_count - 1].from_char = p -
line_start + 1;
}
advance:
/* move forward in the string */
p += max;
if (max == 0)
++p;
/*
* Line accounting (useful for
* pretty parse-fail diagnostics)
*/
if (c.token == TOK_NEWLINE) {
code_lines[line] = line_start;
++line;
line_start = p;
}
}
}
/* the final token is a bit special */
toks[tok_count].start = NULL;
toks[tok_count].len = 0;
toks[tok_count].from_line = line;
toks[tok_count].from_char = p - line_start;
code_lines[line] = line_start;
return toks;
}
void setup_tokenizer()
{
int i = 0;
/*
* Set up the automata that
* match the various token types
*/
for (i = 0; i < 100; i++)
t[i] = new_trie();
/* D: any digit */
add_token(t[tc++], "D+", TOK_INTEGER);
/* W: any whitespace character */
add_token(t[tc++], "W+", TOK_WHITESPACE);
/* A: letter; B: letter or digit */
add_token(t[tc++], "AB*", TOK_IDENT);
/* operators */
add_token(t[tc++], "\\+", TOK_PLUS);
add_token(t[tc++], "-", TOK_MINUS);
add_token(t[tc++], "/", TOK_DIV);
add_token(t[tc++], "\\*", TOK_MUL);
add_token(t[tc++], "=", TOK_ASGN);
/* special characters */
add_token(t[tc++], "(", TOK_LPAREN);
add_token(t[tc++], ")", TOK_RPAREN);
add_token(t[tc++], ";", TOK_SEMICOLON);
add_token(t[tc++], ",", TOK_COMMA);
add_token(t[tc++], "\n", TOK_NEWLINE);
add_token(t[tc++], ":", TOK_COLON);
add_token(t[tc++], "[", TOK_LBRACK);
add_token(t[tc++], "]", TOK_RBRACK);
add_token(t[tc++], "'", TOK_QUOT);
add_token(t[tc++], "@", TOK_NAN);
add_token(t[tc++], "#", TOK_MNUM);
add_token(t[tc++], "\\?", TOK_MVAR);
add_token(t[tc++], "|", TOK_PIPE);
add_token(t[tc++], "\\$", TOK_DOLLAR);
add_token(t[tc++], "\\^", TOK_EXP);
add_token(t[tc++], "::", TOK_CC);
add_token(t[tc++], "|'", TOK_NOTSYMBOL);
add_token(t[tc++], "\\?\\?", TOK_PREVSUB);
}