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