blob: 91fc5b98fc5f45de0255a87d1a143ceea5783b31 [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 */
Carl Worth8bcb6f12010-05-12 13:21:20 -070068| input 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 Worth012295f2010-05-12 13:19:23 -070077| NEWLINE {
78 printf ("\n");
79 }
Carl Worthcd27e642010-05-12 13:11:50 -070080;
81
82directive_with_newline:
83 directive NEWLINE {
84 printf ("\n");
85 }
Carl Worth3a37b872010-05-10 11:44:09 -070086;
87
Carl Worth33cc4002010-05-12 12:17:10 -070088directive:
Carl Worthcd27e642010-05-12 13:11:50 -070089 DEFINE IDENTIFIER replacement_list {
Carl Worth5070a202010-05-12 12:45:33 -070090 talloc_steal ($3, $2);
91 hash_table_insert (parser->defines, $3, $2);
Carl Worthcd27e642010-05-12 13:11:50 -070092 }
93| UNDEF IDENTIFIER {
94 list_t *replacement = hash_table_find (parser->defines, $2);
95 if (replacement) {
96 /* XXX: Need hash table to support a real way
97 * to remove an element rather than prefixing
98 * a new node with data of NULL like this. */
99 hash_table_insert (parser->defines, NULL, $2);
100 talloc_free (replacement);
101 }
102 talloc_free ($2);
Carl Worth33cc4002010-05-12 12:17:10 -0700103 }
104;
105
106replacement_list:
107 /* empty */ {
108 $$ = _list_create (parser);
109 }
110
111| replacement_list token {
112 _list_append ($1, $2);
Carl Worth5070a202010-05-12 12:45:33 -0700113 talloc_free ($2);
Carl Worth33cc4002010-05-12 12:17:10 -0700114 $$ = $1;
115 }
116;
117
118token:
119 TOKEN { $$ = $1; }
120| IDENTIFIER { $$ = $1; }
121;
122
123%%
124
125list_t *
126_list_create (void *ctx)
127{
128 list_t *list;
129
Carl Worth5070a202010-05-12 12:45:33 -0700130 list = xtalloc (ctx, list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700131 list->head = NULL;
132 list->tail = NULL;
133
134 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700135}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700136
Carl Worth33cc4002010-05-12 12:17:10 -0700137void
138_list_append (list_t *list, const char *str)
139{
140 node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700141
Carl Worth5070a202010-05-12 12:45:33 -0700142 node = xtalloc (list, node_t);
143 node->str = xtalloc_strdup (node, str);
Carl Worth33cc4002010-05-12 12:17:10 -0700144
145 node->next = NULL;
146
147 if (list->head == NULL) {
148 list->head = node;
149 } else {
150 list->tail->next = node;
151 }
152
153 list->tail = node;
154}
155
Carl Worth3a37b872010-05-10 11:44:09 -0700156void
Carl Wortha1e32bc2010-05-10 13:17:25 -0700157yyerror (void *scanner, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700158{
159 fprintf (stderr, "Parse error: %s\n", error);
160}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700161
Carl Worth33cc4002010-05-12 12:17:10 -0700162glcpp_parser_t *
163glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700164{
Carl Worth33cc4002010-05-12 12:17:10 -0700165 glcpp_parser_t *parser;
166
Carl Worth5070a202010-05-12 12:45:33 -0700167 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700168
Carl Worth5070a202010-05-12 12:45:33 -0700169 yylex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700170 parser->defines = hash_table_ctor (32, hash_table_string_hash,
171 hash_table_string_compare);
Carl Worth33cc4002010-05-12 12:17:10 -0700172
173 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700174}
175
176int
177glcpp_parser_parse (glcpp_parser_t *parser)
178{
179 return yyparse (parser);
180}
181
182void
Carl Worth33cc4002010-05-12 12:17:10 -0700183glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700184{
185 yylex_destroy (parser->scanner);
186 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700187 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700188}
Carl Worthc6d5af32010-05-11 12:30:09 -0700189
Carl Worth33cc4002010-05-12 12:17:10 -0700190static void
191_print_resolved_recursive (glcpp_parser_t *parser,
192 const char *token,
193 const char *orig,
194 int *first)
Carl Worthc6d5af32010-05-11 12:30:09 -0700195{
Carl Worth33cc4002010-05-12 12:17:10 -0700196 list_t *replacement;
197 node_t *node;
Carl Worthc6d5af32010-05-11 12:30:09 -0700198
Carl Worth33cc4002010-05-12 12:17:10 -0700199 replacement = hash_table_find (parser->defines, token);
200 if (replacement == NULL) {
201 printf ("%s%s", *first ? "" : " ", token);
202 *first = 0;
203 } else {
204 for (node = replacement->head ; node ; node = node->next) {
205 token = node->str;
206 if (strcmp (token, orig) == 0) {
207 printf ("%s%s", *first ? "" : " ", token);
208 *first = 0;
209 } else {
210 _print_resolved_recursive (parser, token, orig, first);
211 }
212 }
Carl Worthc6d5af32010-05-11 12:30:09 -0700213 }
Carl Worthc6d5af32010-05-11 12:30:09 -0700214}
215
Carl Worth33cc4002010-05-12 12:17:10 -0700216void
217_print_resolved_token (glcpp_parser_t *parser, const char *token)
218{
219 int first = 1;
220
221 _print_resolved_recursive (parser, token, token, &first);
222}