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 | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 28 | #include <inttypes.h> |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 29 | |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 30 | #include "glcpp.h" |
| 31 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 32 | static void |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 33 | yyerror (void *scanner, const char *error); |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 34 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 35 | static void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 36 | _define_object_macro (glcpp_parser_t *parser, |
| 37 | const char *macro, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 38 | token_list_t *replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 39 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 40 | static void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 41 | _define_function_macro (glcpp_parser_t *parser, |
| 42 | const char *macro, |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 43 | string_list_t *parameters, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 44 | token_list_t *replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 45 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 46 | static string_list_t * |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 47 | _string_list_create (void *ctx); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 48 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 49 | static void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 50 | _string_list_append_item (string_list_t *list, const char *str); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 51 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 52 | static void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 53 | _string_list_append_list (string_list_t *list, string_list_t *tail); |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 54 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 55 | static void |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 56 | _string_list_push (string_list_t *list, const char *str); |
| 57 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 58 | static void |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 59 | _string_list_pop (string_list_t *list); |
| 60 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 61 | static int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 62 | _string_list_contains (string_list_t *list, const char *member, int *index); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 63 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 64 | static int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 65 | _string_list_length (string_list_t *list); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 66 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 67 | static argument_list_t * |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 68 | _argument_list_create (void *ctx); |
| 69 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 70 | static void |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 71 | _argument_list_append (argument_list_t *list, token_list_t *argument); |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 72 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 73 | static int |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 74 | _argument_list_length (argument_list_t *list); |
| 75 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 76 | static token_list_t * |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 77 | _argument_list_member_at (argument_list_t *list, int index); |
| 78 | |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 79 | /* Note: This function talloc_steal()s the str pointer. */ |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 80 | static token_t * |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 81 | _token_create_str (void *ctx, int type, char *str); |
| 82 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 83 | static token_t * |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 84 | _token_create_ival (void *ctx, int type, int ival); |
| 85 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 86 | static token_list_t * |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 87 | _token_list_create (void *ctx); |
| 88 | |
Carl Worth | b1ae61a | 2010-05-26 08:10:38 -0700 | [diff] [blame] | 89 | /* Note: This function adds a talloc_reference() to token. |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 90 | * |
| 91 | * You may want to talloc_unlink any current reference if you no |
| 92 | * longer need it. */ |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 93 | static void |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 94 | _token_list_append (token_list_t *list, token_t *token); |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 95 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 96 | static void |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 97 | _token_list_append_list (token_list_t *list, token_list_t *tail); |
| 98 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 99 | static void |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 100 | _glcpp_parser_evaluate_defined (glcpp_parser_t *parser, |
| 101 | token_list_t *list); |
| 102 | |
| 103 | static void |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 104 | _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser, |
| 105 | token_list_t *list); |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 106 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 107 | static void |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 108 | _glcpp_parser_expand_token_list_onto (glcpp_parser_t *parser, |
| 109 | token_list_t *list, |
| 110 | token_list_t *result); |
| 111 | |
| 112 | static void |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 113 | _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition); |
| 114 | |
| 115 | static void |
| 116 | _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type, |
| 117 | int condition); |
Carl Worth | 80dc60b | 2010-05-25 14:42:00 -0700 | [diff] [blame] | 118 | |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 119 | static void |
| 120 | _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser); |
| 121 | |
Carl Worth | 0293b2e | 2010-05-19 10:05:40 -0700 | [diff] [blame] | 122 | #define yylex glcpp_parser_lex |
| 123 | |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 124 | static int |
Carl Worth | 0293b2e | 2010-05-19 10:05:40 -0700 | [diff] [blame] | 125 | glcpp_parser_lex (glcpp_parser_t *parser); |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 126 | |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 127 | static void |
| 128 | glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list); |
| 129 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 130 | %} |
| 131 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 132 | %parse-param {glcpp_parser_t *parser} |
Carl Worth | 0293b2e | 2010-05-19 10:05:40 -0700 | [diff] [blame] | 133 | %lex-param {glcpp_parser_t *parser} |
Carl Worth | 38aa835 | 2010-05-10 11:52:29 -0700 | [diff] [blame] | 134 | |
Carl Worth | dd74900 | 2010-05-27 10:12:33 -0700 | [diff] [blame] | 135 | %token COMMA_FINAL DEFINED ELIF_EXPANDED HASH HASH_DEFINE_FUNC HASH_DEFINE_OBJ HASH_ELIF HASH_ELSE HASH_ENDIF HASH_IF HASH_IFDEF HASH_IFNDEF HASH_UNDEF IDENTIFIER IF_EXPANDED INTEGER NEWLINE OTHER SPACE |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 136 | %token PASTE |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 137 | %type <ival> expression INTEGER operator SPACE |
Carl Worth | e939786 | 2010-05-25 17:08:07 -0700 | [diff] [blame] | 138 | %type <str> IDENTIFIER OTHER |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 139 | %type <string_list> identifier_list |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 140 | %type <token> preprocessing_token |
| 141 | %type <token_list> pp_tokens replacement_list text_line |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 142 | %left OR |
| 143 | %left AND |
| 144 | %left '|' |
| 145 | %left '^' |
| 146 | %left '&' |
| 147 | %left EQUAL NOT_EQUAL |
| 148 | %left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL |
| 149 | %left LEFT_SHIFT RIGHT_SHIFT |
| 150 | %left '+' '-' |
| 151 | %left '*' '/' '%' |
| 152 | %right UNARY |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 153 | |
| 154 | %% |
| 155 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 156 | input: |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 157 | /* empty */ |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 158 | | input line |
| 159 | ; |
| 160 | |
| 161 | line: |
| 162 | control_line { |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 163 | if (parser->skip_stack == NULL || |
| 164 | parser->skip_stack->type == SKIP_NO_SKIP) |
| 165 | { |
| 166 | printf ("\n"); |
| 167 | } |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 168 | } |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 169 | | text_line { |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 170 | if (parser->skip_stack == NULL || |
| 171 | parser->skip_stack->type == SKIP_NO_SKIP) |
| 172 | { |
| 173 | _glcpp_parser_print_expanded_token_list (parser, $1); |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 174 | printf ("\n"); |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 175 | } |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 176 | talloc_free ($1); |
| 177 | } |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 178 | | expanded_line |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 179 | | HASH non_directive |
Carl Worth | cd27e64 | 2010-05-12 13:11:50 -0700 | [diff] [blame] | 180 | ; |
| 181 | |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 182 | expanded_line: |
| 183 | IF_EXPANDED expression NEWLINE { |
| 184 | _glcpp_parser_skip_stack_push_if (parser, $2); |
| 185 | } |
| 186 | | ELIF_EXPANDED expression NEWLINE { |
| 187 | _glcpp_parser_skip_stack_change_if (parser, "elif", $2); |
| 188 | } |
| 189 | ; |
| 190 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 191 | control_line: |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 192 | HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE { |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 193 | _define_object_macro (parser, $2, $3); |
| 194 | } |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 195 | | HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE { |
| 196 | _define_function_macro (parser, $2, NULL, $5); |
| 197 | } |
| 198 | | HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE { |
| 199 | _define_function_macro (parser, $2, $4, $6); |
| 200 | } |
Carl Worth | e6fb782 | 2010-05-25 15:28:58 -0700 | [diff] [blame] | 201 | | HASH_UNDEF IDENTIFIER NEWLINE { |
Carl Worth | 0324cad | 2010-05-26 15:53:05 -0700 | [diff] [blame] | 202 | macro_t *macro = hash_table_find (parser->defines, $2); |
Carl Worth | e6fb782 | 2010-05-25 15:28:58 -0700 | [diff] [blame] | 203 | if (macro) { |
| 204 | /* XXX: Need hash table to support a real way |
| 205 | * to remove an element rather than prefixing |
| 206 | * a new node with data of NULL like this. */ |
| 207 | hash_table_insert (parser->defines, NULL, $2); |
| 208 | talloc_free (macro); |
| 209 | } |
| 210 | talloc_free ($2); |
| 211 | } |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 212 | | HASH_IF pp_tokens NEWLINE { |
| 213 | token_list_t *expanded; |
| 214 | token_t *token; |
| 215 | |
| 216 | expanded = _token_list_create (parser); |
| 217 | token = _token_create_ival (parser, IF_EXPANDED, IF_EXPANDED); |
| 218 | _token_list_append (expanded, token); |
| 219 | talloc_unlink (parser, token); |
| 220 | _glcpp_parser_evaluate_defined (parser, $2); |
| 221 | _glcpp_parser_expand_token_list_onto (parser, $2, expanded); |
| 222 | glcpp_parser_lex_from (parser, expanded); |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 223 | } |
| 224 | | HASH_IFDEF IDENTIFIER NEWLINE { |
Carl Worth | 0324cad | 2010-05-26 15:53:05 -0700 | [diff] [blame] | 225 | macro_t *macro = hash_table_find (parser->defines, $2); |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 226 | talloc_free ($2); |
| 227 | _glcpp_parser_skip_stack_push_if (parser, macro != NULL); |
| 228 | } |
| 229 | | HASH_IFNDEF IDENTIFIER NEWLINE { |
Carl Worth | 0324cad | 2010-05-26 15:53:05 -0700 | [diff] [blame] | 230 | macro_t *macro = hash_table_find (parser->defines, $2); |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 231 | talloc_free ($2); |
| 232 | _glcpp_parser_skip_stack_push_if (parser, macro == NULL); |
| 233 | } |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 234 | | HASH_ELIF pp_tokens NEWLINE { |
| 235 | token_list_t *expanded; |
| 236 | token_t *token; |
| 237 | |
| 238 | expanded = _token_list_create (parser); |
| 239 | token = _token_create_ival (parser, ELIF_EXPANDED, ELIF_EXPANDED); |
| 240 | _token_list_append (expanded, token); |
| 241 | talloc_unlink (parser, token); |
| 242 | _glcpp_parser_evaluate_defined (parser, $2); |
| 243 | _glcpp_parser_expand_token_list_onto (parser, $2, expanded); |
| 244 | glcpp_parser_lex_from (parser, expanded); |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 245 | } |
| 246 | | HASH_ELSE NEWLINE { |
| 247 | _glcpp_parser_skip_stack_change_if (parser, "else", 1); |
| 248 | } |
| 249 | | HASH_ENDIF NEWLINE { |
| 250 | _glcpp_parser_skip_stack_pop (parser); |
| 251 | } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 252 | | HASH NEWLINE |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 253 | ; |
| 254 | |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 255 | expression: |
| 256 | INTEGER { |
| 257 | $$ = $1; |
| 258 | } |
| 259 | | expression OR expression { |
| 260 | $$ = $1 || $3; |
| 261 | } |
| 262 | | expression AND expression { |
| 263 | $$ = $1 && $3; |
| 264 | } |
| 265 | | expression '|' expression { |
| 266 | $$ = $1 | $3; |
| 267 | } |
| 268 | | expression '^' expression { |
| 269 | $$ = $1 ^ $3; |
| 270 | } |
| 271 | | expression '&' expression { |
| 272 | $$ = $1 & $3; |
| 273 | } |
| 274 | | expression NOT_EQUAL expression { |
| 275 | $$ = $1 != $3; |
| 276 | } |
| 277 | | expression EQUAL expression { |
| 278 | $$ = $1 == $3; |
| 279 | } |
| 280 | | expression GREATER_OR_EQUAL expression { |
| 281 | $$ = $1 >= $3; |
| 282 | } |
| 283 | | expression LESS_OR_EQUAL expression { |
| 284 | $$ = $1 <= $3; |
| 285 | } |
| 286 | | expression '>' expression { |
| 287 | $$ = $1 > $3; |
| 288 | } |
| 289 | | expression '<' expression { |
| 290 | $$ = $1 < $3; |
| 291 | } |
| 292 | | expression RIGHT_SHIFT expression { |
| 293 | $$ = $1 >> $3; |
| 294 | } |
| 295 | | expression LEFT_SHIFT expression { |
| 296 | $$ = $1 << $3; |
| 297 | } |
| 298 | | expression '-' expression { |
| 299 | $$ = $1 - $3; |
| 300 | } |
| 301 | | expression '+' expression { |
| 302 | $$ = $1 + $3; |
| 303 | } |
| 304 | | expression '%' expression { |
| 305 | $$ = $1 % $3; |
| 306 | } |
| 307 | | expression '/' expression { |
| 308 | $$ = $1 / $3; |
| 309 | } |
| 310 | | expression '*' expression { |
| 311 | $$ = $1 * $3; |
| 312 | } |
| 313 | | '!' expression %prec UNARY { |
| 314 | $$ = ! $2; |
| 315 | } |
| 316 | | '~' expression %prec UNARY { |
| 317 | $$ = ~ $2; |
| 318 | } |
| 319 | | '-' expression %prec UNARY { |
| 320 | $$ = - $2; |
| 321 | } |
| 322 | | '+' expression %prec UNARY { |
| 323 | $$ = + $2; |
| 324 | } |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 325 | | '(' expression ')' { |
| 326 | $$ = $2; |
| 327 | } |
| 328 | ; |
| 329 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 330 | identifier_list: |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 331 | IDENTIFIER { |
| 332 | $$ = _string_list_create (parser); |
| 333 | _string_list_append_item ($$, $1); |
| 334 | talloc_steal ($$, $1); |
| 335 | } |
| 336 | | identifier_list ',' IDENTIFIER { |
| 337 | $$ = $1; |
| 338 | _string_list_append_item ($$, $3); |
| 339 | talloc_steal ($$, $3); |
| 340 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 341 | ; |
| 342 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 343 | text_line: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 344 | NEWLINE { $$ = NULL; } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 345 | | pp_tokens NEWLINE |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 346 | ; |
| 347 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 348 | non_directive: |
| 349 | pp_tokens NEWLINE |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 350 | ; |
| 351 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 352 | replacement_list: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 353 | /* empty */ { $$ = NULL; } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 354 | | pp_tokens |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 355 | ; |
| 356 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 357 | pp_tokens: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 358 | preprocessing_token { |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 359 | parser->space_tokens = 1; |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 360 | $$ = _token_list_create (parser); |
| 361 | _token_list_append ($$, $1); |
| 362 | talloc_unlink (parser, $1); |
| 363 | } |
| 364 | | pp_tokens preprocessing_token { |
| 365 | $$ = $1; |
| 366 | _token_list_append ($$, $2); |
| 367 | talloc_unlink (parser, $2); |
| 368 | } |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 369 | ; |
| 370 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 371 | preprocessing_token: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 372 | IDENTIFIER { |
| 373 | $$ = _token_create_str (parser, IDENTIFIER, $1); |
| 374 | } |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 375 | | INTEGER { |
| 376 | $$ = _token_create_ival (parser, INTEGER, $1); |
| 377 | } |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 378 | | operator { |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 379 | $$ = _token_create_ival (parser, $1, $1); |
| 380 | } |
| 381 | | OTHER { |
| 382 | $$ = _token_create_str (parser, OTHER, $1); |
| 383 | } |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 384 | | SPACE { |
Carl Worth | e939786 | 2010-05-25 17:08:07 -0700 | [diff] [blame] | 385 | $$ = _token_create_ival (parser, SPACE, SPACE); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 386 | } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 387 | ; |
| 388 | |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 389 | operator: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 390 | '[' { $$ = '['; } |
| 391 | | ']' { $$ = ']'; } |
| 392 | | '(' { $$ = '('; } |
| 393 | | ')' { $$ = ')'; } |
| 394 | | '{' { $$ = '{'; } |
| 395 | | '}' { $$ = '}'; } |
| 396 | | '.' { $$ = '.'; } |
| 397 | | '&' { $$ = '&'; } |
| 398 | | '*' { $$ = '*'; } |
| 399 | | '+' { $$ = '+'; } |
| 400 | | '-' { $$ = '-'; } |
| 401 | | '~' { $$ = '~'; } |
| 402 | | '!' { $$ = '!'; } |
| 403 | | '/' { $$ = '/'; } |
| 404 | | '%' { $$ = '%'; } |
| 405 | | LEFT_SHIFT { $$ = LEFT_SHIFT; } |
| 406 | | RIGHT_SHIFT { $$ = RIGHT_SHIFT; } |
| 407 | | '<' { $$ = '<'; } |
| 408 | | '>' { $$ = '>'; } |
| 409 | | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; } |
| 410 | | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; } |
| 411 | | EQUAL { $$ = EQUAL; } |
| 412 | | NOT_EQUAL { $$ = NOT_EQUAL; } |
| 413 | | '^' { $$ = '^'; } |
| 414 | | '|' { $$ = '|'; } |
| 415 | | AND { $$ = AND; } |
| 416 | | OR { $$ = OR; } |
| 417 | | ';' { $$ = ';'; } |
| 418 | | ',' { $$ = ','; } |
| 419 | | PASTE { $$ = PASTE; } |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 420 | | DEFINED { $$ = DEFINED; } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 421 | ; |
| 422 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 423 | %% |
| 424 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 425 | string_list_t * |
| 426 | _string_list_create (void *ctx) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 427 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 428 | string_list_t *list; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 429 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 430 | list = xtalloc (ctx, string_list_t); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 431 | list->head = NULL; |
| 432 | list->tail = NULL; |
| 433 | |
| 434 | return list; |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 435 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 436 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 437 | void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 438 | _string_list_append_list (string_list_t *list, string_list_t *tail) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 439 | { |
| 440 | if (list->head == NULL) { |
| 441 | list->head = tail->head; |
| 442 | } else { |
| 443 | list->tail->next = tail->head; |
| 444 | } |
| 445 | |
| 446 | list->tail = tail->tail; |
| 447 | } |
| 448 | |
| 449 | void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 450 | _string_list_append_item (string_list_t *list, const char *str) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 451 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 452 | string_node_t *node; |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 453 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 454 | node = xtalloc (list, string_node_t); |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 455 | node->str = xtalloc_strdup (node, str); |
Carl Worth | 80dc60b | 2010-05-25 14:42:00 -0700 | [diff] [blame] | 456 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 457 | node->next = NULL; |
| 458 | |
| 459 | if (list->head == NULL) { |
| 460 | list->head = node; |
| 461 | } else { |
| 462 | list->tail->next = node; |
| 463 | } |
| 464 | |
| 465 | list->tail = node; |
| 466 | } |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 467 | |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 468 | void |
| 469 | _string_list_push (string_list_t *list, const char *str) |
| 470 | { |
| 471 | string_node_t *node; |
| 472 | |
| 473 | node = xtalloc (list, string_node_t); |
| 474 | node->str = xtalloc_strdup (node, str); |
| 475 | node->next = list->head; |
| 476 | |
| 477 | if (list->tail == NULL) { |
| 478 | list->tail = node; |
| 479 | } |
| 480 | list->head = node; |
| 481 | } |
| 482 | |
| 483 | void |
| 484 | _string_list_pop (string_list_t *list) |
| 485 | { |
| 486 | string_node_t *node; |
| 487 | |
| 488 | node = list->head; |
| 489 | |
| 490 | if (node == NULL) { |
| 491 | fprintf (stderr, "Internal error: _string_list_pop called on an empty list.\n"); |
| 492 | exit (1); |
| 493 | } |
| 494 | |
| 495 | list->head = node->next; |
| 496 | if (list->tail == node) { |
| 497 | assert (node->next == NULL); |
| 498 | list->tail = NULL; |
| 499 | } |
| 500 | |
| 501 | talloc_free (node); |
| 502 | } |
| 503 | |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 504 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 505 | _string_list_contains (string_list_t *list, const char *member, int *index) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 506 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 507 | string_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 508 | int i; |
| 509 | |
| 510 | if (list == NULL) |
| 511 | return 0; |
| 512 | |
| 513 | for (i = 0, node = list->head; node; i++, node = node->next) { |
| 514 | if (strcmp (node->str, member) == 0) { |
Carl Worth | 420d05a | 2010-05-17 10:15:23 -0700 | [diff] [blame] | 515 | if (index) |
| 516 | *index = i; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 517 | return 1; |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 525 | _string_list_length (string_list_t *list) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 526 | { |
| 527 | int length = 0; |
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 | |
| 530 | if (list == NULL) |
| 531 | return 0; |
| 532 | |
| 533 | for (node = list->head; node; node = node->next) |
| 534 | length++; |
| 535 | |
| 536 | return length; |
| 537 | } |
| 538 | |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 539 | argument_list_t * |
| 540 | _argument_list_create (void *ctx) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 541 | { |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 542 | argument_list_t *list; |
| 543 | |
| 544 | list = xtalloc (ctx, argument_list_t); |
| 545 | list->head = NULL; |
| 546 | list->tail = NULL; |
| 547 | |
| 548 | return list; |
| 549 | } |
| 550 | |
| 551 | void |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 552 | _argument_list_append (argument_list_t *list, token_list_t *argument) |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 553 | { |
| 554 | argument_node_t *node; |
| 555 | |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 556 | node = xtalloc (list, argument_node_t); |
| 557 | node->argument = argument; |
| 558 | |
| 559 | node->next = NULL; |
| 560 | |
| 561 | if (list->head == NULL) { |
| 562 | list->head = node; |
| 563 | } else { |
| 564 | list->tail->next = node; |
| 565 | } |
| 566 | |
| 567 | list->tail = node; |
| 568 | } |
| 569 | |
| 570 | int |
| 571 | _argument_list_length (argument_list_t *list) |
| 572 | { |
| 573 | int length = 0; |
| 574 | argument_node_t *node; |
| 575 | |
| 576 | if (list == NULL) |
| 577 | return 0; |
| 578 | |
| 579 | for (node = list->head; node; node = node->next) |
| 580 | length++; |
| 581 | |
| 582 | return length; |
| 583 | } |
| 584 | |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 585 | token_list_t * |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 586 | _argument_list_member_at (argument_list_t *list, int index) |
| 587 | { |
| 588 | argument_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 589 | int i; |
| 590 | |
| 591 | if (list == NULL) |
| 592 | return NULL; |
| 593 | |
| 594 | node = list->head; |
| 595 | for (i = 0; i < index; i++) { |
| 596 | node = node->next; |
| 597 | if (node == NULL) |
| 598 | break; |
| 599 | } |
| 600 | |
| 601 | if (node) |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 602 | return node->argument; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 603 | |
| 604 | return NULL; |
| 605 | } |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 606 | |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 607 | /* Note: This function talloc_steal()s the str pointer. */ |
| 608 | token_t * |
| 609 | _token_create_str (void *ctx, int type, char *str) |
| 610 | { |
| 611 | token_t *token; |
| 612 | |
| 613 | token = xtalloc (ctx, token_t); |
| 614 | token->type = type; |
| 615 | token->value.str = talloc_steal (token, str); |
| 616 | |
| 617 | return token; |
| 618 | } |
| 619 | |
| 620 | token_t * |
| 621 | _token_create_ival (void *ctx, int type, int ival) |
| 622 | { |
| 623 | token_t *token; |
| 624 | |
| 625 | token = xtalloc (ctx, token_t); |
| 626 | token->type = type; |
| 627 | token->value.ival = ival; |
| 628 | |
| 629 | return token; |
| 630 | } |
| 631 | |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 632 | token_list_t * |
| 633 | _token_list_create (void *ctx) |
| 634 | { |
| 635 | token_list_t *list; |
| 636 | |
| 637 | list = xtalloc (ctx, token_list_t); |
| 638 | list->head = NULL; |
| 639 | list->tail = NULL; |
Carl Worth | 10ae438 | 2010-05-25 20:35:01 -0700 | [diff] [blame] | 640 | list->non_space_tail = NULL; |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 641 | |
| 642 | return list; |
| 643 | } |
| 644 | |
| 645 | void |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 646 | _token_list_append (token_list_t *list, token_t *token) |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 647 | { |
| 648 | token_node_t *node; |
| 649 | |
| 650 | node = xtalloc (list, token_node_t); |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 651 | node->token = xtalloc_reference (list, token); |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 652 | |
| 653 | node->next = NULL; |
| 654 | |
| 655 | if (list->head == NULL) { |
| 656 | list->head = node; |
| 657 | } else { |
| 658 | list->tail->next = node; |
| 659 | } |
| 660 | |
| 661 | list->tail = node; |
Carl Worth | 10ae438 | 2010-05-25 20:35:01 -0700 | [diff] [blame] | 662 | if (token->type != SPACE) |
| 663 | list->non_space_tail = node; |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | void |
| 667 | _token_list_append_list (token_list_t *list, token_list_t *tail) |
| 668 | { |
Carl Worth | a65cf7b | 2010-05-27 11:55:36 -0700 | [diff] [blame] | 669 | if (tail == NULL || tail->head == NULL) |
| 670 | return; |
| 671 | |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 672 | if (list->head == NULL) { |
| 673 | list->head = tail->head; |
| 674 | } else { |
| 675 | list->tail->next = tail->head; |
| 676 | } |
| 677 | |
| 678 | list->tail = tail->tail; |
Carl Worth | 10ae438 | 2010-05-25 20:35:01 -0700 | [diff] [blame] | 679 | list->non_space_tail = tail->non_space_tail; |
| 680 | } |
| 681 | |
| 682 | void |
| 683 | _token_list_trim_trailing_space (token_list_t *list) |
| 684 | { |
| 685 | token_node_t *tail, *next; |
| 686 | |
| 687 | if (list->non_space_tail) { |
| 688 | tail = list->non_space_tail->next; |
| 689 | list->non_space_tail->next = NULL; |
| 690 | list->tail = list->non_space_tail; |
| 691 | |
| 692 | while (tail) { |
| 693 | next = tail->next; |
| 694 | talloc_free (tail); |
| 695 | tail = next; |
| 696 | } |
| 697 | } |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 698 | } |
Carl Worth | 80dc60b | 2010-05-25 14:42:00 -0700 | [diff] [blame] | 699 | |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 700 | static void |
| 701 | _token_print (token_t *token) |
| 702 | { |
| 703 | if (token->type < 256) { |
| 704 | printf ("%c", token->type); |
| 705 | return; |
| 706 | } |
| 707 | |
| 708 | switch (token->type) { |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 709 | case INTEGER: |
| 710 | printf ("%" PRIxMAX, token->value.ival); |
| 711 | break; |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 712 | case IDENTIFIER: |
| 713 | case OTHER: |
| 714 | printf ("%s", token->value.str); |
| 715 | break; |
| 716 | case SPACE: |
| 717 | printf (" "); |
| 718 | break; |
| 719 | case LEFT_SHIFT: |
| 720 | printf ("<<"); |
| 721 | break; |
| 722 | case RIGHT_SHIFT: |
| 723 | printf (">>"); |
| 724 | break; |
| 725 | case LESS_OR_EQUAL: |
| 726 | printf ("<="); |
| 727 | break; |
| 728 | case GREATER_OR_EQUAL: |
| 729 | printf (">="); |
| 730 | break; |
| 731 | case EQUAL: |
| 732 | printf ("=="); |
| 733 | break; |
| 734 | case NOT_EQUAL: |
| 735 | printf ("!="); |
| 736 | break; |
| 737 | case AND: |
| 738 | printf ("&&"); |
| 739 | break; |
| 740 | case OR: |
| 741 | printf ("||"); |
| 742 | break; |
| 743 | case PASTE: |
| 744 | printf ("##"); |
| 745 | break; |
Carl Worth | dd74900 | 2010-05-27 10:12:33 -0700 | [diff] [blame] | 746 | case COMMA_FINAL: |
| 747 | printf (","); |
| 748 | break; |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 749 | default: |
| 750 | fprintf (stderr, "Error: Don't know how to print token type %d\n", token->type); |
| 751 | break; |
| 752 | } |
| 753 | } |
| 754 | |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 755 | /* Change 'token' into a new token formed by pasting 'other'. */ |
| 756 | static void |
| 757 | _token_paste (token_t *token, token_t *other) |
| 758 | { |
| 759 | /* A very few single-character punctuators can be combined |
| 760 | * with another to form a multi-character punctuator. */ |
| 761 | switch (token->type) { |
| 762 | case '<': |
| 763 | if (other->type == '<') { |
| 764 | token->type = LEFT_SHIFT; |
| 765 | token->value.ival = LEFT_SHIFT; |
| 766 | return; |
| 767 | } else if (other->type == '=') { |
| 768 | token->type = LESS_OR_EQUAL; |
| 769 | token->value.ival = LESS_OR_EQUAL; |
| 770 | return; |
| 771 | } |
| 772 | break; |
| 773 | case '>': |
| 774 | if (other->type == '>') { |
| 775 | token->type = RIGHT_SHIFT; |
| 776 | token->value.ival = RIGHT_SHIFT; |
| 777 | return; |
| 778 | } else if (other->type == '=') { |
| 779 | token->type = GREATER_OR_EQUAL; |
| 780 | token->value.ival = GREATER_OR_EQUAL; |
| 781 | return; |
| 782 | } |
| 783 | break; |
| 784 | case '=': |
| 785 | if (other->type == '=') { |
| 786 | token->type = EQUAL; |
| 787 | token->value.ival = EQUAL; |
| 788 | return; |
| 789 | } |
| 790 | break; |
| 791 | case '!': |
| 792 | if (other->type == '=') { |
| 793 | token->type = NOT_EQUAL; |
| 794 | token->value.ival = NOT_EQUAL; |
| 795 | return; |
| 796 | } |
| 797 | break; |
| 798 | case '&': |
| 799 | if (other->type == '&') { |
| 800 | token->type = AND; |
| 801 | token->value.ival = AND; |
| 802 | return; |
| 803 | } |
| 804 | break; |
| 805 | case '|': |
| 806 | if (other->type == '|') { |
| 807 | token->type = OR; |
| 808 | token->value.ival = OR; |
| 809 | return; |
| 810 | } |
| 811 | break; |
| 812 | } |
| 813 | |
| 814 | /* Two string-valued tokens can usually just be mashed |
| 815 | * together. |
| 816 | * |
| 817 | * XXX: Since our 'OTHER' case is currently so loose, this may |
| 818 | * allow some things thruogh that should be treated as |
| 819 | * errors. */ |
| 820 | if ((token->type == IDENTIFIER || token->type == OTHER) && |
| 821 | (other->type == IDENTIFIER || other->type == OTHER)) |
| 822 | { |
| 823 | token->value.str = talloc_strdup_append (token->value.str, |
| 824 | other->value.str); |
| 825 | return; |
| 826 | } |
| 827 | |
| 828 | printf ("Error: Pasting \""); |
| 829 | _token_print (token); |
| 830 | printf ("\" and \""); |
| 831 | _token_print (other); |
| 832 | printf ("\" does not give a valid preprocessing token.\n"); |
| 833 | } |
| 834 | |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 835 | static void |
| 836 | _token_list_print (token_list_t *list) |
| 837 | { |
| 838 | token_node_t *node; |
| 839 | |
| 840 | if (list == NULL) |
| 841 | return; |
| 842 | |
| 843 | for (node = list->head; node; node = node->next) |
| 844 | _token_print (node->token); |
| 845 | } |
| 846 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 847 | void |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 848 | yyerror (void *scanner, const char *error) |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 849 | { |
| 850 | fprintf (stderr, "Parse error: %s\n", error); |
| 851 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 852 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 853 | glcpp_parser_t * |
| 854 | glcpp_parser_create (void) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 855 | { |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 856 | glcpp_parser_t *parser; |
| 857 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 858 | parser = xtalloc (NULL, glcpp_parser_t); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 859 | |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 860 | glcpp_lex_init_extra (parser, &parser->scanner); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 861 | parser->defines = hash_table_ctor (32, hash_table_string_hash, |
| 862 | hash_table_string_compare); |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 863 | parser->active = _string_list_create (parser); |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 864 | parser->space_tokens = 1; |
Carl Worth | 95951ea | 2010-05-26 15:57:10 -0700 | [diff] [blame] | 865 | parser->newline_as_space = 0; |
| 866 | parser->in_control_line = 0; |
| 867 | parser->paren_count = 0; |
Carl Worth | 5a6b9a2 | 2010-05-20 14:29:43 -0700 | [diff] [blame] | 868 | |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 869 | parser->skip_stack = NULL; |
| 870 | |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 871 | parser->lex_from_list = NULL; |
| 872 | parser->lex_from_node = NULL; |
| 873 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 874 | return parser; |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | int |
| 878 | glcpp_parser_parse (glcpp_parser_t *parser) |
| 879 | { |
| 880 | return yyparse (parser); |
| 881 | } |
| 882 | |
| 883 | void |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 884 | glcpp_parser_destroy (glcpp_parser_t *parser) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 885 | { |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 886 | if (parser->skip_stack) |
| 887 | fprintf (stderr, "Error: Unterminated #if\n"); |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 888 | glcpp_lex_destroy (parser->scanner); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 889 | hash_table_dtor (parser->defines); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 890 | talloc_free (parser); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 891 | } |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 892 | |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 893 | /* Replace any occurences of DEFINED tokens in 'list' with either a |
| 894 | * '0' or '1' INTEGER token depending on whether the next token in the |
| 895 | * list is defined or not. */ |
| 896 | static void |
| 897 | _glcpp_parser_evaluate_defined (glcpp_parser_t *parser, |
| 898 | token_list_t *list) |
| 899 | { |
| 900 | token_node_t *node, *next; |
Carl Worth | 0324cad | 2010-05-26 15:53:05 -0700 | [diff] [blame] | 901 | macro_t *macro; |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 902 | |
| 903 | if (list == NULL) |
| 904 | return; |
| 905 | |
| 906 | for (node = list->head; node; node = node->next) { |
| 907 | if (node->token->type != DEFINED) |
| 908 | continue; |
| 909 | next = node->next; |
| 910 | while (next && next->token->type == SPACE) |
| 911 | next = next->next; |
| 912 | if (next == NULL || next->token->type != IDENTIFIER) { |
| 913 | fprintf (stderr, "Error: operator \"defined\" requires an identifier\n"); |
| 914 | exit (1); |
| 915 | } |
| 916 | macro = hash_table_find (parser->defines, |
| 917 | next->token->value.str); |
| 918 | |
| 919 | node->token->type = INTEGER; |
| 920 | node->token->value.ival = (macro != NULL); |
| 921 | node->next = next->next; |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 926 | /* Appends onto 'expansion' a non-macro token or the expansion of an |
| 927 | * object-like macro. |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 928 | * |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 929 | * Returns 0 if this token is completely processed. |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 930 | * |
| 931 | * Returns 1 in the case that 'token' is a function-like macro that |
| 932 | * needs further expansion. |
| 933 | */ |
| 934 | static int |
Carl Worth | 7db2402 | 2010-05-26 17:01:57 -0700 | [diff] [blame] | 935 | _expand_token_onto (glcpp_parser_t *parser, |
| 936 | token_t *token, |
| 937 | token_list_t *result) |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 938 | { |
| 939 | const char *identifier; |
| 940 | macro_t *macro; |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 941 | token_list_t *expansion; |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 942 | |
| 943 | /* We only expand identifiers */ |
| 944 | if (token->type != IDENTIFIER) { |
Carl Worth | dd74900 | 2010-05-27 10:12:33 -0700 | [diff] [blame] | 945 | /* We change any COMMA into a COMMA_FINAL to prevent |
| 946 | * it being mistaken for an argument separator |
| 947 | * later. */ |
| 948 | if (token->type == ',') { |
| 949 | token_t *new_token; |
| 950 | |
| 951 | new_token = _token_create_ival (result, COMMA_FINAL, |
| 952 | COMMA_FINAL); |
| 953 | _token_list_append (result, new_token); |
| 954 | } else { |
| 955 | _token_list_append (result, token); |
| 956 | } |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 957 | return 0; |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | /* Look up this identifier in the hash table. */ |
| 961 | identifier = token->value.str; |
| 962 | macro = hash_table_find (parser->defines, identifier); |
| 963 | |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 964 | /* Not a macro, so just append. */ |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 965 | if (macro == NULL) { |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 966 | _token_list_append (result, token); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 967 | return 0; |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 968 | } |
| 969 | |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 970 | /* Finally, don't expand this macro if we're already actively |
| 971 | * expanding it, (to avoid infinite recursion). */ |
Carl Worth | ec4ada0 | 2010-05-26 08:15:49 -0700 | [diff] [blame] | 972 | if (_string_list_contains (parser->active, identifier, NULL)) |
| 973 | { |
| 974 | /* We change the token type here from IDENTIFIER to |
| 975 | * OTHER to prevent any future expansion of this |
| 976 | * unexpanded token. */ |
| 977 | char *str; |
| 978 | token_t *new_token; |
| 979 | |
| 980 | str = xtalloc_strdup (result, token->value.str); |
| 981 | new_token = _token_create_str (result, OTHER, str); |
| 982 | _token_list_append (result, new_token); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 983 | return 0; |
| 984 | } |
| 985 | |
Carl Worth | c0607d5 | 2010-05-26 08:01:42 -0700 | [diff] [blame] | 986 | /* For function-like macros return 1 for further processing. */ |
| 987 | if (macro->is_function) { |
| 988 | return 1; |
| 989 | } |
| 990 | |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 991 | _string_list_push (parser->active, identifier); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 992 | _glcpp_parser_expand_token_list_onto (parser, |
| 993 | macro->replacements, |
| 994 | result); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 995 | _string_list_pop (parser->active); |
| 996 | |
| 997 | return 0; |
| 998 | } |
| 999 | |
| 1000 | typedef enum function_status |
| 1001 | { |
| 1002 | FUNCTION_STATUS_SUCCESS, |
| 1003 | FUNCTION_NOT_A_FUNCTION, |
| 1004 | FUNCTION_UNBALANCED_PARENTHESES |
| 1005 | } function_status_t; |
| 1006 | |
| 1007 | /* Find a set of function-like macro arguments by looking for a |
| 1008 | * balanced set of parentheses. Upon return *node will be the last |
| 1009 | * consumed node, such that further processing can continue with |
| 1010 | * node->next. |
| 1011 | * |
| 1012 | * Return values: |
| 1013 | * |
| 1014 | * FUNCTION_STATUS_SUCCESS: |
| 1015 | * |
| 1016 | * Successfully parsed a set of function arguments. |
| 1017 | * |
| 1018 | * FUNCTION_NOT_A_FUNCTION: |
| 1019 | * |
| 1020 | * Macro name not followed by a '('. This is not an error, but |
| 1021 | * simply that the macro name should be treated as a non-macro. |
| 1022 | * |
| 1023 | * FUNCTION_UNBLANCED_PARENTHESES |
| 1024 | * |
| 1025 | * Macro name is not followed by a balanced set of parentheses. |
| 1026 | */ |
| 1027 | static function_status_t |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1028 | _arguments_parse (argument_list_t *arguments, token_node_t **node_ret) |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1029 | { |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1030 | token_list_t *argument; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1031 | token_node_t *node = *node_ret, *last; |
| 1032 | int paren_count; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1033 | |
| 1034 | last = node; |
| 1035 | node = node->next; |
| 1036 | |
| 1037 | /* Ignore whitespace before first parenthesis. */ |
| 1038 | while (node && node->token->type == SPACE) |
| 1039 | node = node->next; |
| 1040 | |
| 1041 | if (node == NULL || node->token->type != '(') |
| 1042 | return FUNCTION_NOT_A_FUNCTION; |
| 1043 | |
Carl Worth | 652fa27 | 2010-05-25 17:45:22 -0700 | [diff] [blame] | 1044 | last = node; |
| 1045 | node = node->next; |
| 1046 | |
Carl Worth | a19297b | 2010-05-27 13:29:19 -0700 | [diff] [blame^] | 1047 | argument = _token_list_create (arguments); |
| 1048 | _argument_list_append (arguments, argument); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1049 | |
Carl Worth | 652fa27 | 2010-05-25 17:45:22 -0700 | [diff] [blame] | 1050 | for (paren_count = 1; node; last = node, node = node->next) { |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1051 | if (node->token->type == '(') |
| 1052 | { |
| 1053 | paren_count++; |
| 1054 | } |
| 1055 | else if (node->token->type == ')') |
| 1056 | { |
| 1057 | paren_count--; |
Carl Worth | c7581c2 | 2010-05-25 17:41:07 -0700 | [diff] [blame] | 1058 | if (paren_count == 0) { |
| 1059 | last = node; |
| 1060 | node = node->next; |
| 1061 | break; |
| 1062 | } |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1063 | } |
Carl Worth | 652fa27 | 2010-05-25 17:45:22 -0700 | [diff] [blame] | 1064 | |
| 1065 | if (node->token->type == ',' && |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1066 | paren_count == 1) |
| 1067 | { |
Carl Worth | a19297b | 2010-05-27 13:29:19 -0700 | [diff] [blame^] | 1068 | _token_list_trim_trailing_space (argument); |
| 1069 | argument = _token_list_create (arguments); |
| 1070 | _argument_list_append (arguments, argument); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1071 | } |
| 1072 | else { |
Carl Worth | a19297b | 2010-05-27 13:29:19 -0700 | [diff] [blame^] | 1073 | if (argument->head == NULL) { |
Carl Worth | c7581c2 | 2010-05-25 17:41:07 -0700 | [diff] [blame] | 1074 | /* Don't treat initial whitespace as |
| 1075 | * part of the arguement. */ |
| 1076 | if (node->token->type == SPACE) |
| 1077 | continue; |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1078 | } |
| 1079 | _token_list_append (argument, node->token); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1080 | } |
Carl Worth | c7581c2 | 2010-05-25 17:41:07 -0700 | [diff] [blame] | 1081 | } |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1082 | |
| 1083 | if (node && paren_count) |
| 1084 | return FUNCTION_UNBALANCED_PARENTHESES; |
| 1085 | |
| 1086 | *node_ret = last; |
| 1087 | |
| 1088 | return FUNCTION_STATUS_SUCCESS; |
| 1089 | } |
| 1090 | |
| 1091 | /* Prints the expansion of *node (consuming further tokens from the |
| 1092 | * list as necessary). Upon return *node will be the last consumed |
| 1093 | * node, such that further processing can continue with node->next. */ |
Carl Worth | 7db2402 | 2010-05-26 17:01:57 -0700 | [diff] [blame] | 1094 | static function_status_t |
| 1095 | _expand_function_onto (glcpp_parser_t *parser, |
| 1096 | token_node_t **node_ret, |
| 1097 | token_list_t *result) |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1098 | { |
| 1099 | macro_t *macro; |
| 1100 | token_node_t *node; |
| 1101 | const char *identifier; |
| 1102 | argument_list_t *arguments; |
| 1103 | function_status_t status; |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 1104 | token_list_t *substituted; |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1105 | int parameter_index; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1106 | |
| 1107 | node = *node_ret; |
| 1108 | identifier = node->token->value.str; |
| 1109 | |
| 1110 | macro = hash_table_find (parser->defines, identifier); |
| 1111 | |
| 1112 | assert (macro->is_function); |
| 1113 | |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1114 | arguments = _argument_list_create (parser); |
| 1115 | status = _arguments_parse (arguments, node_ret); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1116 | |
| 1117 | switch (status) { |
| 1118 | case FUNCTION_STATUS_SUCCESS: |
| 1119 | break; |
| 1120 | case FUNCTION_NOT_A_FUNCTION: |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 1121 | _token_list_append (result, node->token); |
Carl Worth | 7db2402 | 2010-05-26 17:01:57 -0700 | [diff] [blame] | 1122 | return FUNCTION_NOT_A_FUNCTION; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1123 | case FUNCTION_UNBALANCED_PARENTHESES: |
| 1124 | fprintf (stderr, "Error: Macro %s call has unbalanced parentheses\n", |
| 1125 | identifier); |
| 1126 | exit (1); |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 1127 | } |
| 1128 | |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1129 | if (macro->replacements == NULL) { |
| 1130 | talloc_free (arguments); |
Carl Worth | 7db2402 | 2010-05-26 17:01:57 -0700 | [diff] [blame] | 1131 | return FUNCTION_STATUS_SUCCESS; |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1132 | } |
| 1133 | |
Carl Worth | a19297b | 2010-05-27 13:29:19 -0700 | [diff] [blame^] | 1134 | if (! ((_argument_list_length (arguments) == |
| 1135 | _string_list_length (macro->parameters)) || |
| 1136 | (_string_list_length (macro->parameters) == 0 && |
| 1137 | _argument_list_length (arguments) == 1 && |
| 1138 | arguments->head->argument->head == NULL))) |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1139 | { |
| 1140 | fprintf (stderr, |
| 1141 | "Error: macro %s invoked with %d arguments (expected %d)\n", |
| 1142 | identifier, |
| 1143 | _argument_list_length (arguments), |
| 1144 | _string_list_length (macro->parameters)); |
Carl Worth | 7db2402 | 2010-05-26 17:01:57 -0700 | [diff] [blame] | 1145 | exit (1); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1146 | } |
| 1147 | |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 1148 | /* Perform argument substitution on the replacement list. */ |
| 1149 | substituted = _token_list_create (arguments); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1150 | |
Carl Worth | ce540f2 | 2010-05-26 08:25:44 -0700 | [diff] [blame] | 1151 | for (node = macro->replacements->head; node; node = node->next) |
| 1152 | { |
| 1153 | if (node->token->type == IDENTIFIER && |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1154 | _string_list_contains (macro->parameters, |
Carl Worth | ce540f2 | 2010-05-26 08:25:44 -0700 | [diff] [blame] | 1155 | node->token->value.str, |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1156 | ¶meter_index)) |
| 1157 | { |
| 1158 | token_list_t *argument; |
| 1159 | argument = _argument_list_member_at (arguments, |
| 1160 | parameter_index); |
Carl Worth | d5cd403 | 2010-05-26 08:09:29 -0700 | [diff] [blame] | 1161 | /* Before substituting, we expand the argument |
| 1162 | * tokens. */ |
| 1163 | _glcpp_parser_expand_token_list_onto (parser, |
| 1164 | argument, |
| 1165 | substituted); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1166 | } else { |
Carl Worth | ce540f2 | 2010-05-26 08:25:44 -0700 | [diff] [blame] | 1167 | _token_list_append (substituted, node->token); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1168 | } |
| 1169 | } |
| 1170 | |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 1171 | /* After argument substitution, and before further expansion |
| 1172 | * below, implement token pasting. */ |
| 1173 | |
| 1174 | node = substituted->head; |
| 1175 | while (node) |
| 1176 | { |
| 1177 | token_node_t *next_non_space; |
| 1178 | |
| 1179 | /* Look ahead for a PASTE token, skipping space. */ |
| 1180 | next_non_space = node->next; |
| 1181 | while (next_non_space && next_non_space->token->type == SPACE) |
| 1182 | next_non_space = next_non_space->next; |
| 1183 | |
| 1184 | if (next_non_space == NULL) |
| 1185 | break; |
| 1186 | |
| 1187 | if (next_non_space->token->type != PASTE) { |
| 1188 | node = next_non_space; |
| 1189 | continue; |
| 1190 | } |
| 1191 | |
| 1192 | /* Now find the next non-space token after the PASTE. */ |
| 1193 | next_non_space = next_non_space->next; |
| 1194 | while (next_non_space && next_non_space->token->type == SPACE) |
| 1195 | next_non_space = next_non_space->next; |
| 1196 | |
| 1197 | if (next_non_space == NULL) { |
| 1198 | fprintf (stderr, "Error: '##' cannot appear at either end of a macro expansion\n"); |
| 1199 | exit (1); |
| 1200 | } |
| 1201 | |
| 1202 | _token_paste (node->token, next_non_space->token); |
| 1203 | node->next = next_non_space->next; |
| 1204 | |
| 1205 | node = node->next; |
| 1206 | } |
| 1207 | |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 1208 | _string_list_push (parser->active, identifier); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 1209 | _glcpp_parser_expand_token_list_onto (parser, substituted, result); |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 1210 | _string_list_pop (parser->active); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1211 | |
| 1212 | talloc_free (arguments); |
Carl Worth | 7db2402 | 2010-05-26 17:01:57 -0700 | [diff] [blame] | 1213 | |
| 1214 | return FUNCTION_STATUS_SUCCESS; |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 1215 | } |
| 1216 | |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 1217 | static void |
| 1218 | _glcpp_parser_expand_token_list_onto (glcpp_parser_t *parser, |
| 1219 | token_list_t *list, |
| 1220 | token_list_t *result) |
| 1221 | { |
| 1222 | token_node_t *node; |
Carl Worth | 7db2402 | 2010-05-26 17:01:57 -0700 | [diff] [blame] | 1223 | token_list_t *intermediate, *list_orig = list; |
| 1224 | int i, need_rescan = 0; |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 1225 | |
Carl Worth | a65cf7b | 2010-05-27 11:55:36 -0700 | [diff] [blame] | 1226 | if (list == NULL || list->head == NULL) |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 1227 | return; |
| 1228 | |
Carl Worth | 7db2402 | 2010-05-26 17:01:57 -0700 | [diff] [blame] | 1229 | intermediate = _token_list_create (parser); |
| 1230 | |
| 1231 | /* XXX: The two-pass expansion here is really ugly. The |
| 1232 | * problem this is solving is that we can expand a macro into |
| 1233 | * a function-like macro name, and then we need to recognize |
| 1234 | * that as a function-like macro, but perhaps the parentheses |
| 1235 | * and arguments aren't on the token list yet, (since they are |
| 1236 | * in the actual content so they are part of what we are |
| 1237 | * expanding. |
| 1238 | * |
| 1239 | * This ugly hack works, but is messy, fragile, and hard to |
| 1240 | * maintain. I think a cleaner solution would separate the |
| 1241 | * notions of expanding and appending and avoid this problem |
| 1242 | * altogether. |
| 1243 | */ |
| 1244 | |
| 1245 | for (i = 0; i < 2; i++) { |
| 1246 | if (i == 1) { |
| 1247 | list = intermediate; |
| 1248 | intermediate = _token_list_create (parser); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 1249 | } |
Carl Worth | 7db2402 | 2010-05-26 17:01:57 -0700 | [diff] [blame] | 1250 | for (node = list->head; node; node = node->next) |
| 1251 | { |
| 1252 | if (_expand_token_onto (parser, node->token, |
| 1253 | intermediate)) |
| 1254 | { |
| 1255 | if (_expand_function_onto (parser, &node, |
| 1256 | intermediate)) |
| 1257 | { |
| 1258 | need_rescan = 1; |
| 1259 | } |
| 1260 | } |
| 1261 | } |
| 1262 | if (list != list_orig) |
| 1263 | talloc_free (list); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 1264 | } |
Carl Worth | 7db2402 | 2010-05-26 17:01:57 -0700 | [diff] [blame] | 1265 | |
| 1266 | _token_list_append_list (result, intermediate); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 1267 | } |
| 1268 | |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 1269 | void |
| 1270 | _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser, |
| 1271 | token_list_t *list) |
| 1272 | { |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 1273 | token_list_t *expanded; |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 1274 | token_node_t *node; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1275 | function_status_t function_status; |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 1276 | |
| 1277 | if (list == NULL) |
| 1278 | return; |
| 1279 | |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 1280 | expanded = _token_list_create (parser); |
Carl Worth | 10ae438 | 2010-05-25 20:35:01 -0700 | [diff] [blame] | 1281 | |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 1282 | _glcpp_parser_expand_token_list_onto (parser, list, expanded); |
| 1283 | |
| 1284 | _token_list_trim_trailing_space (expanded); |
| 1285 | |
| 1286 | _token_list_print (expanded); |
| 1287 | |
| 1288 | talloc_free (expanded); |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1292 | _define_object_macro (glcpp_parser_t *parser, |
| 1293 | const char *identifier, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 1294 | token_list_t *replacements) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1295 | { |
| 1296 | macro_t *macro; |
| 1297 | |
| 1298 | macro = xtalloc (parser, macro_t); |
| 1299 | |
| 1300 | macro->is_function = 0; |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 1301 | macro->parameters = NULL; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 1302 | macro->identifier = talloc_strdup (macro, identifier); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 1303 | macro->replacements = talloc_steal (macro, replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1304 | |
| 1305 | hash_table_insert (parser->defines, macro, identifier); |
| 1306 | } |
| 1307 | |
| 1308 | void |
| 1309 | _define_function_macro (glcpp_parser_t *parser, |
| 1310 | const char *identifier, |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 1311 | string_list_t *parameters, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 1312 | token_list_t *replacements) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1313 | { |
| 1314 | macro_t *macro; |
| 1315 | |
| 1316 | macro = xtalloc (parser, macro_t); |
| 1317 | |
| 1318 | macro->is_function = 1; |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 1319 | macro->parameters = talloc_steal (macro, parameters); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 1320 | macro->identifier = talloc_strdup (macro, identifier); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 1321 | macro->replacements = talloc_steal (macro, replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1322 | |
| 1323 | hash_table_insert (parser->defines, macro, identifier); |
| 1324 | } |
| 1325 | |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 1326 | static int |
Carl Worth | 0293b2e | 2010-05-19 10:05:40 -0700 | [diff] [blame] | 1327 | glcpp_parser_lex (glcpp_parser_t *parser) |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 1328 | { |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 1329 | token_node_t *node; |
| 1330 | int ret; |
| 1331 | |
Carl Worth | 95951ea | 2010-05-26 15:57:10 -0700 | [diff] [blame] | 1332 | if (parser->lex_from_list == NULL) { |
| 1333 | ret = glcpp_lex (parser->scanner); |
| 1334 | |
| 1335 | /* XXX: This ugly block of code exists for the sole |
| 1336 | * purpose of converting a NEWLINE token into a SPACE |
| 1337 | * token, but only in the case where we have seen a |
| 1338 | * function-like macro name, but have not yet seen its |
| 1339 | * closing parenthesis. |
| 1340 | * |
| 1341 | * There's perhaps a more compact way to do this with |
| 1342 | * mid-rule actions in the grammar. |
| 1343 | * |
| 1344 | * I'm definitely not pleased with the complexity of |
| 1345 | * this code here. |
| 1346 | */ |
| 1347 | if (parser->newline_as_space) |
| 1348 | { |
| 1349 | if (ret == '(') { |
| 1350 | parser->paren_count++; |
| 1351 | } else if (ret == ')') { |
| 1352 | parser->paren_count--; |
| 1353 | if (parser->paren_count == 0) |
| 1354 | parser->newline_as_space = 0; |
| 1355 | } else if (ret == NEWLINE) { |
| 1356 | ret = SPACE; |
| 1357 | } else if (ret != SPACE) { |
| 1358 | if (parser->paren_count == 0) |
| 1359 | parser->newline_as_space = 0; |
| 1360 | } |
| 1361 | } |
| 1362 | else if (parser->in_control_line) |
| 1363 | { |
| 1364 | if (ret == NEWLINE) |
| 1365 | parser->in_control_line = 0; |
| 1366 | } |
| 1367 | else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC || |
| 1368 | ret == HASH_UNDEF || ret == HASH_IF || |
| 1369 | ret == HASH_IFDEF || ret == HASH_IFNDEF || |
| 1370 | ret == HASH_ELIF || ret == HASH_ELSE || |
| 1371 | ret == HASH_ENDIF || ret == HASH) |
| 1372 | { |
| 1373 | parser->in_control_line = 1; |
| 1374 | } |
| 1375 | else if (ret == IDENTIFIER) |
| 1376 | { |
| 1377 | macro_t *macro; |
| 1378 | macro = hash_table_find (parser->defines, |
| 1379 | yylval.str); |
| 1380 | if (macro && macro->is_function) { |
| 1381 | parser->newline_as_space = 1; |
| 1382 | parser->paren_count = 0; |
| 1383 | } |
| 1384 | } |
| 1385 | |
| 1386 | return ret; |
| 1387 | } |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 1388 | |
| 1389 | node = parser->lex_from_node; |
| 1390 | |
| 1391 | if (node == NULL) { |
| 1392 | talloc_free (parser->lex_from_list); |
| 1393 | parser->lex_from_list = NULL; |
| 1394 | return NEWLINE; |
| 1395 | } |
| 1396 | |
| 1397 | yylval = node->token->value; |
| 1398 | ret = node->token->type; |
| 1399 | |
| 1400 | parser->lex_from_node = node->next; |
| 1401 | |
| 1402 | return ret; |
| 1403 | } |
| 1404 | |
| 1405 | static void |
| 1406 | glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list) |
| 1407 | { |
| 1408 | token_node_t *node; |
| 1409 | |
| 1410 | assert (parser->lex_from_list == NULL); |
| 1411 | |
| 1412 | /* Copy list, eliminating any space tokens. */ |
| 1413 | parser->lex_from_list = _token_list_create (parser); |
| 1414 | |
| 1415 | for (node = list->head; node; node = node->next) { |
| 1416 | if (node->token->type == SPACE) |
| 1417 | continue; |
| 1418 | _token_list_append (parser->lex_from_list, node->token); |
| 1419 | } |
| 1420 | |
| 1421 | talloc_free (list); |
| 1422 | |
| 1423 | parser->lex_from_node = parser->lex_from_list->head; |
| 1424 | |
| 1425 | /* It's possible the list consisted of nothing but whitespace. */ |
| 1426 | if (parser->lex_from_node == NULL) { |
| 1427 | talloc_free (parser->lex_from_list); |
| 1428 | parser->lex_from_list = NULL; |
| 1429 | } |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 1430 | } |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 1431 | |
| 1432 | static void |
| 1433 | _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition) |
| 1434 | { |
| 1435 | skip_type_t current = SKIP_NO_SKIP; |
| 1436 | skip_node_t *node; |
| 1437 | |
| 1438 | if (parser->skip_stack) |
| 1439 | current = parser->skip_stack->type; |
| 1440 | |
| 1441 | node = xtalloc (parser, skip_node_t); |
| 1442 | |
| 1443 | if (current == SKIP_NO_SKIP) { |
| 1444 | if (condition) |
| 1445 | node->type = SKIP_NO_SKIP; |
| 1446 | else |
| 1447 | node->type = SKIP_TO_ELSE; |
| 1448 | } else { |
| 1449 | node->type = SKIP_TO_ENDIF; |
| 1450 | } |
| 1451 | |
| 1452 | node->next = parser->skip_stack; |
| 1453 | parser->skip_stack = node; |
| 1454 | } |
| 1455 | |
| 1456 | static void |
| 1457 | _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type, |
| 1458 | int condition) |
| 1459 | { |
| 1460 | if (parser->skip_stack == NULL) { |
| 1461 | fprintf (stderr, "Error: %s without #if\n", type); |
| 1462 | exit (1); |
| 1463 | } |
| 1464 | |
| 1465 | if (parser->skip_stack->type == SKIP_TO_ELSE) { |
| 1466 | if (condition) |
| 1467 | parser->skip_stack->type = SKIP_NO_SKIP; |
| 1468 | } else { |
| 1469 | parser->skip_stack->type = SKIP_TO_ENDIF; |
| 1470 | } |
| 1471 | } |
Carl Worth | 80dc60b | 2010-05-25 14:42:00 -0700 | [diff] [blame] | 1472 | |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 1473 | static void |
| 1474 | _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser) |
| 1475 | { |
| 1476 | skip_node_t *node; |
| 1477 | |
| 1478 | if (parser->skip_stack == NULL) { |
| 1479 | fprintf (stderr, "Error: #endif without #if\n"); |
| 1480 | exit (1); |
| 1481 | } |
| 1482 | |
| 1483 | node = parser->skip_stack; |
| 1484 | parser->skip_stack = node->next; |
| 1485 | talloc_free (node); |
| 1486 | } |