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 | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 27 | #include <assert.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 | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 31 | void |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 32 | yyerror (void *scanner, const char *error); |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 33 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 34 | void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 35 | _define_object_macro (glcpp_parser_t *parser, |
| 36 | const char *macro, |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame^] | 37 | string_list_t *replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 38 | |
| 39 | void |
| 40 | _define_function_macro (glcpp_parser_t *parser, |
| 41 | const char *macro, |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 42 | string_list_t *parameters, |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame^] | 43 | string_list_t *replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 44 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 45 | void |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 46 | _expand_object_macro (glcpp_parser_t *parser, const char *identifier); |
| 47 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 48 | void |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 49 | _expand_function_macro (glcpp_parser_t *parser, |
| 50 | const char *identifier, |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 51 | argument_list_t *arguments); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 52 | |
| 53 | void |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 54 | _print_string_list (string_list_t *list); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 55 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 56 | string_list_t * |
| 57 | _string_list_create (void *ctx); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 58 | |
| 59 | void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 60 | _string_list_append_item (string_list_t *list, const char *str); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 61 | |
| 62 | void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 63 | _string_list_append_list (string_list_t *list, string_list_t *tail); |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 64 | |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 65 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 66 | _string_list_contains (string_list_t *list, const char *member, int *index); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 67 | |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 68 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 69 | _string_list_length (string_list_t *list); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 70 | |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 71 | argument_list_t * |
| 72 | _argument_list_create (void *ctx); |
| 73 | |
| 74 | void |
| 75 | _argument_list_append (argument_list_t *list, string_list_t *argument); |
| 76 | |
| 77 | int |
| 78 | _argument_list_length (argument_list_t *list); |
| 79 | |
| 80 | string_list_t * |
| 81 | _argument_list_member_at (argument_list_t *list, int index); |
| 82 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame^] | 83 | static void |
| 84 | glcpp_parser_push_expansion_macro (glcpp_parser_t *parser, |
| 85 | macro_t *macro, |
| 86 | argument_list_t *arguments); |
| 87 | |
| 88 | static void |
| 89 | glcpp_parser_pop_expansion (glcpp_parser_t *parser); |
| 90 | |
Carl Worth | 0293b2e | 2010-05-19 10:05:40 -0700 | [diff] [blame] | 91 | #define yylex glcpp_parser_lex |
| 92 | |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 93 | static int |
Carl Worth | 0293b2e | 2010-05-19 10:05:40 -0700 | [diff] [blame] | 94 | glcpp_parser_lex (glcpp_parser_t *parser); |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 95 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 96 | %} |
| 97 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 98 | %union { |
| 99 | char *str; |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 100 | string_list_t *string_list; |
| 101 | argument_list_t *argument_list; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 104 | %parse-param {glcpp_parser_t *parser} |
Carl Worth | 0293b2e | 2010-05-19 10:05:40 -0700 | [diff] [blame] | 105 | %lex-param {glcpp_parser_t *parser} |
Carl Worth | 38aa835 | 2010-05-10 11:52:29 -0700 | [diff] [blame] | 106 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame^] | 107 | %token DEFINE FUNC_MACRO IDENTIFIER OBJ_MACRO NEWLINE SPACE TOKEN UNDEF |
| 108 | %type <str> argument_word FUNC_MACRO IDENTIFIER OBJ_MACRO TOKEN |
| 109 | %type <string_list> argument macro parameter_list replacement_list pp_tokens |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 110 | %type <argument_list> argument_list |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 111 | |
Carl Worth | 796e1f0 | 2010-05-17 12:45:16 -0700 | [diff] [blame] | 112 | /* Hard to remove shift/reduce conflicts documented as follows: |
| 113 | * |
| 114 | * 1. '(' after FUNC_MACRO name which is correctly resolved to shift |
| 115 | * to form macro invocation rather than reducing directly to |
| 116 | * content. |
Carl Worth | 69f390d | 2010-05-19 07:42:42 -0700 | [diff] [blame] | 117 | * |
| 118 | * 2. Similarly, '(' after FUNC_MACRO which is correctly resolved to |
| 119 | * shift to form macro invocation rather than reducing directly to |
| 120 | * argument. |
Carl Worth | 796e1f0 | 2010-05-17 12:45:16 -0700 | [diff] [blame] | 121 | */ |
Carl Worth | 69f390d | 2010-05-19 07:42:42 -0700 | [diff] [blame] | 122 | %expect 2 |
Carl Worth | 796e1f0 | 2010-05-17 12:45:16 -0700 | [diff] [blame] | 123 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 124 | %% |
| 125 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 126 | input: |
| 127 | /* empty */ |
Carl Worth | 8bcb6f1 | 2010-05-12 13:21:20 -0700 | [diff] [blame] | 128 | | input content |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 129 | ; |
| 130 | |
Carl Worth | 04af135 | 2010-05-14 10:17:38 -0700 | [diff] [blame] | 131 | /* We do all printing at the content level */ |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 132 | content: |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 133 | IDENTIFIER { |
| 134 | printf ("%s", $1); |
| 135 | talloc_free ($1); |
| 136 | } |
| 137 | | TOKEN { |
| 138 | printf ("%s", $1); |
| 139 | talloc_free ($1); |
| 140 | } |
Carl Worth | acf87bc | 2010-05-17 10:34:29 -0700 | [diff] [blame] | 141 | | FUNC_MACRO { |
| 142 | printf ("%s", $1); |
| 143 | talloc_free ($1); |
| 144 | } |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 145 | | directive { |
| 146 | printf ("\n"); |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 147 | } |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 148 | | '(' { printf ("("); } |
| 149 | | ')' { printf (")"); } |
| 150 | | ',' { printf (","); } |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 151 | | macro |
Carl Worth | cd27e64 | 2010-05-12 13:11:50 -0700 | [diff] [blame] | 152 | ; |
| 153 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 154 | macro: |
| 155 | FUNC_MACRO '(' argument_list ')' { |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 156 | _expand_function_macro (parser, $1, $3); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 157 | } |
| 158 | | OBJ_MACRO { |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 159 | _expand_object_macro (parser, $1); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 160 | talloc_free ($1); |
| 161 | } |
| 162 | ; |
| 163 | |
| 164 | argument_list: |
Carl Worth | ac070e8 | 2010-05-14 11:33:00 -0700 | [diff] [blame] | 165 | /* empty */ { |
| 166 | $$ = _argument_list_create (parser); |
| 167 | } |
| 168 | | argument { |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 169 | $$ = _argument_list_create (parser); |
| 170 | _argument_list_append ($$, $1); |
| 171 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 172 | | argument_list ',' argument { |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 173 | _argument_list_append ($1, $3); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 174 | $$ = $1; |
| 175 | } |
| 176 | ; |
| 177 | |
| 178 | argument: |
Carl Worth | 59ca989 | 2010-05-19 07:49:47 -0700 | [diff] [blame] | 179 | argument_word { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 180 | $$ = _string_list_create (parser); |
Carl Worth | ac070e8 | 2010-05-14 11:33:00 -0700 | [diff] [blame] | 181 | _string_list_append_item ($$, $1); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 182 | } |
Carl Worth | 59ca989 | 2010-05-19 07:49:47 -0700 | [diff] [blame] | 183 | | argument argument_word { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 184 | _string_list_append_item ($1, $2); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 185 | talloc_free ($2); |
Carl Worth | 3596bb1 | 2010-05-14 16:53:52 -0700 | [diff] [blame] | 186 | $$ = $1; |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 187 | } |
Carl Worth | 3596bb1 | 2010-05-14 16:53:52 -0700 | [diff] [blame] | 188 | | argument '(' argument ')' { |
| 189 | _string_list_append_item ($1, "("); |
| 190 | _string_list_append_list ($1, $3); |
| 191 | _string_list_append_item ($1, ")"); |
| 192 | $$ = $1; |
| 193 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 194 | ; |
| 195 | |
Carl Worth | 59ca989 | 2010-05-19 07:49:47 -0700 | [diff] [blame] | 196 | argument_word: |
| 197 | IDENTIFIER { $$ = $1; } |
| 198 | | TOKEN { $$ = $1; } |
| 199 | | FUNC_MACRO { $$ = $1; } |
Carl Worth | 5d21142 | 2010-05-19 07:57:03 -0700 | [diff] [blame] | 200 | | macro { $$ = xtalloc_strdup (parser, ""); } |
Carl Worth | 59ca989 | 2010-05-19 07:49:47 -0700 | [diff] [blame] | 201 | ; |
| 202 | |
| 203 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 204 | directive: |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame^] | 205 | DEFINE IDENTIFIER NEWLINE { |
| 206 | string_list_t *list = _string_list_create (parser); |
| 207 | _define_object_macro (parser, $2, list); |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 208 | } |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame^] | 209 | | DEFINE IDENTIFIER SPACE replacement_list NEWLINE { |
| 210 | _define_object_macro (parser, $2, $4); |
| 211 | } |
| 212 | | DEFINE IDENTIFIER '(' parameter_list ')' replacement_list NEWLINE { |
Carl Worth | 81f0143 | 2010-05-14 17:08:45 -0700 | [diff] [blame] | 213 | _define_function_macro (parser, $2, $4, $6); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 214 | } |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 215 | | UNDEF IDENTIFIER { |
| 216 | string_list_t *macro = hash_table_find (parser->defines, $2); |
| 217 | if (macro) { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 218 | /* XXX: Need hash table to support a real way |
| 219 | * to remove an element rather than prefixing |
| 220 | * a new node with data of NULL like this. */ |
| 221 | hash_table_insert (parser->defines, NULL, $2); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 222 | talloc_free (macro); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 223 | } |
| 224 | talloc_free ($2); |
| 225 | } |
Carl Worth | 38bd27b | 2010-05-14 12:05:37 -0700 | [diff] [blame] | 226 | ; |
| 227 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 228 | parameter_list: |
| 229 | /* empty */ { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 230 | $$ = _string_list_create (parser); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 231 | } |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 232 | | IDENTIFIER { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 233 | $$ = _string_list_create (parser); |
| 234 | _string_list_append_item ($$, $1); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 235 | talloc_free ($1); |
| 236 | } |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 237 | | parameter_list ',' IDENTIFIER { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 238 | _string_list_append_item ($1, $3); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 239 | talloc_free ($3); |
| 240 | $$ = $1; |
| 241 | } |
| 242 | ; |
| 243 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame^] | 244 | replacement_list: |
| 245 | /* empty */ { |
| 246 | $$ = _string_list_create (parser); |
| 247 | } |
| 248 | | pp_tokens { |
| 249 | $$ = $1; |
| 250 | } |
| 251 | ; |
| 252 | |
| 253 | |
| 254 | pp_tokens: |
| 255 | TOKEN { |
| 256 | $$ = _string_list_create (parser); |
| 257 | _string_list_append_item ($$, $1); |
| 258 | } |
| 259 | | pp_tokens TOKEN { |
| 260 | _string_list_append_item ($1, $2); |
| 261 | $$ = $1; |
| 262 | } |
| 263 | ; |
| 264 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 265 | %% |
| 266 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 267 | string_list_t * |
| 268 | _string_list_create (void *ctx) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 269 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 270 | string_list_t *list; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 271 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 272 | list = xtalloc (ctx, string_list_t); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 273 | list->head = NULL; |
| 274 | list->tail = NULL; |
| 275 | |
| 276 | return list; |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 277 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 278 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 279 | void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 280 | _string_list_append_list (string_list_t *list, string_list_t *tail) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 281 | { |
| 282 | if (list->head == NULL) { |
| 283 | list->head = tail->head; |
| 284 | } else { |
| 285 | list->tail->next = tail->head; |
| 286 | } |
| 287 | |
| 288 | list->tail = tail->tail; |
| 289 | } |
| 290 | |
| 291 | void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 292 | _string_list_append_item (string_list_t *list, const char *str) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 293 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 294 | string_node_t *node; |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 295 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 296 | node = xtalloc (list, string_node_t); |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 297 | node->str = xtalloc_strdup (node, str); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 298 | |
| 299 | node->next = NULL; |
| 300 | |
| 301 | if (list->head == NULL) { |
| 302 | list->head = node; |
| 303 | } else { |
| 304 | list->tail->next = node; |
| 305 | } |
| 306 | |
| 307 | list->tail = node; |
| 308 | } |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 309 | |
| 310 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 311 | _string_list_contains (string_list_t *list, const char *member, int *index) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 312 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 313 | string_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 314 | int i; |
| 315 | |
| 316 | if (list == NULL) |
| 317 | return 0; |
| 318 | |
| 319 | for (i = 0, node = list->head; node; i++, node = node->next) { |
| 320 | if (strcmp (node->str, member) == 0) { |
Carl Worth | 420d05a | 2010-05-17 10:15:23 -0700 | [diff] [blame] | 321 | if (index) |
| 322 | *index = i; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 323 | return 1; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 331 | _string_list_length (string_list_t *list) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 332 | { |
| 333 | int length = 0; |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 334 | string_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 335 | |
| 336 | if (list == NULL) |
| 337 | return 0; |
| 338 | |
| 339 | for (node = list->head; node; node = node->next) |
| 340 | length++; |
| 341 | |
| 342 | return length; |
| 343 | } |
| 344 | |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 345 | void |
| 346 | _print_string_list (string_list_t *list) |
| 347 | { |
| 348 | string_node_t *node; |
| 349 | |
| 350 | if (list == NULL) |
| 351 | return; |
| 352 | |
Carl Worth | 81f0143 | 2010-05-14 17:08:45 -0700 | [diff] [blame] | 353 | for (node = list->head; node; node = node->next) { |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 354 | printf ("%s", node->str); |
Carl Worth | 81f0143 | 2010-05-14 17:08:45 -0700 | [diff] [blame] | 355 | if (node->next) |
| 356 | printf (" "); |
| 357 | } |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 360 | argument_list_t * |
| 361 | _argument_list_create (void *ctx) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 362 | { |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 363 | argument_list_t *list; |
| 364 | |
| 365 | list = xtalloc (ctx, argument_list_t); |
| 366 | list->head = NULL; |
| 367 | list->tail = NULL; |
| 368 | |
| 369 | return list; |
| 370 | } |
| 371 | |
| 372 | void |
| 373 | _argument_list_append (argument_list_t *list, string_list_t *argument) |
| 374 | { |
| 375 | argument_node_t *node; |
| 376 | |
| 377 | if (argument == NULL || argument->head == NULL) |
| 378 | return; |
| 379 | |
| 380 | node = xtalloc (list, argument_node_t); |
| 381 | node->argument = argument; |
| 382 | |
| 383 | node->next = NULL; |
| 384 | |
| 385 | if (list->head == NULL) { |
| 386 | list->head = node; |
| 387 | } else { |
| 388 | list->tail->next = node; |
| 389 | } |
| 390 | |
| 391 | list->tail = node; |
| 392 | } |
| 393 | |
| 394 | int |
| 395 | _argument_list_length (argument_list_t *list) |
| 396 | { |
| 397 | int length = 0; |
| 398 | argument_node_t *node; |
| 399 | |
| 400 | if (list == NULL) |
| 401 | return 0; |
| 402 | |
| 403 | for (node = list->head; node; node = node->next) |
| 404 | length++; |
| 405 | |
| 406 | return length; |
| 407 | } |
| 408 | |
| 409 | string_list_t * |
| 410 | _argument_list_member_at (argument_list_t *list, int index) |
| 411 | { |
| 412 | argument_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 413 | int i; |
| 414 | |
| 415 | if (list == NULL) |
| 416 | return NULL; |
| 417 | |
| 418 | node = list->head; |
| 419 | for (i = 0; i < index; i++) { |
| 420 | node = node->next; |
| 421 | if (node == NULL) |
| 422 | break; |
| 423 | } |
| 424 | |
| 425 | if (node) |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 426 | return node->argument; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 427 | |
| 428 | return NULL; |
| 429 | } |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 430 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 431 | void |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 432 | yyerror (void *scanner, const char *error) |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 433 | { |
| 434 | fprintf (stderr, "Parse error: %s\n", error); |
| 435 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 436 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 437 | glcpp_parser_t * |
| 438 | glcpp_parser_create (void) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 439 | { |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 440 | glcpp_parser_t *parser; |
| 441 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 442 | parser = xtalloc (NULL, glcpp_parser_t); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 443 | |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 444 | glcpp_lex_init_extra (parser, &parser->scanner); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 445 | parser->defines = hash_table_ctor (32, hash_table_string_hash, |
| 446 | hash_table_string_compare); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 447 | parser->expansions = NULL; |
| 448 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 449 | return parser; |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | int |
| 453 | glcpp_parser_parse (glcpp_parser_t *parser) |
| 454 | { |
| 455 | return yyparse (parser); |
| 456 | } |
| 457 | |
| 458 | void |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 459 | glcpp_parser_destroy (glcpp_parser_t *parser) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 460 | { |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 461 | glcpp_lex_destroy (parser->scanner); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 462 | hash_table_dtor (parser->defines); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 463 | talloc_free (parser); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 464 | } |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 465 | |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 466 | static int |
| 467 | glcpp_parser_is_expanding (glcpp_parser_t *parser, const char *member) |
| 468 | { |
| 469 | expansion_node_t *node; |
| 470 | |
| 471 | for (node = parser->expansions; node; node = node->next) { |
| 472 | if (node->macro && |
| 473 | strcmp (node->macro->identifier, member) == 0) |
| 474 | { |
| 475 | return 1; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | return 0; |
| 480 | } |
| 481 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 482 | token_class_t |
| 483 | glcpp_parser_classify_token (glcpp_parser_t *parser, |
| 484 | const char *identifier, |
| 485 | int *parameter_index) |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 486 | { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 487 | macro_t *macro; |
| 488 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 489 | /* First we check if we are currently expanding a |
| 490 | * function-like macro, and if so, whether the parameter list |
| 491 | * contains a parameter matching this token name. */ |
| 492 | if (parser->expansions && |
| 493 | parser->expansions->macro && |
| 494 | parser->expansions->macro->parameters) |
| 495 | { |
| 496 | string_list_t *list; |
| 497 | |
| 498 | list = parser->expansions->macro->parameters; |
| 499 | |
| 500 | if (_string_list_contains (list, identifier, parameter_index)) |
| 501 | return TOKEN_CLASS_ARGUMENT; |
| 502 | } |
| 503 | |
| 504 | /* If not a function-like macro parameter, we next check if |
| 505 | * this token is a macro itself. */ |
| 506 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 507 | macro = hash_table_find (parser->defines, identifier); |
| 508 | |
| 509 | if (macro == NULL) |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 510 | return TOKEN_CLASS_IDENTIFIER; |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 511 | |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 512 | /* Don't consider this a macro if we are already actively |
| 513 | * expanding this macro. */ |
| 514 | if (glcpp_parser_is_expanding (parser, identifier)) |
| 515 | return TOKEN_CLASS_IDENTIFIER; |
| 516 | |
| 517 | /* Definitely a macro. Just need to check if it's function-like. */ |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 518 | if (macro->is_function) |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 519 | return TOKEN_CLASS_FUNC_MACRO; |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 520 | else |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 521 | return TOKEN_CLASS_OBJ_MACRO; |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 524 | void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 525 | _define_object_macro (glcpp_parser_t *parser, |
| 526 | const char *identifier, |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame^] | 527 | string_list_t *replacements) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 528 | { |
| 529 | macro_t *macro; |
| 530 | |
| 531 | macro = xtalloc (parser, macro_t); |
| 532 | |
| 533 | macro->is_function = 0; |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 534 | macro->parameters = NULL; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 535 | macro->identifier = talloc_strdup (macro, identifier); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame^] | 536 | macro->replacements = talloc_steal (macro, replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 537 | |
| 538 | hash_table_insert (parser->defines, macro, identifier); |
| 539 | } |
| 540 | |
| 541 | void |
| 542 | _define_function_macro (glcpp_parser_t *parser, |
| 543 | const char *identifier, |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 544 | string_list_t *parameters, |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame^] | 545 | string_list_t *replacements) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 546 | { |
| 547 | macro_t *macro; |
| 548 | |
| 549 | macro = xtalloc (parser, macro_t); |
| 550 | |
| 551 | macro->is_function = 1; |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 552 | macro->parameters = talloc_steal (macro, parameters); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 553 | macro->identifier = talloc_strdup (macro, identifier); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame^] | 554 | macro->replacements = talloc_steal (macro, replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 555 | |
| 556 | hash_table_insert (parser->defines, macro, identifier); |
| 557 | } |
| 558 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 559 | static void |
| 560 | _glcpp_parser_push_expansion_internal (glcpp_parser_t *parser, |
| 561 | macro_t *macro, |
| 562 | argument_list_t *arguments, |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame^] | 563 | string_node_t *replacements) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 564 | { |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 565 | expansion_node_t *node; |
| 566 | |
| 567 | node = xtalloc (parser, expansion_node_t); |
| 568 | |
| 569 | node->macro = macro; |
| 570 | node->arguments = arguments; |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame^] | 571 | node->replacements = replacements; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 572 | |
| 573 | node->next = parser->expansions; |
| 574 | parser->expansions = node; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 575 | } |
| 576 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame^] | 577 | static void |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 578 | glcpp_parser_push_expansion_macro (glcpp_parser_t *parser, |
| 579 | macro_t *macro, |
| 580 | argument_list_t *arguments) |
| 581 | { |
| 582 | _glcpp_parser_push_expansion_internal (parser, macro, arguments, |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame^] | 583 | macro->replacements->head); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | void |
| 587 | glcpp_parser_push_expansion_argument (glcpp_parser_t *parser, |
| 588 | int argument_index) |
| 589 | { |
| 590 | argument_list_t *arguments; |
| 591 | string_list_t *argument; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 592 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 593 | arguments = parser->expansions->arguments; |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 594 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 595 | argument = _argument_list_member_at (arguments, argument_index); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 596 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 597 | _glcpp_parser_push_expansion_internal (parser, NULL, NULL, |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame^] | 598 | argument->head); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 599 | } |
| 600 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame^] | 601 | static void |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 602 | glcpp_parser_pop_expansion (glcpp_parser_t *parser) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 603 | { |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 604 | expansion_node_t *node; |
Carl Worth | 420d05a | 2010-05-17 10:15:23 -0700 | [diff] [blame] | 605 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 606 | node = parser->expansions; |
Carl Worth | 420d05a | 2010-05-17 10:15:23 -0700 | [diff] [blame] | 607 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 608 | if (node == NULL) { |
| 609 | fprintf (stderr, "Internal error: _expansion_list_pop called on an empty list.\n"); |
| 610 | exit (1); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 613 | parser->expansions = node->next; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 614 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 615 | talloc_free (node); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 616 | } |
| 617 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 618 | void |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 619 | _expand_object_macro (glcpp_parser_t *parser, const char *identifier) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 620 | { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 621 | macro_t *macro; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 622 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 623 | macro = hash_table_find (parser->defines, identifier); |
| 624 | assert (! macro->is_function); |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 625 | assert (! glcpp_parser_is_expanding (parser, identifier)); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 626 | |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 627 | glcpp_parser_push_expansion_macro (parser, macro, NULL); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 628 | } |
| 629 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 630 | void |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 631 | _expand_function_macro (glcpp_parser_t *parser, |
| 632 | const char *identifier, |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 633 | argument_list_t *arguments) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 634 | { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 635 | macro_t *macro; |
| 636 | |
| 637 | macro = hash_table_find (parser->defines, identifier); |
| 638 | assert (macro->is_function); |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 639 | assert (! glcpp_parser_is_expanding (parser, identifier)); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 640 | |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 641 | if (_argument_list_length (arguments) != |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 642 | _string_list_length (macro->parameters)) |
| 643 | { |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 644 | fprintf (stderr, |
| 645 | "Error: macro %s invoked with %d arguments (expected %d)\n", |
| 646 | identifier, |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 647 | _argument_list_length (arguments), |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 648 | _string_list_length (macro->parameters)); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 649 | return; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 650 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 651 | |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 652 | glcpp_parser_push_expansion_macro (parser, macro, arguments); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 653 | } |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 654 | |
| 655 | static int |
Carl Worth | 0293b2e | 2010-05-19 10:05:40 -0700 | [diff] [blame] | 656 | glcpp_parser_lex (glcpp_parser_t *parser) |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 657 | { |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame^] | 658 | expansion_node_t *expansion; |
| 659 | string_node_t *replacements; |
| 660 | int parameter_index; |
| 661 | |
| 662 | /* Who says C can't do efficient tail recursion? */ |
| 663 | RECURSE: |
| 664 | |
| 665 | expansion = parser->expansions; |
| 666 | |
| 667 | if (expansion == NULL) |
| 668 | return glcpp_lex (parser->scanner); |
| 669 | |
| 670 | replacements = expansion->replacements; |
| 671 | |
| 672 | /* Pop expansion when replacements is exhausted. */ |
| 673 | if (replacements == NULL) { |
| 674 | glcpp_parser_pop_expansion (parser); |
| 675 | goto RECURSE; |
| 676 | } |
| 677 | |
| 678 | expansion->replacements = replacements->next; |
| 679 | |
| 680 | if (strcmp (replacements->str, "(") == 0) |
| 681 | return '('; |
| 682 | else if (strcmp (replacements->str, ")") == 0) |
| 683 | return ')'; |
| 684 | else if (strcmp (replacements->str, ",") == 0) |
| 685 | return ','; |
| 686 | |
| 687 | yylval.str = xtalloc_strdup (parser, replacements->str); |
| 688 | |
| 689 | switch (glcpp_parser_classify_token (parser, yylval.str, |
| 690 | ¶meter_index)) |
| 691 | { |
| 692 | case TOKEN_CLASS_ARGUMENT: |
| 693 | talloc_free (yylval.str); |
| 694 | glcpp_parser_push_expansion_argument (parser, |
| 695 | parameter_index); |
| 696 | goto RECURSE; |
| 697 | break; |
| 698 | case TOKEN_CLASS_IDENTIFIER: |
| 699 | return IDENTIFIER; |
| 700 | break; |
| 701 | case TOKEN_CLASS_FUNC_MACRO: |
| 702 | return FUNC_MACRO; |
| 703 | break; |
| 704 | default: |
| 705 | case TOKEN_CLASS_OBJ_MACRO: |
| 706 | return OBJ_MACRO; |
| 707 | break; |
| 708 | } |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 709 | } |