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