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 <DEFINE,INITIAL>"/*" { 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 yylval->str = ralloc_strdup (yyextra, yytext);
190 {HASH}line{HSPACE}+ {
196 yyextra->lexing_directive = 1;
197 yyextra->space_tokens = 0;
202 yyextra->lexing_directive = 1;
203 yyextra->space_tokens = 0;
207 {HASH}if/[^_a-zA-Z0-9] {
208 yyextra->lexing_directive = 1;
209 yyextra->space_tokens = 0;
213 {HASH}elif/[^_a-zA-Z0-9] {
214 yyextra->lexing_directive = 1;
215 yyextra->space_tokens = 0;
220 yyextra->space_tokens = 0;
225 yyextra->space_tokens = 0;
235 for (p = yytext; !isalpha(p[0]); p++); /* skip " # " */
236 p += 5; /* skip "error" */
237 glcpp_error(yylloc, yyextra, "#error%s", p);
240 /* After we see a "#define" we enter the <DEFINE> start state
241 * for the lexer. Within <DEFINE> we are looking for the first
242 * identifier and specifically checking whether the identifier
243 * is followed by a '(' or not, (to lex either a
244 * FUNC_IDENTIFIER or an OBJ_IDENITIFIER token).
246 * While in the <DEFINE> state we also need to explicitly
247 * handle a few other things that may appear before the
250 * * Comments, (handled above with the main support for
253 * * Whitespace (simply ignored)
255 * * Anything else, (not an identifier, not a comment,
256 * and not whitespace). This will generate an error.
258 {HASH}define{HSPACE}+ {
259 yyextra->space_tokens = 0;
260 yy_push_state(DEFINE, yyscanner);
264 /* An identifier immediately followed by '(' */
265 <DEFINE>{IDENTIFIER}/"(" {
266 yy_pop_state(yyscanner);
267 yylval->str = ralloc_strdup (yyextra, yytext);
268 return FUNC_IDENTIFIER;
271 /* An identifier not immediately followed by '(' */
272 <DEFINE>{IDENTIFIER} {
273 yy_pop_state(yyscanner);
274 yylval->str = ralloc_strdup (yyextra, yytext);
275 return OBJ_IDENTIFIER;
280 /* Just ignore it. Nothing to do here. */
283 /* '/' not followed by '*', so not a comment. This is an error. */
284 <DEFINE>[/][^*]{NONSPACE}* {
286 glcpp_error(yylloc, yyextra, "#define followed by a non-identifier: %s", yytext);
287 return INTEGER_STRING;
290 /* A character that can't start an identifier, comment, or
291 * space. This is an error. */
292 <DEFINE>[^_a-zA-Z/[:space:]]{NONSPACE}* {
294 glcpp_error(yylloc, yyextra, "#define followed by a non-identifier: %s", yytext);
295 return INTEGER_STRING;
299 yyextra->space_tokens = 0;
304 yyextra->space_tokens = 0;
309 yylval->str = ralloc_strdup (yyextra, yytext);
310 return INTEGER_STRING;
314 yylval->str = ralloc_strdup (yyextra, yytext);
315 return INTEGER_STRING;
318 {HEXADECIMAL_INTEGER} {
319 yylval->str = ralloc_strdup (yyextra, yytext);
320 return INTEGER_STRING;
332 return LESS_OR_EQUAL;
336 return GREATER_OR_EQUAL;
357 glcpp_error(yylloc, yyextra, "Token pasting (##) is illegal in GLES");
366 yylval->str = ralloc_strdup (yyextra, yytext);
371 yylval->str = ralloc_strdup (yyextra, yytext);
380 yylval->str = ralloc_strdup (yyextra, yytext);
385 if (yyextra->space_tokens) {
391 if (parser->commented_newlines) {
392 BEGIN NEWLINE_CATCHUP;
394 yyextra->space_tokens = 1;
395 yyextra->lexing_directive = 0;
401 /* Handle missing newline at EOF. */
403 BEGIN DONE; /* Don't keep matching this rule forever. */
404 yyextra->lexing_directive = 0;
408 /* We don't actually use the UNREACHABLE start condition. We
409 only have this action here so that we can pretend to call some
410 generated functions, (to avoid "defined but not used"
414 yy_top_state(yyextra);
420 glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader)
422 yy_scan_string(shader, parser->scanner);