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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
|
/*************************************************************************
* Copyright (C) 2010 Tavian Barnes <tavianator@gmail.com> *
* *
* This file is part of Dimension. *
* *
* Dimension is free software; you can redistribute it and/or modify it *
* under the terms of the GNU General Public License as published by the *
* Free Software Foundation; either version 3 of the License, or (at *
* your option) any later version. *
* *
* Dimension is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
#include "tokenize.h"
#include "directives.h"
#include "utility.h"
#include <libgen.h>
#include <stdbool.h>
#include <string.h>
typedef struct dmnsn_buffered_token {
int type;
dmnsn_parse_item lval;
dmnsn_parse_location lloc;
} dmnsn_buffered_token;
typedef struct dmnsn_token_buffer {
int type;
/* Indicate that the first token should be returned as-is */
#define DMNSN_T_LEX_VERBATIM DMNSN_T_EOF
dmnsn_array *buffered;
unsigned int i;
struct dmnsn_token_buffer *prev;
const char *filename;
void *ptr;
} dmnsn_token_buffer;
static dmnsn_token_buffer *
dmnsn_new_token_buffer(int type, dmnsn_token_buffer *prev, const char *filename)
{
dmnsn_token_buffer *tbuffer = malloc(sizeof(dmnsn_token_buffer));
if (!tbuffer) {
dmnsn_error(DMNSN_SEVERITY_HIGH, "Failed to allocate token buffer.");
}
tbuffer->type = type;
tbuffer->buffered = dmnsn_new_array(sizeof(dmnsn_buffered_token));
tbuffer->i = 0;
tbuffer->prev = prev;
tbuffer->filename = filename;
tbuffer->ptr = NULL;
return tbuffer;
}
static void
dmnsn_delete_token_buffer(dmnsn_token_buffer *tbuffer)
{
if (tbuffer) {
unsigned int i;
for (i = 0; i < dmnsn_array_size(tbuffer->buffered); ++i) {
dmnsn_buffered_token buffered;
dmnsn_array_get(tbuffer->buffered, i, &buffered);
free(buffered.lval.value);
}
dmnsn_delete_array(tbuffer->buffered);
free(tbuffer);
}
}
static int
dmnsn_yylex_wrapper(dmnsn_parse_item *lvalp, dmnsn_parse_location *llocp,
const char *filename, dmnsn_symbol_table *symtable,
void *yyscanner);
int dmnsn_ld_yyparse(const char *filename, void *yyscanner,
dmnsn_symbol_table *symtable);
static dmnsn_token_buffer *
dmnsn_include_buffer(int token, dmnsn_token_buffer *prev,
dmnsn_parse_item *lvalp, dmnsn_parse_location *llocp,
const char *filename, dmnsn_symbol_table *symtable,
void *yyscanner)
{
dmnsn_token_buffer *include_buffer
= dmnsn_new_token_buffer(DMNSN_T_LEX_VERBATIM, prev, filename);
/* Buffer the current token */
dmnsn_buffered_token buffered = {
.type = token,
.lval = *lvalp,
.lloc = *llocp
};
dmnsn_array_push(include_buffer->buffered, &buffered);
/* Recursive call */
buffered.type = dmnsn_yylex(&buffered.lval, &buffered.lloc,
filename, symtable, yyscanner);
if (buffered.type == DMNSN_T_EOF) {
dmnsn_diagnostic(buffered.lloc.first_filename, buffered.lloc.first_line,
buffered.lloc.first_column,
"syntax error, unexpected end-of-file");
dmnsn_delete_token_buffer(include_buffer);
return NULL;
} else if (buffered.type == DMNSN_T_LEX_ERROR) {
dmnsn_delete_token_buffer(include_buffer);
return NULL;
}
/* Buffer the next token */
dmnsn_array_push(include_buffer->buffered, &buffered);
bool is_strexp = buffered.type != DMNSN_T_STRING;
if (is_strexp && buffered.type == DMNSN_T_IDENTIFIER) {
/* Check if it's a string identifier or a macro */
dmnsn_astnode *inode = dmnsn_find_symbol(symtable, buffered.lval.value);
if (!inode || inode->type == DMNSN_AST_STRING) {
is_strexp = false;
}
}
if (is_strexp) {
/* Grab all the tokens belonging to the string expression */
int parenlevel = -1;
while (1) {
/* Recursive call - permit other directives inside the condition */
buffered.type = dmnsn_yylex(&buffered.lval, &buffered.lloc,
filename, symtable, yyscanner);
if (buffered.type == DMNSN_T_EOF) {
dmnsn_diagnostic(buffered.lloc.first_filename, buffered.lloc.first_line,
buffered.lloc.first_column,
"syntax error, unexpected end-of-file");
dmnsn_delete_token_buffer(include_buffer);
return NULL;
} else if (buffered.type == DMNSN_T_LEX_ERROR) {
dmnsn_delete_token_buffer(include_buffer);
return NULL;
}
dmnsn_array_push(include_buffer->buffered, &buffered);
if (buffered.type == DMNSN_T_LPAREN) {
if (parenlevel < 0)
parenlevel = 1;
else
++parenlevel;
} else if (buffered.type == DMNSN_T_RPAREN) {
--parenlevel;
if (parenlevel == 0) {
break;
}
}
}
}
/* Fake EOF */
buffered.type = DMNSN_T_EOF;
buffered.lval.value = NULL;
dmnsn_array_push(include_buffer->buffered, &buffered);
dmnsn_yyset_extra(include_buffer, yyscanner);
if (dmnsn_ld_yyparse(filename, yyscanner, symtable) != 0) {
dmnsn_yyset_extra(include_buffer->prev, yyscanner);
dmnsn_delete_token_buffer(include_buffer);
return NULL;
}
dmnsn_yyset_extra(include_buffer->prev, yyscanner);
dmnsn_delete_token_buffer(include_buffer);
dmnsn_token_buffer *tbuffer = dmnsn_new_token_buffer(token, prev, filename);
dmnsn_astnode *inode = dmnsn_find_symbol(symtable, "__include__");
dmnsn_assert(inode, "__include__ unset.");
dmnsn_assert(inode->type == DMNSN_AST_STRING, "__include__ has wrong type.");
const char *include = inode->ptr;
char *filename_copy = strdup(filename);
char *localdir = dirname(filename_copy);
char *local_include = malloc(strlen(localdir) + strlen(include) + 2);
strcpy(local_include, localdir);
strcat(local_include, "/");
strcat(local_include, include);
free(filename_copy);
dmnsn_undef_symbol(symtable, "__include__");
FILE *file = fopen(local_include, "r");
if (!file) {
dmnsn_diagnostic(llocp->first_filename, llocp->first_line,
llocp->first_column,
"Couldn't open include file '%s'", local_include);
free(local_include);
dmnsn_delete_token_buffer(tbuffer);
return NULL;
}
tbuffer->ptr = file;
void *buffer = dmnsn_yy_make_buffer(file, yyscanner);
if (!buffer) {
dmnsn_diagnostic(llocp->first_filename, llocp->first_line,
llocp->first_column,
"Couldn't allocate buffer for include file '%s'",
local_include);
fclose(file);
free(local_include);
dmnsn_delete_token_buffer(tbuffer);
return NULL;
}
dmnsn_yy_push_buffer(buffer, yyscanner);
/* Stuff the filename in the symbol table to persist it */
dmnsn_astnode *includes = dmnsn_find_symbol(symtable, "__includes__");
if (!includes) {
dmnsn_declare_symbol(symtable, "__includes__", dmnsn_new_ast_array());
includes = dmnsn_find_symbol(symtable, "__includes__");
}
dmnsn_assert(includes, "__includes__ unset.");
dmnsn_assert(includes->type == DMNSN_AST_ARRAY,
"__includes__ has wrong type.");
dmnsn_astnode fnode = dmnsn_new_ast_string(local_include);
free(local_include);
tbuffer->filename = fnode.ptr;
dmnsn_array_push(includes->children, &fnode);
dmnsn_push_scope(symtable);
return tbuffer;
}
static dmnsn_token_buffer *
dmnsn_declaration_buffer(int token, dmnsn_token_buffer *prev,
dmnsn_parse_item *lvalp, dmnsn_parse_location *llocp,
const char *filename, dmnsn_symbol_table *symtable,
void *yyscanner)
{
dmnsn_token_buffer *tbuffer
= dmnsn_new_token_buffer(DMNSN_T_LEX_VERBATIM, prev, filename);
/* Buffer the current token */
dmnsn_buffered_token buffered = {
.type = token,
.lval = *lvalp,
.lloc = *llocp
};
dmnsn_array_push(tbuffer->buffered, &buffered);
/* Grab all the tokens belonging to the #declare/#local, i.e. until the braces
balance or we hit a semicolon */
int bracelevel = -1;
while (1) {
/* Recursive call - permit other directives inside the declaration */
buffered.type = dmnsn_yylex(&buffered.lval, &buffered.lloc,
filename, symtable, yyscanner);
if (buffered.type == DMNSN_T_EOF) {
dmnsn_diagnostic(filename, buffered.lloc.first_line,
buffered.lloc.first_column,
"syntax error, unexpected end-of-file");
dmnsn_delete_token_buffer(tbuffer);
return NULL;
} else if (buffered.type == DMNSN_T_LEX_ERROR) {
dmnsn_delete_token_buffer(tbuffer);
return NULL;
}
dmnsn_array_push(tbuffer->buffered, &buffered);
if (buffered.type == DMNSN_T_LBRACE) {
if (bracelevel < 0)
bracelevel = 1;
else
++bracelevel;
} else if (buffered.type == DMNSN_T_RBRACE) {
--bracelevel;
if (bracelevel == 0) {
break;
}
} else if (buffered.type == DMNSN_T_SEMICOLON) {
break;
}
}
/* Fake EOF */
buffered.type = DMNSN_T_EOF;
buffered.lval.value = NULL;
dmnsn_array_push(tbuffer->buffered, &buffered);
return tbuffer;
}
static dmnsn_token_buffer *
dmnsn_undef_buffer(int token, dmnsn_token_buffer *prev,
dmnsn_parse_item *lvalp, dmnsn_parse_location *llocp,
const char *filename, dmnsn_symbol_table *symtable,
void *yyscanner)
{
dmnsn_token_buffer *tbuffer
= dmnsn_new_token_buffer(DMNSN_T_LEX_VERBATIM, prev, filename);
/* Buffer the current token */
dmnsn_buffered_token buffered = {
.type = token,
.lval = *lvalp,
.lloc = *llocp
};
dmnsn_array_push(tbuffer->buffered, &buffered);
/* Recursive call */
buffered.type = dmnsn_yylex(&buffered.lval, &buffered.lloc,
filename, symtable, yyscanner);
if (buffered.type == DMNSN_T_EOF) {
dmnsn_diagnostic(filename, buffered.lloc.first_line,
buffered.lloc.first_column,
"syntax error, unexpected end-of-file");
dmnsn_delete_token_buffer(tbuffer);
return NULL;
} else if (buffered.type == DMNSN_T_LEX_ERROR) {
dmnsn_delete_token_buffer(tbuffer);
return NULL;
}
/* Buffer the next token */
dmnsn_array_push(tbuffer->buffered, &buffered);
/* Fake EOF */
buffered.type = DMNSN_T_EOF;
buffered.lval.value = NULL;
dmnsn_array_push(tbuffer->buffered, &buffered);
return tbuffer;
}
static dmnsn_token_buffer *
dmnsn_if_buffer(int token, dmnsn_token_buffer *prev,
dmnsn_parse_item *lvalp, dmnsn_parse_location *llocp,
const char *filename, dmnsn_symbol_table *symtable,
void *yyscanner)
{
dmnsn_token_buffer *cond_buffer
= dmnsn_new_token_buffer(DMNSN_T_LEX_VERBATIM, prev, filename);
/* Buffer the current token */
dmnsn_buffered_token buffered = {
.type = token,
.lval = *lvalp,
.lloc = *llocp
};
dmnsn_array_push(cond_buffer->buffered, &buffered);
/* Grab all the tokens belonging to the #if (...) */
int parenlevel = -1;
while (1) {
/* Recursive call - permit other directives inside the condition */
buffered.type = dmnsn_yylex(&buffered.lval, &buffered.lloc,
filename, symtable, yyscanner);
if (buffered.type == DMNSN_T_EOF) {
dmnsn_diagnostic(buffered.lloc.first_filename, buffered.lloc.first_line,
buffered.lloc.first_column,
"syntax error, unexpected end-of-file");
dmnsn_delete_token_buffer(cond_buffer);
return NULL;
} else if (buffered.type == DMNSN_T_LEX_ERROR) {
dmnsn_delete_token_buffer(cond_buffer);
return NULL;
}
dmnsn_array_push(cond_buffer->buffered, &buffered);
if (buffered.type == DMNSN_T_LPAREN) {
if (parenlevel < 0)
parenlevel = 1;
else
++parenlevel;
} else if (buffered.type == DMNSN_T_RPAREN) {
--parenlevel;
if (parenlevel == 0) {
break;
}
}
}
/* Fake EOF */
buffered.type = DMNSN_T_EOF;
buffered.lval.value = NULL;
dmnsn_array_push(cond_buffer->buffered, &buffered);
dmnsn_yyset_extra(cond_buffer, yyscanner);
if (dmnsn_ld_yyparse(filename, yyscanner, symtable) != 0) {
dmnsn_yyset_extra(cond_buffer->prev, yyscanner);
dmnsn_delete_token_buffer(cond_buffer);
return NULL;
}
dmnsn_yyset_extra(cond_buffer->prev, yyscanner);
dmnsn_delete_token_buffer(cond_buffer);
dmnsn_token_buffer *tbuffer = dmnsn_new_token_buffer(token, prev, filename);
dmnsn_astnode *cnode = dmnsn_find_symbol(symtable, "__cond__");
dmnsn_assert(cnode, "__cond__ unset.");
bool cond = false;
if (cnode->type == DMNSN_AST_INTEGER) {
cond = (*(long *)cnode->ptr) ? true : false;
} else if (cnode->type == DMNSN_AST_FLOAT) {
cond = (*(double *)cnode->ptr) ? true : false;
} else {
dmnsn_assert(false, "__cond__ has wrong type.");
}
dmnsn_undef_symbol(symtable, "__cond__");
int nesting = 1, else_seen = 0;
while (1) {
/* Non-recursive call */
buffered.type = dmnsn_yylex_wrapper(&buffered.lval, &buffered.lloc,
filename, symtable, yyscanner);
if (buffered.type == DMNSN_T_EOF) {
dmnsn_diagnostic(filename, buffered.lloc.first_line,
buffered.lloc.first_column,
"syntax error, unexpected end-of-file");
dmnsn_delete_token_buffer(tbuffer);
return NULL;
}
switch (buffered.type) {
case DMNSN_T_IF:
case DMNSN_T_IFDEF:
case DMNSN_T_IFNDEF:
case DMNSN_T_MACRO:
case DMNSN_T_SWITCH:
case DMNSN_T_WHILE:
++nesting;
break;
case DMNSN_T_END:
--nesting;
break;
default:
break;
}
if (nesting == 0) {
break;
} else if (nesting == 1 && buffered.type == DMNSN_T_ELSE) {
if (else_seen
|| (tbuffer->prev && tbuffer->prev->type == DMNSN_T_WHILE))
{
dmnsn_diagnostic(filename, buffered.lloc.first_line,
buffered.lloc.first_column,
"syntax error, unexpected #else");
dmnsn_delete_token_buffer(tbuffer);
return NULL;
} else {
cond = !cond;
else_seen = 1;
continue;
}
}
if (cond) {
dmnsn_array_push(tbuffer->buffered, &buffered);
} else {
free(buffered.lval.value);
}
}
return tbuffer;
}
static dmnsn_token_buffer *
dmnsn_while_buffer(int token, dmnsn_token_buffer *prev,
dmnsn_parse_item *lvalp, dmnsn_parse_location *llocp,
const char *filename, dmnsn_symbol_table *symtable,
void *yyscanner)
{
dmnsn_token_buffer *tbuffer = dmnsn_new_token_buffer(token, prev, filename);
/* Pretend to be an if */
dmnsn_buffered_token buffered = {
.type = DMNSN_T_IF,
.lval = *lvalp,
.lloc = *llocp
};
dmnsn_array_push(tbuffer->buffered, &buffered);
/* Grab all the tokens belonging to the #while ... #end */
int nesting = 1;
while (1) {
/* Non-recursive call */
buffered.type = dmnsn_yylex_wrapper(&buffered.lval, &buffered.lloc,
filename, symtable, yyscanner);
if (buffered.type == DMNSN_T_EOF) {
dmnsn_diagnostic(filename, buffered.lloc.first_line,
buffered.lloc.first_column,
"syntax error, unexpected end-of-file");
dmnsn_delete_token_buffer(tbuffer);
return NULL;
}
dmnsn_array_push(tbuffer->buffered, &buffered);
switch (buffered.type) {
case DMNSN_T_IF:
case DMNSN_T_IFDEF:
case DMNSN_T_IFNDEF:
case DMNSN_T_MACRO:
case DMNSN_T_SWITCH:
case DMNSN_T_WHILE:
++nesting;
break;
case DMNSN_T_END:
--nesting;
break;
default:
break;
}
if (nesting == 0) {
break;
}
}
return tbuffer;
}
static dmnsn_token_buffer *
dmnsn_version_buffer(int token, dmnsn_token_buffer *prev,
dmnsn_parse_item *lvalp, dmnsn_parse_location *llocp,
const char *filename, dmnsn_symbol_table *symtable,
void *yyscanner)
{
dmnsn_token_buffer *tbuffer
= dmnsn_new_token_buffer(DMNSN_T_LEX_VERBATIM, prev, filename);
/* Buffer the current token */
dmnsn_buffered_token buffered = {
.type = token,
.lval = *lvalp,
.lloc = *llocp
};
dmnsn_array_push(tbuffer->buffered, &buffered);
while (buffered.type != DMNSN_T_SEMICOLON) {
/* Recursive call */
buffered.type = dmnsn_yylex(&buffered.lval, &buffered.lloc,
filename, symtable, yyscanner);
if (buffered.type == DMNSN_T_EOF) {
dmnsn_diagnostic(filename, buffered.lloc.first_line,
buffered.lloc.first_column,
"syntax error, unexpected end-of-file");
dmnsn_delete_token_buffer(tbuffer);
return NULL;
} else if (buffered.type == DMNSN_T_LEX_ERROR) {
dmnsn_delete_token_buffer(tbuffer);
return NULL;
}
dmnsn_array_push(tbuffer->buffered, &buffered);
}
/* Fake EOF */
buffered.type = DMNSN_T_EOF;
buffered.lval.value = NULL;
dmnsn_array_push(tbuffer->buffered, &buffered);
return tbuffer;
}
int dmnsn_yylex_impl(dmnsn_parse_item *lvalp, dmnsn_parse_location *llocp,
const char *filename, void *yyscanner);
static int
dmnsn_yylex_wrapper(dmnsn_parse_item *lvalp, dmnsn_parse_location *llocp,
const char *filename, dmnsn_symbol_table *symtable,
void *yyscanner)
{
dmnsn_token_buffer *tbuffer = dmnsn_yyget_extra(yyscanner);
if (tbuffer && tbuffer->type != DMNSN_T_INCLUDE) {
while (tbuffer && tbuffer->i >= dmnsn_array_size(tbuffer->buffered)) {
if (tbuffer->type == DMNSN_T_WHILE) {
tbuffer->i = 0;
} else {
if (dmnsn_array_size(tbuffer->buffered) == 0
&& tbuffer->prev && tbuffer->prev->type == DMNSN_T_WHILE)
{
dmnsn_yyset_extra(tbuffer->prev, yyscanner);
dmnsn_delete_token_buffer(tbuffer);
tbuffer = dmnsn_yyget_extra(yyscanner);
}
dmnsn_yyset_extra(tbuffer->prev, yyscanner);
dmnsn_delete_token_buffer(tbuffer);
tbuffer = dmnsn_yyget_extra(yyscanner);
}
}
}
int token;
if (tbuffer && tbuffer->type != DMNSN_T_INCLUDE) {
/* Return buffered tokens */
dmnsn_buffered_token buffered;
dmnsn_array_get(tbuffer->buffered, tbuffer->i, &buffered);
token = buffered.type;
if (buffered.lval.value) {
lvalp->value = strdup(buffered.lval.value);
} else {
lvalp->value = NULL;
}
*llocp = buffered.lloc;
++tbuffer->i;
} else {
const char *real_filename = tbuffer ? tbuffer->filename : filename;
token = dmnsn_yylex_impl(lvalp, llocp, real_filename, yyscanner);
if (token == DMNSN_T_EOF && tbuffer && tbuffer->type == DMNSN_T_INCLUDE) {
dmnsn_yy_pop_buffer(yyscanner);
fclose(tbuffer->ptr);
dmnsn_pop_scope(symtable);
dmnsn_yyset_extra(tbuffer->prev, yyscanner);
dmnsn_delete_token_buffer(tbuffer);
return dmnsn_yylex_wrapper(lvalp, llocp, filename, symtable, yyscanner);
}
}
return token;
}
int
dmnsn_yylex(dmnsn_parse_item *lvalp, dmnsn_parse_location *llocp,
const char *filename, dmnsn_symbol_table *symtable, void *yyscanner)
{
/*
* So... this is kind of ugly. POV-Ray's language directives are not parsable
* by any reasonable bison grammar, since some require skipping arbitrary
* amounts of tokens, or repeatedly parsing tokens. Instead, they are
* implemented transparently by the lexer. The lexing function calls a
* separate parser to handle language directives as they arrise, and returns
* only non-directive tokens.
*
* Ideally we'd use a push parser for the language directives, but bison
* doesn't support GLR push parsers. Instead, we buffer all the appropriate
* tokens and call a pull parser, then discard the buffer and continue.
*/
while (1) {
int token = dmnsn_yylex_wrapper(
lvalp, llocp, filename, symtable, yyscanner
);
dmnsn_token_buffer *tbuffer = dmnsn_yyget_extra(yyscanner);
if (tbuffer && tbuffer->type == DMNSN_T_LEX_VERBATIM && tbuffer->i == 1) {
return token;
}
switch (token) {
case DMNSN_T_INCLUDE:
{
dmnsn_token_buffer *tb = dmnsn_include_buffer(
token, tbuffer, lvalp, llocp, filename, symtable, yyscanner
);
if (!tb) {
return DMNSN_T_LEX_ERROR;
}
dmnsn_yyset_extra(tb, yyscanner);
break;
}
case DMNSN_T_DECLARE:
case DMNSN_T_LOCAL:
{
dmnsn_token_buffer *tb = dmnsn_declaration_buffer(
token, tbuffer, lvalp, llocp, filename, symtable, yyscanner
);
if (!tb) {
return DMNSN_T_LEX_ERROR;
}
dmnsn_yyset_extra(tb, yyscanner);
if (dmnsn_ld_yyparse(filename, yyscanner, symtable) != 0) {
dmnsn_yyset_extra(tb->prev, yyscanner);
dmnsn_delete_token_buffer(tb);
return DMNSN_T_LEX_ERROR;
}
/* Restore the previous extra pointer */
dmnsn_yyset_extra(tb->prev, yyscanner);
dmnsn_delete_token_buffer(tb);
break;
}
case DMNSN_T_UNDEF:
{
dmnsn_token_buffer *tb = dmnsn_undef_buffer(
token, tbuffer, lvalp, llocp, filename, symtable, yyscanner
);
if (!tb) {
return DMNSN_T_LEX_ERROR;
}
dmnsn_yyset_extra(tb, yyscanner);
if (dmnsn_ld_yyparse(filename, yyscanner, symtable) != 0) {
dmnsn_yyset_extra(tb->prev, yyscanner);
dmnsn_delete_token_buffer(tb);
return DMNSN_T_LEX_ERROR;
}
/* Restore the previous extra pointer */
dmnsn_yyset_extra(tb->prev, yyscanner);
dmnsn_delete_token_buffer(tb);
break;
}
case DMNSN_T_IF:
case DMNSN_T_IFDEF:
case DMNSN_T_IFNDEF:
{
dmnsn_token_buffer *tb = dmnsn_if_buffer(
token, tbuffer, lvalp, llocp, filename, symtable, yyscanner
);
if (!tb) {
return DMNSN_T_LEX_ERROR;
}
dmnsn_yyset_extra(tb, yyscanner);
break;
}
case DMNSN_T_WHILE:
{
dmnsn_token_buffer *tb = dmnsn_while_buffer(
token, tbuffer, lvalp, llocp, filename, symtable, yyscanner
);
if (!tb) {
return DMNSN_T_LEX_ERROR;
}
dmnsn_yyset_extra(tb, yyscanner);
break;
}
case DMNSN_T_VERSION:
{
dmnsn_token_buffer *tb = dmnsn_version_buffer(
token, tbuffer, lvalp, llocp, filename, symtable, yyscanner
);
if (!tb) {
return DMNSN_T_LEX_ERROR;
}
dmnsn_yyset_extra(tb, yyscanner);
if (dmnsn_ld_yyparse(filename, yyscanner, symtable) != 0) {
dmnsn_yyset_extra(tb->prev, yyscanner);
dmnsn_delete_token_buffer(tb);
return DMNSN_T_LEX_ERROR;
}
/* Restore the previous extra pointer */
dmnsn_yyset_extra(tb->prev, yyscanner);
dmnsn_delete_token_buffer(tb);
break;
}
default:
return token;
}
}
}
void
dmnsn_yylex_cleanup(void *yyscanner)
{
dmnsn_token_buffer *tbuffer = dmnsn_yyget_extra(yyscanner);
while (tbuffer) {
dmnsn_token_buffer *prev = tbuffer->prev;
dmnsn_delete_token_buffer(tbuffer);
tbuffer = prev;
}
dmnsn_yyset_extra(NULL, yyscanner);
}
|