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 [][(){}.&*~!/%<>^|;,=+-]
74 /* The OTHER class is simply a catch-all for things that the CPP
75 parser just doesn't care about. Since flex regular expressions that
76 match longer strings take priority over those matching shorter
77 strings, we have to be careful to avoid OTHER matching and hiding
78 something that CPP does care about. So we simply exclude all
79 characters that appear in any other expressions. */
81 OTHER [^][_#[:space:]#a-zA-Z0-9(){}.&*~!/%<>^|;,=+-]
84 DECIMAL_INTEGER [1-9][0-9]*[uU]?
85 OCTAL_INTEGER 0[0-7]*[uU]?
86 HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]?
89 /* Implicitly switch between SKIP and INITIAL (non-skipping);
90 * don't switch if some other state was explicitly set.
92 glcpp_parser_t *parser = yyextra;
93 if (YY_START == 0 || YY_START == SKIP) {
94 if (parser->lexing_if || parser->skip_stack == NULL || parser->skip_stack->type == SKIP_NO_SKIP) {
101 /* Single-line comments */
105 /* Multi-line comments */
106 "/*" { yy_push_state(COMMENT, yyscanner); }
108 <COMMENT>[^*\n]*\n { yylineno++; yycolumn = 0; return NEWLINE; }
109 <COMMENT>"*"+[^*/\n]*
110 <COMMENT>"*"+[^*/\n]*\n { yylineno++; yycolumn = 0; return NEWLINE; }
112 yy_pop_state(yyscanner);
113 if (yyextra->space_tokens)
118 yylval->str = ralloc_strdup (yyextra, yytext);
119 yyextra->space_tokens = 0;
123 /* glcpp doesn't handle #extension, #version, or #pragma directives.
124 * Simply pass them through to the main compiler's lexer/parser. */
125 {HASH}(extension|pragma)[^\n]+ {
126 yylval->str = ralloc_strdup (yyextra, yytext);
132 {HASH}line{HSPACE}+{DIGITS}{HSPACE}+{DIGITS}{HSPACE}*$ {
133 /* Eat characters until the first digit is
137 while (!isdigit(*ptr))
140 /* Subtract one from the line number because
141 * yylineno is zero-based instead of
144 yylineno = strtol(ptr, &ptr, 0) - 1;
145 yylloc->source = strtol(ptr, NULL, 0);
148 {HASH}line{HSPACE}+{DIGITS}{HSPACE}*$ {
149 /* Eat characters until the first digit is
153 while (!isdigit(*ptr))
156 /* Subtract one from the line number because
157 * yylineno is zero-based instead of
160 yylineno = strtol(ptr, &ptr, 0) - 1;
165 yyextra->lexing_if = 1;
166 yyextra->space_tokens = 0;
171 yyextra->lexing_if = 1;
172 yyextra->space_tokens = 0;
176 {HASH}if/[^_a-zA-Z0-9] {
177 yyextra->lexing_if = 1;
178 yyextra->space_tokens = 0;
183 yyextra->lexing_if = 1;
184 yyextra->space_tokens = 0;
189 yyextra->space_tokens = 0;
194 yyextra->space_tokens = 0;
203 for (p = yytext; !isalpha(p[0]); p++); /* skip " # " */
204 p += 5; /* skip "error" */
205 glcpp_error(yylloc, yyextra, "#error%s", p);
208 {HASH}define{HSPACE}+/{IDENTIFIER}"(" {
209 yyextra->space_tokens = 0;
210 return HASH_DEFINE_FUNC;
214 yyextra->space_tokens = 0;
215 return HASH_DEFINE_OBJ;
219 yyextra->space_tokens = 0;
224 yyextra->space_tokens = 0;
229 yylval->str = ralloc_strdup (yyextra, yytext);
230 return INTEGER_STRING;
234 yylval->str = ralloc_strdup (yyextra, yytext);
235 return INTEGER_STRING;
238 {HEXADECIMAL_INTEGER} {
239 yylval->str = ralloc_strdup (yyextra, yytext);
240 return INTEGER_STRING;
252 return LESS_OR_EQUAL;
256 return GREATER_OR_EQUAL;
284 yylval->str = ralloc_strdup (yyextra, yytext);
293 yylval->str = ralloc_strdup (yyextra, yytext);
298 if (yyextra->space_tokens) {
304 yyextra->lexing_if = 0;
310 /* Handle missing newline at EOF. */
312 BEGIN DONE; /* Don't keep matching this rule forever. */
313 yyextra->lexing_if = 0;
317 /* We don't actually use the UNREACHABLE start condition. We
318 only have this action here so that we can pretend to call some
319 generated functions, (to avoid "defined but not used"
323 yy_top_state(yyextra);
329 glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader)
331 yy_scan_string(shader, parser->scanner);