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