3 * Copyright © 2010 Intel Corporation
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:
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
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.
31 #define YYLEX_PARAM parser->scanner
35 struct hash_table *defines;
39 yyerror (void *scanner, const char *error);
42 _print_resolved_token (glcpp_parser_t *parser, const char *token);
45 _list_create (void *ctx);
48 _list_append (list_t *list, const char *str);
57 %parse-param {glcpp_parser_t *parser}
58 %lex-param {void *scanner}
60 %token DEFINE IDENTIFIER NEWLINE TOKEN
61 %type <str> token IDENTIFIER TOKEN
62 %type <list> replacement_list
73 _print_resolved_token (parser, $1);
78 _print_resolved_token (parser, $2);
85 DEFINE IDENTIFIER replacement_list NEWLINE {
86 char *key = talloc_strdup ($3, $2);
88 hash_table_insert (parser->defines, $3, key);
95 $$ = _list_create (parser);
98 | replacement_list token {
99 _list_append ($1, $2);
107 | IDENTIFIER { $$ = $1; }
113 _list_create (void *ctx)
117 list = talloc (ctx, list_t);
119 fprintf (stderr, "Out of memory.\n");
130 _list_append (list_t *list, const char *str)
134 node = talloc (list, node_t);
136 fprintf (stderr, "Out of memory.\n");
140 node->str = talloc_strdup (node, str);
141 if (node->str == NULL) {
142 fprintf (stderr, "Out of memory.\n");
148 if (list->head == NULL) {
151 list->tail->next = node;
158 yyerror (void *scanner, const char *error)
160 fprintf (stderr, "Parse error: %s\n", error);
164 glcpp_parser_create (void)
166 glcpp_parser_t *parser;
168 parser = talloc (NULL, glcpp_parser_t);
169 if (parser == NULL) {
170 fprintf (stderr, "Out of memory.\n");
174 yylex_init (&parser->scanner);
175 parser->defines = hash_table_ctor (32, hash_table_string_hash,
176 hash_table_string_compare);
182 glcpp_parser_parse (glcpp_parser_t *parser)
184 return yyparse (parser);
188 glcpp_parser_destroy (glcpp_parser_t *parser)
190 yylex_destroy (parser->scanner);
191 hash_table_dtor (parser->defines);
192 talloc_free (parser);
196 _print_resolved_recursive (glcpp_parser_t *parser,
204 replacement = hash_table_find (parser->defines, token);
205 if (replacement == NULL) {
206 printf ("%s%s", *first ? "" : " ", token);
209 for (node = replacement->head ; node ; node = node->next) {
211 if (strcmp (token, orig) == 0) {
212 printf ("%s%s", *first ? "" : " ", token);
215 _print_resolved_recursive (parser, token, orig, first);
222 _print_resolved_token (glcpp_parser_t *parser, const char *token)
226 _print_resolved_recursive (parser, token, token, &first);