glcpp: Add __VERSION__ define to the current language version.
[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 if (extensions && extensions->ARB_shading_language_120) {
967 language_version = 120;
968 }
969 add_builtin_define(parser, "__VERSION__", language_version);
970
971 return parser;
972 }
973
974 int
975 glcpp_parser_parse (glcpp_parser_t *parser)
976 {
977 return yyparse (parser);
978 }
979
980 void
981 glcpp_parser_destroy (glcpp_parser_t *parser)
982 {
983 if (parser->skip_stack)
984 glcpp_error (&parser->skip_stack->loc, parser, "Unterminated #if\n");
985 glcpp_lex_destroy (parser->scanner);
986 hash_table_dtor (parser->defines);
987 talloc_free (parser);
988 }
989
990 typedef enum function_status
991 {
992 FUNCTION_STATUS_SUCCESS,
993 FUNCTION_NOT_A_FUNCTION,
994 FUNCTION_UNBALANCED_PARENTHESES
995 } function_status_t;
996
997 /* Find a set of function-like macro arguments by looking for a
998 * balanced set of parentheses.
999 *
1000 * When called, 'node' should be the opening-parenthesis token, (or
1001 * perhaps preceeding SPACE tokens). Upon successful return *last will
1002 * be the last consumed node, (corresponding to the closing right
1003 * parenthesis).
1004 *
1005 * Return values:
1006 *
1007 * FUNCTION_STATUS_SUCCESS:
1008 *
1009 * Successfully parsed a set of function arguments.
1010 *
1011 * FUNCTION_NOT_A_FUNCTION:
1012 *
1013 * Macro name not followed by a '('. This is not an error, but
1014 * simply that the macro name should be treated as a non-macro.
1015 *
1016 * FUNCTION_UNBALANCED_PARENTHESES
1017 *
1018 * Macro name is not followed by a balanced set of parentheses.
1019 */
1020 static function_status_t
1021 _arguments_parse (argument_list_t *arguments,
1022 token_node_t *node,
1023 token_node_t **last)
1024 {
1025 token_list_t *argument;
1026 int paren_count;
1027
1028 node = node->next;
1029
1030 /* Ignore whitespace before first parenthesis. */
1031 while (node && node->token->type == SPACE)
1032 node = node->next;
1033
1034 if (node == NULL || node->token->type != '(')
1035 return FUNCTION_NOT_A_FUNCTION;
1036
1037 node = node->next;
1038
1039 argument = _token_list_create (arguments);
1040 _argument_list_append (arguments, argument);
1041
1042 for (paren_count = 1; node; node = node->next) {
1043 if (node->token->type == '(')
1044 {
1045 paren_count++;
1046 }
1047 else if (node->token->type == ')')
1048 {
1049 paren_count--;
1050 if (paren_count == 0)
1051 break;
1052 }
1053
1054 if (node->token->type == ',' &&
1055 paren_count == 1)
1056 {
1057 _token_list_trim_trailing_space (argument);
1058 argument = _token_list_create (arguments);
1059 _argument_list_append (arguments, argument);
1060 }
1061 else {
1062 if (argument->head == NULL) {
1063 /* Don't treat initial whitespace as
1064 * part of the arguement. */
1065 if (node->token->type == SPACE)
1066 continue;
1067 }
1068 _token_list_append (argument, node->token);
1069 }
1070 }
1071
1072 if (paren_count)
1073 return FUNCTION_UNBALANCED_PARENTHESES;
1074
1075 *last = node;
1076
1077 return FUNCTION_STATUS_SUCCESS;
1078 }
1079
1080 static token_list_t *
1081 _token_list_create_with_one_space (void *ctx)
1082 {
1083 token_list_t *list;
1084 token_t *space;
1085
1086 list = _token_list_create (ctx);
1087 space = _token_create_ival (list, SPACE, SPACE);
1088 _token_list_append (list, space);
1089
1090 return list;
1091 }
1092
1093 /* This is a helper function that's essentially part of the
1094 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1095 * except for by that function.
1096 *
1097 * Returns NULL if node is a simple token with no expansion, (that is,
1098 * although 'node' corresponds to an identifier defined as a
1099 * function-like macro, it is not followed with a parenthesized
1100 * argument list).
1101 *
1102 * Compute the complete expansion of node (which is a function-like
1103 * macro) and subsequent nodes which are arguments.
1104 *
1105 * Returns the token list that results from the expansion and sets
1106 * *last to the last node in the list that was consumed by the
1107 * expansion. Specifically, *last will be set as follows: as the
1108 * token of the closing right parenthesis.
1109 */
1110 static token_list_t *
1111 _glcpp_parser_expand_function (glcpp_parser_t *parser,
1112 token_node_t *node,
1113 token_node_t **last)
1114
1115 {
1116 macro_t *macro;
1117 const char *identifier;
1118 argument_list_t *arguments;
1119 function_status_t status;
1120 token_list_t *substituted;
1121 int parameter_index;
1122
1123 identifier = node->token->value.str;
1124
1125 macro = hash_table_find (parser->defines, identifier);
1126
1127 assert (macro->is_function);
1128
1129 arguments = _argument_list_create (parser);
1130 status = _arguments_parse (arguments, node, last);
1131
1132 switch (status) {
1133 case FUNCTION_STATUS_SUCCESS:
1134 break;
1135 case FUNCTION_NOT_A_FUNCTION:
1136 return NULL;
1137 case FUNCTION_UNBALANCED_PARENTHESES:
1138 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
1139 return NULL;
1140 }
1141
1142 /* Replace a macro defined as empty with a SPACE token. */
1143 if (macro->replacements == NULL) {
1144 talloc_free (arguments);
1145 return _token_list_create_with_one_space (parser);
1146 }
1147
1148 if (! ((_argument_list_length (arguments) ==
1149 _string_list_length (macro->parameters)) ||
1150 (_string_list_length (macro->parameters) == 0 &&
1151 _argument_list_length (arguments) == 1 &&
1152 arguments->head->argument->head == NULL)))
1153 {
1154 glcpp_error (&node->token->location, parser,
1155 "Error: macro %s invoked with %d arguments (expected %d)\n",
1156 identifier,
1157 _argument_list_length (arguments),
1158 _string_list_length (macro->parameters));
1159 return NULL;
1160 }
1161
1162 /* Perform argument substitution on the replacement list. */
1163 substituted = _token_list_create (arguments);
1164
1165 for (node = macro->replacements->head; node; node = node->next)
1166 {
1167 if (node->token->type == IDENTIFIER &&
1168 _string_list_contains (macro->parameters,
1169 node->token->value.str,
1170 &parameter_index))
1171 {
1172 token_list_t *argument;
1173 argument = _argument_list_member_at (arguments,
1174 parameter_index);
1175 /* Before substituting, we expand the argument
1176 * tokens, or append a placeholder token for
1177 * an empty argument. */
1178 if (argument->head) {
1179 token_list_t *expanded_argument;
1180 expanded_argument = _token_list_copy (parser,
1181 argument);
1182 _glcpp_parser_expand_token_list (parser,
1183 expanded_argument);
1184 _token_list_append_list (substituted,
1185 expanded_argument);
1186 } else {
1187 token_t *new_token;
1188
1189 new_token = _token_create_ival (substituted,
1190 PLACEHOLDER,
1191 PLACEHOLDER);
1192 _token_list_append (substituted, new_token);
1193 }
1194 } else {
1195 _token_list_append (substituted, node->token);
1196 }
1197 }
1198
1199 /* After argument substitution, and before further expansion
1200 * below, implement token pasting. */
1201
1202 _token_list_trim_trailing_space (substituted);
1203
1204 node = substituted->head;
1205 while (node)
1206 {
1207 token_node_t *next_non_space;
1208
1209 /* Look ahead for a PASTE token, skipping space. */
1210 next_non_space = node->next;
1211 while (next_non_space && next_non_space->token->type == SPACE)
1212 next_non_space = next_non_space->next;
1213
1214 if (next_non_space == NULL)
1215 break;
1216
1217 if (next_non_space->token->type != PASTE) {
1218 node = next_non_space;
1219 continue;
1220 }
1221
1222 /* Now find the next non-space token after the PASTE. */
1223 next_non_space = next_non_space->next;
1224 while (next_non_space && next_non_space->token->type == SPACE)
1225 next_non_space = next_non_space->next;
1226
1227 if (next_non_space == NULL) {
1228 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1229 return NULL;
1230 }
1231
1232 node->token = _token_paste (parser, node->token, next_non_space->token);
1233 node->next = next_non_space->next;
1234 if (next_non_space == substituted->tail)
1235 substituted->tail = node;
1236
1237 node = node->next;
1238 }
1239
1240 substituted->non_space_tail = substituted->tail;
1241
1242 return substituted;
1243 }
1244
1245 /* Compute the complete expansion of node, (and subsequent nodes after
1246 * 'node' in the case that 'node' is a function-like macro and
1247 * subsequent nodes are arguments).
1248 *
1249 * Returns NULL if node is a simple token with no expansion.
1250 *
1251 * Otherwise, returns the token list that results from the expansion
1252 * and sets *last to the last node in the list that was consumed by
1253 * the expansion. Specifically, *last will be set as follows:
1254 *
1255 * As 'node' in the case of object-like macro expansion.
1256 *
1257 * As the token of the closing right parenthesis in the case of
1258 * function-like macro expansion.
1259 */
1260 static token_list_t *
1261 _glcpp_parser_expand_node (glcpp_parser_t *parser,
1262 token_node_t *node,
1263 token_node_t **last)
1264 {
1265 token_t *token = node->token;
1266 const char *identifier;
1267 macro_t *macro;
1268
1269 /* We only expand identifiers */
1270 if (token->type != IDENTIFIER) {
1271 /* We change any COMMA into a COMMA_FINAL to prevent
1272 * it being mistaken for an argument separator
1273 * later. */
1274 if (token->type == ',') {
1275 token->type = COMMA_FINAL;
1276 token->value.ival = COMMA_FINAL;
1277 }
1278
1279 return NULL;
1280 }
1281
1282 /* Look up this identifier in the hash table. */
1283 identifier = token->value.str;
1284 macro = hash_table_find (parser->defines, identifier);
1285
1286 /* Not a macro, so no expansion needed. */
1287 if (macro == NULL)
1288 return NULL;
1289
1290 /* Finally, don't expand this macro if we're already actively
1291 * expanding it, (to avoid infinite recursion). */
1292 if (_active_list_contains (parser->active, identifier)) {
1293 /* We change the token type here from IDENTIFIER to
1294 * OTHER to prevent any future expansion of this
1295 * unexpanded token. */
1296 char *str;
1297 token_list_t *expansion;
1298 token_t *final;
1299
1300 str = xtalloc_strdup (parser, token->value.str);
1301 final = _token_create_str (parser, OTHER, str);
1302 expansion = _token_list_create (parser);
1303 _token_list_append (expansion, final);
1304 *last = node;
1305 return expansion;
1306 }
1307
1308 if (! macro->is_function)
1309 {
1310 *last = node;
1311
1312 /* Replace a macro defined as empty with a SPACE token. */
1313 if (macro->replacements == NULL)
1314 return _token_list_create_with_one_space (parser);
1315
1316 return _token_list_copy (parser, macro->replacements);
1317 }
1318
1319 return _glcpp_parser_expand_function (parser, node, last);
1320 }
1321
1322 /* Push a new identifier onto the active list, returning the new list.
1323 *
1324 * Here, 'marker' is the token node that appears in the list after the
1325 * expansion of 'identifier'. That is, when the list iterator begins
1326 * examinging 'marker', then it is time to pop this node from the
1327 * active stack.
1328 */
1329 active_list_t *
1330 _active_list_push (active_list_t *list,
1331 const char *identifier,
1332 token_node_t *marker)
1333 {
1334 active_list_t *node;
1335
1336 node = xtalloc (list, active_list_t);
1337 node->identifier = xtalloc_strdup (node, identifier);
1338 node->marker = marker;
1339 node->next = list;
1340
1341 return node;
1342 }
1343
1344 active_list_t *
1345 _active_list_pop (active_list_t *list)
1346 {
1347 active_list_t *node = list;
1348
1349 if (node == NULL)
1350 return NULL;
1351
1352 node = list->next;
1353 talloc_free (list);
1354
1355 return node;
1356 }
1357
1358 int
1359 _active_list_contains (active_list_t *list, const char *identifier)
1360 {
1361 active_list_t *node;
1362
1363 if (list == NULL)
1364 return 0;
1365
1366 for (node = list; node; node = node->next)
1367 if (strcmp (node->identifier, identifier) == 0)
1368 return 1;
1369
1370 return 0;
1371 }
1372
1373 /* Walk over the token list replacing nodes with their expansion.
1374 * Whenever nodes are expanded the walking will walk over the new
1375 * nodes, continuing to expand as necessary. The results are placed in
1376 * 'list' itself;
1377 */
1378 static void
1379 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1380 token_list_t *list)
1381 {
1382 token_node_t *node_prev;
1383 token_node_t *node, *last = NULL;
1384 token_list_t *expansion;
1385
1386 if (list == NULL)
1387 return;
1388
1389 _token_list_trim_trailing_space (list);
1390
1391 node_prev = NULL;
1392 node = list->head;
1393
1394 while (node) {
1395
1396 while (parser->active && parser->active->marker == node)
1397 parser->active = _active_list_pop (parser->active);
1398
1399 /* Find the expansion for node, which will replace all
1400 * nodes from node to last, inclusive. */
1401 expansion = _glcpp_parser_expand_node (parser, node, &last);
1402 if (expansion) {
1403 token_node_t *n;
1404
1405 for (n = node; n != last->next; n = n->next)
1406 while (parser->active &&
1407 parser->active->marker == n)
1408 {
1409 parser->active = _active_list_pop (parser->active);
1410 }
1411
1412 parser->active = _active_list_push (parser->active,
1413 node->token->value.str,
1414 last->next);
1415
1416 /* Splice expansion into list, supporting a
1417 * simple deletion if the expansion is
1418 * empty. */
1419 if (expansion->head) {
1420 if (node_prev)
1421 node_prev->next = expansion->head;
1422 else
1423 list->head = expansion->head;
1424 expansion->tail->next = last->next;
1425 if (last == list->tail)
1426 list->tail = expansion->tail;
1427 } else {
1428 if (node_prev)
1429 node_prev->next = last->next;
1430 else
1431 list->head = last->next;
1432 if (last == list->tail)
1433 list->tail = NULL;
1434 }
1435 } else {
1436 node_prev = node;
1437 }
1438 node = node_prev ? node_prev->next : list->head;
1439 }
1440
1441 while (parser->active)
1442 parser->active = _active_list_pop (parser->active);
1443
1444 list->non_space_tail = list->tail;
1445 }
1446
1447 void
1448 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1449 token_list_t *list)
1450 {
1451 if (list == NULL)
1452 return;
1453
1454 _glcpp_parser_expand_token_list (parser, list);
1455
1456 _token_list_trim_trailing_space (list);
1457
1458 _token_list_print (parser, list);
1459 }
1460
1461 static void
1462 _check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1463 const char *identifier)
1464 {
1465 /* According to the GLSL specification, macro names starting with "__"
1466 * or "GL_" are reserved for future use. So, don't allow them.
1467 */
1468 if (strncmp(identifier, "__", 2) == 0) {
1469 glcpp_error (loc, parser, "Macro names starting with \"__\" are reserved.\n");
1470 }
1471 if (strncmp(identifier, "GL_", 3) == 0) {
1472 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
1473 }
1474 }
1475
1476 void
1477 _define_object_macro (glcpp_parser_t *parser,
1478 YYLTYPE *loc,
1479 const char *identifier,
1480 token_list_t *replacements)
1481 {
1482 macro_t *macro;
1483
1484 if (loc != NULL)
1485 _check_for_reserved_macro_name(parser, loc, identifier);
1486
1487 macro = xtalloc (parser, macro_t);
1488
1489 macro->is_function = 0;
1490 macro->parameters = NULL;
1491 macro->identifier = talloc_strdup (macro, identifier);
1492 macro->replacements = talloc_steal (macro, replacements);
1493
1494 hash_table_insert (parser->defines, macro, identifier);
1495 }
1496
1497 void
1498 _define_function_macro (glcpp_parser_t *parser,
1499 YYLTYPE *loc,
1500 const char *identifier,
1501 string_list_t *parameters,
1502 token_list_t *replacements)
1503 {
1504 macro_t *macro;
1505
1506 _check_for_reserved_macro_name(parser, loc, identifier);
1507
1508 macro = xtalloc (parser, macro_t);
1509
1510 macro->is_function = 1;
1511 macro->parameters = talloc_steal (macro, parameters);
1512 macro->identifier = talloc_strdup (macro, identifier);
1513 macro->replacements = talloc_steal (macro, replacements);
1514
1515 hash_table_insert (parser->defines, macro, identifier);
1516 }
1517
1518 static int
1519 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
1520 {
1521 token_node_t *node;
1522 int ret;
1523
1524 if (parser->lex_from_list == NULL) {
1525 ret = glcpp_lex (yylval, yylloc, parser->scanner);
1526
1527 /* XXX: This ugly block of code exists for the sole
1528 * purpose of converting a NEWLINE token into a SPACE
1529 * token, but only in the case where we have seen a
1530 * function-like macro name, but have not yet seen its
1531 * closing parenthesis.
1532 *
1533 * There's perhaps a more compact way to do this with
1534 * mid-rule actions in the grammar.
1535 *
1536 * I'm definitely not pleased with the complexity of
1537 * this code here.
1538 */
1539 if (parser->newline_as_space)
1540 {
1541 if (ret == '(') {
1542 parser->paren_count++;
1543 } else if (ret == ')') {
1544 parser->paren_count--;
1545 if (parser->paren_count == 0)
1546 parser->newline_as_space = 0;
1547 } else if (ret == NEWLINE) {
1548 ret = SPACE;
1549 } else if (ret != SPACE) {
1550 if (parser->paren_count == 0)
1551 parser->newline_as_space = 0;
1552 }
1553 }
1554 else if (parser->in_control_line)
1555 {
1556 if (ret == NEWLINE)
1557 parser->in_control_line = 0;
1558 }
1559 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1560 ret == HASH_UNDEF || ret == HASH_IF ||
1561 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1562 ret == HASH_ELIF || ret == HASH_ELSE ||
1563 ret == HASH_ENDIF || ret == HASH)
1564 {
1565 parser->in_control_line = 1;
1566 }
1567 else if (ret == IDENTIFIER)
1568 {
1569 macro_t *macro;
1570 macro = hash_table_find (parser->defines,
1571 yylval->str);
1572 if (macro && macro->is_function) {
1573 parser->newline_as_space = 1;
1574 parser->paren_count = 0;
1575 }
1576 }
1577
1578 return ret;
1579 }
1580
1581 node = parser->lex_from_node;
1582
1583 if (node == NULL) {
1584 talloc_free (parser->lex_from_list);
1585 parser->lex_from_list = NULL;
1586 return NEWLINE;
1587 }
1588
1589 *yylval = node->token->value;
1590 ret = node->token->type;
1591
1592 parser->lex_from_node = node->next;
1593
1594 return ret;
1595 }
1596
1597 static void
1598 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1599 {
1600 token_node_t *node;
1601
1602 assert (parser->lex_from_list == NULL);
1603
1604 /* Copy list, eliminating any space tokens. */
1605 parser->lex_from_list = _token_list_create (parser);
1606
1607 for (node = list->head; node; node = node->next) {
1608 if (node->token->type == SPACE)
1609 continue;
1610 _token_list_append (parser->lex_from_list, node->token);
1611 }
1612
1613 talloc_free (list);
1614
1615 parser->lex_from_node = parser->lex_from_list->head;
1616
1617 /* It's possible the list consisted of nothing but whitespace. */
1618 if (parser->lex_from_node == NULL) {
1619 talloc_free (parser->lex_from_list);
1620 parser->lex_from_list = NULL;
1621 }
1622 }
1623
1624 static void
1625 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1626 int condition)
1627 {
1628 skip_type_t current = SKIP_NO_SKIP;
1629 skip_node_t *node;
1630
1631 if (parser->skip_stack)
1632 current = parser->skip_stack->type;
1633
1634 node = xtalloc (parser, skip_node_t);
1635 node->loc = *loc;
1636
1637 if (current == SKIP_NO_SKIP) {
1638 if (condition)
1639 node->type = SKIP_NO_SKIP;
1640 else
1641 node->type = SKIP_TO_ELSE;
1642 } else {
1643 node->type = SKIP_TO_ENDIF;
1644 }
1645
1646 node->next = parser->skip_stack;
1647 parser->skip_stack = node;
1648 }
1649
1650 static void
1651 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
1652 const char *type, int condition)
1653 {
1654 if (parser->skip_stack == NULL) {
1655 glcpp_error (loc, parser, "%s without #if\n", type);
1656 return;
1657 }
1658
1659 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1660 if (condition)
1661 parser->skip_stack->type = SKIP_NO_SKIP;
1662 } else {
1663 parser->skip_stack->type = SKIP_TO_ENDIF;
1664 }
1665 }
1666
1667 static void
1668 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
1669 {
1670 skip_node_t *node;
1671
1672 if (parser->skip_stack == NULL) {
1673 glcpp_error (loc, parser, "#endif without #if\n");
1674 return;
1675 }
1676
1677 node = parser->skip_stack;
1678 parser->skip_stack = node->next;
1679 talloc_free (node);
1680 }