blob: 29614fb1a4dbe51314db1673e34a41a80b65ba61 [file] [log] [blame]
Carl Worth3a37b872010-05-10 11:44:09 -07001%{
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>
Carl Worth33cc4002010-05-12 12:17:10 -070027#include <talloc.h>
Carl Worth3a37b872010-05-10 11:44:09 -070028
Carl Wortha1e32bc2010-05-10 13:17:25 -070029#include "glcpp.h"
30
Carl Worth0b27b5f2010-05-10 16:16:06 -070031#define YYLEX_PARAM parser->scanner
Carl Worth3a37b872010-05-10 11:44:09 -070032
Carl Worth33cc4002010-05-12 12:17:10 -070033struct glcpp_parser {
34 yyscan_t scanner;
35 struct hash_table *defines;
36};
37
Carl Worth3a37b872010-05-10 11:44:09 -070038void
Carl Wortha1e32bc2010-05-10 13:17:25 -070039yyerror (void *scanner, const char *error);
Carl Worth3a37b872010-05-10 11:44:09 -070040
Carl Worth33cc4002010-05-12 12:17:10 -070041void
42_print_resolved_token (glcpp_parser_t *parser, const char *token);
43
44list_t *
45_list_create (void *ctx);
46
47void
48_list_append (list_t *list, const char *str);
Carl Worthc6d5af32010-05-11 12:30:09 -070049
Carl Worth3a37b872010-05-10 11:44:09 -070050%}
51
Carl Worth33cc4002010-05-12 12:17:10 -070052%union {
53 char *str;
54 list_t *list;
55}
56
Carl Worth0b27b5f2010-05-10 16:16:06 -070057%parse-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -070058%lex-param {void *scanner}
59
Carl Worthcd27e642010-05-12 13:11:50 -070060%token DEFINE IDENTIFIER NEWLINE TOKEN UNDEF
Carl Worth33cc4002010-05-12 12:17:10 -070061%type <str> token IDENTIFIER TOKEN
62%type <list> replacement_list
Carl Worth3a37b872010-05-10 11:44:09 -070063
64%%
65
Carl Worth33cc4002010-05-12 12:17:10 -070066input:
67 /* empty */
68| content
Carl Worth3a37b872010-05-10 11:44:09 -070069;
70
Carl Worth33cc4002010-05-12 12:17:10 -070071content:
72 token {
73 _print_resolved_token (parser, $1);
Carl Worth5070a202010-05-12 12:45:33 -070074 talloc_free ($1);
Carl Worth33cc4002010-05-12 12:17:10 -070075 }
Carl Worthcd27e642010-05-12 13:11:50 -070076| directive_with_newline
Carl Worth33cc4002010-05-12 12:17:10 -070077| content token {
78 _print_resolved_token (parser, $2);
Carl Worth5070a202010-05-12 12:45:33 -070079 talloc_free ($2);
Carl Worth33cc4002010-05-12 12:17:10 -070080 }
Carl Worthcd27e642010-05-12 13:11:50 -070081| content directive_with_newline
82;
83
84directive_with_newline:
85 directive NEWLINE {
86 printf ("\n");
87 }
Carl Worth3a37b872010-05-10 11:44:09 -070088;
89
Carl Worth33cc4002010-05-12 12:17:10 -070090directive:
Carl Worthcd27e642010-05-12 13:11:50 -070091 DEFINE IDENTIFIER replacement_list {
Carl Worth5070a202010-05-12 12:45:33 -070092 talloc_steal ($3, $2);
93 hash_table_insert (parser->defines, $3, $2);
Carl Worthcd27e642010-05-12 13:11:50 -070094 }
95| UNDEF IDENTIFIER {
96 list_t *replacement = hash_table_find (parser->defines, $2);
97 if (replacement) {
98 /* XXX: Need hash table to support a real way
99 * to remove an element rather than prefixing
100 * a new node with data of NULL like this. */
101 hash_table_insert (parser->defines, NULL, $2);
102 talloc_free (replacement);
103 }
104 talloc_free ($2);
Carl Worth33cc4002010-05-12 12:17:10 -0700105 }
106;
107
108replacement_list:
109 /* empty */ {
110 $$ = _list_create (parser);
111 }
112
113| replacement_list token {
114 _list_append ($1, $2);
Carl Worth5070a202010-05-12 12:45:33 -0700115 talloc_free ($2);
Carl Worth33cc4002010-05-12 12:17:10 -0700116 $$ = $1;
117 }
118;
119
120token:
121 TOKEN { $$ = $1; }
122| IDENTIFIER { $$ = $1; }
123;
124
125%%
126
127list_t *
128_list_create (void *ctx)
129{
130 list_t *list;
131
Carl Worth5070a202010-05-12 12:45:33 -0700132 list = xtalloc (ctx, list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700133 list->head = NULL;
134 list->tail = NULL;
135
136 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700137}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700138
Carl Worth33cc4002010-05-12 12:17:10 -0700139void
140_list_append (list_t *list, const char *str)
141{
142 node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700143
Carl Worth5070a202010-05-12 12:45:33 -0700144 node = xtalloc (list, node_t);
145 node->str = xtalloc_strdup (node, str);
Carl Worth33cc4002010-05-12 12:17:10 -0700146
147 node->next = NULL;
148
149 if (list->head == NULL) {
150 list->head = node;
151 } else {
152 list->tail->next = node;
153 }
154
155 list->tail = node;
156}
157
Carl Worth3a37b872010-05-10 11:44:09 -0700158void
Carl Wortha1e32bc2010-05-10 13:17:25 -0700159yyerror (void *scanner, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700160{
161 fprintf (stderr, "Parse error: %s\n", error);
162}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700163
Carl Worth33cc4002010-05-12 12:17:10 -0700164glcpp_parser_t *
165glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700166{
Carl Worth33cc4002010-05-12 12:17:10 -0700167 glcpp_parser_t *parser;
168
Carl Worth5070a202010-05-12 12:45:33 -0700169 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700170
Carl Worth5070a202010-05-12 12:45:33 -0700171 yylex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700172 parser->defines = hash_table_ctor (32, hash_table_string_hash,
173 hash_table_string_compare);
Carl Worth33cc4002010-05-12 12:17:10 -0700174
175 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700176}
177
178int
179glcpp_parser_parse (glcpp_parser_t *parser)
180{
181 return yyparse (parser);
182}
183
184void
Carl Worth33cc4002010-05-12 12:17:10 -0700185glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700186{
187 yylex_destroy (parser->scanner);
188 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700189 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700190}
Carl Worthc6d5af32010-05-11 12:30:09 -0700191
Carl Worth33cc4002010-05-12 12:17:10 -0700192static void
193_print_resolved_recursive (glcpp_parser_t *parser,
194 const char *token,
195 const char *orig,
196 int *first)
Carl Worthc6d5af32010-05-11 12:30:09 -0700197{
Carl Worth33cc4002010-05-12 12:17:10 -0700198 list_t *replacement;
199 node_t *node;
Carl Worthc6d5af32010-05-11 12:30:09 -0700200
Carl Worth33cc4002010-05-12 12:17:10 -0700201 replacement = hash_table_find (parser->defines, token);
202 if (replacement == NULL) {
203 printf ("%s%s", *first ? "" : " ", token);
204 *first = 0;
205 } else {
206 for (node = replacement->head ; node ; node = node->next) {
207 token = node->str;
208 if (strcmp (token, orig) == 0) {
209 printf ("%s%s", *first ? "" : " ", token);
210 *first = 0;
211 } else {
212 _print_resolved_recursive (parser, token, orig, first);
213 }
214 }
Carl Worthc6d5af32010-05-11 12:30:09 -0700215 }
Carl Worthc6d5af32010-05-11 12:30:09 -0700216}
217
Carl Worth33cc4002010-05-12 12:17:10 -0700218void
219_print_resolved_token (glcpp_parser_t *parser, const char *token)
220{
221 int first = 1;
222
223 _print_resolved_recursive (parser, token, token, &first);
224}