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_"
54 %x DONE COMMENT UNREACHABLE
60 HASH ^{HSPACE}*#{HSPACE}*
61 IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]*
62 PUNCTUATION [][(){}.&*~!/%<>^|;,=+-]
63 OTHER [^][(){}.&*~!/%<>^|;,=#[:space:]+-]+
65 DECIMAL_INTEGER [1-9][0-9]*[uU]?
66 OCTAL_INTEGER 0[0-7]*[uU]?
67 HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]?
71 /* Single-line comments */
78 /* Multi-line comments */
79 "/*" { yy_push_state(COMMENT, yyscanner); }
81 <COMMENT>[^*\n]*\n { yylineno++; yycolumn = 0; }
83 <COMMENT>"*"+[^*/\n]*\n { yylineno++; yycolumn = 0; }
85 yy_pop_state(yyscanner);
86 if (yyextra->space_tokens)
91 yylval->str = talloc_strdup (yyextra, yytext);
94 yyextra->space_tokens = 0;
98 /* glcpp doesn't handle #extension, #version, or #pragma directives.
99 * Simply pass them through to the main compiler's lexer/parser. */
100 {HASH}(extension|pragma)[^\n]+ {
101 yylval->str = talloc_strdup (yyextra, yytext);
108 yyextra->lexing_if = 1;
109 yyextra->space_tokens = 0;
114 yyextra->lexing_if = 1;
115 yyextra->space_tokens = 0;
119 {HASH}if/[^_a-zA-Z0-9].*\n {
120 yyextra->lexing_if = 1;
121 yyextra->space_tokens = 0;
126 yyextra->lexing_if = 1;
127 yyextra->space_tokens = 0;
132 yyextra->space_tokens = 0;
137 yyextra->space_tokens = 0;
141 /* When skipping (due to an #if 0 or similar) consume anything
142 * up to a newline. We do this with less priority than any
143 * #if-related directive (#if, #elif, #else, #endif), but with
144 * more priority than any other directive or token to avoid
145 * any side-effects from skipped content.
147 * We use the lexing_if flag to avoid skipping any part of an
148 * if conditional expression. */
150 /* Since this rule always matches, YY_USER_ACTION gets called for it,
151 * wrongly incrementing yycolumn. We undo that effect here. */
153 if (yyextra->lexing_if ||
154 yyextra->skip_stack == NULL ||
155 yyextra->skip_stack->type == SKIP_NO_SKIP)
163 for (p = yytext; !isalpha(p[0]); p++); /* skip " # " */
164 p += 5; /* skip "error" */
165 glcpp_error(yylloc, yyextra, "#error%s", p);
168 {HASH}define{HSPACE}+/{IDENTIFIER}"(" {
169 yyextra->space_tokens = 0;
170 return HASH_DEFINE_FUNC;
174 yyextra->space_tokens = 0;
175 return HASH_DEFINE_OBJ;
179 yyextra->space_tokens = 0;
184 yyextra->space_tokens = 0;
189 yylval->str = talloc_strdup (yyextra, yytext);
190 return INTEGER_STRING;
194 yylval->str = talloc_strdup (yyextra, yytext);
195 return INTEGER_STRING;
198 {HEXADECIMAL_INTEGER} {
199 yylval->str = talloc_strdup (yyextra, yytext);
200 return INTEGER_STRING;
212 return LESS_OR_EQUAL;
216 return GREATER_OR_EQUAL;
244 yylval->str = talloc_strdup (yyextra, yytext);
253 yylval->str = talloc_strdup (yyextra, yytext);
258 if (yyextra->space_tokens) {
264 yyextra->lexing_if = 0;
270 /* Handle missing newline at EOF. */
272 BEGIN DONE; /* Don't keep matching this rule forever. */
273 yyextra->lexing_if = 0;
277 /* We don't actually use the UNREACHABLE start condition. We
278 only have this action here so that we can pretend to call some
279 generated functions, (to avoid "defined but not used"
283 yy_top_state(yyextra);
289 glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader)
291 yy_scan_string(shader, parser->scanner);