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