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 if (parser->has_new_line_number) \
46 yylineno = parser->new_line_number; \
47 if (parser->has_new_source_number) \
48 yylloc->source = parser->new_source_number; \
49 yylloc->first_column = yycolumn + 1; \
50 yylloc->first_line = yylineno; \
52 parser->has_new_line_number = 0; \
53 parser->has_new_source_number = 0; \
56 #define YY_USER_INIT \
64 %option bison-bridge bison-locations reentrant noyywrap
65 %option extra-type="glcpp_parser_t *"
66 %option prefix="glcpp_"
68 %option never-interactive
70 %x DONE COMMENT UNREACHABLE SKIP DEFINE
76 HASH ^{HSPACE}*#{HSPACE}*
77 IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]*
78 PUNCTUATION [][(){}.&*~!/%<>^|;,=+-]
80 /* The OTHER class is simply a catch-all for things that the CPP
81 parser just doesn't care about. Since flex regular expressions that
82 match longer strings take priority over those matching shorter
83 strings, we have to be careful to avoid OTHER matching and hiding
84 something that CPP does care about. So we simply exclude all
85 characters that appear in any other expressions. */
87 OTHER [^][_#[:space:]#a-zA-Z0-9(){}.&*~!/%<>^|;,=+-]
90 DECIMAL_INTEGER [1-9][0-9]*[uU]?
91 OCTAL_INTEGER 0[0-7]*[uU]?
92 HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]?
95 /* Implicitly switch between SKIP and INITIAL (non-skipping);
96 * don't switch if some other state was explicitly set.
98 glcpp_parser_t *parser = yyextra;
99 if (YY_START == 0 || YY_START == SKIP) {
100 if (parser->lexing_if || parser->skip_stack == NULL || parser->skip_stack->type == SKIP_NO_SKIP) {
107 /* Single-line comments */
111 /* Multi-line comments */
112 "/*" { yy_push_state(COMMENT, yyscanner); }
114 <COMMENT>[^*\n]*\n { yylineno++; yycolumn = 0; return NEWLINE; }
115 <COMMENT>"*"+[^*/\n]*
116 <COMMENT>"*"+[^*/\n]*\n { yylineno++; yycolumn = 0; return NEWLINE; }
118 yy_pop_state(yyscanner);
119 if (yyextra->space_tokens)
123 {HASH}version{HSPACE}+ {
124 yylval->str = ralloc_strdup (yyextra, yytext);
125 yyextra->space_tokens = 0;
129 /* glcpp doesn't handle #extension, #version, or #pragma directives.
130 * Simply pass them through to the main compiler's lexer/parser. */
131 {HASH}(extension|pragma)[^\n]+ {
132 yylval->str = ralloc_strdup (yyextra, yytext);
138 {HASH}line{HSPACE}+ {
144 yyextra->lexing_if = 1;
145 yyextra->space_tokens = 0;
150 yyextra->lexing_if = 1;
151 yyextra->space_tokens = 0;
155 {HASH}if/[^_a-zA-Z0-9] {
156 yyextra->lexing_if = 1;
157 yyextra->space_tokens = 0;
161 {HASH}elif/[^_a-zA-Z0-9] {
162 yyextra->lexing_if = 1;
163 yyextra->space_tokens = 0;
168 yyextra->space_tokens = 0;
173 yyextra->space_tokens = 0;
182 for (p = yytext; !isalpha(p[0]); p++); /* skip " # " */
183 p += 5; /* skip "error" */
184 glcpp_error(yylloc, yyextra, "#error%s", p);
187 {HASH}define{HSPACE}+ {
188 yyextra->space_tokens = 0;
189 yy_push_state(DEFINE, yyscanner);
193 <DEFINE>{IDENTIFIER}/"(" {
194 yy_pop_state(yyscanner);
195 yylval->str = ralloc_strdup (yyextra, yytext);
196 return FUNC_IDENTIFIER;
199 <DEFINE>{IDENTIFIER} {
200 yy_pop_state(yyscanner);
201 yylval->str = ralloc_strdup (yyextra, yytext);
202 return OBJ_IDENTIFIER;
206 yyextra->space_tokens = 0;
211 yyextra->space_tokens = 0;
216 yylval->str = ralloc_strdup (yyextra, yytext);
217 return INTEGER_STRING;
221 yylval->str = ralloc_strdup (yyextra, yytext);
222 return INTEGER_STRING;
225 {HEXADECIMAL_INTEGER} {
226 yylval->str = ralloc_strdup (yyextra, yytext);
227 return INTEGER_STRING;
239 return LESS_OR_EQUAL;
243 return GREATER_OR_EQUAL;
264 glcpp_error(yylloc, yyextra, "Token pasting (##) is illegal in GLES");
273 yylval->str = ralloc_strdup (yyextra, yytext);
282 yylval->str = ralloc_strdup (yyextra, yytext);
287 if (yyextra->space_tokens) {
293 yyextra->lexing_if = 0;
299 /* Handle missing newline at EOF. */
301 BEGIN DONE; /* Don't keep matching this rule forever. */
302 yyextra->lexing_if = 0;
306 /* We don't actually use the UNREACHABLE start condition. We
307 only have this action here so that we can pretend to call some
308 generated functions, (to avoid "defined but not used"
312 yy_top_state(yyextra);
318 glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader)
320 yy_scan_string(shader, parser->scanner);