-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtokenizer.lex
358 lines (256 loc) · 6.9 KB
/
tokenizer.lex
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
%{
#define YYSTYPE void *
#include "ast.h"
#include "parser.tab.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void yyerror(YYLTYPE *, stmt_node **, char *);
int yywrap(void);
char *str, *curptr;
int cursz, chars;
#define SZMUL 128
int writing_html = 0;
void str_init(void) {
str = malloc(SZMUL);
curptr = str;
cursz = SZMUL;
chars = 0;
}
void str_putc(char c) {
*curptr++ = c;
chars++;
if(chars >= cursz) {
str = realloc(str, cursz + SZMUL);
curptr = str + chars;
cursz += SZMUL;
}
}
/* http://stackoverflow.com/questions/656703/how-does-flex-support-bison-location-exactly */
/* Many thanks to hugomg and David Elson! */
static void update_loc(YYLTYPE *yylloc, char *yytext){
int curr_line;
int curr_col;
curr_line = yylloc->first_line = yylloc->last_line;
curr_col = yylloc->first_column = yylloc->last_column;
{char * s; for(s = yytext; *s != '\0'; s++){
if(*s == '\n'){
curr_line++;
curr_col = 1;
}else{
curr_col++;
}
}}
yylloc->last_line = curr_line;
yylloc->last_column = curr_col-1;
}
char *FONTS[] = {
"Adobe Courier",
"Adobe Helvetica",
"Adobe New Century Schoolbook",
"Adobe Times",
"Andale Mono",
"Arial",
"Arial Black",
"C059",
"Cantarell",
"Century Schoolbook L",
"Comic Sans MS",
"Courier New",
"cursor.pcf",
"D050000L",
"DejaVu Math TeX Gyre",
"DejaVu Sans",
"DejaVu Sans,DejaVu Sans Condensed",
"DejaVu Sans,DejaVu Sans Light",
"DejaVu Sans Mono",
"DejaVu Serif",
"DejaVu Serif,DejaVu Serif Condensed",
"Denemo",
"Dingbats",
"Emmentaler",
"feta26",
"Georgia",
"GNU Unifont",
"GNU Unifont CSUR",
"GNU Unifont Sample",
"Impact",
"Misc Fixed",
"Misc Fixed Wide",
"Nimbus Mono L",
"Nimbus Mono PS",
"Nimbus Roman",
"Nimbus Roman No9 L",
"NimbusSans",
"Nimbus Sans",
"Nimbus Sans L",
"Nimbus Sans Narrow",
"P052",
"Standard Symbols L",
"Standard Symbols PS",
"Times New Roman",
"Trebuchet MS",
"Unifont",
"Unifont CSUR",
"Unifont Sample",
"Unifont Upper",
"URW Bookman",
"URW Bookman L",
"URW Chancery L",
"URW Gothic",
"URW Gothic L",
"URW Palladio L",
"Verdana",
"Webdings",
"Z003",
};
static void write_html(char *yytext) {
if(writing_html) {
printf("<span style=\"font-family: %s;%s%s%s%s\">%s</span>",
FONTS[rand() % (sizeof(FONTS) / sizeof(*FONTS))],
rand() & 1 ? "font-weight: bold;" : "",
rand() & 1 ? "font-style: italic;" : "",
rand() & 1 ? "text-decoration: underline;" : "",
rand() & 1 ? "font-variant: small-caps;" : "",
yytext
);
}
}
#define YY_USER_ACTION update_loc(yylloc, yytext); write_html(yytext);
%}
DIGIT [0-9]
HEXDIGIT [0-9a-fA-F]
ALPHA [a-zA-Z]
IDENT [a-zA-Z_][a-zA-Z0-9_]*
/* This is the right way to do it, but it keeps generating token $undefined.
%x STRING
\" { str_init(); BEGIN STRING; }
<STRING>\\n { str_putc('\n'); }
<STRING>\\t { str_putc('\t'); }
<STRING>\\b { str_putc('\b'); }
<STRING>\\r { str_putc('\r'); }
<STRING>\\x{HEXDIGIT}{HEXDIGIT} { str_putc(strtol(yytext+2, NULL, 16)); }
<STRING>\\\" { str_putc('"'); }
<STRING>\" { str_putc('\0'); yylval = str; BEGIN 0; return STRING; }
<STRING>. { str_putc(*yytext); }
*/
%option bison-bridge bison-locations
%%
{DIGIT}+"."{DIGIT}* { *yylval = malloc(sizeof(double)); *((double *) *yylval) = atof(yytext); return FLOAT; }
{DIGIT}+ { *yylval = malloc(sizeof(long)); *((long *) *yylval) = atol(yytext); return INT; }
\"[^"]*\" |
\'[^']*\' { *yylval = malloc(sizeof(unsigned long) + (yyleng - 2) * sizeof(char)); *((unsigned long *) *yylval) = yyleng - 2; memcpy(((char *) *yylval) + sizeof(unsigned long), yytext + 1, yyleng - 2); return STRING; }
if { return IF; }
then { return THEN; }
else { return ELSE; }
elseif { return ELSEIF; }
while { return WHILE; }
for { return FOR; }
in { return IN; }
do { return DO; }
func { return FUNC; }
macro { return MACRO; }
lambda { return LAMBDA; }
return { return RETURN; }
break { return BREAK; }
continue { return CONTINUE; }
end { return END; }
None { return NONE; }
"+" { return PLUS; }
"-" { return MINUS; }
"*" { return STAR; }
"/" { return SLASH; }
"%" { return PERCENT; }
"mod" { return PERCENT; }
"**" { return DSTAR; }
"&" { return BAND; }
"|" { return BOR; }
"^" { return BXOR; }
"~" { return BNOT; }
"&&" { return LAND; }
"and" { return LAND; }
"||" { return LOR; }
"or" { return LOR; }
"!" { return LNOT; }
"not" { return LNOT; }
"true" { *yylval = malloc(sizeof(long)); *((long *) *yylval) = 1; return INT; }
"True" { *yylval = malloc(sizeof(long)); *((long *) *yylval) = 1; return INT; }
"false" { *yylval = malloc(sizeof(long)); *((long *) *yylval) = 0; return INT; }
"False" { *yylval = malloc(sizeof(long)); *((long *) *yylval) = 0; return INT; }
"=" { return ASSIGN; }
"+=" { return ASSIGNPLUS; }
"-=" { return ASSIGNMINUS; }
"*=" { return ASSIGNSTAR; }
"/=" { return ASSIGNSLASH; }
"**=" { return ASSIGNDSTAR; }
"&=" { return ASSIGNBAND; }
"|=" { return ASSIGNBOR; }
"^=" { return ASSIGNBXOR; }
"==" { return EQUAL; }
"!=" { return NEQUAL; }
"<" { return LESS; }
">" { return GREATER; }
"<=" { return LESSEQ; }
">=" { return GREATEREQ; }
">>" { return RSHIFT; }
"<<" { return LSHIFT; }
"{" { return LBRACE; }
"}" { return RBRACE; }
"[" { return LBRACKET; }
"]" { return RBRACKET; }
^[ \t]*"(" { return BLPAREN; } /* "Breaking" paren, not allowed to introduce a call_expr */
"(" { return LPAREN; }
")" { return RPAREN; }
"." { return DOT; }
":" { return COLON; }
";" { return SEMICOLON; }
"," { return COMMA; }
"#" { return POUND; }
"!!!" { return TBANG; }
{IDENT} { *yylval = (void *) strdup(yytext); return IDENT; }
--[^\n]*\n /* Skip comments */
[ \t\n]+ /* Skip whitespace */
%%
int yywrap(void) {
return 1;
}
void yyerror(YYLTYPE *locp, stmt_node **prog, char *err) {
fputs(err, stderr);
fprintf(stderr, "\n(at lines %d-%d, cols %d-%d)\n", locp->first_line, locp->last_line, locp->first_column, locp->last_column);
if(prog && *prog) {
fprintf(stderr, "(while building a stmt of type %d)\n", (*prog)->type);
}
}
stmt_node *sol_compile(const char *prgstr) {
stmt_node *program = NULL;
YY_BUFFER_STATE buf = yy_scan_string(prgstr);
yyparse(&program);
yy_delete_buffer(buf);
return program;
}
stmt_node *sol_compile_buffer(const char *prgbuf, size_t sz) {
stmt_node *program = NULL;
YY_BUFFER_STATE buf = yy_scan_bytes(prgbuf, sz);
yyparse(&program);
yy_delete_buffer(buf);
return program;
}
stmt_node *sol_compile_file(FILE *prgfile) {
stmt_node *program = NULL;
YY_BUFFER_STATE buf = yy_create_buffer(prgfile, YY_BUF_SIZE);
yy_switch_to_buffer(buf);
yyparse(&program);
yy_delete_buffer(buf);
return program;
}
void sol_write_html(FILE *prgfile) {
stmt_node *program = NULL;
YY_BUFFER_STATE buf = yy_create_buffer(prgfile, YY_BUF_SIZE);
writing_html = 1;
printf("<html><head><title>Sol Source File</title></head><body style=\"white-space: pre-wrap;\">\n");
yy_switch_to_buffer(buf);
yyparse(&program);
yy_delete_buffer(buf);
//stmt_free(program);
printf("</body></html>\n");
}