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> |
Kenneth Graunke | 7e908a6 | 2010-07-02 15:31:26 -0700 | [diff] [blame] | 27 | #include <ctype.h> |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 28 | |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 29 | #include "glcpp.h" |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 30 | #include "glcpp-parse.h" |
Kenneth Graunke | db93810 | 2010-06-16 17:41:12 -0700 | [diff] [blame] | 31 | |
Carl Worth | a9bb4bc | 2010-07-20 15:03:20 -0700 | [diff] [blame] | 32 | /* Flex annoyingly generates some functions without making them |
| 33 | * static. Let's declare them here. */ |
| 34 | int glcpp_get_column (yyscan_t yyscanner); |
| 35 | void glcpp_set_column (int column_no , yyscan_t yyscanner); |
| 36 | |
Carl Worth | fb90560 | 2010-07-20 15:53:14 -0700 | [diff] [blame] | 37 | #define YY_NO_INPUT |
| 38 | |
Kenneth Graunke | db93810 | 2010-06-16 17:41:12 -0700 | [diff] [blame] | 39 | #define YY_USER_ACTION \ |
| 40 | do { \ |
| 41 | yylloc->source = 0; \ |
| 42 | yylloc->first_column = yycolumn + 1; \ |
Kenneth Graunke | 8f32221 | 2010-06-18 15:23:50 -0700 | [diff] [blame] | 43 | yylloc->first_line = yylineno; \ |
Kenneth Graunke | db93810 | 2010-06-16 17:41:12 -0700 | [diff] [blame] | 44 | yycolumn += yyleng; \ |
| 45 | } while(0); |
Carl Worth | bc64b89 | 2010-08-11 13:09:14 -0700 | [diff] [blame] | 46 | #define YY_USER_INIT yylineno = 1; yycolumn = 1; |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 47 | %} |
| 48 | |
Kenneth Graunke | 465e03e | 2010-06-16 16:35:57 -0700 | [diff] [blame] | 49 | %option bison-bridge bison-locations reentrant noyywrap |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 50 | %option extra-type="glcpp_parser_t *" |
Kenneth Graunke | 254a485 | 2010-06-16 11:51:43 -0700 | [diff] [blame] | 51 | %option prefix="glcpp_" |
Kenneth Graunke | db93810 | 2010-06-16 17:41:12 -0700 | [diff] [blame] | 52 | %option stack |
Ian Romanick | ab18be7 | 2010-08-13 13:08:54 -0700 | [diff] [blame] | 53 | %option never-interactive |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 54 | |
Carl Worth | fb90560 | 2010-07-20 15:53:14 -0700 | [diff] [blame] | 55 | %x DONE COMMENT UNREACHABLE |
Kenneth Graunke | f82d673 | 2010-06-16 12:53:19 -0700 | [diff] [blame] | 56 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 57 | SPACE [[:space:]] |
| 58 | NONSPACE [^[:space:]] |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 59 | NEWLINE [\n] |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 60 | HSPACE [ \t] |
Carl Worth | e36a4d5 | 2010-05-14 17:29:24 -0700 | [diff] [blame] | 61 | HASH ^{HSPACE}*#{HSPACE}* |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 62 | IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]* |
Carl Worth | 6310169 | 2010-05-29 05:07:24 -0700 | [diff] [blame] | 63 | PUNCTUATION [][(){}.&*~!/%<>^|;,=+-] |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 64 | OTHER [^][(){}.&*~!/%<>^|;,=#[:space:]+-]+ |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 65 | |
Carl Worth | 03f6d5d | 2010-05-24 11:29:02 -0700 | [diff] [blame] | 66 | DECIMAL_INTEGER [1-9][0-9]*[uU]? |
| 67 | OCTAL_INTEGER 0[0-7]*[uU]? |
| 68 | HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]? |
| 69 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 70 | %% |
| 71 | |
Carl Worth | 2571415 | 2010-06-01 12:18:43 -0700 | [diff] [blame] | 72 | /* Single-line comments */ |
Kenneth Graunke | e6ae7af | 2010-06-21 15:11:01 -0700 | [diff] [blame] | 73 | "//"[^\n]*\n { |
Kenneth Graunke | db93810 | 2010-06-16 17:41:12 -0700 | [diff] [blame] | 74 | yylineno++; |
| 75 | yycolumn = 0; |
Carl Worth | 2571415 | 2010-06-01 12:18:43 -0700 | [diff] [blame] | 76 | return NEWLINE; |
| 77 | } |
| 78 | |
| 79 | /* Multi-line comments */ |
Kenneth Graunke | db93810 | 2010-06-16 17:41:12 -0700 | [diff] [blame] | 80 | "/*" { yy_push_state(COMMENT, yyscanner); } |
| 81 | <COMMENT>[^*\n]* |
| 82 | <COMMENT>[^*\n]*\n { yylineno++; yycolumn = 0; } |
| 83 | <COMMENT>"*"+[^*/\n]* |
| 84 | <COMMENT>"*"+[^*/\n]*\n { yylineno++; yycolumn = 0; } |
| 85 | <COMMENT>"*"+"/" { |
| 86 | yy_pop_state(yyscanner); |
Carl Worth | 2571415 | 2010-06-01 12:18:43 -0700 | [diff] [blame] | 87 | if (yyextra->space_tokens) |
| 88 | return SPACE; |
| 89 | } |
| 90 | |
Eric Anholt | d4a04f3 | 2010-07-28 16:58:39 -0700 | [diff] [blame] | 91 | {HASH}(version) { |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 92 | yylval->str = talloc_strdup (yyextra, yytext); |
Eric Anholt | d4a04f3 | 2010-07-28 16:58:39 -0700 | [diff] [blame] | 93 | yylineno++; |
| 94 | yycolumn = 0; |
| 95 | yyextra->space_tokens = 0; |
| 96 | return HASH_VERSION; |
| 97 | } |
| 98 | |
Kenneth Graunke | 3b73ea3 | 2010-06-16 12:21:17 -0700 | [diff] [blame] | 99 | /* glcpp doesn't handle #extension, #version, or #pragma directives. |
| 100 | * Simply pass them through to the main compiler's lexer/parser. */ |
Eric Anholt | d4a04f3 | 2010-07-28 16:58:39 -0700 | [diff] [blame] | 101 | {HASH}(extension|pragma)[^\n]+ { |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 102 | yylval->str = talloc_strdup (yyextra, yytext); |
Kenneth Graunke | 8f32221 | 2010-06-18 15:23:50 -0700 | [diff] [blame] | 103 | yylineno++; |
| 104 | yycolumn = 0; |
Kenneth Graunke | 3b73ea3 | 2010-06-16 12:21:17 -0700 | [diff] [blame] | 105 | return OTHER; |
| 106 | } |
| 107 | |
Kenneth Graunke | 77260fc | 2010-06-17 14:36:34 -0700 | [diff] [blame] | 108 | {HASH}ifdef/.*\n { |
Carl Worth | 1d7e03e | 2010-07-20 14:13:32 -0700 | [diff] [blame] | 109 | yyextra->lexing_if = 1; |
Kenneth Graunke | 77260fc | 2010-06-17 14:36:34 -0700 | [diff] [blame] | 110 | yyextra->space_tokens = 0; |
| 111 | return HASH_IFDEF; |
| 112 | } |
| 113 | |
| 114 | {HASH}ifndef/.*\n { |
Carl Worth | 1d7e03e | 2010-07-20 14:13:32 -0700 | [diff] [blame] | 115 | yyextra->lexing_if = 1; |
Kenneth Graunke | 77260fc | 2010-06-17 14:36:34 -0700 | [diff] [blame] | 116 | yyextra->space_tokens = 0; |
| 117 | return HASH_IFNDEF; |
| 118 | } |
| 119 | |
Carl Worth | 1d7e03e | 2010-07-20 14:13:32 -0700 | [diff] [blame] | 120 | {HASH}if/[^_a-zA-Z0-9].*\n { |
Carl Worth | a771a40 | 2010-06-01 11:20:18 -0700 | [diff] [blame] | 121 | yyextra->lexing_if = 1; |
| 122 | yyextra->space_tokens = 0; |
| 123 | return HASH_IF; |
| 124 | } |
| 125 | |
| 126 | {HASH}elif/.*\n { |
| 127 | yyextra->lexing_if = 1; |
| 128 | yyextra->space_tokens = 0; |
| 129 | return HASH_ELIF; |
| 130 | } |
| 131 | |
| 132 | {HASH}else/.*\n { |
| 133 | yyextra->space_tokens = 0; |
| 134 | return HASH_ELSE; |
| 135 | } |
| 136 | |
| 137 | {HASH}endif/.*\n { |
| 138 | yyextra->space_tokens = 0; |
| 139 | return HASH_ENDIF; |
| 140 | } |
| 141 | |
| 142 | /* When skipping (due to an #if 0 or similar) consume anything |
Carl Worth | 1d7e03e | 2010-07-20 14:13:32 -0700 | [diff] [blame] | 143 | * up to a newline. We do this with less priority than any |
Carl Worth | a771a40 | 2010-06-01 11:20:18 -0700 | [diff] [blame] | 144 | * #if-related directive (#if, #elif, #else, #endif), but with |
| 145 | * more priority than any other directive or token to avoid |
| 146 | * any side-effects from skipped content. |
| 147 | * |
| 148 | * We use the lexing_if flag to avoid skipping any part of an |
| 149 | * if conditional expression. */ |
| 150 | [^\n]+/\n { |
Kenneth Graunke | 8f32221 | 2010-06-18 15:23:50 -0700 | [diff] [blame] | 151 | /* Since this rule always matches, YY_USER_ACTION gets called for it, |
| 152 | * wrongly incrementing yycolumn. We undo that effect here. */ |
| 153 | yycolumn -= yyleng; |
Carl Worth | a771a40 | 2010-06-01 11:20:18 -0700 | [diff] [blame] | 154 | if (yyextra->lexing_if || |
| 155 | yyextra->skip_stack == NULL || |
| 156 | yyextra->skip_stack->type == SKIP_NO_SKIP) |
| 157 | { |
| 158 | REJECT; |
| 159 | } |
| 160 | } |
| 161 | |
Kenneth Graunke | 7e908a6 | 2010-07-02 15:31:26 -0700 | [diff] [blame] | 162 | {HASH}error.* { |
| 163 | char *p; |
| 164 | for (p = yytext; !isalpha(p[0]); p++); /* skip " # " */ |
| 165 | p += 5; /* skip "error" */ |
| 166 | glcpp_error(yylloc, yyextra, "#error%s", p); |
| 167 | } |
| 168 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 169 | {HASH}define{HSPACE}+/{IDENTIFIER}"(" { |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 170 | yyextra->space_tokens = 0; |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 171 | return HASH_DEFINE_FUNC; |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 174 | {HASH}define { |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 175 | yyextra->space_tokens = 0; |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 176 | return HASH_DEFINE_OBJ; |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 179 | {HASH}undef { |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 180 | yyextra->space_tokens = 0; |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 181 | return HASH_UNDEF; |
Carl Worth | 03f6d5d | 2010-05-24 11:29:02 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 184 | {HASH} { |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 185 | yyextra->space_tokens = 0; |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 186 | return HASH; |
Carl Worth | 81f0143 | 2010-05-14 17:08:45 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 189 | {DECIMAL_INTEGER} { |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 190 | yylval->str = talloc_strdup (yyextra, yytext); |
Carl Worth | 050e3de | 2010-05-27 14:36:29 -0700 | [diff] [blame] | 191 | return INTEGER_STRING; |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | {OCTAL_INTEGER} { |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 195 | yylval->str = talloc_strdup (yyextra, yytext); |
Carl Worth | 050e3de | 2010-05-27 14:36:29 -0700 | [diff] [blame] | 196 | return INTEGER_STRING; |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | {HEXADECIMAL_INTEGER} { |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 200 | yylval->str = talloc_strdup (yyextra, yytext); |
Carl Worth | 050e3de | 2010-05-27 14:36:29 -0700 | [diff] [blame] | 201 | return INTEGER_STRING; |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 204 | "<<" { |
| 205 | return LEFT_SHIFT; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 208 | ">>" { |
| 209 | return RIGHT_SHIFT; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 212 | "<=" { |
| 213 | return LESS_OR_EQUAL; |
| 214 | } |
| 215 | |
| 216 | ">=" { |
| 217 | return GREATER_OR_EQUAL; |
| 218 | } |
| 219 | |
| 220 | "==" { |
| 221 | return EQUAL; |
| 222 | } |
| 223 | |
| 224 | "!=" { |
| 225 | return NOT_EQUAL; |
| 226 | } |
| 227 | |
| 228 | "&&" { |
| 229 | return AND; |
| 230 | } |
| 231 | |
| 232 | "||" { |
| 233 | return OR; |
| 234 | } |
| 235 | |
| 236 | "##" { |
| 237 | return PASTE; |
| 238 | } |
| 239 | |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 240 | "defined" { |
| 241 | return DEFINED; |
| 242 | } |
| 243 | |
Carl Worth | 16c1e98 | 2010-05-26 09:35:34 -0700 | [diff] [blame] | 244 | {IDENTIFIER} { |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 245 | yylval->str = talloc_strdup (yyextra, yytext); |
Carl Worth | 16c1e98 | 2010-05-26 09:35:34 -0700 | [diff] [blame] | 246 | return IDENTIFIER; |
| 247 | } |
| 248 | |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 249 | {PUNCTUATION} { |
| 250 | return yytext[0]; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Carl Worth | 9fb8b7a | 2010-05-25 15:04:32 -0700 | [diff] [blame] | 253 | {OTHER}+ { |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 254 | yylval->str = talloc_strdup (yyextra, yytext); |
Carl Worth | 9fb8b7a | 2010-05-25 15:04:32 -0700 | [diff] [blame] | 255 | return OTHER; |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Carl Worth | 9fb8b7a | 2010-05-25 15:04:32 -0700 | [diff] [blame] | 258 | {HSPACE}+ { |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 259 | if (yyextra->space_tokens) { |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 260 | return SPACE; |
| 261 | } |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 262 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 263 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 264 | \n { |
Carl Worth | a771a40 | 2010-06-01 11:20:18 -0700 | [diff] [blame] | 265 | yyextra->lexing_if = 0; |
Kenneth Graunke | db93810 | 2010-06-16 17:41:12 -0700 | [diff] [blame] | 266 | yylineno++; |
| 267 | yycolumn = 0; |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 268 | return NEWLINE; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 269 | } |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 270 | |
Kenneth Graunke | f82d673 | 2010-06-16 12:53:19 -0700 | [diff] [blame] | 271 | /* Handle missing newline at EOF. */ |
| 272 | <INITIAL><<EOF>> { |
| 273 | BEGIN DONE; /* Don't keep matching this rule forever. */ |
| 274 | yyextra->lexing_if = 0; |
| 275 | return NEWLINE; |
| 276 | } |
| 277 | |
Carl Worth | fb90560 | 2010-07-20 15:53:14 -0700 | [diff] [blame] | 278 | /* We don't actually use the UNREACHABLE start condition. We |
| 279 | only have this action here so that we can pretend to call some |
| 280 | generated functions, (to avoid "defined but not used" |
| 281 | warnings. */ |
| 282 | <UNREACHABLE>. { |
| 283 | unput('.'); |
| 284 | yy_top_state(yyextra); |
| 285 | } |
| 286 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 287 | %% |
Kenneth Graunke | 1b1f43e | 2010-06-16 12:01:17 -0700 | [diff] [blame] | 288 | |
| 289 | void |
| 290 | glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader) |
| 291 | { |
| 292 | yy_scan_string(shader, parser->scanner); |
| 293 | } |