3 * Copyright © 2010 Intel Corporation
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:
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
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.
30 #include "glcpp-parse.h"
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);
39 #define YY_USER_ACTION \
41 yylloc->first_column = yycolumn + 1; \
42 yylloc->first_line = yylineno; \
46 #define YY_USER_INIT \
54 %option bison-bridge bison-locations reentrant noyywrap
55 %option extra-type="glcpp_parser_t *"
56 %option prefix="glcpp_"
58 %option never-interactive
60 %x DONE COMMENT UNREACHABLE
66 HASH ^{HSPACE}*#{HSPACE}*
67 IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]*
68 PUNCTUATION [][(){}.&*~!/%<>^|;,=+-]
69 OTHER [^][(){}.&*~!/%<>^|;,=#[:space:]+-]+
72 DECIMAL_INTEGER [1-9][0-9]*[uU]?
73 OCTAL_INTEGER 0[0-7]*[uU]?
74 HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]?
78 /* Single-line comments */
82 /* Multi-line comments */
83 "/*" { yy_push_state(COMMENT, yyscanner); }
85 <COMMENT>[^*\n]*\n { yylineno++; yycolumn = 0; return NEWLINE; }
87 <COMMENT>"*"+[^*/\n]*\n { yylineno++; yycolumn = 0; return NEWLINE; }
89 yy_pop_state(yyscanner);
90 if (yyextra->space_tokens)
95 yylval->str = ralloc_strdup (yyextra, yytext);
96 yyextra->space_tokens = 0;
100 /* glcpp doesn't handle #extension, #version, or #pragma directives.
101 * Simply pass them through to the main compiler's lexer/parser. */
102 {HASH}(extension|pragma)[^\n]+ {
103 yylval->str = ralloc_strdup (yyextra, yytext);
109 {HASH}line{HSPACE}+{DIGITS}{HSPACE}+{DIGITS}{HSPACE}*$ {
110 /* Eat characters until the first digit is
114 while (!isdigit(*ptr))
117 /* Subtract one from the line number because
118 * yylineno is zero-based instead of
121 yylineno = strtol(ptr, &ptr, 0) - 1;
122 yylloc->source = strtol(ptr, NULL, 0);
125 {HASH}line{HSPACE}+{DIGITS}{HSPACE}*$ {
126 /* Eat characters until the first digit is
130 while (!isdigit(*ptr))
133 /* Subtract one from the line number because
134 * yylineno is zero-based instead of
137 yylineno = strtol(ptr, &ptr, 0) - 1;
141 yyextra->lexing_if = 1;
142 yyextra->space_tokens = 0;
147 yyextra->lexing_if = 1;
148 yyextra->space_tokens = 0;
152 {HASH}if/[^_a-zA-Z0-9].*\n {
153 yyextra->lexing_if = 1;
154 yyextra->space_tokens = 0;
159 yyextra->lexing_if = 1;
160 yyextra->space_tokens = 0;
165 yyextra->space_tokens = 0;
170 yyextra->space_tokens = 0;
174 /* When skipping (due to an #if 0 or similar) consume anything
175 * up to a newline. We do this with less priority than any
176 * #if-related directive (#if, #elif, #else, #endif), but with
177 * more priority than any other directive or token to avoid
178 * any side-effects from skipped content.
180 * We use the lexing_if flag to avoid skipping any part of an
181 * if conditional expression. */
183 /* Since this rule always matches, YY_USER_ACTION gets called for it,
184 * wrongly incrementing yycolumn. We undo that effect here. */
186 if (yyextra->lexing_if ||
187 yyextra->skip_stack == NULL ||
188 yyextra->skip_stack->type == SKIP_NO_SKIP)
196 for (p = yytext; !isalpha(p[0]); p++); /* skip " # " */
197 p += 5; /* skip "error" */
198 glcpp_error(yylloc, yyextra, "#error%s", p);
201 {HASH}define{HSPACE}+/{IDENTIFIER}"(" {
202 yyextra->space_tokens = 0;
203 return HASH_DEFINE_FUNC;
207 yyextra->space_tokens = 0;
208 return HASH_DEFINE_OBJ;
212 yyextra->space_tokens = 0;
217 yyextra->space_tokens = 0;
222 yylval->str = ralloc_strdup (yyextra, yytext);
223 return INTEGER_STRING;
227 yylval->str = ralloc_strdup (yyextra, yytext);
228 return INTEGER_STRING;
231 {HEXADECIMAL_INTEGER} {
232 yylval->str = ralloc_strdup (yyextra, yytext);
233 return INTEGER_STRING;
245 return LESS_OR_EQUAL;
249 return GREATER_OR_EQUAL;
277 yylval->str = ralloc_strdup (yyextra, yytext);
286 yylval->str = ralloc_strdup (yyextra, yytext);
291 if (yyextra->space_tokens) {
297 yyextra->lexing_if = 0;
303 /* Handle missing newline at EOF. */
305 BEGIN DONE; /* Don't keep matching this rule forever. */
306 yyextra->lexing_if = 0;
310 /* We don't actually use the UNREACHABLE start condition. We
311 only have this action here so that we can pretend to call some
312 generated functions, (to avoid "defined but not used"
316 yy_top_state(yyextra);
322 glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader)
324 yy_scan_string(shader, parser->scanner);