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 \
64 /* It's ugly to have macros that have return statements inside of
65 * them, but flex-based lexer generation is all built around the
68 * To mitigate the ugliness, we defer as much of the logic as possible
69 * to an actual function, not a macro (see
70 * glcpplex_update_state_per_token) and we make the word RETURN
71 * prominent in all of the macros which may return.
73 * The most-commonly-used macro is RETURN_TOKEN which will perform all
74 * necessary state updates based on the provided token,, then
75 * conditionally return the token. It will not return a token if the
76 * parser is currently skipping tokens, (such as within #if
79 * The RETURN_TOKEN_NEVER_SKIP macro is a lower-level variant that
80 * makes the token returning unconditional. This is needed for things
81 * like #if and the tokens of its condition, (since these must be
82 * evaluated by the parser even when otherwise skipping).
84 * Finally, RETURN_STRING_TOKEN is a simple convenience wrapper on top
85 * of RETURN_TOKEN that performs a string copy of yytext before the
88 #define RETURN_TOKEN_NEVER_SKIP(token) \
90 if (token == NEWLINE) \
91 parser->last_token_was_newline = 1; \
93 parser->last_token_was_newline = 0; \
97 #define RETURN_TOKEN(token) \
99 if (! parser->skipping) { \
100 RETURN_TOKEN_NEVER_SKIP(token); \
104 #define RETURN_STRING_TOKEN(token) \
106 if (! parser->skipping) { \
107 yylval->str = ralloc_strdup (yyextra, yytext); \
108 RETURN_TOKEN_NEVER_SKIP (token); \
114 %option bison-bridge bison-locations reentrant noyywrap
115 %option extra-type="glcpp_parser_t *"
116 %option prefix="glcpp_"
118 %option never-interactive
120 %x DONE COMMENT UNREACHABLE DEFINE NEWLINE_CATCHUP
123 NONSPACE [^[:space:]]
126 HASH ^{HSPACE}*#{HSPACE}*
127 IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]*
128 PP_NUMBER [.]?[0-9]([._a-zA-Z0-9]|[eEpP][-+])*
129 PUNCTUATION [][(){}.&*~!/%<>^|;,=+-]
131 /* The OTHER class is simply a catch-all for things that the CPP
132 parser just doesn't care about. Since flex regular expressions that
133 match longer strings take priority over those matching shorter
134 strings, we have to be careful to avoid OTHER matching and hiding
135 something that CPP does care about. So we simply exclude all
136 characters that appear in any other expressions. */
138 OTHER [^][_#[:space:]#a-zA-Z0-9(){}.&*~!/%<>^|;,=+-]
141 DECIMAL_INTEGER [1-9][0-9]*[uU]?
142 OCTAL_INTEGER 0[0-7]*[uU]?
143 HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]?
147 glcpp_parser_t *parser = yyextra;
149 /* When we lex a multi-line comment, we replace it (as
150 * specified) with a single space. But if the comment spanned
151 * multiple lines, then subsequent parsing stages will not
152 * count correct line numbers. To avoid this problem we keep
153 * track of all newlines that were commented out by a
154 * multi-line comment, and we emit a NEWLINE token for each at
155 * the next legal opportunity, (which is when the lexer would
156 * be emitting a NEWLINE token anyway).
158 if (YY_START == NEWLINE_CATCHUP) {
159 if (parser->commented_newlines)
160 parser->commented_newlines--;
161 if (parser->commented_newlines == 0)
163 RETURN_TOKEN (NEWLINE);
166 /* Set up the parser->skipping bit here before doing any lexing.
168 * This bit controls whether tokens are skipped, (as implemented by
169 * RETURN_TOKEN), such as between "#if 0" and "#endif".
171 * The parser maintains a skip_stack indicating whether we should be
172 * skipping, (and nested levels of #if/#ifdef/#ifndef/#endif) will
173 * push and pop items from the stack.
175 * Here are the rules for determining whether we are skipping:
177 * 1. If the skip stack is NULL, we are outside of all #if blocks
178 * and we are not skipping.
180 * 2. If the skip stack is non-NULL, the type of the top node in
181 * the stack determines whether to skip. A type of
182 * SKIP_NO_SKIP is used for blocks wheere we are emitting
183 * tokens, (such as between #if 1 and #endif, or after the
184 * #else of an #if 0, etc.).
186 * 3. The lexing_directive bit overrides the skip stack. This bit
187 * is set when we are actively lexing the expression for a
188 * pre-processor condition, (such as #if, #elif, or #else). In
189 * this case, even if otherwise skipping, we need to emit the
190 * tokens for this condition so that the parser can evaluate
191 * the expression. (For, #else, there's no expression, but we
192 * emit tokens so the parser can generate a nice error message
193 * if there are any tokens here).
195 if (parser->skip_stack &&
196 parser->skip_stack->type != SKIP_NO_SKIP &&
197 ! parser->lexing_directive)
199 parser->skipping = 1;
201 parser->skipping = 0;
204 /* Single-line comments */
208 /* Multi-line comments */
209 <DEFINE,INITIAL>"/*" { yy_push_state(COMMENT, yyscanner); }
211 <COMMENT>[^*\n]*\n { yylineno++; yycolumn = 0; parser->commented_newlines++; }
212 <COMMENT>"*"+[^*/\n]*
213 <COMMENT>"*"+[^*/\n]*\n { yylineno++; yycolumn = 0; parser->commented_newlines++; }
215 yy_pop_state(yyscanner);
216 if (yyextra->space_tokens)
217 RETURN_TOKEN (SPACE);
220 {HASH}version{HSPACE}+ {
221 yyextra->space_tokens = 0;
222 RETURN_STRING_TOKEN (HASH_VERSION);
225 /* glcpp doesn't handle #extension, #version, or #pragma directives.
226 * Simply pass them through to the main compiler's lexer/parser. */
227 {HASH}(extension|pragma)[^\n]* {
230 RETURN_STRING_TOKEN (OTHER);
233 {HASH}line{HSPACE}+ {
234 RETURN_TOKEN (HASH_LINE);
237 /* For the pre-processor directives, we return these tokens
238 * even when we are otherwise skipping. */
240 yyextra->lexing_directive = 1;
241 yyextra->space_tokens = 0;
242 RETURN_TOKEN_NEVER_SKIP (HASH_IFDEF);
246 yyextra->lexing_directive = 1;
247 yyextra->space_tokens = 0;
248 RETURN_TOKEN_NEVER_SKIP (HASH_IFNDEF);
251 {HASH}if/[^_a-zA-Z0-9] {
252 yyextra->lexing_directive = 1;
253 yyextra->space_tokens = 0;
254 RETURN_TOKEN_NEVER_SKIP (HASH_IF);
257 {HASH}elif/[^_a-zA-Z0-9] {
258 yyextra->lexing_directive = 1;
259 yyextra->space_tokens = 0;
260 RETURN_TOKEN_NEVER_SKIP (HASH_ELIF);
264 yyextra->space_tokens = 0;
265 RETURN_TOKEN_NEVER_SKIP (HASH_ELSE);
269 yyextra->space_tokens = 0;
270 RETURN_TOKEN_NEVER_SKIP (HASH_ENDIF);
274 if (! parser->skipping) {
276 for (p = yytext; !isalpha(p[0]); p++); /* skip " # " */
277 p += 5; /* skip "error" */
278 glcpp_error(yylloc, yyextra, "#error%s", p);
282 /* After we see a "#define" we enter the <DEFINE> start state
283 * for the lexer. Within <DEFINE> we are looking for the first
284 * identifier and specifically checking whether the identifier
285 * is followed by a '(' or not, (to lex either a
286 * FUNC_IDENTIFIER or an OBJ_IDENITIFIER token).
288 * While in the <DEFINE> state we also need to explicitly
289 * handle a few other things that may appear before the
292 * * Comments, (handled above with the main support for
295 * * Whitespace (simply ignored)
297 * * Anything else, (not an identifier, not a comment,
298 * and not whitespace). This will generate an error.
300 {HASH}define{HSPACE}+ {
301 if (! parser->skipping) {
303 yyextra->space_tokens = 0;
304 RETURN_TOKEN (HASH_DEFINE);
308 /* An identifier immediately followed by '(' */
309 <DEFINE>{IDENTIFIER}/"(" {
311 RETURN_STRING_TOKEN (FUNC_IDENTIFIER);
314 /* An identifier not immediately followed by '(' */
315 <DEFINE>{IDENTIFIER} {
317 RETURN_STRING_TOKEN (OBJ_IDENTIFIER);
322 /* Just ignore it. Nothing to do here. */
325 /* '/' not followed by '*', so not a comment. This is an error. */
326 <DEFINE>[/][^*]{NONSPACE}* {
328 glcpp_error(yylloc, yyextra, "#define followed by a non-identifier: %s", yytext);
329 RETURN_STRING_TOKEN (INTEGER_STRING);
332 /* A character that can't start an identifier, comment, or
333 * space. This is an error. */
334 <DEFINE>[^_a-zA-Z/[:space:]]{NONSPACE}* {
336 glcpp_error(yylloc, yyextra, "#define followed by a non-identifier: %s", yytext);
337 RETURN_STRING_TOKEN (INTEGER_STRING);
341 yyextra->space_tokens = 0;
342 RETURN_TOKEN (HASH_UNDEF);
346 yyextra->space_tokens = 0;
347 RETURN_TOKEN (HASH_TOKEN);
351 RETURN_STRING_TOKEN (INTEGER_STRING);
355 RETURN_STRING_TOKEN (INTEGER_STRING);
358 {HEXADECIMAL_INTEGER} {
359 RETURN_STRING_TOKEN (INTEGER_STRING);
363 RETURN_TOKEN (LEFT_SHIFT);
367 RETURN_TOKEN (RIGHT_SHIFT);
371 RETURN_TOKEN (LESS_OR_EQUAL);
375 RETURN_TOKEN (GREATER_OR_EQUAL);
379 RETURN_TOKEN (EQUAL);
383 RETURN_TOKEN (NOT_EQUAL);
395 if (! parser->skipping) {
397 glcpp_error(yylloc, yyextra, "Token pasting (##) is illegal in GLES");
398 RETURN_TOKEN (PASTE);
403 RETURN_TOKEN (DEFINED);
407 RETURN_STRING_TOKEN (IDENTIFIER);
411 RETURN_STRING_TOKEN (OTHER);
415 RETURN_TOKEN (yytext[0]);
419 RETURN_STRING_TOKEN (OTHER);
423 if (yyextra->space_tokens) {
424 RETURN_TOKEN (SPACE);
428 /* We preserve all newlines, even between #if 0..#endif, so no
431 if (parser->commented_newlines) {
432 BEGIN NEWLINE_CATCHUP;
434 yyextra->space_tokens = 1;
435 yyextra->lexing_directive = 0;
438 RETURN_TOKEN_NEVER_SKIP (NEWLINE);
441 <INITIAL,COMMENT,DEFINE><<EOF>> {
442 if (YY_START == COMMENT)
443 glcpp_error(yylloc, yyextra, "Unterminated comment");
444 if (YY_START == DEFINE)
445 glcpp_error(yylloc, yyextra, "#define without macro name");
446 BEGIN DONE; /* Don't keep matching this rule forever. */
447 yyextra->lexing_directive = 0;
448 if (! parser->last_token_was_newline)
449 RETURN_TOKEN (NEWLINE);
452 /* We don't actually use the UNREACHABLE start condition. We
453 only have this action here so that we can pretend to call some
454 generated functions, (to avoid "defined but not used"
458 yy_top_state(yyextra);
464 glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader)
466 yy_scan_string(shader, parser->scanner);