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