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 \
42 yylloc->first_column = yycolumn + 1; \
43 yylloc->first_line = yylineno; \
46 #define YY_USER_INIT yylineno = 1; yycolumn = 1;
49 %option bison-bridge bison-locations reentrant noyywrap
50 %option extra-type="glcpp_parser_t *"
51 %option prefix="glcpp_"
53 %option never-interactive
55 %x DONE COMMENT UNREACHABLE
61 HASH ^{HSPACE}*#{HSPACE}*
62 IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]*
63 PUNCTUATION [][(){}.&*~!/%<>^|;,=+-]
64 OTHER [^][(){}.&*~!/%<>^|;,=#[:space:]+-]+
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]?
72 /* Single-line comments */
79 /* Multi-line comments */
80 "/*" { yy_push_state(COMMENT, yyscanner); }
82 <COMMENT>[^*\n]*\n { yylineno++; yycolumn = 0; }
84 <COMMENT>"*"+[^*/\n]*\n { yylineno++; yycolumn = 0; }
86 yy_pop_state(yyscanner);
87 if (yyextra->space_tokens)
92 yylval->str = talloc_strdup (yyextra, yytext);
95 yyextra->space_tokens = 0;
99 /* glcpp doesn't handle #extension, #version, or #pragma directives.
100 * Simply pass them through to the main compiler's lexer/parser. */
101 {HASH}(extension|pragma)[^\n]+ {
102 yylval->str = talloc_strdup (yyextra, yytext);
109 yyextra->lexing_if = 1;
110 yyextra->space_tokens = 0;
115 yyextra->lexing_if = 1;
116 yyextra->space_tokens = 0;
120 {HASH}if/[^_a-zA-Z0-9].*\n {
121 yyextra->lexing_if = 1;
122 yyextra->space_tokens = 0;
127 yyextra->lexing_if = 1;
128 yyextra->space_tokens = 0;
133 yyextra->space_tokens = 0;
138 yyextra->space_tokens = 0;
142 /* When skipping (due to an #if 0 or similar) consume anything
143 * up to a newline. We do this with less priority than any
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.
148 * We use the lexing_if flag to avoid skipping any part of an
149 * if conditional expression. */
151 /* Since this rule always matches, YY_USER_ACTION gets called for it,
152 * wrongly incrementing yycolumn. We undo that effect here. */
154 if (yyextra->lexing_if ||
155 yyextra->skip_stack == NULL ||
156 yyextra->skip_stack->type == SKIP_NO_SKIP)
164 for (p = yytext; !isalpha(p[0]); p++); /* skip " # " */
165 p += 5; /* skip "error" */
166 glcpp_error(yylloc, yyextra, "#error%s", p);
169 {HASH}define{HSPACE}+/{IDENTIFIER}"(" {
170 yyextra->space_tokens = 0;
171 return HASH_DEFINE_FUNC;
175 yyextra->space_tokens = 0;
176 return HASH_DEFINE_OBJ;
180 yyextra->space_tokens = 0;
185 yyextra->space_tokens = 0;
190 yylval->str = talloc_strdup (yyextra, yytext);
191 return INTEGER_STRING;
195 yylval->str = talloc_strdup (yyextra, yytext);
196 return INTEGER_STRING;
199 {HEXADECIMAL_INTEGER} {
200 yylval->str = talloc_strdup (yyextra, yytext);
201 return INTEGER_STRING;
213 return LESS_OR_EQUAL;
217 return GREATER_OR_EQUAL;
245 yylval->str = talloc_strdup (yyextra, yytext);
254 yylval->str = talloc_strdup (yyextra, yytext);
259 if (yyextra->space_tokens) {
265 yyextra->lexing_if = 0;
271 /* Handle missing newline at EOF. */
273 BEGIN DONE; /* Don't keep matching this rule forever. */
274 yyextra->lexing_if = 0;
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"
284 yy_top_state(yyextra);
290 glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader)
292 yy_scan_string(shader, parser->scanner);