blob: eae96efb30a4ccd2372f7797d88a779940bce1af [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 Worth33cc4002010-05-12 12:17:10 -070060%token DEFINE IDENTIFIER NEWLINE TOKEN
61%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);
74 free ($1);
75 }
76| directive
77| content token {
78 _print_resolved_token (parser, $2);
79 free ($2);
80 }
81| content directive
Carl Worth3a37b872010-05-10 11:44:09 -070082;
83
Carl Worth33cc4002010-05-12 12:17:10 -070084directive:
85 DEFINE IDENTIFIER replacement_list NEWLINE {
86 char *key = talloc_strdup ($3, $2);
87 free ($2);
88 hash_table_insert (parser->defines, $3, key);
89 printf ("\n");
90 }
91;
92
93replacement_list:
94 /* empty */ {
95 $$ = _list_create (parser);
96 }
97
98| replacement_list token {
99 _list_append ($1, $2);
100 free ($2);
101 $$ = $1;
102 }
103;
104
105token:
106 TOKEN { $$ = $1; }
107| IDENTIFIER { $$ = $1; }
108;
109
110%%
111
112list_t *
113_list_create (void *ctx)
114{
115 list_t *list;
116
117 list = talloc (ctx, list_t);
118 if (list == NULL) {
119 fprintf (stderr, "Out of memory.\n");
120 exit (1);
121 }
122
123 list->head = NULL;
124 list->tail = NULL;
125
126 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700127}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700128
Carl Worth33cc4002010-05-12 12:17:10 -0700129void
130_list_append (list_t *list, const char *str)
131{
132 node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700133
Carl Worth33cc4002010-05-12 12:17:10 -0700134 node = talloc (list, node_t);
135 if (node == NULL) {
136 fprintf (stderr, "Out of memory.\n");
137 exit (1);
138 }
Carl Worth3a37b872010-05-10 11:44:09 -0700139
Carl Worth33cc4002010-05-12 12:17:10 -0700140 node->str = talloc_strdup (node, str);
141 if (node->str == NULL) {
142 fprintf (stderr, "Out of memory.\n");
143 exit (1);
144 }
145
146 node->next = NULL;
147
148 if (list->head == NULL) {
149 list->head = node;
150 } else {
151 list->tail->next = node;
152 }
153
154 list->tail = node;
155}
156
Carl Worth3a37b872010-05-10 11:44:09 -0700157void
Carl Wortha1e32bc2010-05-10 13:17:25 -0700158yyerror (void *scanner, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700159{
160 fprintf (stderr, "Parse error: %s\n", error);
161}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700162
Carl Worth33cc4002010-05-12 12:17:10 -0700163glcpp_parser_t *
164glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700165{
Carl Worth33cc4002010-05-12 12:17:10 -0700166 glcpp_parser_t *parser;
167
168 parser = talloc (NULL, glcpp_parser_t);
169 if (parser == NULL) {
170 fprintf (stderr, "Out of memory.\n");
171 exit (1);
172 }
173
Carl Worth0b27b5f2010-05-10 16:16:06 -0700174 yylex_init (&parser->scanner);
175 parser->defines = hash_table_ctor (32, hash_table_string_hash,
176 hash_table_string_compare);
Carl Worth33cc4002010-05-12 12:17:10 -0700177
178 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700179}
180
181int
182glcpp_parser_parse (glcpp_parser_t *parser)
183{
184 return yyparse (parser);
185}
186
187void
Carl Worth33cc4002010-05-12 12:17:10 -0700188glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700189{
190 yylex_destroy (parser->scanner);
191 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700192 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700193}
Carl Worthc6d5af32010-05-11 12:30:09 -0700194
Carl Worth33cc4002010-05-12 12:17:10 -0700195static void
196_print_resolved_recursive (glcpp_parser_t *parser,
197 const char *token,
198 const char *orig,
199 int *first)
Carl Worthc6d5af32010-05-11 12:30:09 -0700200{
Carl Worth33cc4002010-05-12 12:17:10 -0700201 list_t *replacement;
202 node_t *node;
Carl Worthc6d5af32010-05-11 12:30:09 -0700203
Carl Worth33cc4002010-05-12 12:17:10 -0700204 replacement = hash_table_find (parser->defines, token);
205 if (replacement == NULL) {
206 printf ("%s%s", *first ? "" : " ", token);
207 *first = 0;
208 } else {
209 for (node = replacement->head ; node ; node = node->next) {
210 token = node->str;
211 if (strcmp (token, orig) == 0) {
212 printf ("%s%s", *first ? "" : " ", token);
213 *first = 0;
214 } else {
215 _print_resolved_recursive (parser, token, orig, first);
216 }
217 }
Carl Worthc6d5af32010-05-11 12:30:09 -0700218 }
Carl Worthc6d5af32010-05-11 12:30:09 -0700219}
220
Carl Worth33cc4002010-05-12 12:17:10 -0700221void
222_print_resolved_token (glcpp_parser_t *parser, const char *token)
223{
224 int first = 1;
225
226 _print_resolved_recursive (parser, token, token, &first);
227}