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