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 = yylloc->last_line = yylineno; \
52 yylloc->last_column = yycolumn + 1; \
53 parser->has_new_line_number = 0; \
54 parser->has_new_source_number = 0; \
57 #define YY_USER_INIT \
65 %option bison-bridge bison-locations reentrant noyywrap
66 %option extra-type="glcpp_parser_t *"
67 %option prefix="glcpp_"
69 %option never-interactive
71 %x DONE COMMENT UNREACHABLE SKIP DEFINE NEWLINE_CATCHUP
77 HASH ^{HSPACE}*#{HSPACE}*
78 IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]*
79 PP_NUMBER [.]?[0-9]([._a-zA-Z0-9]|[eEpP][-+])*
80 PUNCTUATION [][(){}.&*~!/%<>^|;,=+-]
82 /* The OTHER class is simply a catch-all for things that the CPP
83 parser just doesn't care about. Since flex regular expressions that
84 match longer strings take priority over those matching shorter
85 strings, we have to be careful to avoid OTHER matching and hiding
86 something that CPP does care about. So we simply exclude all
87 characters that appear in any other expressions. */
89 OTHER [^][_#[:space:]#a-zA-Z0-9(){}.&*~!/%<>^|;,=+-]
92 DECIMAL_INTEGER [1-9][0-9]*[uU]?
93 OCTAL_INTEGER 0[0-7]*[uU]?
94 HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]?
98 glcpp_parser_t *parser = yyextra;
100 /* When we lex a multi-line comment, we replace it (as
101 * specified) with a single space. But if the comment spanned
102 * multiple lines, then subsequent parsing stages will not
103 * count correct line numbers. To avoid this problem we keep
104 * track of all newlines that were commented out by a
105 * multi-line comment, and we emit a NEWLINE token for each at
106 * the next legal opportunity, (which is when the lexer would
107 * be emitting a NEWLINE token anyway).
109 if (YY_START == NEWLINE_CATCHUP) {
110 if (parser->commented_newlines)
111 parser->commented_newlines--;
112 if (parser->commented_newlines == 0)
117 /* The handling of the SKIP vs INITIAL start states requires
118 * some special handling. Typically, a lexer would change
119 * start states with statements like "BEGIN SKIP" within the
120 * lexer rules. We can't get away with that here, since we
121 * need the parser to actually evaluate expressions for
122 * directives like "#if".
124 * So, here, in code that will be executed on every call to
125 * the lexer,and before any rules, we examine the skip_stack
126 * as set by the parser to know whether to change from INITIAL
127 * to SKIP or from SKIP back to INITIAL.
129 * Three cases cause us to switch out of the SKIP state and
130 * back to the INITIAL state:
132 * 1. The top of the skip_stack is of type SKIP_NO_SKIP
133 * This means we're still evaluating some #if
134 * hierarchy, but we're on a branch of it where
135 * content should not be skipped (such as "#if 1" or
138 * 2. The skip_stack is NULL meaning that we've reached
141 * 3. The lexing_directive bit is set. This indicates that we are
142 * lexing a pre-processor directive, (such as #if, #elif, or
143 * #else). For the #if and #elif directives we always need to
144 * parse the conditions, (even if otherwise within an #if
145 * 0). And for #else, we want to be able to generate an error
146 * if any garbage follows #else.
148 if (YY_START == INITIAL || YY_START == SKIP) {
149 if (parser->lexing_directive ||
150 parser->skip_stack == NULL ||
151 parser->skip_stack->type == SKIP_NO_SKIP)
159 /* Single-line comments */
163 /* Multi-line comments */
164 "/*" { yy_push_state(COMMENT, yyscanner); }
166 <COMMENT>[^*\n]*\n { yylineno++; yycolumn = 0; parser->commented_newlines++; }
167 <COMMENT>"*"+[^*/\n]*
168 <COMMENT>"*"+[^*/\n]*\n { yylineno++; yycolumn = 0; parser->commented_newlines++; }
170 yy_pop_state(yyscanner);
171 if (yyextra->space_tokens)
175 {HASH}version{HSPACE}+ {
176 yylval->str = ralloc_strdup (yyextra, yytext);
177 yyextra->space_tokens = 0;
181 /* glcpp doesn't handle #extension, #version, or #pragma directives.
182 * Simply pass them through to the main compiler's lexer/parser. */
183 {HASH}(extension|pragma)[^\n]* {
184 if (parser->commented_newlines)
185 BEGIN NEWLINE_CATCHUP;
186 yylval->str = ralloc_strdup (yyextra, yytext);
192 {HASH}line{HSPACE}+ {
198 yyextra->lexing_directive = 1;
199 yyextra->space_tokens = 0;
204 yyextra->lexing_directive = 1;
205 yyextra->space_tokens = 0;
209 {HASH}if/[^_a-zA-Z0-9] {
210 yyextra->lexing_directive = 1;
211 yyextra->space_tokens = 0;
215 {HASH}elif/[^_a-zA-Z0-9] {
216 yyextra->lexing_directive = 1;
217 yyextra->space_tokens = 0;
222 yyextra->space_tokens = 0;
227 yyextra->space_tokens = 0;
233 if (parser->commented_newlines)
234 BEGIN NEWLINE_CATCHUP;
239 for (p = yytext; !isalpha(p[0]); p++); /* skip " # " */
240 p += 5; /* skip "error" */
241 glcpp_error(yylloc, yyextra, "#error%s", p);
244 {HASH}define{HSPACE}+ {
245 yyextra->space_tokens = 0;
246 yy_push_state(DEFINE, yyscanner);
250 <DEFINE>{IDENTIFIER}/"(" {
251 yy_pop_state(yyscanner);
252 yylval->str = ralloc_strdup (yyextra, yytext);
253 return FUNC_IDENTIFIER;
256 <DEFINE>{IDENTIFIER} {
257 yy_pop_state(yyscanner);
258 yylval->str = ralloc_strdup (yyextra, yytext);
259 return OBJ_IDENTIFIER;
262 <DEFINE>[^_a-zA-Z]{NONSPACE}* {
264 glcpp_error(yylloc, yyextra, "#define followed by a non-identifier: %s", yytext);
265 return INTEGER_STRING;
269 yyextra->space_tokens = 0;
274 yyextra->space_tokens = 0;
279 yylval->str = ralloc_strdup (yyextra, yytext);
280 return INTEGER_STRING;
284 yylval->str = ralloc_strdup (yyextra, yytext);
285 return INTEGER_STRING;
288 {HEXADECIMAL_INTEGER} {
289 yylval->str = ralloc_strdup (yyextra, yytext);
290 return INTEGER_STRING;
302 return LESS_OR_EQUAL;
306 return GREATER_OR_EQUAL;
327 glcpp_error(yylloc, yyextra, "Token pasting (##) is illegal in GLES");
336 yylval->str = ralloc_strdup (yyextra, yytext);
341 yylval->str = ralloc_strdup (yyextra, yytext);
350 yylval->str = ralloc_strdup (yyextra, yytext);
355 if (yyextra->space_tokens) {
361 if (parser->commented_newlines) {
362 BEGIN NEWLINE_CATCHUP;
364 yyextra->space_tokens = 1;
365 yyextra->lexing_directive = 0;
371 /* Handle missing newline at EOF. */
373 BEGIN DONE; /* Don't keep matching this rule forever. */
374 yyextra->lexing_directive = 0;
378 /* We don't actually use the UNREACHABLE start condition. We
379 only have this action here so that we can pretend to call some
380 generated functions, (to avoid "defined but not used"
384 yy_top_state(yyextra);
390 glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader)
392 yy_scan_string(shader, parser->scanner);