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 = 0; yycolumn = 0;
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)
90 /* glcpp doesn't handle #extension, #version, or #pragma directives.
91 * Simply pass them through to the main compiler's lexer/parser. */
92 {HASH}(extension|version|pragma)[^\n]+ {
93 yylval->str = xtalloc_strdup (yyextra, yytext);
100 yyextra->lexing_if = 1;
101 yyextra->space_tokens = 0;
106 yyextra->lexing_if = 1;
107 yyextra->space_tokens = 0;
111 {HASH}if/[^_a-zA-Z0-9].*\n {
112 yyextra->lexing_if = 1;
113 yyextra->space_tokens = 0;
118 yyextra->lexing_if = 1;
119 yyextra->space_tokens = 0;
124 yyextra->space_tokens = 0;
129 yyextra->space_tokens = 0;
133 /* When skipping (due to an #if 0 or similar) consume anything
134 * up to a newline. We do this with less priority than any
135 * #if-related directive (#if, #elif, #else, #endif), but with
136 * more priority than any other directive or token to avoid
137 * any side-effects from skipped content.
139 * We use the lexing_if flag to avoid skipping any part of an
140 * if conditional expression. */
142 /* Since this rule always matches, YY_USER_ACTION gets called for it,
143 * wrongly incrementing yycolumn. We undo that effect here. */
145 if (yyextra->lexing_if ||
146 yyextra->skip_stack == NULL ||
147 yyextra->skip_stack->type == SKIP_NO_SKIP)
155 for (p = yytext; !isalpha(p[0]); p++); /* skip " # " */
156 p += 5; /* skip "error" */
157 glcpp_error(yylloc, yyextra, "#error%s", p);
160 {HASH}define{HSPACE}+/{IDENTIFIER}"(" {
161 yyextra->space_tokens = 0;
162 return HASH_DEFINE_FUNC;
166 yyextra->space_tokens = 0;
167 return HASH_DEFINE_OBJ;
171 yyextra->space_tokens = 0;
176 yyextra->space_tokens = 0;
181 yylval->str = xtalloc_strdup (yyextra, yytext);
182 return INTEGER_STRING;
186 yylval->str = xtalloc_strdup (yyextra, yytext);
187 return INTEGER_STRING;
190 {HEXADECIMAL_INTEGER} {
191 yylval->str = xtalloc_strdup (yyextra, yytext);
192 return INTEGER_STRING;
204 return LESS_OR_EQUAL;
208 return GREATER_OR_EQUAL;
236 yylval->str = xtalloc_strdup (yyextra, yytext);
245 yylval->str = xtalloc_strdup (yyextra, yytext);
250 if (yyextra->space_tokens) {
256 yyextra->lexing_if = 0;
262 /* Handle missing newline at EOF. */
264 BEGIN DONE; /* Don't keep matching this rule forever. */
265 yyextra->lexing_if = 0;
269 /* We don't actually use the UNREACHABLE start condition. We
270 only have this action here so that we can pretend to call some
271 generated functions, (to avoid "defined but not used"
275 yy_top_state(yyextra);
281 glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader)
283 yy_scan_string(shader, parser->scanner);