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