Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 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> |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 27 | #include <talloc.h> |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 28 | |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 29 | #include "glcpp.h" |
| 30 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 31 | #define YYLEX_PARAM parser->scanner |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 32 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 33 | struct glcpp_parser { |
| 34 | yyscan_t scanner; |
| 35 | struct hash_table *defines; |
| 36 | }; |
| 37 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 38 | void |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 39 | yyerror (void *scanner, const char *error); |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 40 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 41 | void |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 42 | _print_expanded_macro (glcpp_parser_t *parser, const char *macro); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 43 | |
| 44 | list_t * |
| 45 | _list_create (void *ctx); |
| 46 | |
| 47 | void |
| 48 | _list_append (list_t *list, const char *str); |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 49 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 50 | %} |
| 51 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 52 | %union { |
| 53 | char *str; |
| 54 | list_t *list; |
| 55 | } |
| 56 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 57 | %parse-param {glcpp_parser_t *parser} |
Carl Worth | 38aa835 | 2010-05-10 11:52:29 -0700 | [diff] [blame] | 58 | %lex-param {void *scanner} |
| 59 | |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 60 | %token DEFINE IDENTIFIER MACRO NEWLINE TOKEN UNDEF |
| 61 | %type <str> IDENTIFIER MACRO TOKEN string |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 62 | %type <list> replacement_list |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 63 | |
| 64 | %% |
| 65 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 66 | input: |
| 67 | /* empty */ |
Carl Worth | 8bcb6f1 | 2010-05-12 13:21:20 -0700 | [diff] [blame] | 68 | | input content |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 69 | ; |
| 70 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 71 | content: |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 72 | IDENTIFIER { |
| 73 | printf ("%s", $1); |
| 74 | talloc_free ($1); |
| 75 | } |
| 76 | | TOKEN { |
| 77 | printf ("%s", $1); |
| 78 | talloc_free ($1); |
| 79 | } |
| 80 | | MACRO { |
| 81 | _print_expanded_macro (parser, $1); |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 82 | talloc_free ($1); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 83 | } |
Carl Worth | cd27e64 | 2010-05-12 13:11:50 -0700 | [diff] [blame] | 84 | | directive_with_newline |
Carl Worth | 012295f | 2010-05-12 13:19:23 -0700 | [diff] [blame] | 85 | | NEWLINE { |
| 86 | printf ("\n"); |
| 87 | } |
Carl Worth | cd27e64 | 2010-05-12 13:11:50 -0700 | [diff] [blame] | 88 | ; |
| 89 | |
| 90 | directive_with_newline: |
| 91 | directive NEWLINE { |
| 92 | printf ("\n"); |
| 93 | } |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 94 | ; |
| 95 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 96 | directive: |
Carl Worth | cd27e64 | 2010-05-12 13:11:50 -0700 | [diff] [blame] | 97 | DEFINE IDENTIFIER replacement_list { |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 98 | talloc_steal ($3, $2); |
| 99 | hash_table_insert (parser->defines, $3, $2); |
Carl Worth | cd27e64 | 2010-05-12 13:11:50 -0700 | [diff] [blame] | 100 | } |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 101 | | UNDEF MACRO { |
Carl Worth | cd27e64 | 2010-05-12 13:11:50 -0700 | [diff] [blame] | 102 | list_t *replacement = hash_table_find (parser->defines, $2); |
| 103 | if (replacement) { |
| 104 | /* XXX: Need hash table to support a real way |
| 105 | * to remove an element rather than prefixing |
| 106 | * a new node with data of NULL like this. */ |
| 107 | hash_table_insert (parser->defines, NULL, $2); |
| 108 | talloc_free (replacement); |
| 109 | } |
| 110 | talloc_free ($2); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 111 | } |
| 112 | ; |
| 113 | |
| 114 | replacement_list: |
| 115 | /* empty */ { |
| 116 | $$ = _list_create (parser); |
| 117 | } |
| 118 | |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 119 | | replacement_list string { |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 120 | _list_append ($1, $2); |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 121 | talloc_free ($2); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 122 | $$ = $1; |
| 123 | } |
| 124 | ; |
| 125 | |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 126 | string: |
| 127 | IDENTIFIER { $$ = $1; } |
| 128 | | MACRO { $$ = $1; } |
| 129 | | TOKEN { $$ = $1; } |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 130 | ; |
| 131 | |
| 132 | %% |
| 133 | |
| 134 | list_t * |
| 135 | _list_create (void *ctx) |
| 136 | { |
| 137 | list_t *list; |
| 138 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 139 | list = xtalloc (ctx, list_t); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 140 | list->head = NULL; |
| 141 | list->tail = NULL; |
| 142 | |
| 143 | return list; |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 144 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 145 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 146 | void |
| 147 | _list_append (list_t *list, const char *str) |
| 148 | { |
| 149 | node_t *node; |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 150 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 151 | node = xtalloc (list, node_t); |
| 152 | node->str = xtalloc_strdup (node, str); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 153 | |
| 154 | node->next = NULL; |
| 155 | |
| 156 | if (list->head == NULL) { |
| 157 | list->head = node; |
| 158 | } else { |
| 159 | list->tail->next = node; |
| 160 | } |
| 161 | |
| 162 | list->tail = node; |
| 163 | } |
| 164 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 165 | void |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 166 | yyerror (void *scanner, const char *error) |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 167 | { |
| 168 | fprintf (stderr, "Parse error: %s\n", error); |
| 169 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 170 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 171 | glcpp_parser_t * |
| 172 | glcpp_parser_create (void) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 173 | { |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 174 | glcpp_parser_t *parser; |
| 175 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 176 | parser = xtalloc (NULL, glcpp_parser_t); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 177 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 178 | yylex_init_extra (parser, &parser->scanner); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 179 | parser->defines = hash_table_ctor (32, hash_table_string_hash, |
| 180 | hash_table_string_compare); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 181 | |
| 182 | return parser; |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | int |
| 186 | glcpp_parser_parse (glcpp_parser_t *parser) |
| 187 | { |
| 188 | return yyparse (parser); |
| 189 | } |
| 190 | |
| 191 | void |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 192 | glcpp_parser_destroy (glcpp_parser_t *parser) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 193 | { |
| 194 | yylex_destroy (parser->scanner); |
| 195 | hash_table_dtor (parser->defines); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 196 | talloc_free (parser); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 197 | } |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 198 | |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 199 | int |
| 200 | glcpp_parser_macro_defined (glcpp_parser_t *parser, const char *identifier) |
| 201 | { |
| 202 | return (hash_table_find (parser->defines, identifier) != NULL); |
| 203 | } |
| 204 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 205 | static void |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 206 | _print_expanded_macro_recursive (glcpp_parser_t *parser, |
| 207 | const char *token, |
| 208 | const char *orig, |
| 209 | int *first) |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 210 | { |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 211 | list_t *replacement; |
| 212 | node_t *node; |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 213 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 214 | replacement = hash_table_find (parser->defines, token); |
| 215 | if (replacement == NULL) { |
| 216 | printf ("%s%s", *first ? "" : " ", token); |
| 217 | *first = 0; |
| 218 | } else { |
| 219 | for (node = replacement->head ; node ; node = node->next) { |
| 220 | token = node->str; |
| 221 | if (strcmp (token, orig) == 0) { |
| 222 | printf ("%s%s", *first ? "" : " ", token); |
| 223 | *first = 0; |
| 224 | } else { |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 225 | _print_expanded_macro_recursive (parser, |
| 226 | token, orig, |
| 227 | first); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 230 | } |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 233 | void |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 234 | _print_expanded_macro (glcpp_parser_t *parser, const char *macro) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 235 | { |
| 236 | int first = 1; |
| 237 | |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 238 | _print_expanded_macro_recursive (parser, macro, macro, &first); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 239 | } |