glsl2: Actually fix glsl-version-define.
[mesa.git] / src / glsl / glcpp / glcpp-parse.y
1 %{
2 /*
3 * Copyright © 2010 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <assert.h>
28 #include <inttypes.h>
29
30 #include "glcpp.h"
31 #include "main/mtypes.h"
32
33 #define glcpp_print(stream, str) stream = talloc_strdup_append(stream, str)
34 #define glcpp_printf(stream, fmt, args...) \
35 stream = talloc_asprintf_append(stream, fmt, args)
36
37 static void
38 yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error);
39
40 static void
41 _define_object_macro (glcpp_parser_t *parser,
42 YYLTYPE *loc,
43 const char *macro,
44 token_list_t *replacements);
45
46 static void
47 _define_function_macro (glcpp_parser_t *parser,
48 YYLTYPE *loc,
49 const char *macro,
50 string_list_t *parameters,
51 token_list_t *replacements);
52
53 static string_list_t *
54 _string_list_create (void *ctx);
55
56 static void
57 _string_list_append_item (string_list_t *list, const char *str);
58
59 static int
60 _string_list_contains (string_list_t *list, const char *member, int *index);
61
62 static int
63 _string_list_length (string_list_t *list);
64
65 static argument_list_t *
66 _argument_list_create (void *ctx);
67
68 static void
69 _argument_list_append (argument_list_t *list, token_list_t *argument);
70
71 static int
72 _argument_list_length (argument_list_t *list);
73
74 static token_list_t *
75 _argument_list_member_at (argument_list_t *list, int index);
76
77 /* Note: This function talloc_steal()s the str pointer. */
78 static token_t *
79 _token_create_str (void *ctx, int type, char *str);
80
81 static token_t *
82 _token_create_ival (void *ctx, int type, int ival);
83
84 static token_list_t *
85 _token_list_create (void *ctx);
86
87 /* Note: This function adds a talloc_reference() to token.
88 *
89 * You may want to talloc_unlink any current reference if you no
90 * longer need it. */
91 static void
92 _token_list_append (token_list_t *list, token_t *token);
93
94 static void
95 _token_list_append_list (token_list_t *list, token_list_t *tail);
96
97 static active_list_t *
98 _active_list_push (active_list_t *list,
99 const char *identifier,
100 token_node_t *marker);
101
102 static active_list_t *
103 _active_list_pop (active_list_t *list);
104
105 int
106 _active_list_contains (active_list_t *list, const char *identifier);
107
108 static void
109 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
110 token_list_t *list);
111
112 static void
113 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
114 token_list_t *list);
115
116 static void
117 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
118 int condition);
119
120 static void
121 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
122 const char *type, int condition);
123
124 static void
125 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc);
126
127 #define yylex glcpp_parser_lex
128
129 static int
130 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
131
132 static void
133 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
134
135 static void
136 add_builtin_define(glcpp_parser_t *parser, const char *name, int value);
137
138 %}
139
140 %pure-parser
141 %error-verbose
142 %locations
143
144 %parse-param {glcpp_parser_t *parser}
145 %lex-param {glcpp_parser_t *parser}
146
147 %expect 0
148 %token COMMA_FINAL DEFINED ELIF_EXPANDED HASH HASH_DEFINE_FUNC HASH_DEFINE_OBJ HASH_ELIF HASH_ELSE HASH_ENDIF HASH_IF HASH_IFDEF HASH_IFNDEF HASH_UNDEF HASH_VERSION IDENTIFIER IF_EXPANDED INTEGER INTEGER_STRING NEWLINE OTHER PLACEHOLDER SPACE
149 %token PASTE
150 %type <ival> expression INTEGER operator SPACE integer_constant
151 %type <str> IDENTIFIER INTEGER_STRING OTHER
152 %type <string_list> identifier_list
153 %type <token> preprocessing_token conditional_token
154 %type <token_list> pp_tokens replacement_list text_line conditional_tokens
155 %left OR
156 %left AND
157 %left '|'
158 %left '^'
159 %left '&'
160 %left EQUAL NOT_EQUAL
161 %left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
162 %left LEFT_SHIFT RIGHT_SHIFT
163 %left '+' '-'
164 %left '*' '/' '%'
165 %right UNARY
166
167 %%
168
169 input:
170 /* empty */
171 | input line
172 ;
173
174 line:
175 control_line {
176 glcpp_print(parser->output, "\n");
177 }
178 | text_line {
179 _glcpp_parser_print_expanded_token_list (parser, $1);
180 glcpp_print(parser->output, "\n");
181 talloc_free ($1);
182 }
183 | expanded_line
184 | HASH non_directive
185 ;
186
187 expanded_line:
188 IF_EXPANDED expression NEWLINE {
189 _glcpp_parser_skip_stack_push_if (parser, & @1, $2);
190 }
191 | ELIF_EXPANDED expression NEWLINE {
192 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2);
193 }
194 ;
195
196 control_line:
197 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
198 _define_object_macro (parser, & @2, $2, $3);
199 }
200 | HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
201 _define_function_macro (parser, & @2, $2, NULL, $5);
202 }
203 | HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
204 _define_function_macro (parser, & @2, $2, $4, $6);
205 }
206 | HASH_UNDEF IDENTIFIER NEWLINE {
207 macro_t *macro = hash_table_find (parser->defines, $2);
208 if (macro) {
209 hash_table_remove (parser->defines, $2);
210 talloc_free (macro);
211 }
212 talloc_free ($2);
213 }
214 | HASH_IF conditional_tokens NEWLINE {
215 token_list_t *expanded;
216 token_t *token;
217
218 expanded = _token_list_create (parser);
219 token = _token_create_ival (parser, IF_EXPANDED, IF_EXPANDED);
220 _token_list_append (expanded, token);
221 talloc_unlink (parser, token);
222 _glcpp_parser_expand_token_list (parser, $2);
223 _token_list_append_list (expanded, $2);
224 glcpp_parser_lex_from (parser, expanded);
225 }
226 | HASH_IFDEF IDENTIFIER junk NEWLINE {
227 macro_t *macro = hash_table_find (parser->defines, $2);
228 talloc_free ($2);
229 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
230 }
231 | HASH_IFNDEF IDENTIFIER junk NEWLINE {
232 macro_t *macro = hash_table_find (parser->defines, $2);
233 talloc_free ($2);
234 _glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL);
235 }
236 | HASH_ELIF conditional_tokens NEWLINE {
237 token_list_t *expanded;
238 token_t *token;
239
240 expanded = _token_list_create (parser);
241 token = _token_create_ival (parser, ELIF_EXPANDED, ELIF_EXPANDED);
242 _token_list_append (expanded, token);
243 talloc_unlink (parser, token);
244 _glcpp_parser_expand_token_list (parser, $2);
245 _token_list_append_list (expanded, $2);
246 glcpp_parser_lex_from (parser, expanded);
247 }
248 | HASH_ELIF NEWLINE {
249 /* #elif without an expression results in a warning if the
250 * condition doesn't matter (we just handled #if 1 or such)
251 * but an error otherwise. */
252 if (parser->skip_stack != NULL && parser->skip_stack->type == SKIP_NO_SKIP) {
253 parser->skip_stack->type = SKIP_TO_ENDIF;
254 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
255 } else {
256 glcpp_error(& @1, parser, "#elif needs an expression");
257 }
258 }
259 | HASH_ELSE NEWLINE {
260 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
261 }
262 | HASH_ENDIF NEWLINE {
263 _glcpp_parser_skip_stack_pop (parser, & @1);
264 }
265 | HASH_VERSION integer_constant NEWLINE {
266 macro_t *macro = hash_table_find (parser->defines, "__VERSION__");
267 if (macro) {
268 hash_table_remove (parser->defines, "__VERSION__");
269 talloc_free (macro);
270 }
271 add_builtin_define (parser, "__VERSION__", $2);
272 glcpp_printf(parser->output, "#version %" PRIiMAX "\n", $2);
273 }
274 | HASH NEWLINE
275 ;
276
277 integer_constant:
278 INTEGER_STRING {
279 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
280 $$ = strtoll ($1 + 2, NULL, 16);
281 } else if ($1[0] == '0') {
282 $$ = strtoll ($1, NULL, 8);
283 } else {
284 $$ = strtoll ($1, NULL, 10);
285 }
286 }
287 | INTEGER {
288 $$ = $1;
289 }
290
291 expression:
292 integer_constant
293 | expression OR expression {
294 $$ = $1 || $3;
295 }
296 | expression AND expression {
297 $$ = $1 && $3;
298 }
299 | expression '|' expression {
300 $$ = $1 | $3;
301 }
302 | expression '^' expression {
303 $$ = $1 ^ $3;
304 }
305 | expression '&' expression {
306 $$ = $1 & $3;
307 }
308 | expression NOT_EQUAL expression {
309 $$ = $1 != $3;
310 }
311 | expression EQUAL expression {
312 $$ = $1 == $3;
313 }
314 | expression GREATER_OR_EQUAL expression {
315 $$ = $1 >= $3;
316 }
317 | expression LESS_OR_EQUAL expression {
318 $$ = $1 <= $3;
319 }
320 | expression '>' expression {
321 $$ = $1 > $3;
322 }
323 | expression '<' expression {
324 $$ = $1 < $3;
325 }
326 | expression RIGHT_SHIFT expression {
327 $$ = $1 >> $3;
328 }
329 | expression LEFT_SHIFT expression {
330 $$ = $1 << $3;
331 }
332 | expression '-' expression {
333 $$ = $1 - $3;
334 }
335 | expression '+' expression {
336 $$ = $1 + $3;
337 }
338 | expression '%' expression {
339 $$ = $1 % $3;
340 }
341 | expression '/' expression {
342 $$ = $1 / $3;
343 }
344 | expression '*' expression {
345 $$ = $1 * $3;
346 }
347 | '!' expression %prec UNARY {
348 $$ = ! $2;
349 }
350 | '~' expression %prec UNARY {
351 $$ = ~ $2;
352 }
353 | '-' expression %prec UNARY {
354 $$ = - $2;
355 }
356 | '+' expression %prec UNARY {
357 $$ = + $2;
358 }
359 | '(' expression ')' {
360 $$ = $2;
361 }
362 ;
363
364 identifier_list:
365 IDENTIFIER {
366 $$ = _string_list_create (parser);
367 _string_list_append_item ($$, $1);
368 talloc_steal ($$, $1);
369 }
370 | identifier_list ',' IDENTIFIER {
371 $$ = $1;
372 _string_list_append_item ($$, $3);
373 talloc_steal ($$, $3);
374 }
375 ;
376
377 text_line:
378 NEWLINE { $$ = NULL; }
379 | pp_tokens NEWLINE
380 ;
381
382 non_directive:
383 pp_tokens NEWLINE {
384 yyerror (& @1, parser, "Invalid tokens after #");
385 }
386 ;
387
388 replacement_list:
389 /* empty */ { $$ = NULL; }
390 | pp_tokens
391 ;
392
393 junk:
394 /* empty */
395 | pp_tokens {
396 glcpp_warning(&@1, parser, "extra tokens at end of directive");
397 }
398 ;
399
400 conditional_token:
401 /* Handle "defined" operator */
402 DEFINED IDENTIFIER {
403 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
404 $$ = _token_create_ival (parser, INTEGER, v);
405 }
406 | DEFINED '(' IDENTIFIER ')' {
407 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
408 $$ = _token_create_ival (parser, INTEGER, v);
409 }
410 | preprocessing_token
411 ;
412
413 conditional_tokens:
414 /* Exactly the same as pp_tokens, but using conditional_token */
415 conditional_token {
416 parser->space_tokens = 1;
417 $$ = _token_list_create (parser);
418 _token_list_append ($$, $1);
419 talloc_unlink (parser, $1);
420 }
421 | conditional_tokens conditional_token {
422 $$ = $1;
423 _token_list_append ($$, $2);
424 talloc_unlink (parser, $2);
425 }
426 ;
427
428 pp_tokens:
429 preprocessing_token {
430 parser->space_tokens = 1;
431 $$ = _token_list_create (parser);
432 _token_list_append ($$, $1);
433 talloc_unlink (parser, $1);
434 }
435 | pp_tokens preprocessing_token {
436 $$ = $1;
437 _token_list_append ($$, $2);
438 talloc_unlink (parser, $2);
439 }
440 ;
441
442 preprocessing_token:
443 IDENTIFIER {
444 $$ = _token_create_str (parser, IDENTIFIER, $1);
445 $$->location = yylloc;
446 }
447 | INTEGER_STRING {
448 $$ = _token_create_str (parser, INTEGER_STRING, $1);
449 $$->location = yylloc;
450 }
451 | operator {
452 $$ = _token_create_ival (parser, $1, $1);
453 $$->location = yylloc;
454 }
455 | OTHER {
456 $$ = _token_create_str (parser, OTHER, $1);
457 $$->location = yylloc;
458 }
459 | SPACE {
460 $$ = _token_create_ival (parser, SPACE, SPACE);
461 $$->location = yylloc;
462 }
463 ;
464
465 operator:
466 '[' { $$ = '['; }
467 | ']' { $$ = ']'; }
468 | '(' { $$ = '('; }
469 | ')' { $$ = ')'; }
470 | '{' { $$ = '{'; }
471 | '}' { $$ = '}'; }
472 | '.' { $$ = '.'; }
473 | '&' { $$ = '&'; }
474 | '*' { $$ = '*'; }
475 | '+' { $$ = '+'; }
476 | '-' { $$ = '-'; }
477 | '~' { $$ = '~'; }
478 | '!' { $$ = '!'; }
479 | '/' { $$ = '/'; }
480 | '%' { $$ = '%'; }
481 | LEFT_SHIFT { $$ = LEFT_SHIFT; }
482 | RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
483 | '<' { $$ = '<'; }
484 | '>' { $$ = '>'; }
485 | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
486 | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
487 | EQUAL { $$ = EQUAL; }
488 | NOT_EQUAL { $$ = NOT_EQUAL; }
489 | '^' { $$ = '^'; }
490 | '|' { $$ = '|'; }
491 | AND { $$ = AND; }
492 | OR { $$ = OR; }
493 | ';' { $$ = ';'; }
494 | ',' { $$ = ','; }
495 | '=' { $$ = '='; }
496 | PASTE { $$ = PASTE; }
497 ;
498
499 %%
500
501 string_list_t *
502 _string_list_create (void *ctx)
503 {
504 string_list_t *list;
505
506 list = xtalloc (ctx, string_list_t);
507 list->head = NULL;
508 list->tail = NULL;
509
510 return list;
511 }
512
513 void
514 _string_list_append_item (string_list_t *list, const char *str)
515 {
516 string_node_t *node;
517
518 node = xtalloc (list, string_node_t);
519 node->str = xtalloc_strdup (node, str);
520
521 node->next = NULL;
522
523 if (list->head == NULL) {
524 list->head = node;
525 } else {
526 list->tail->next = node;
527 }
528
529 list->tail = node;
530 }
531
532 int
533 _string_list_contains (string_list_t *list, const char *member, int *index)
534 {
535 string_node_t *node;
536 int i;
537
538 if (list == NULL)
539 return 0;
540
541 for (i = 0, node = list->head; node; i++, node = node->next) {
542 if (strcmp (node->str, member) == 0) {
543 if (index)
544 *index = i;
545 return 1;
546 }
547 }
548
549 return 0;
550 }
551
552 int
553 _string_list_length (string_list_t *list)
554 {
555 int length = 0;
556 string_node_t *node;
557
558 if (list == NULL)
559 return 0;
560
561 for (node = list->head; node; node = node->next)
562 length++;
563
564 return length;
565 }
566
567 argument_list_t *
568 _argument_list_create (void *ctx)
569 {
570 argument_list_t *list;
571
572 list = xtalloc (ctx, argument_list_t);
573 list->head = NULL;
574 list->tail = NULL;
575
576 return list;
577 }
578
579 void
580 _argument_list_append (argument_list_t *list, token_list_t *argument)
581 {
582 argument_node_t *node;
583
584 node = xtalloc (list, argument_node_t);
585 node->argument = argument;
586
587 node->next = NULL;
588
589 if (list->head == NULL) {
590 list->head = node;
591 } else {
592 list->tail->next = node;
593 }
594
595 list->tail = node;
596 }
597
598 int
599 _argument_list_length (argument_list_t *list)
600 {
601 int length = 0;
602 argument_node_t *node;
603
604 if (list == NULL)
605 return 0;
606
607 for (node = list->head; node; node = node->next)
608 length++;
609
610 return length;
611 }
612
613 token_list_t *
614 _argument_list_member_at (argument_list_t *list, int index)
615 {
616 argument_node_t *node;
617 int i;
618
619 if (list == NULL)
620 return NULL;
621
622 node = list->head;
623 for (i = 0; i < index; i++) {
624 node = node->next;
625 if (node == NULL)
626 break;
627 }
628
629 if (node)
630 return node->argument;
631
632 return NULL;
633 }
634
635 /* Note: This function talloc_steal()s the str pointer. */
636 token_t *
637 _token_create_str (void *ctx, int type, char *str)
638 {
639 token_t *token;
640
641 token = xtalloc (ctx, token_t);
642 token->type = type;
643 token->value.str = talloc_steal (token, str);
644
645 return token;
646 }
647
648 token_t *
649 _token_create_ival (void *ctx, int type, int ival)
650 {
651 token_t *token;
652
653 token = xtalloc (ctx, token_t);
654 token->type = type;
655 token->value.ival = ival;
656
657 return token;
658 }
659
660 token_list_t *
661 _token_list_create (void *ctx)
662 {
663 token_list_t *list;
664
665 list = xtalloc (ctx, token_list_t);
666 list->head = NULL;
667 list->tail = NULL;
668 list->non_space_tail = NULL;
669
670 return list;
671 }
672
673 void
674 _token_list_append (token_list_t *list, token_t *token)
675 {
676 token_node_t *node;
677
678 node = xtalloc (list, token_node_t);
679 node->token = xtalloc_reference (list, token);
680
681 node->next = NULL;
682
683 if (list->head == NULL) {
684 list->head = node;
685 } else {
686 list->tail->next = node;
687 }
688
689 list->tail = node;
690 if (token->type != SPACE)
691 list->non_space_tail = node;
692 }
693
694 void
695 _token_list_append_list (token_list_t *list, token_list_t *tail)
696 {
697 if (tail == NULL || tail->head == NULL)
698 return;
699
700 if (list->head == NULL) {
701 list->head = tail->head;
702 } else {
703 list->tail->next = tail->head;
704 }
705
706 list->tail = tail->tail;
707 list->non_space_tail = tail->non_space_tail;
708 }
709
710 static token_list_t *
711 _token_list_copy (void *ctx, token_list_t *other)
712 {
713 token_list_t *copy;
714 token_node_t *node;
715
716 if (other == NULL)
717 return NULL;
718
719 copy = _token_list_create (ctx);
720 for (node = other->head; node; node = node->next)
721 _token_list_append (copy, node->token);
722
723 return copy;
724 }
725
726 static void
727 _token_list_trim_trailing_space (token_list_t *list)
728 {
729 token_node_t *tail, *next;
730
731 if (list->non_space_tail) {
732 tail = list->non_space_tail->next;
733 list->non_space_tail->next = NULL;
734 list->tail = list->non_space_tail;
735
736 while (tail) {
737 next = tail->next;
738 talloc_free (tail);
739 tail = next;
740 }
741 }
742 }
743
744 static void
745 _token_print (char **out, token_t *token)
746 {
747 if (token->type < 256) {
748 glcpp_printf (*out, "%c", token->type);
749 return;
750 }
751
752 switch (token->type) {
753 case INTEGER:
754 glcpp_printf (*out, "%" PRIiMAX, token->value.ival);
755 break;
756 case IDENTIFIER:
757 case INTEGER_STRING:
758 case OTHER:
759 glcpp_print (*out, token->value.str);
760 break;
761 case SPACE:
762 glcpp_print (*out, " ");
763 break;
764 case LEFT_SHIFT:
765 glcpp_print (*out, "<<");
766 break;
767 case RIGHT_SHIFT:
768 glcpp_print (*out, ">>");
769 break;
770 case LESS_OR_EQUAL:
771 glcpp_print (*out, "<=");
772 break;
773 case GREATER_OR_EQUAL:
774 glcpp_print (*out, ">=");
775 break;
776 case EQUAL:
777 glcpp_print (*out, "==");
778 break;
779 case NOT_EQUAL:
780 glcpp_print (*out, "!=");
781 break;
782 case AND:
783 glcpp_print (*out, "&&");
784 break;
785 case OR:
786 glcpp_print (*out, "||");
787 break;
788 case PASTE:
789 glcpp_print (*out, "##");
790 break;
791 case COMMA_FINAL:
792 glcpp_print (*out, ",");
793 break;
794 case PLACEHOLDER:
795 /* Nothing to print. */
796 break;
797 default:
798 assert(!"Error: Don't know how to print token.");
799 break;
800 }
801 }
802
803 /* Return a new token (talloc()ed off of 'token') formed by pasting
804 * 'token' and 'other'. Note that this function may return 'token' or
805 * 'other' directly rather than allocating anything new.
806 *
807 * Caution: Only very cursory error-checking is performed to see if
808 * the final result is a valid single token. */
809 static token_t *
810 _token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
811 {
812 token_t *combined = NULL;
813
814 /* Pasting a placeholder onto anything makes no change. */
815 if (other->type == PLACEHOLDER)
816 return token;
817
818 /* When 'token' is a placeholder, just return 'other'. */
819 if (token->type == PLACEHOLDER)
820 return other;
821
822 /* A very few single-character punctuators can be combined
823 * with another to form a multi-character punctuator. */
824 switch (token->type) {
825 case '<':
826 if (other->type == '<')
827 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
828 else if (other->type == '=')
829 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
830 break;
831 case '>':
832 if (other->type == '>')
833 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
834 else if (other->type == '=')
835 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
836 break;
837 case '=':
838 if (other->type == '=')
839 combined = _token_create_ival (token, EQUAL, EQUAL);
840 break;
841 case '!':
842 if (other->type == '=')
843 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
844 break;
845 case '&':
846 if (other->type == '&')
847 combined = _token_create_ival (token, AND, AND);
848 break;
849 case '|':
850 if (other->type == '|')
851 combined = _token_create_ival (token, OR, OR);
852 break;
853 }
854
855 if (combined != NULL) {
856 /* Inherit the location from the first token */
857 combined->location = token->location;
858 return combined;
859 }
860
861 /* Two string-valued tokens can usually just be mashed
862 * together.
863 *
864 * XXX: This isn't actually legitimate. Several things here
865 * should result in a diagnostic since the result cannot be a
866 * valid, single pre-processing token. For example, pasting
867 * "123" and "abc" is not legal, but we don't catch that
868 * here. */
869 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
870 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
871 {
872 char *str;
873
874 str = xtalloc_asprintf (token, "%s%s",
875 token->value.str, other->value.str);
876 combined = _token_create_str (token, token->type, str);
877 combined->location = token->location;
878 return combined;
879 }
880
881 glcpp_error (&token->location, parser, "");
882 glcpp_print (parser->info_log, "Pasting \"");
883 _token_print (&parser->info_log, token);
884 glcpp_print (parser->info_log, "\" and \"");
885 _token_print (&parser->info_log, other);
886 glcpp_print (parser->info_log, "\" does not give a valid preprocessing token.\n");
887
888 return token;
889 }
890
891 static void
892 _token_list_print (glcpp_parser_t *parser, token_list_t *list)
893 {
894 token_node_t *node;
895
896 if (list == NULL)
897 return;
898
899 for (node = list->head; node; node = node->next)
900 _token_print (&parser->output, node->token);
901 }
902
903 void
904 yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
905 {
906 glcpp_error(locp, parser, "%s", error);
907 }
908
909 static void add_builtin_define(glcpp_parser_t *parser,
910 const char *name, int value)
911 {
912 token_t *tok;
913 token_list_t *list;
914
915 tok = _token_create_ival (parser, INTEGER, value);
916
917 list = _token_list_create(parser);
918 _token_list_append(list, tok);
919 _define_object_macro(parser, NULL, name, list);
920
921 talloc_unlink(parser, tok);
922 }
923
924 glcpp_parser_t *
925 glcpp_parser_create (const struct gl_extensions *extensions)
926 {
927 glcpp_parser_t *parser;
928 int language_version;
929
930 parser = xtalloc (NULL, glcpp_parser_t);
931
932 glcpp_lex_init_extra (parser, &parser->scanner);
933 parser->defines = hash_table_ctor (32, hash_table_string_hash,
934 hash_table_string_compare);
935 parser->active = NULL;
936 parser->lexing_if = 0;
937 parser->space_tokens = 1;
938 parser->newline_as_space = 0;
939 parser->in_control_line = 0;
940 parser->paren_count = 0;
941
942 parser->skip_stack = NULL;
943
944 parser->lex_from_list = NULL;
945 parser->lex_from_node = NULL;
946
947 parser->output = talloc_strdup(parser, "");
948 parser->info_log = talloc_strdup(parser, "");
949 parser->error = 0;
950
951 /* Add pre-defined macros. */
952 add_builtin_define(parser, "GL_ARB_draw_buffers", 1);
953 add_builtin_define(parser, "GL_ARB_texture_rectangle", 1);
954
955 if (extensions != NULL) {
956 if (extensions->EXT_texture_array) {
957 add_builtin_define(parser, "GL_EXT_texture_array", 1);
958 }
959
960 if (extensions->ARB_fragment_coord_conventions)
961 add_builtin_define(parser, "GL_ARB_fragment_coord_conventions",
962 1);
963 }
964
965 language_version = 110;
966 add_builtin_define(parser, "__VERSION__", language_version);
967
968 return parser;
969 }
970
971 int
972 glcpp_parser_parse (glcpp_parser_t *parser)
973 {
974 return yyparse (parser);
975 }
976
977 void
978 glcpp_parser_destroy (glcpp_parser_t *parser)
979 {
980 if (parser->skip_stack)
981 glcpp_error (&parser->skip_stack->loc, parser, "Unterminated #if\n");
982 glcpp_lex_destroy (parser->scanner);
983 hash_table_dtor (parser->defines);
984 talloc_free (parser);
985 }
986
987 typedef enum function_status
988 {
989 FUNCTION_STATUS_SUCCESS,
990 FUNCTION_NOT_A_FUNCTION,
991 FUNCTION_UNBALANCED_PARENTHESES
992 } function_status_t;
993
994 /* Find a set of function-like macro arguments by looking for a
995 * balanced set of parentheses.
996 *
997 * When called, 'node' should be the opening-parenthesis token, (or
998 * perhaps preceeding SPACE tokens). Upon successful return *last will
999 * be the last consumed node, (corresponding to the closing right
1000 * parenthesis).
1001 *
1002 * Return values:
1003 *
1004 * FUNCTION_STATUS_SUCCESS:
1005 *
1006 * Successfully parsed a set of function arguments.
1007 *
1008 * FUNCTION_NOT_A_FUNCTION:
1009 *
1010 * Macro name not followed by a '('. This is not an error, but
1011 * simply that the macro name should be treated as a non-macro.
1012 *
1013 * FUNCTION_UNBALANCED_PARENTHESES
1014 *
1015 * Macro name is not followed by a balanced set of parentheses.
1016 */
1017 static function_status_t
1018 _arguments_parse (argument_list_t *arguments,
1019 token_node_t *node,
1020 token_node_t **last)
1021 {
1022 token_list_t *argument;
1023 int paren_count;
1024
1025 node = node->next;
1026
1027 /* Ignore whitespace before first parenthesis. */
1028 while (node && node->token->type == SPACE)
1029 node = node->next;
1030
1031 if (node == NULL || node->token->type != '(')
1032 return FUNCTION_NOT_A_FUNCTION;
1033
1034 node = node->next;
1035
1036 argument = _token_list_create (arguments);
1037 _argument_list_append (arguments, argument);
1038
1039 for (paren_count = 1; node; node = node->next) {
1040 if (node->token->type == '(')
1041 {
1042 paren_count++;
1043 }
1044 else if (node->token->type == ')')
1045 {
1046 paren_count--;
1047 if (paren_count == 0)
1048 break;
1049 }
1050
1051 if (node->token->type == ',' &&
1052 paren_count == 1)
1053 {
1054 _token_list_trim_trailing_space (argument);
1055 argument = _token_list_create (arguments);
1056 _argument_list_append (arguments, argument);
1057 }
1058 else {
1059 if (argument->head == NULL) {
1060 /* Don't treat initial whitespace as
1061 * part of the arguement. */
1062 if (node->token->type == SPACE)
1063 continue;
1064 }
1065 _token_list_append (argument, node->token);
1066 }
1067 }
1068
1069 if (paren_count)
1070 return FUNCTION_UNBALANCED_PARENTHESES;
1071
1072 *last = node;
1073
1074 return FUNCTION_STATUS_SUCCESS;
1075 }
1076
1077 static token_list_t *
1078 _token_list_create_with_one_space (void *ctx)
1079 {
1080 token_list_t *list;
1081 token_t *space;
1082
1083 list = _token_list_create (ctx);
1084 space = _token_create_ival (list, SPACE, SPACE);
1085 _token_list_append (list, space);
1086
1087 return list;
1088 }
1089
1090 /* This is a helper function that's essentially part of the
1091 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1092 * except for by that function.
1093 *
1094 * Returns NULL if node is a simple token with no expansion, (that is,
1095 * although 'node' corresponds to an identifier defined as a
1096 * function-like macro, it is not followed with a parenthesized
1097 * argument list).
1098 *
1099 * Compute the complete expansion of node (which is a function-like
1100 * macro) and subsequent nodes which are arguments.
1101 *
1102 * Returns the token list that results from the expansion and sets
1103 * *last to the last node in the list that was consumed by the
1104 * expansion. Specifically, *last will be set as follows: as the
1105 * token of the closing right parenthesis.
1106 */
1107 static token_list_t *
1108 _glcpp_parser_expand_function (glcpp_parser_t *parser,
1109 token_node_t *node,
1110 token_node_t **last)
1111
1112 {
1113 macro_t *macro;
1114 const char *identifier;
1115 argument_list_t *arguments;
1116 function_status_t status;
1117 token_list_t *substituted;
1118 int parameter_index;
1119
1120 identifier = node->token->value.str;
1121
1122 macro = hash_table_find (parser->defines, identifier);
1123
1124 assert (macro->is_function);
1125
1126 arguments = _argument_list_create (parser);
1127 status = _arguments_parse (arguments, node, last);
1128
1129 switch (status) {
1130 case FUNCTION_STATUS_SUCCESS:
1131 break;
1132 case FUNCTION_NOT_A_FUNCTION:
1133 return NULL;
1134 case FUNCTION_UNBALANCED_PARENTHESES:
1135 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
1136 return NULL;
1137 }
1138
1139 /* Replace a macro defined as empty with a SPACE token. */
1140 if (macro->replacements == NULL) {
1141 talloc_free (arguments);
1142 return _token_list_create_with_one_space (parser);
1143 }
1144
1145 if (! ((_argument_list_length (arguments) ==
1146 _string_list_length (macro->parameters)) ||
1147 (_string_list_length (macro->parameters) == 0 &&
1148 _argument_list_length (arguments) == 1 &&
1149 arguments->head->argument->head == NULL)))
1150 {
1151 glcpp_error (&node->token->location, parser,
1152 "Error: macro %s invoked with %d arguments (expected %d)\n",
1153 identifier,
1154 _argument_list_length (arguments),
1155 _string_list_length (macro->parameters));
1156 return NULL;
1157 }
1158
1159 /* Perform argument substitution on the replacement list. */
1160 substituted = _token_list_create (arguments);
1161
1162 for (node = macro->replacements->head; node; node = node->next)
1163 {
1164 if (node->token->type == IDENTIFIER &&
1165 _string_list_contains (macro->parameters,
1166 node->token->value.str,
1167 &parameter_index))
1168 {
1169 token_list_t *argument;
1170 argument = _argument_list_member_at (arguments,
1171 parameter_index);
1172 /* Before substituting, we expand the argument
1173 * tokens, or append a placeholder token for
1174 * an empty argument. */
1175 if (argument->head) {
1176 token_list_t *expanded_argument;
1177 expanded_argument = _token_list_copy (parser,
1178 argument);
1179 _glcpp_parser_expand_token_list (parser,
1180 expanded_argument);
1181 _token_list_append_list (substituted,
1182 expanded_argument);
1183 } else {
1184 token_t *new_token;
1185
1186 new_token = _token_create_ival (substituted,
1187 PLACEHOLDER,
1188 PLACEHOLDER);
1189 _token_list_append (substituted, new_token);
1190 }
1191 } else {
1192 _token_list_append (substituted, node->token);
1193 }
1194 }
1195
1196 /* After argument substitution, and before further expansion
1197 * below, implement token pasting. */
1198
1199 _token_list_trim_trailing_space (substituted);
1200
1201 node = substituted->head;
1202 while (node)
1203 {
1204 token_node_t *next_non_space;
1205
1206 /* Look ahead for a PASTE token, skipping space. */
1207 next_non_space = node->next;
1208 while (next_non_space && next_non_space->token->type == SPACE)
1209 next_non_space = next_non_space->next;
1210
1211 if (next_non_space == NULL)
1212 break;
1213
1214 if (next_non_space->token->type != PASTE) {
1215 node = next_non_space;
1216 continue;
1217 }
1218
1219 /* Now find the next non-space token after the PASTE. */
1220 next_non_space = next_non_space->next;
1221 while (next_non_space && next_non_space->token->type == SPACE)
1222 next_non_space = next_non_space->next;
1223
1224 if (next_non_space == NULL) {
1225 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1226 return NULL;
1227 }
1228
1229 node->token = _token_paste (parser, node->token, next_non_space->token);
1230 node->next = next_non_space->next;
1231 if (next_non_space == substituted->tail)
1232 substituted->tail = node;
1233
1234 node = node->next;
1235 }
1236
1237 substituted->non_space_tail = substituted->tail;
1238
1239 return substituted;
1240 }
1241
1242 /* Compute the complete expansion of node, (and subsequent nodes after
1243 * 'node' in the case that 'node' is a function-like macro and
1244 * subsequent nodes are arguments).
1245 *
1246 * Returns NULL if node is a simple token with no expansion.
1247 *
1248 * Otherwise, returns the token list that results from the expansion
1249 * and sets *last to the last node in the list that was consumed by
1250 * the expansion. Specifically, *last will be set as follows:
1251 *
1252 * As 'node' in the case of object-like macro expansion.
1253 *
1254 * As the token of the closing right parenthesis in the case of
1255 * function-like macro expansion.
1256 */
1257 static token_list_t *
1258 _glcpp_parser_expand_node (glcpp_parser_t *parser,
1259 token_node_t *node,
1260 token_node_t **last)
1261 {
1262 token_t *token = node->token;
1263 const char *identifier;
1264 macro_t *macro;
1265
1266 /* We only expand identifiers */
1267 if (token->type != IDENTIFIER) {
1268 /* We change any COMMA into a COMMA_FINAL to prevent
1269 * it being mistaken for an argument separator
1270 * later. */
1271 if (token->type == ',') {
1272 token->type = COMMA_FINAL;
1273 token->value.ival = COMMA_FINAL;
1274 }
1275
1276 return NULL;
1277 }
1278
1279 /* Look up this identifier in the hash table. */
1280 identifier = token->value.str;
1281 macro = hash_table_find (parser->defines, identifier);
1282
1283 /* Not a macro, so no expansion needed. */
1284 if (macro == NULL)
1285 return NULL;
1286
1287 /* Finally, don't expand this macro if we're already actively
1288 * expanding it, (to avoid infinite recursion). */
1289 if (_active_list_contains (parser->active, identifier)) {
1290 /* We change the token type here from IDENTIFIER to
1291 * OTHER to prevent any future expansion of this
1292 * unexpanded token. */
1293 char *str;
1294 token_list_t *expansion;
1295 token_t *final;
1296
1297 str = xtalloc_strdup (parser, token->value.str);
1298 final = _token_create_str (parser, OTHER, str);
1299 expansion = _token_list_create (parser);
1300 _token_list_append (expansion, final);
1301 *last = node;
1302 return expansion;
1303 }
1304
1305 if (! macro->is_function)
1306 {
1307 *last = node;
1308
1309 /* Replace a macro defined as empty with a SPACE token. */
1310 if (macro->replacements == NULL)
1311 return _token_list_create_with_one_space (parser);
1312
1313 return _token_list_copy (parser, macro->replacements);
1314 }
1315
1316 return _glcpp_parser_expand_function (parser, node, last);
1317 }
1318
1319 /* Push a new identifier onto the active list, returning the new list.
1320 *
1321 * Here, 'marker' is the token node that appears in the list after the
1322 * expansion of 'identifier'. That is, when the list iterator begins
1323 * examinging 'marker', then it is time to pop this node from the
1324 * active stack.
1325 */
1326 active_list_t *
1327 _active_list_push (active_list_t *list,
1328 const char *identifier,
1329 token_node_t *marker)
1330 {
1331 active_list_t *node;
1332
1333 node = xtalloc (list, active_list_t);
1334 node->identifier = xtalloc_strdup (node, identifier);
1335 node->marker = marker;
1336 node->next = list;
1337
1338 return node;
1339 }
1340
1341 active_list_t *
1342 _active_list_pop (active_list_t *list)
1343 {
1344 active_list_t *node = list;
1345
1346 if (node == NULL)
1347 return NULL;
1348
1349 node = list->next;
1350 talloc_free (list);
1351
1352 return node;
1353 }
1354
1355 int
1356 _active_list_contains (active_list_t *list, const char *identifier)
1357 {
1358 active_list_t *node;
1359
1360 if (list == NULL)
1361 return 0;
1362
1363 for (node = list; node; node = node->next)
1364 if (strcmp (node->identifier, identifier) == 0)
1365 return 1;
1366
1367 return 0;
1368 }
1369
1370 /* Walk over the token list replacing nodes with their expansion.
1371 * Whenever nodes are expanded the walking will walk over the new
1372 * nodes, continuing to expand as necessary. The results are placed in
1373 * 'list' itself;
1374 */
1375 static void
1376 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1377 token_list_t *list)
1378 {
1379 token_node_t *node_prev;
1380 token_node_t *node, *last = NULL;
1381 token_list_t *expansion;
1382
1383 if (list == NULL)
1384 return;
1385
1386 _token_list_trim_trailing_space (list);
1387
1388 node_prev = NULL;
1389 node = list->head;
1390
1391 while (node) {
1392
1393 while (parser->active && parser->active->marker == node)
1394 parser->active = _active_list_pop (parser->active);
1395
1396 /* Find the expansion for node, which will replace all
1397 * nodes from node to last, inclusive. */
1398 expansion = _glcpp_parser_expand_node (parser, node, &last);
1399 if (expansion) {
1400 token_node_t *n;
1401
1402 for (n = node; n != last->next; n = n->next)
1403 while (parser->active &&
1404 parser->active->marker == n)
1405 {
1406 parser->active = _active_list_pop (parser->active);
1407 }
1408
1409 parser->active = _active_list_push (parser->active,
1410 node->token->value.str,
1411 last->next);
1412
1413 /* Splice expansion into list, supporting a
1414 * simple deletion if the expansion is
1415 * empty. */
1416 if (expansion->head) {
1417 if (node_prev)
1418 node_prev->next = expansion->head;
1419 else
1420 list->head = expansion->head;
1421 expansion->tail->next = last->next;
1422 if (last == list->tail)
1423 list->tail = expansion->tail;
1424 } else {
1425 if (node_prev)
1426 node_prev->next = last->next;
1427 else
1428 list->head = last->next;
1429 if (last == list->tail)
1430 list->tail = NULL;
1431 }
1432 } else {
1433 node_prev = node;
1434 }
1435 node = node_prev ? node_prev->next : list->head;
1436 }
1437
1438 while (parser->active)
1439 parser->active = _active_list_pop (parser->active);
1440
1441 list->non_space_tail = list->tail;
1442 }
1443
1444 void
1445 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1446 token_list_t *list)
1447 {
1448 if (list == NULL)
1449 return;
1450
1451 _glcpp_parser_expand_token_list (parser, list);
1452
1453 _token_list_trim_trailing_space (list);
1454
1455 _token_list_print (parser, list);
1456 }
1457
1458 static void
1459 _check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1460 const char *identifier)
1461 {
1462 /* According to the GLSL specification, macro names starting with "__"
1463 * or "GL_" are reserved for future use. So, don't allow them.
1464 */
1465 if (strncmp(identifier, "__", 2) == 0) {
1466 glcpp_error (loc, parser, "Macro names starting with \"__\" are reserved.\n");
1467 }
1468 if (strncmp(identifier, "GL_", 3) == 0) {
1469 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
1470 }
1471 }
1472
1473 void
1474 _define_object_macro (glcpp_parser_t *parser,
1475 YYLTYPE *loc,
1476 const char *identifier,
1477 token_list_t *replacements)
1478 {
1479 macro_t *macro;
1480
1481 if (loc != NULL)
1482 _check_for_reserved_macro_name(parser, loc, identifier);
1483
1484 macro = xtalloc (parser, macro_t);
1485
1486 macro->is_function = 0;
1487 macro->parameters = NULL;
1488 macro->identifier = talloc_strdup (macro, identifier);
1489 macro->replacements = talloc_steal (macro, replacements);
1490
1491 hash_table_insert (parser->defines, macro, identifier);
1492 }
1493
1494 void
1495 _define_function_macro (glcpp_parser_t *parser,
1496 YYLTYPE *loc,
1497 const char *identifier,
1498 string_list_t *parameters,
1499 token_list_t *replacements)
1500 {
1501 macro_t *macro;
1502
1503 _check_for_reserved_macro_name(parser, loc, identifier);
1504
1505 macro = xtalloc (parser, macro_t);
1506
1507 macro->is_function = 1;
1508 macro->parameters = talloc_steal (macro, parameters);
1509 macro->identifier = talloc_strdup (macro, identifier);
1510 macro->replacements = talloc_steal (macro, replacements);
1511
1512 hash_table_insert (parser->defines, macro, identifier);
1513 }
1514
1515 static int
1516 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
1517 {
1518 token_node_t *node;
1519 int ret;
1520
1521 if (parser->lex_from_list == NULL) {
1522 ret = glcpp_lex (yylval, yylloc, parser->scanner);
1523
1524 /* XXX: This ugly block of code exists for the sole
1525 * purpose of converting a NEWLINE token into a SPACE
1526 * token, but only in the case where we have seen a
1527 * function-like macro name, but have not yet seen its
1528 * closing parenthesis.
1529 *
1530 * There's perhaps a more compact way to do this with
1531 * mid-rule actions in the grammar.
1532 *
1533 * I'm definitely not pleased with the complexity of
1534 * this code here.
1535 */
1536 if (parser->newline_as_space)
1537 {
1538 if (ret == '(') {
1539 parser->paren_count++;
1540 } else if (ret == ')') {
1541 parser->paren_count--;
1542 if (parser->paren_count == 0)
1543 parser->newline_as_space = 0;
1544 } else if (ret == NEWLINE) {
1545 ret = SPACE;
1546 } else if (ret != SPACE) {
1547 if (parser->paren_count == 0)
1548 parser->newline_as_space = 0;
1549 }
1550 }
1551 else if (parser->in_control_line)
1552 {
1553 if (ret == NEWLINE)
1554 parser->in_control_line = 0;
1555 }
1556 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1557 ret == HASH_UNDEF || ret == HASH_IF ||
1558 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1559 ret == HASH_ELIF || ret == HASH_ELSE ||
1560 ret == HASH_ENDIF || ret == HASH)
1561 {
1562 parser->in_control_line = 1;
1563 }
1564 else if (ret == IDENTIFIER)
1565 {
1566 macro_t *macro;
1567 macro = hash_table_find (parser->defines,
1568 yylval->str);
1569 if (macro && macro->is_function) {
1570 parser->newline_as_space = 1;
1571 parser->paren_count = 0;
1572 }
1573 }
1574
1575 return ret;
1576 }
1577
1578 node = parser->lex_from_node;
1579
1580 if (node == NULL) {
1581 talloc_free (parser->lex_from_list);
1582 parser->lex_from_list = NULL;
1583 return NEWLINE;
1584 }
1585
1586 *yylval = node->token->value;
1587 ret = node->token->type;
1588
1589 parser->lex_from_node = node->next;
1590
1591 return ret;
1592 }
1593
1594 static void
1595 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1596 {
1597 token_node_t *node;
1598
1599 assert (parser->lex_from_list == NULL);
1600
1601 /* Copy list, eliminating any space tokens. */
1602 parser->lex_from_list = _token_list_create (parser);
1603
1604 for (node = list->head; node; node = node->next) {
1605 if (node->token->type == SPACE)
1606 continue;
1607 _token_list_append (parser->lex_from_list, node->token);
1608 }
1609
1610 talloc_free (list);
1611
1612 parser->lex_from_node = parser->lex_from_list->head;
1613
1614 /* It's possible the list consisted of nothing but whitespace. */
1615 if (parser->lex_from_node == NULL) {
1616 talloc_free (parser->lex_from_list);
1617 parser->lex_from_list = NULL;
1618 }
1619 }
1620
1621 static void
1622 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1623 int condition)
1624 {
1625 skip_type_t current = SKIP_NO_SKIP;
1626 skip_node_t *node;
1627
1628 if (parser->skip_stack)
1629 current = parser->skip_stack->type;
1630
1631 node = xtalloc (parser, skip_node_t);
1632 node->loc = *loc;
1633
1634 if (current == SKIP_NO_SKIP) {
1635 if (condition)
1636 node->type = SKIP_NO_SKIP;
1637 else
1638 node->type = SKIP_TO_ELSE;
1639 } else {
1640 node->type = SKIP_TO_ENDIF;
1641 }
1642
1643 node->next = parser->skip_stack;
1644 parser->skip_stack = node;
1645 }
1646
1647 static void
1648 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
1649 const char *type, int condition)
1650 {
1651 if (parser->skip_stack == NULL) {
1652 glcpp_error (loc, parser, "%s without #if\n", type);
1653 return;
1654 }
1655
1656 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1657 if (condition)
1658 parser->skip_stack->type = SKIP_NO_SKIP;
1659 } else {
1660 parser->skip_stack->type = SKIP_TO_ENDIF;
1661 }
1662 }
1663
1664 static void
1665 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
1666 {
1667 skip_node_t *node;
1668
1669 if (parser->skip_stack == NULL) {
1670 glcpp_error (loc, parser, "#endif without #if\n");
1671 return;
1672 }
1673
1674 node = parser->skip_stack;
1675 parser->skip_stack = node->next;
1676 talloc_free (node);
1677 }