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 <string.h> |
| 27 | |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 28 | #include "glcpp.h" |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 29 | #include "glcpp-parse.h" |
| 30 | %} |
| 31 | |
Carl Worth | 38aa835 | 2010-05-10 11:52:29 -0700 | [diff] [blame] | 32 | %option reentrant noyywrap |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 33 | %option extra-type="glcpp_parser_t *" |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 34 | |
Carl Worth | 81f0143 | 2010-05-14 17:08:45 -0700 | [diff] [blame] | 35 | %x ST_DEFINE |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame^] | 36 | %x ST_DEFINE_OBJ_OR_FUNC |
| 37 | %x ST_DEFINE_PARAMETER |
| 38 | %x ST_DEFINE_VALUE |
Carl Worth | 1a29500 | 2010-05-17 13:19:04 -0700 | [diff] [blame] | 39 | %x ST_UNDEF |
| 40 | %x ST_UNDEF_END |
Carl Worth | 81f0143 | 2010-05-14 17:08:45 -0700 | [diff] [blame] | 41 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 42 | SPACE [[:space:]] |
| 43 | NONSPACE [^[:space:]] |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 44 | NEWLINE [\n] |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 45 | HSPACE [ \t] |
Carl Worth | e36a4d5 | 2010-05-14 17:29:24 -0700 | [diff] [blame] | 46 | HASH ^{HSPACE}*#{HSPACE}* |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 47 | IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]* |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 48 | TOKEN [^[:space:](),]+ |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 49 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 50 | %% |
| 51 | |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 52 | {HASH}undef{HSPACE}* { |
Carl Worth | 1a29500 | 2010-05-17 13:19:04 -0700 | [diff] [blame] | 53 | BEGIN ST_UNDEF; |
Carl Worth | cd27e64 | 2010-05-12 13:11:50 -0700 | [diff] [blame] | 54 | return UNDEF; |
| 55 | } |
| 56 | |
Carl Worth | 1a29500 | 2010-05-17 13:19:04 -0700 | [diff] [blame] | 57 | <ST_UNDEF>{IDENTIFIER} { |
| 58 | BEGIN ST_UNDEF_END; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame^] | 59 | yylval.str = xtalloc_strdup (yyextra, yytext); |
| 60 | return IDENTIFIER; |
Carl Worth | 1a29500 | 2010-05-17 13:19:04 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame^] | 63 | <ST_UNDEF_END>{HSPACE}* |
| 64 | |
Carl Worth | 1a29500 | 2010-05-17 13:19:04 -0700 | [diff] [blame] | 65 | <ST_UNDEF_END>\n { |
| 66 | BEGIN INITIAL; |
Carl Worth | 1a29500 | 2010-05-17 13:19:04 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 69 | /* We use the ST_DEFINE and ST_DEFVAL states so that we can |
| 70 | * pass a space token, (yes, a token for whitespace!), since |
| 71 | * the preprocessor specification requires distinguishing |
| 72 | * "#define foo()" from "#define foo ()". |
| 73 | */ |
| 74 | {HASH}define{HSPACE}* { |
Carl Worth | 81f0143 | 2010-05-14 17:08:45 -0700 | [diff] [blame] | 75 | BEGIN ST_DEFINE; |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 76 | return DEFINE; |
| 77 | } |
Carl Worth | 012295f | 2010-05-12 13:19:23 -0700 | [diff] [blame] | 78 | |
Carl Worth | 81f0143 | 2010-05-14 17:08:45 -0700 | [diff] [blame] | 79 | <ST_DEFINE>{IDENTIFIER} { |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame^] | 80 | BEGIN ST_DEFINE_OBJ_OR_FUNC; |
Carl Worth | 81f0143 | 2010-05-14 17:08:45 -0700 | [diff] [blame] | 81 | yylval.str = xtalloc_strdup (yyextra, yytext); |
| 82 | return IDENTIFIER; |
| 83 | } |
| 84 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame^] | 85 | <ST_DEFINE_OBJ_OR_FUNC>\n { |
Carl Worth | 1a29500 | 2010-05-17 13:19:04 -0700 | [diff] [blame] | 86 | BEGIN INITIAL; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame^] | 87 | yylval.str = xtalloc_strdup (yyextra, ""); |
| 88 | return REPLACEMENT; |
Carl Worth | 1a29500 | 2010-05-17 13:19:04 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame^] | 91 | <ST_DEFINE_OBJ_OR_FUNC>{HSPACE}+ { |
| 92 | BEGIN ST_DEFINE_VALUE; |
Carl Worth | 1a29500 | 2010-05-17 13:19:04 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame^] | 95 | <ST_DEFINE_OBJ_OR_FUNC>"(" { |
| 96 | BEGIN ST_DEFINE_PARAMETER; |
Carl Worth | 1a29500 | 2010-05-17 13:19:04 -0700 | [diff] [blame] | 97 | return '('; |
| 98 | } |
| 99 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame^] | 100 | <ST_DEFINE_PARAMETER>{IDENTIFIER} { |
Carl Worth | 1a29500 | 2010-05-17 13:19:04 -0700 | [diff] [blame] | 101 | yylval.str = xtalloc_strdup (yyextra, yytext); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame^] | 102 | return IDENTIFIER; |
Carl Worth | 1a29500 | 2010-05-17 13:19:04 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame^] | 105 | <ST_DEFINE_PARAMETER>"," { |
| 106 | return ','; |
| 107 | } |
| 108 | |
| 109 | <ST_DEFINE_PARAMETER>")" { |
| 110 | BEGIN ST_DEFINE_VALUE; |
| 111 | return ')'; |
| 112 | } |
| 113 | |
| 114 | <ST_DEFINE_PARAMETER>{HSPACE}+ |
| 115 | |
| 116 | <ST_DEFINE_VALUE>.*\n { |
Carl Worth | 81f0143 | 2010-05-14 17:08:45 -0700 | [diff] [blame] | 117 | BEGIN INITIAL; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame^] | 118 | yylval.str = xtalloc_strndup (yyextra, yytext, strlen (yytext) - 1); |
| 119 | return REPLACEMENT; |
Carl Worth | 81f0143 | 2010-05-14 17:08:45 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 122 | {IDENTIFIER} { |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame^] | 123 | int parameter_index; |
| 124 | yylval.str = xtalloc_strdup (yyextra, yytext); |
| 125 | switch (glcpp_parser_classify_token (yyextra, yylval.str, |
| 126 | ¶meter_index)) |
| 127 | { |
| 128 | case TOKEN_CLASS_ARGUMENT: |
| 129 | talloc_free (yylval.str); |
| 130 | /* We don't return a value here since the |
| 131 | * current token will be replaced by new |
| 132 | * tokens. */ |
| 133 | glcpp_parser_push_expansion_argument (yyextra, |
| 134 | parameter_index); |
| 135 | break; |
| 136 | case TOKEN_CLASS_IDENTIFIER: |
| 137 | return IDENTIFIER; |
| 138 | break; |
| 139 | case TOKEN_CLASS_FUNC_MACRO: |
| 140 | return FUNC_MACRO; |
| 141 | break; |
| 142 | case TOKEN_CLASS_OBJ_MACRO: |
| 143 | return OBJ_MACRO; |
| 144 | break; |
| 145 | |
| 146 | } |
Carl Worth | cd27e64 | 2010-05-12 13:11:50 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 149 | [(),] { |
| 150 | return yytext[0]; |
| 151 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 152 | |
Carl Worth | 012295f | 2010-05-12 13:19:23 -0700 | [diff] [blame] | 153 | {TOKEN} { |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 154 | yylval.str = xtalloc_strdup (yyextra, yytext); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 155 | return TOKEN; |
| 156 | } |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 157 | |
Carl Worth | 012295f | 2010-05-12 13:19:23 -0700 | [diff] [blame] | 158 | \n { |
Carl Worth | 1a29500 | 2010-05-17 13:19:04 -0700 | [diff] [blame] | 159 | printf ("\n"); |
Carl Worth | 012295f | 2010-05-12 13:19:23 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Carl Worth | e36a4d5 | 2010-05-14 17:29:24 -0700 | [diff] [blame] | 162 | {HSPACE}+ |
Carl Worth | 012295f | 2010-05-12 13:19:23 -0700 | [diff] [blame] | 163 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame^] | 164 | <<EOF>> { |
| 165 | int done; |
| 166 | |
| 167 | done = glcpp_lex_stack_pop (yyextra->lex_stack); |
| 168 | |
| 169 | if (done) |
| 170 | yyterminate (); |
| 171 | |
| 172 | glcpp_parser_pop_expansion (yyextra); |
| 173 | } |
| 174 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 175 | %% |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame^] | 176 | |
| 177 | void |
| 178 | glcpp_lex_stack_push (glcpp_lex_stack_t *stack, const char *string) |
| 179 | { |
| 180 | struct yyguts_t *yyg = (struct yyguts_t*) stack->parser->scanner; |
| 181 | glcpp_lex_node_t *node; |
| 182 | |
| 183 | /* Save the current buffer on the top of the stack. */ |
| 184 | node = xtalloc (stack, glcpp_lex_node_t); |
| 185 | node->buffer = YY_CURRENT_BUFFER; |
| 186 | |
| 187 | node->next = stack->head; |
| 188 | stack->head = node; |
| 189 | |
| 190 | /* Then switch to a new scan buffer for string. */ |
| 191 | yy_scan_string (string, stack->parser->scanner); |
| 192 | } |
| 193 | |
| 194 | int |
| 195 | glcpp_lex_stack_pop (glcpp_lex_stack_t *stack) |
| 196 | { |
| 197 | struct yyguts_t *yyg = (struct yyguts_t*) stack->parser->scanner; |
| 198 | glcpp_lex_node_t *node; |
| 199 | |
| 200 | node = stack->head; |
| 201 | |
| 202 | if (node == NULL) |
| 203 | return 1; |
| 204 | |
| 205 | stack->head = node->next; |
| 206 | |
| 207 | yy_delete_buffer (YY_CURRENT_BUFFER, stack->parser->scanner); |
| 208 | yy_switch_to_buffer ((YY_BUFFER_STATE) node->buffer, |
| 209 | stack->parser->scanner); |
| 210 | |
| 211 | talloc_free (node); |
| 212 | |
| 213 | return 0; |
| 214 | } |