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);
38 #define YY_NO_UNISTD_H
43 #define YY_USER_ACTION \
45 yylloc->first_column = yycolumn + 1; \
46 yylloc->first_line = yylineno; \
50 #define YY_USER_INIT \
58 %option bison-bridge bison-locations reentrant noyywrap
59 %option extra-type="glcpp_parser_t *"
60 %option prefix="glcpp_"
62 %option never-interactive
64 %x DONE COMMENT UNREACHABLE SKIP
70 HASH ^{HSPACE}*#{HSPACE}*
71 IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]*
72 PUNCTUATION [][(){}.&*~!/%<>^|;,=+-]
73 OTHER [^][(){}.&*~!/%<>^|;,=#[:space:]+-]+
76 DECIMAL_INTEGER [1-9][0-9]*[uU]?
77 OCTAL_INTEGER 0[0-7]*[uU]?
78 HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]?
81 /* Implicitly switch between SKIP and INITIAL (non-skipping);
82 * don't switch if some other state was explicitly set.
84 glcpp_parser_t *parser = yyextra;
85 if (YY_START == 0 || YY_START == SKIP) {
86 if (parser->lexing_if || parser->skip_stack == NULL || parser->skip_stack->type == SKIP_NO_SKIP) {
93 /* Single-line comments */
97 /* Multi-line comments */
98 "/*" { yy_push_state(COMMENT, yyscanner); }
100 <COMMENT>[^*\n]*\n { yylineno++; yycolumn = 0; return NEWLINE; }
101 <COMMENT>"*"+[^*/\n]*
102 <COMMENT>"*"+[^*/\n]*\n { yylineno++; yycolumn = 0; return NEWLINE; }
104 yy_pop_state(yyscanner);
105 if (yyextra->space_tokens)
110 yylval->str = ralloc_strdup (yyextra, yytext);
111 yyextra->space_tokens = 0;
115 /* glcpp doesn't handle #extension, #version, or #pragma directives.
116 * Simply pass them through to the main compiler's lexer/parser. */
117 {HASH}(extension|pragma)[^\n]+ {
118 yylval->str = ralloc_strdup (yyextra, yytext);
124 {HASH}line{HSPACE}+{DIGITS}{HSPACE}+{DIGITS}{HSPACE}*$ {
125 /* Eat characters until the first digit is
129 while (!isdigit(*ptr))
132 /* Subtract one from the line number because
133 * yylineno is zero-based instead of
136 yylineno = strtol(ptr, &ptr, 0) - 1;
137 yylloc->source = strtol(ptr, NULL, 0);
140 {HASH}line{HSPACE}+{DIGITS}{HSPACE}*$ {
141 /* Eat characters until the first digit is
145 while (!isdigit(*ptr))
148 /* Subtract one from the line number because
149 * yylineno is zero-based instead of
152 yylineno = strtol(ptr, &ptr, 0) - 1;
157 yyextra->lexing_if = 1;
158 yyextra->space_tokens = 0;
163 yyextra->lexing_if = 1;
164 yyextra->space_tokens = 0;
168 {HASH}if/[^_a-zA-Z0-9] {
169 yyextra->lexing_if = 1;
170 yyextra->space_tokens = 0;
175 yyextra->lexing_if = 1;
176 yyextra->space_tokens = 0;
181 yyextra->space_tokens = 0;
186 yyextra->space_tokens = 0;
195 for (p = yytext; !isalpha(p[0]); p++); /* skip " # " */
196 p += 5; /* skip "error" */
197 glcpp_error(yylloc, yyextra, "#error%s", p);
200 {HASH}define{HSPACE}+/{IDENTIFIER}"(" {
201 yyextra->space_tokens = 0;
202 return HASH_DEFINE_FUNC;
206 yyextra->space_tokens = 0;
207 return HASH_DEFINE_OBJ;
211 yyextra->space_tokens = 0;
216 yyextra->space_tokens = 0;
221 yylval->str = ralloc_strdup (yyextra, yytext);
222 return INTEGER_STRING;
226 yylval->str = ralloc_strdup (yyextra, yytext);
227 return INTEGER_STRING;
230 {HEXADECIMAL_INTEGER} {
231 yylval->str = ralloc_strdup (yyextra, yytext);
232 return INTEGER_STRING;
244 return LESS_OR_EQUAL;
248 return GREATER_OR_EQUAL;
276 yylval->str = ralloc_strdup (yyextra, yytext);
285 yylval->str = ralloc_strdup (yyextra, yytext);
290 if (yyextra->space_tokens) {
296 yyextra->lexing_if = 0;
302 /* Handle missing newline at EOF. */
304 BEGIN DONE; /* Don't keep matching this rule forever. */
305 yyextra->lexing_if = 0;
309 /* We don't actually use the UNREACHABLE start condition. We
310 only have this action here so that we can pretend to call some
311 generated functions, (to avoid "defined but not used"
315 yy_top_state(yyextra);
321 glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader)
323 yy_scan_string(shader, parser->scanner);