This repository has been archived by the owner on Jun 27, 2018. It is now read-only.
/
parser.y
579 lines (512 loc) · 12.8 KB
/
parser.y
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
%{
/**
File: parser.y
Project: DCPU-16 Tools
Component: LibDCPU-pp
Authors: James Rhodes
Description: Defines parser for the preprocessor.
**/
#define YYDEBUG 1
#define YYERROR_VERBOSE
#include <lexfix.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <bstrlib.h>
#include <bstraux.h>
#include <simclist.h>
#include <osutil.h>
#include "ppfind.h"
#include "dcpu.h"
list_t equates;
list_t scopes;
list_t macros;
void pp_discard_buffer();
void yyerror(void* scanner, const char *str);
void handle_uline(int line, bstring file, void* current);
void handle_line(int line, bstring file, void* current);
void handle_start(freed_bstring name, FILE* current);
void handle_output(bstring output, void* scanner);
int handle_include(bstring fname, void* current);
bool is_globally_initialized = false;
struct equate_entry
{
bstring name;
bstring replace;
};
struct scope_entry
{
bool active;
};
struct macrodef_entry
{
bstring name;
list_t args;
bstring content;
};
struct macroarg_entry
{
bstring string;
int number;
};
size_t equate_entry_meter(const void* el)
{
return sizeof(struct equate_entry);
}
int equate_entry_seeker(const void* el, const void* key)
{
if (biseq((bstring)key, ((struct equate_entry*)el)->name))
return 1;
return 0;
}
size_t macro_entry_meter(const void* el)
{
return sizeof(struct macrodef_entry);
}
int macro_entry_seeker(const void* el, const void* key)
{
if (biseq((bstring)key, ((struct macrodef_entry*)el)->name))
return 1;
return 0;
}
struct scope_entry* active_scope = NULL;
struct macrodef_entry* active_macro = NULL;
%}
// Defines a union for storing all of our data types.
%union
{
int number;
bstring string;
char any;
int* token;
struct macrodef_entry* macro;
struct macroarg_entry* arg;
list_t list;
}
// Define our lexical token names.
%token <token> LINE ULINE INCLUDE EQUATE IF IFDEF IFNDEF ELSE ENDIF MACRO
%token <token> ENDMACRO PARAM_OPEN PARAM_CLOSE COMMA UNDEF MACROCALL
%token <string> STRING WORD
%token <any> WHITESPACE NEWLINE TEXT
%token <number> NUMBER
// Define our types.
%type <token> lines line preprocessor
%type <string> text
%type <macro> macrodef
%type <arg> callarg
%type <list> macrodefargs macrocallargs
// Start at the root node.
%start root
// Initialize everything.
%initial-action
{
// We don't want to reinitialize our equates
// list when entering new files (via includes).
if (!is_globally_initialized)
{
list_init(&equates);
list_init(&scopes);
list_init(¯os);
list_attributes_copy(&equates, equate_entry_meter, 1);
list_attributes_copy(¯os, macro_entry_meter, 1);
list_attributes_seeker(&equates, equate_entry_seeker);
list_attributes_seeker(¯os, macro_entry_seeker);
active_scope = malloc(sizeof(struct scope_entry));
active_scope->active = true;
is_globally_initialized = true;
}
};
// We need a reentrant parser to do #include.
%pure-parser
%name-prefix="pp_yy"
%lex-param {void* scanner}
%parse-param {void* scanner}
%%
root:
lines;
lines:
line |
lines line ;
line:
NEWLINE |
preprocessor NEWLINE |
text NEWLINE
{
unsigned int i;
struct equate_entry* e;
// Skip replacement if we are inside a macro.
if (active_macro == NULL)
{
// Do substring replacements for equated directives.
for (i = 0; i < list_size(&equates); i++)
{
e = (struct equate_entry*)list_get_at(&equates, i);
bfindreplace($1, e->name, e->replace, 0);
}
}
handle_output($1, scanner);
handle_output(bfromcstr("\n"), scanner);
bdestroy($1);
} ;
preprocessor:
INCLUDE STRING
{
/* We must recursively invoke the preprocessor on the new
input, sending to standard output. */
if (!handle_include($2, scanner))
{
YYERROR;
}
} |
LINE NUMBER STRING
{
handle_line($2, $3, scanner);
} |
LINE NUMBER WORD
{
handle_line($2, $3, scanner);
} |
ULINE NUMBER STRING
{
handle_uline($2, $3, scanner);
} |
ULINE NUMBER WORD
{
handle_uline($2, $3, scanner);
} |
EQUATE WORD text
{
struct equate_entry* entry = malloc(sizeof(struct equate_entry));
entry->name = $2;
entry->replace = $3;
list_append(&equates, entry);
} |
macrodef
{
if ($1 != NULL)
{
// Set our active macro definition.
active_macro = $1;
}
else
{
// TODO: Throw an error here because we're trying to define
// a macro inside another macro, which is not permitted.
// (note that if macrodef handles this, then the if clause
// is no longer required at all).
}
} |
ENDMACRO
{
// We have finished defining the macro. Put it into our
// list of macros that are defined and clear the active_macro
// state.
list_append(¯os, active_macro);
active_macro = NULL;
} |
MACROCALL WORD PARAM_OPEN macrocallargs PARAM_CLOSE
{
unsigned int i, a;
struct macrodef_entry* m;
struct macroarg_entry* arg;
bstring temp;
bstring argname;
FILE* tfile;
// Evaluate the macro that need evaluating.
for (i = 0; i < list_size(¯os); i++)
{
m = (struct macrodef_entry*)list_get_at(¯os, i);
if (biseq(m->name, $2))
{
// Check to make sure the arguments passed and
// arguments required are the same size.
if (list_size(&$4) != list_size(&m->args))
{
// Throw an error here about the parameters
// not being the required amount.
}
else
{
// We need to write out a temporary file; Yuck!
// We output all of the parameters as definitions,
// followed by the text, then .UNDEFs to remove them
// from scope.
temp = bfromcstr(tempnam(".", "pp.macro."));
tfile = fopen((const char*)(temp->data), "w");
if (tfile == NULL)
{
// TODO: Throw an error here because we were unable
// to output the macro to a file.
}
else
{
// Write out each of the parameters.
for (a = 0; a < list_size(&m->args); a++)
{
argname = (bstring)list_get_at(&m->args, a);
arg = (struct macroarg_entry*)list_get_at(&$4, a);
if (arg->string != NULL)
fprintf(tfile, ".EQUATE %s \"%s\"\n", argname->data, arg->string->data);
else
fprintf(tfile, ".EQUATE %s %i\n", argname->data, arg->number);
}
// Write out the content.
fprintf(tfile, "%s\n", m->content->data);
// Write out each of the undefine commands.
for (a = 0; a < list_size(&m->args); a++)
{
argname = (bstring)list_get_at(&m->args, a);
arg = (struct macroarg_entry*)list_get_at(&$4, a);
if (arg->string != NULL)
fprintf(tfile, ".UNDEF %s\n", argname->data, arg->string->data);
else
fprintf(tfile, ".UNDEF %s\n", argname->data, arg->number);
}
// Write out another newline and then close the file.
fprintf(tfile, "\n");
fflush(tfile);
fclose(tfile);
// Now we invoke the file as if it was an include.
handle_include(temp, scanner);
// Now remove the file and cleanup.
// TODO: Free any used memory here.
remove((const char*)(temp->data));
}
}
}
}
// TODO: Probably should error if we get to here, since this means
// no macro was evaluated.
} |
IFDEF WORD
{
unsigned int i;
struct equate_entry* e;
// First push our existing scope onto the list and create
// a new scope.
list_append(&scopes, active_scope);
active_scope = malloc(sizeof(struct scope_entry));
active_scope->active = false;
// Check to see whether the specified equate exists.
for (i = 0; i < list_size(&equates); i++)
{
e = (struct equate_entry*)list_get_at(&equates, i);
if (biseq(e->name, $2))
{
// Equate found.
active_scope->active = true;
break;
}
}
} |
IFNDEF WORD
{
unsigned int i;
struct equate_entry* e;
// First push our existing scope onto the list and create
// a new scope.
list_append(&scopes, active_scope);
active_scope = malloc(sizeof(struct scope_entry));
active_scope->active = true;
// Check to see whether the specified equate exists.
for (i = 0; i < list_size(&equates); i++)
{
e = (struct equate_entry*)list_get_at(&equates, i);
if (biseq(e->name, $2))
{
// Equate found.
active_scope->active = false;
break;
}
}
} |
UNDEF WORD
{
// TODO: Implement this!
} |
ELSE
{
// Switch the current silencing mode.
active_scope->active = !active_scope->active;
} |
ENDIF
{
struct scope_entry* result;
// Check to make sure we have an outer-scope.
if (list_size(&scopes) > 0)
{
// Exit this scope.
free(active_scope);
result = list_extract_at(&scopes, list_size(&scopes) - 1);
active_scope = result;
}
else
{
// TODO: Throw an error here because we encountered
// one more .ENDIF than we should have.
}
} ;
text:
TEXT
{
$$ = bfromcstralloc(1, " ");
$$->data[0] = $1;
} |
text TEXT
{
bstring s = bfromcstralloc(1, " ");
s->data[0] = $2;
bconcat($$, s);
bdestroy(s);
} ;
macrodef:
MACRO WORD PARAM_OPEN macrodefargs PARAM_CLOSE
{
if (active_macro != NULL)
{
// TODO: Throw an error here because we're trying to define
// a macro inside another macro, which is not permitted.
$$ = NULL;
}
else
{
// Define the macro's header.
struct macrodef_entry* entry = malloc(sizeof(struct macrodef_entry));
entry->name = $2;
entry->args = $4;
entry->content = bfromcstr("");
$$ = entry;
}
} ;
macrodefargs:
WORD
{
list_init(&$$);
list_append(&$$, $1);
} |
macrodefargs COMMA WORD
{
$$ = $1;
list_append(&$$, $3);
} ;
macrocallargs:
callarg
{
list_init(&$$);
list_append(&$$, $1);
} |
macrocallargs COMMA callarg
{
$$ = $1;
list_append(&$$, $3);
} ;
callarg:
WORD
{
$$ = malloc(sizeof(struct macroarg_entry));
$$->string = $1;
} |
STRING
{
$$ = malloc(sizeof(struct macroarg_entry));
$$->string = $1;
} |
NUMBER
{
$$ = malloc(sizeof(struct macroarg_entry));
$$->number = $1;
};
%%
#include "lexer.h"
void yyerror(void* scanner, const char *str)
{
fprintf(stderr,"error at line %i: %s\n", pp_yyget_lineno(scanner), str);
}
void handle_output(bstring output, void* scanner)
{
if (active_scope->active && active_macro == NULL)
fprintf(pp_yyget_out(scanner), "%s", output->data);
else if (active_scope->active && active_macro != NULL)
{
bconcat(active_macro->content, output);
bconchar(active_macro->content, '\n');
}
}
void handle_line(int line, bstring file, void* current)
{
fprintf(pp_yyget_out((yyscan_t)current), "# %i %s", line, file->data);
}
void handle_uline(int line, bstring file, void* current)
{
fprintf(pp_yyget_out((yyscan_t)current), "#U %i %s", line, file->data);
}
bstring pp_yyfilename = NULL;
void handle_start(freed_bstring name, FILE* output)
{
fprintf(output, "# 1 %s\n", name.ref->data);
pp_yyfilename = bstrcpy(name.ref);
bautodestroy(name);
}
int handle_include(bstring fname, void* current)
{
yyscan_t scanner;
FILE* ffnew;
bstring dir;
bstring path;
int origline;
bstring origfile;
int result;
// Search for the file using ppfind.
if (fname->data[0] != '/' && fname->data[1] != ':')
path = ppfind_locate(bautocpy(fname));
else
path = bstrcpy(fname);
if (path == NULL)
{
// TODO: We need error handling the preprocessor
// so we can pass errors back like 'include not found'.
// Maybe we can model this off the ahalt() mechanism
// in the assembler.
bstring fmt = bformat("can't include '%s'.", fname->data);
char* fmtc = bstr2cstr(fmt, '0');
yyerror(current, fmtc);
bcstrfree(fmtc);
bdestroy(fmt);
return false;
}
// Calculate the directory that the include
// file is located in and add that directory
// to the include path.
dir = osutil_dirname(path);
ppfind_add_path(bautocpy(dir));
// Check to make sure the original filename is set.
assert(pp_yyfilename != NULL);
// Store existing state (line number
// and filename).
origfile = bstrcpy(pp_yyfilename);
origline = pp_yyget_lineno(current);
// Output the new state.
fprintf(pp_yyget_out(current), "\n# 1 %s\n", path->data);
// Include the file.
ffnew = fopen((const char*)(path->data), "r");
assert(ffnew != NULL);
pp_yylex_init(&scanner);
pp_yyset_in(ffnew, scanner);
pp_yyset_out(pp_yyget_out(current), scanner);
result = pp_yyparse(scanner);
fclose(ffnew);
pp_yylex_destroy(scanner);
bdestroy(path);
// Check to see if there were errors.
if (result != 0)
return false;
// Restore the old state and output
// a line adjustment.
bdestroy(pp_yyfilename);
pp_yyfilename = origfile;
pp_yyset_lineno(origline, current);
fprintf(pp_yyget_out(current), "\n# %i %s\n", origline, origfile->data);
// Remove the include path again.
ppfind_remove_path(bautocpy(dir));
return true;
}