Avoid printing a space at the beginning of lines in the output.
[mesa.git] / 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
29 #include "glcpp.h"
30
31 void
32 yyerror (void *scanner, const char *error);
33
34 void
35 _define_object_macro (glcpp_parser_t *parser,
36 const char *macro,
37 token_list_t *replacements);
38
39 void
40 _define_function_macro (glcpp_parser_t *parser,
41 const char *macro,
42 string_list_t *parameters,
43 token_list_t *replacements);
44
45 void
46 _expand_object_macro (glcpp_parser_t *parser, const char *identifier);
47
48 void
49 _expand_function_macro (glcpp_parser_t *parser,
50 const char *identifier,
51 argument_list_t *arguments);
52
53 string_list_t *
54 _string_list_create (void *ctx);
55
56 void
57 _string_list_append_item (string_list_t *list, const char *str);
58
59 void
60 _string_list_append_list (string_list_t *list, string_list_t *tail);
61
62 int
63 _string_list_contains (string_list_t *list, const char *member, int *index);
64
65 int
66 _string_list_length (string_list_t *list);
67
68 argument_list_t *
69 _argument_list_create (void *ctx);
70
71 void
72 _argument_list_append (argument_list_t *list, token_list_t *argument);
73
74 int
75 _argument_list_length (argument_list_t *list);
76
77 token_list_t *
78 _argument_list_member_at (argument_list_t *list, int index);
79
80 token_list_t *
81 _token_list_create (void *ctx);
82
83 void
84 _token_list_append (token_list_t *list, int type, const char *value);
85
86 void
87 _token_list_append_list (token_list_t *list, token_list_t *tail);
88
89 static void
90 glcpp_parser_push_expansion_macro (glcpp_parser_t *parser,
91 macro_t *macro,
92 argument_list_t *arguments);
93
94 static void
95 glcpp_parser_pop_expansion (glcpp_parser_t *parser);
96
97 #define yylex glcpp_parser_lex
98
99 static int
100 glcpp_parser_lex (glcpp_parser_t *parser);
101
102 %}
103
104 %union {
105 int ival;
106 char *str;
107 argument_list_t *argument_list;
108 string_list_t *string_list;
109 token_t token;
110 token_list_t *token_list;
111 }
112
113 %parse-param {glcpp_parser_t *parser}
114 %lex-param {glcpp_parser_t *parser}
115
116 %token DEFINE FUNC_MACRO IDENTIFIER IDENTIFIER_FINALIZED OBJ_MACRO NEWLINE SEPARATOR SPACE TOKEN UNDEF
117 %type <ival> punctuator
118 %type <str> content FUNC_MACRO IDENTIFIER IDENTIFIER_FINALIZED OBJ_MACRO
119 %type <argument_list> argument_list
120 %type <string_list> macro parameter_list
121 %type <token> TOKEN argument_word argument_word_or_comma
122 %type <token_list> argument argument_or_comma replacement_list pp_tokens
123
124 /* Hard to remove shift/reduce conflicts documented as follows:
125 *
126 * 1. '(' after FUNC_MACRO name which is correctly resolved to shift
127 * to form macro invocation rather than reducing directly to
128 * content.
129 *
130 * 2. Similarly, '(' after FUNC_MACRO which is correctly resolved to
131 * shift to form macro invocation rather than reducing directly to
132 * argument.
133 *
134 * 3. Similarly again now that we added argument_or_comma as well.
135 */
136 %expect 3
137
138 %%
139
140 /* We do all printing at the input level.
141 *
142 * The value for "input" is simply TOKEN or SEPARATOR so we
143 * can decide whether it's necessary to print a space
144 * character between any two. */
145 input:
146 /* empty */ {
147 parser->just_printed_separator = 1;
148 }
149 | input content {
150 int is_token;
151
152 if ($2 && strlen ($2)) {
153 int c = $2[0];
154 int is_not_separator = ((c >= 'a' && c <= 'z') ||
155 (c >= 'A' && c <= 'Z') ||
156 (c >= 'A' && c <= 'Z') ||
157 (c >= '0' && c <= '9') ||
158 (c == '_'));
159
160 if (! parser->just_printed_separator && is_not_separator)
161 {
162 printf (" ");
163 }
164 printf ("%s", $2);
165
166 if (is_not_separator)
167 parser->just_printed_separator = 0;
168 else
169 parser->just_printed_separator = 1;
170 }
171
172 if ($2)
173 talloc_free ($2);
174 }
175 ;
176
177 content:
178 IDENTIFIER {
179 $$ = $1;
180 }
181 | IDENTIFIER_FINALIZED {
182 $$ = $1;
183 }
184 | TOKEN {
185 $$ = $1.value;
186 }
187 | FUNC_MACRO {
188 $$ = $1;
189 }
190 | directive {
191 $$ = talloc_strdup (parser, "\n");
192 }
193 | punctuator {
194 $$ = talloc_asprintf (parser, "%c", $1);
195 }
196 | macro {
197 $$ = NULL;
198 }
199 ;
200
201 punctuator:
202 '(' { $$ = '('; }
203 | ')' { $$ = ')'; }
204 | ',' { $$ = ','; }
205 ;
206
207 macro:
208 FUNC_MACRO '(' argument_list ')' {
209 _expand_function_macro (parser, $1, $3);
210 }
211 | OBJ_MACRO {
212 _expand_object_macro (parser, $1);
213 talloc_free ($1);
214 }
215 ;
216
217 argument_list:
218 /* empty */ {
219 $$ = _argument_list_create (parser);
220 }
221 | argument {
222 $$ = _argument_list_create (parser);
223 _argument_list_append ($$, $1);
224 }
225 | argument_list ',' argument {
226 _argument_list_append ($1, $3);
227 $$ = $1;
228 }
229 ;
230
231 argument:
232 argument_word {
233 $$ = _token_list_create (parser);
234 _token_list_append ($$, $1.type, $1.value);
235 }
236 | argument argument_word {
237 _token_list_append ($1, $2.type, $2.value);
238 talloc_free ($2.value);
239 $$ = $1;
240 }
241 | argument '(' argument_or_comma ')' {
242 _token_list_append ($1, '(', "(");
243 _token_list_append_list ($1, $3);
244 _token_list_append ($1, ')', ")");
245 $$ = $1;
246 }
247 ;
248
249 argument_word:
250 IDENTIFIER { $$.type = IDENTIFIER; $$.value = $1; }
251 | IDENTIFIER_FINALIZED { $$.type = IDENTIFIER_FINALIZED; $$.value = $1; }
252 | TOKEN { $$ = $1; }
253 | FUNC_MACRO { $$.type = FUNC_MACRO; $$.value = $1; }
254 | macro { $$.type = TOKEN; $$.value = xtalloc_strdup (parser, ""); }
255 ;
256
257 /* XXX: The body of argument_or_comma is the same as the body
258 * of argument, but with "argument" and "argument_word"
259 * changed to "argument_or_comma" and
260 * "argument_word_or_comma". It would be nice to have less
261 * redundancy here, but I'm not sure how.
262 *
263 * It would also be nice to have a less ugly grammar to have
264 * to implement, but such is the C preprocessor.
265 */
266 argument_or_comma:
267 argument_word_or_comma {
268 $$ = _token_list_create (parser);
269 _token_list_append ($$, $1.type, $1.value);
270 }
271 | argument_or_comma argument_word_or_comma {
272 _token_list_append ($1, $2.type, $2.value);
273 $$ = $1;
274 }
275 | argument_or_comma '(' argument_or_comma ')' {
276 _token_list_append ($1, '(', "(");
277 _token_list_append_list ($1, $3);
278 _token_list_append ($1, ')', ")");
279 $$ = $1;
280 }
281 ;
282
283 argument_word_or_comma:
284 IDENTIFIER { $$.type = IDENTIFIER; $$.value = $1; }
285 | IDENTIFIER_FINALIZED { $$.type = IDENTIFIER_FINALIZED; $$.value = $1; }
286 | TOKEN { $$ = $1; }
287 | FUNC_MACRO { $$.type = FUNC_MACRO; $$.value = $1; }
288 | macro { $$.type = TOKEN; $$.value = xtalloc_strdup (parser, ""); }
289 | ',' { $$.type = ','; $$.value = xtalloc_strdup (parser, ","); }
290 ;
291
292 directive:
293 DEFINE IDENTIFIER NEWLINE {
294 token_list_t *list = _token_list_create (parser);
295 _define_object_macro (parser, $2, list);
296 }
297 | DEFINE IDENTIFIER SPACE replacement_list NEWLINE {
298 _define_object_macro (parser, $2, $4);
299 }
300 | DEFINE IDENTIFIER '(' parameter_list ')' replacement_list NEWLINE {
301 _define_function_macro (parser, $2, $4, $6);
302 }
303 | UNDEF IDENTIFIER {
304 string_list_t *macro = hash_table_find (parser->defines, $2);
305 if (macro) {
306 /* XXX: Need hash table to support a real way
307 * to remove an element rather than prefixing
308 * a new node with data of NULL like this. */
309 hash_table_insert (parser->defines, NULL, $2);
310 talloc_free (macro);
311 }
312 talloc_free ($2);
313 }
314 ;
315
316 parameter_list:
317 /* empty */ {
318 $$ = _string_list_create (parser);
319 }
320 | IDENTIFIER {
321 $$ = _string_list_create (parser);
322 _string_list_append_item ($$, $1);
323 talloc_free ($1);
324 }
325 | parameter_list ',' IDENTIFIER {
326 _string_list_append_item ($1, $3);
327 talloc_free ($3);
328 $$ = $1;
329 }
330 ;
331
332 replacement_list:
333 /* empty */ {
334 $$ = _token_list_create (parser);
335 }
336 | pp_tokens {
337 $$ = $1;
338 }
339 ;
340
341
342 pp_tokens:
343 TOKEN {
344 $$ = _token_list_create (parser);
345 _token_list_append ($$, $1.type, $1.value);
346 }
347 | pp_tokens TOKEN {
348 _token_list_append ($1, $2.type, $2.value);
349 $$ = $1;
350 }
351 ;
352
353 %%
354
355 string_list_t *
356 _string_list_create (void *ctx)
357 {
358 string_list_t *list;
359
360 list = xtalloc (ctx, string_list_t);
361 list->head = NULL;
362 list->tail = NULL;
363
364 return list;
365 }
366
367 void
368 _string_list_append_list (string_list_t *list, string_list_t *tail)
369 {
370 if (list->head == NULL) {
371 list->head = tail->head;
372 } else {
373 list->tail->next = tail->head;
374 }
375
376 list->tail = tail->tail;
377 }
378
379 void
380 _string_list_append_item (string_list_t *list, const char *str)
381 {
382 string_node_t *node;
383
384 node = xtalloc (list, string_node_t);
385 node->str = xtalloc_strdup (node, str);
386
387 node->next = NULL;
388
389 if (list->head == NULL) {
390 list->head = node;
391 } else {
392 list->tail->next = node;
393 }
394
395 list->tail = node;
396 }
397
398 int
399 _string_list_contains (string_list_t *list, const char *member, int *index)
400 {
401 string_node_t *node;
402 int i;
403
404 if (list == NULL)
405 return 0;
406
407 for (i = 0, node = list->head; node; i++, node = node->next) {
408 if (strcmp (node->str, member) == 0) {
409 if (index)
410 *index = i;
411 return 1;
412 }
413 }
414
415 return 0;
416 }
417
418 int
419 _string_list_length (string_list_t *list)
420 {
421 int length = 0;
422 string_node_t *node;
423
424 if (list == NULL)
425 return 0;
426
427 for (node = list->head; node; node = node->next)
428 length++;
429
430 return length;
431 }
432
433 argument_list_t *
434 _argument_list_create (void *ctx)
435 {
436 argument_list_t *list;
437
438 list = xtalloc (ctx, argument_list_t);
439 list->head = NULL;
440 list->tail = NULL;
441
442 return list;
443 }
444
445 void
446 _argument_list_append (argument_list_t *list, token_list_t *argument)
447 {
448 argument_node_t *node;
449
450 if (argument == NULL || argument->head == NULL)
451 return;
452
453 node = xtalloc (list, argument_node_t);
454 node->argument = argument;
455
456 node->next = NULL;
457
458 if (list->head == NULL) {
459 list->head = node;
460 } else {
461 list->tail->next = node;
462 }
463
464 list->tail = node;
465 }
466
467 int
468 _argument_list_length (argument_list_t *list)
469 {
470 int length = 0;
471 argument_node_t *node;
472
473 if (list == NULL)
474 return 0;
475
476 for (node = list->head; node; node = node->next)
477 length++;
478
479 return length;
480 }
481
482 token_list_t *
483 _argument_list_member_at (argument_list_t *list, int index)
484 {
485 argument_node_t *node;
486 int i;
487
488 if (list == NULL)
489 return NULL;
490
491 node = list->head;
492 for (i = 0; i < index; i++) {
493 node = node->next;
494 if (node == NULL)
495 break;
496 }
497
498 if (node)
499 return node->argument;
500
501 return NULL;
502 }
503
504 token_list_t *
505 _token_list_create (void *ctx)
506 {
507 token_list_t *list;
508
509 list = xtalloc (ctx, token_list_t);
510 list->head = NULL;
511 list->tail = NULL;
512
513 return list;
514 }
515
516 void
517 _token_list_append (token_list_t *list, int type, const char *value)
518 {
519 token_node_t *node;
520
521 node = xtalloc (list, token_node_t);
522 node->type = type;
523 node->value = xtalloc_strdup (list, value);
524
525 node->next = NULL;
526
527 if (list->head == NULL) {
528 list->head = node;
529 } else {
530 list->tail->next = node;
531 }
532
533 list->tail = node;
534 }
535
536 void
537 _token_list_append_list (token_list_t *list, token_list_t *tail)
538 {
539 if (list->head == NULL) {
540 list->head = tail->head;
541 } else {
542 list->tail->next = tail->head;
543 }
544
545 list->tail = tail->tail;
546 }
547
548 void
549 yyerror (void *scanner, const char *error)
550 {
551 fprintf (stderr, "Parse error: %s\n", error);
552 }
553
554 glcpp_parser_t *
555 glcpp_parser_create (void)
556 {
557 glcpp_parser_t *parser;
558
559 parser = xtalloc (NULL, glcpp_parser_t);
560
561 glcpp_lex_init_extra (parser, &parser->scanner);
562 parser->defines = hash_table_ctor (32, hash_table_string_hash,
563 hash_table_string_compare);
564 parser->expansions = NULL;
565
566 parser->just_printed_separator = 1;
567
568 return parser;
569 }
570
571 int
572 glcpp_parser_parse (glcpp_parser_t *parser)
573 {
574 return yyparse (parser);
575 }
576
577 void
578 glcpp_parser_destroy (glcpp_parser_t *parser)
579 {
580 glcpp_lex_destroy (parser->scanner);
581 hash_table_dtor (parser->defines);
582 talloc_free (parser);
583 }
584
585 static int
586 glcpp_parser_is_expanding (glcpp_parser_t *parser, const char *member)
587 {
588 expansion_node_t *node;
589
590 for (node = parser->expansions; node; node = node->next) {
591 if (node->macro &&
592 strcmp (node->macro->identifier, member) == 0)
593 {
594 return 1;
595 }
596 }
597
598 return 0;
599 }
600
601 token_class_t
602 glcpp_parser_classify_token (glcpp_parser_t *parser,
603 const char *identifier,
604 int *parameter_index)
605 {
606 macro_t *macro;
607
608 /* First we check if we are currently expanding a
609 * function-like macro, and if so, whether the parameter list
610 * contains a parameter matching this token name. */
611 if (parser->expansions &&
612 parser->expansions->macro &&
613 parser->expansions->macro->parameters)
614 {
615 string_list_t *list;
616
617 list = parser->expansions->macro->parameters;
618
619 if (_string_list_contains (list, identifier, parameter_index))
620 return TOKEN_CLASS_ARGUMENT;
621 }
622
623 /* If not a function-like macro parameter, we next check if
624 * this token is a macro itself. */
625
626 macro = hash_table_find (parser->defines, identifier);
627
628 if (macro == NULL)
629 return TOKEN_CLASS_IDENTIFIER;
630
631 /* Don't consider this a macro if we are already actively
632 * expanding this macro. */
633 if (glcpp_parser_is_expanding (parser, identifier))
634 return TOKEN_CLASS_IDENTIFIER_FINALIZED;
635
636 /* Definitely a macro. Just need to check if it's function-like. */
637 if (macro->is_function)
638 return TOKEN_CLASS_FUNC_MACRO;
639 else
640 return TOKEN_CLASS_OBJ_MACRO;
641 }
642
643 void
644 _define_object_macro (glcpp_parser_t *parser,
645 const char *identifier,
646 token_list_t *replacements)
647 {
648 macro_t *macro;
649
650 macro = xtalloc (parser, macro_t);
651
652 macro->is_function = 0;
653 macro->parameters = NULL;
654 macro->identifier = talloc_strdup (macro, identifier);
655 macro->replacements = talloc_steal (macro, replacements);
656
657 hash_table_insert (parser->defines, macro, identifier);
658 }
659
660 void
661 _define_function_macro (glcpp_parser_t *parser,
662 const char *identifier,
663 string_list_t *parameters,
664 token_list_t *replacements)
665 {
666 macro_t *macro;
667
668 macro = xtalloc (parser, macro_t);
669
670 macro->is_function = 1;
671 macro->parameters = talloc_steal (macro, parameters);
672 macro->identifier = talloc_strdup (macro, identifier);
673 macro->replacements = talloc_steal (macro, replacements);
674
675 hash_table_insert (parser->defines, macro, identifier);
676 }
677
678 static void
679 _glcpp_parser_push_expansion_internal (glcpp_parser_t *parser,
680 macro_t *macro,
681 argument_list_t *arguments,
682 token_node_t *replacements)
683 {
684 expansion_node_t *node;
685
686 node = xtalloc (parser, expansion_node_t);
687
688 node->macro = macro;
689 node->arguments = arguments;
690 node->replacements = replacements;
691
692 node->next = parser->expansions;
693 parser->expansions = node;
694 }
695
696 static void
697 glcpp_parser_push_expansion_macro (glcpp_parser_t *parser,
698 macro_t *macro,
699 argument_list_t *arguments)
700 {
701 _glcpp_parser_push_expansion_internal (parser, macro, arguments,
702 macro->replacements->head);
703 }
704
705 void
706 glcpp_parser_push_expansion_argument (glcpp_parser_t *parser,
707 int argument_index)
708 {
709 argument_list_t *arguments;
710 token_list_t *argument;
711
712 arguments = parser->expansions->arguments;
713
714 argument = _argument_list_member_at (arguments, argument_index);
715
716 _glcpp_parser_push_expansion_internal (parser, NULL, NULL,
717 argument->head);
718 }
719
720 static void
721 glcpp_parser_pop_expansion (glcpp_parser_t *parser)
722 {
723 expansion_node_t *node;
724
725 node = parser->expansions;
726
727 if (node == NULL) {
728 fprintf (stderr, "Internal error: _expansion_list_pop called on an empty list.\n");
729 exit (1);
730 }
731
732 parser->expansions = node->next;
733
734 talloc_free (node);
735 }
736
737 void
738 _expand_object_macro (glcpp_parser_t *parser, const char *identifier)
739 {
740 macro_t *macro;
741
742 macro = hash_table_find (parser->defines, identifier);
743 assert (! macro->is_function);
744 assert (! glcpp_parser_is_expanding (parser, identifier));
745
746 glcpp_parser_push_expansion_macro (parser, macro, NULL);
747 }
748
749 void
750 _expand_function_macro (glcpp_parser_t *parser,
751 const char *identifier,
752 argument_list_t *arguments)
753 {
754 macro_t *macro;
755
756 macro = hash_table_find (parser->defines, identifier);
757 assert (macro->is_function);
758 assert (! glcpp_parser_is_expanding (parser, identifier));
759
760 if (_argument_list_length (arguments) !=
761 _string_list_length (macro->parameters))
762 {
763 fprintf (stderr,
764 "Error: macro %s invoked with %d arguments (expected %d)\n",
765 identifier,
766 _argument_list_length (arguments),
767 _string_list_length (macro->parameters));
768 return;
769 }
770
771 glcpp_parser_push_expansion_macro (parser, macro, arguments);
772 }
773
774 static int
775 glcpp_parser_lex (glcpp_parser_t *parser)
776 {
777 expansion_node_t *expansion;
778 token_node_t *replacements;
779 int parameter_index;
780
781 /* Who says C can't do efficient tail recursion? */
782 RECURSE:
783
784 expansion = parser->expansions;
785
786 if (expansion == NULL)
787 return glcpp_lex (parser->scanner);
788
789 replacements = expansion->replacements;
790
791 /* Pop expansion when replacements is exhausted. */
792 if (replacements == NULL) {
793 glcpp_parser_pop_expansion (parser);
794 goto RECURSE;
795 }
796
797 expansion->replacements = replacements->next;
798
799 if (strcmp (replacements->value, "(") == 0)
800 return '(';
801 else if (strcmp (replacements->value, ")") == 0)
802 return ')';
803
804 yylval.str = xtalloc_strdup (parser, replacements->value);
805
806 /* Carefully refuse to expand any finalized identifier. */
807 if (replacements->type == IDENTIFIER_FINALIZED)
808 return IDENTIFIER_FINALIZED;
809
810 switch (glcpp_parser_classify_token (parser, yylval.str,
811 &parameter_index))
812 {
813 case TOKEN_CLASS_ARGUMENT:
814 talloc_free (yylval.str);
815 glcpp_parser_push_expansion_argument (parser,
816 parameter_index);
817 goto RECURSE;
818 break;
819 case TOKEN_CLASS_IDENTIFIER:
820 return IDENTIFIER;
821 break;
822 case TOKEN_CLASS_IDENTIFIER_FINALIZED:
823 return IDENTIFIER_FINALIZED;
824 break;
825 case TOKEN_CLASS_FUNC_MACRO:
826 return FUNC_MACRO;
827 break;
828 default:
829 case TOKEN_CLASS_OBJ_MACRO:
830 return OBJ_MACRO;
831 break;
832 }
833 }