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