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 (glcpp_lex_update_state_per_token (parser, token)) \
94 #define RETURN_TOKEN(token) \
96 if (! parser->skipping) { \
97 RETURN_TOKEN_NEVER_SKIP(token); \
101 #define RETURN_STRING_TOKEN(token) \
103 if (! parser->skipping) { \
104 /* We're not doing linear_strdup here, to avoid \
105 * an implicit call on strlen() for the length \
106 * of the string, as this is already found by \
107 * flex and stored in yyleng */ \
108 void *mem_ctx = yyextra->linalloc; \
109 yylval->str = linear_alloc_child(mem_ctx, \
111 memcpy(yylval->str, yytext, yyleng + 1); \
112 RETURN_TOKEN_NEVER_SKIP (token); \
117 /* Update all state necessary for each token being returned.
119 * Here we'll be tracking newlines and spaces so that the lexer can
120 * alter its behavior as necessary, (for example, '#' has special
121 * significance if it is the first non-whitespace, non-comment token
122 * in a line, but does not otherwise).
124 * NOTE: If this function returns FALSE, then no token should be
125 * returned at all. This is used to suprress duplicate SPACE tokens.
128 glcpp_lex_update_state_per_token (glcpp_parser_t *parser, int token)
130 if (token != NEWLINE && token != SPACE && token != HASH_TOKEN &&
131 !parser->lexing_version_directive) {
132 glcpp_parser_resolve_implicit_version(parser);
135 /* After the first non-space token in a line, we won't
136 * allow any '#' to introduce a directive. */
137 if (token == NEWLINE) {
138 parser->first_non_space_token_this_line = 1;
139 } else if (token != SPACE) {
140 parser->first_non_space_token_this_line = 0;
143 /* Track newlines just to know whether a newline needs
144 * to be inserted if end-of-file comes early. */
145 if (token == NEWLINE) {
146 parser->last_token_was_newline = 1;
148 parser->last_token_was_newline = 0;
151 /* Track spaces to avoid emitting multiple SPACE
152 * tokens in a row. */
153 if (token == SPACE) {
154 if (! parser->last_token_was_space) {
155 parser->last_token_was_space = 1;
158 parser->last_token_was_space = 1;
162 parser->last_token_was_space = 0;
170 %option bison-bridge bison-locations reentrant noyywrap
171 %option extra-type="glcpp_parser_t *"
172 %option prefix="glcpp_"
174 %option never-interactive
175 %option warn nodefault
177 /* Note: When adding any start conditions to this list, you must also
178 * update the "Internal compiler error" catch-all rule near the end of
181 %x COMMENT DEFINE DONE HASH NEWLINE_CATCHUP UNREACHABLE
184 NONSPACE [^[:space:]]
187 NEWLINE (\r\n|\n\r|\r|\n)
188 IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]*
189 PP_NUMBER [.]?[0-9]([._a-zA-Z0-9]|[eEpP][-+])*
190 PUNCTUATION [][(){}.&*~!/%<>^|;,=+-]
192 /* The OTHER class is simply a catch-all for things that the CPP
193 parser just doesn't care about. Since flex regular expressions that
194 match longer strings take priority over those matching shorter
195 strings, we have to be careful to avoid OTHER matching and hiding
196 something that CPP does care about. So we simply exclude all
197 characters that appear in any other expressions. */
199 OTHER [^][_#[:space:]#a-zA-Z0-9(){}.&*~!/%<>^|;,=+-]
202 DECIMAL_INTEGER [1-9][0-9]*[uU]?
203 OCTAL_INTEGER 0[0-7]*[uU]?
204 HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]?
205 PATH ["][]^./ _A-Za-z0-9+*%[(){}|&~=!:;,?-]*["]
209 glcpp_parser_t *parser = yyextra;
211 /* When we lex a multi-line comment, we replace it (as
212 * specified) with a single space. But if the comment spanned
213 * multiple lines, then subsequent parsing stages will not
214 * count correct line numbers. To avoid this problem we keep
215 * track of all newlines that were commented out by a
216 * multi-line comment, and we emit a NEWLINE token for each at
217 * the next legal opportunity, (which is when the lexer would
218 * be emitting a NEWLINE token anyway).
220 if (YY_START == NEWLINE_CATCHUP) {
221 if (parser->commented_newlines)
222 parser->commented_newlines--;
223 if (parser->commented_newlines == 0)
225 RETURN_TOKEN_NEVER_SKIP (NEWLINE);
228 /* Set up the parser->skipping bit here before doing any lexing.
230 * This bit controls whether tokens are skipped, (as implemented by
231 * RETURN_TOKEN), such as between "#if 0" and "#endif".
233 * The parser maintains a skip_stack indicating whether we should be
234 * skipping, (and nested levels of #if/#ifdef/#ifndef/#endif) will
235 * push and pop items from the stack.
237 * Here are the rules for determining whether we are skipping:
239 * 1. If the skip stack is NULL, we are outside of all #if blocks
240 * and we are not skipping.
242 * 2. If the skip stack is non-NULL, the type of the top node in
243 * the stack determines whether to skip. A type of
244 * SKIP_NO_SKIP is used for blocks wheere we are emitting
245 * tokens, (such as between #if 1 and #endif, or after the
246 * #else of an #if 0, etc.).
248 * 3. The lexing_directive bit overrides the skip stack. This bit
249 * is set when we are actively lexing the expression for a
250 * pre-processor condition, (such as #if, #elif, or #else). In
251 * this case, even if otherwise skipping, we need to emit the
252 * tokens for this condition so that the parser can evaluate
253 * the expression. (For, #else, there's no expression, but we
254 * emit tokens so the parser can generate a nice error message
255 * if there are any tokens here).
257 if (parser->skip_stack &&
258 parser->skip_stack->type != SKIP_NO_SKIP &&
259 ! parser->lexing_directive)
261 parser->skipping = 1;
263 parser->skipping = 0;
266 /* Single-line comments */
267 <INITIAL,DEFINE,HASH>"//"[^\r\n]* {
270 /* Multi-line comments */
271 <INITIAL,DEFINE,HASH>"/*" { yy_push_state(COMMENT, yyscanner); }
273 <COMMENT>[^*\r\n]*{NEWLINE} { yylineno++; yycolumn = 0; parser->commented_newlines++; }
274 <COMMENT>"*"+[^*/\r\n]*
275 <COMMENT>"*"+[^*/\r\n]*{NEWLINE} { yylineno++; yycolumn = 0; parser->commented_newlines++; }
277 yy_pop_state(yyscanner);
278 /* In the <HASH> start condition, we don't want any SPACE token. */
279 if (yyextra->space_tokens && YY_START != HASH)
280 RETURN_TOKEN (SPACE);
285 /* If the '#' is the first non-whitespace, non-comment token on this
286 * line, then it introduces a directive, switch to the <HASH> start
289 * Otherwise, this is just punctuation, so return the HASH_TOKEN
291 if (parser->first_non_space_token_this_line) {
293 yyextra->in_define = false;
296 RETURN_TOKEN_NEVER_SKIP (HASH_TOKEN);
299 <HASH>version{HSPACE}+ {
301 yyextra->space_tokens = 0;
302 yyextra->lexing_version_directive = 1;
303 RETURN_STRING_TOKEN (VERSION_TOKEN);
306 /* Swallow empty #pragma directives, (to avoid confusing the
307 * downstream compiler).
309 * Note: We use a simple regular expression for the lookahead
310 * here. Specifically, we cannot use the complete {NEWLINE} expression
311 * since it uses alternation and we've found that there's a flex bug
312 * where using alternation in the lookahead portion of a pattern
313 * triggers a buffer overrun. */
314 <HASH>pragma{HSPACE}*/[\r\n] {
318 /* glcpp doesn't handle #extension, #version, or #pragma directives.
319 * Simply pass them through to the main compiler's lexer/parser. */
320 <HASH>(extension|pragma)[^\r\n]* {
322 RETURN_STRING_TOKEN (PRAGMA);
325 <HASH>include{HSPACE}+["<][]^./ _A-Za-z0-9+*%[(){}|&~=!:;,?-]+[">] {
327 RETURN_STRING_TOKEN (INCLUDE);
330 <HASH>line{HSPACE}+ {
337 yyextra->space_tokens = 0;
340 RETURN_TOKEN_NEVER_SKIP (NEWLINE);
343 /* For the pre-processor directives, we return these tokens
344 * even when we are otherwise skipping. */
346 if (!yyextra->in_define) {
348 yyextra->lexing_directive = 1;
349 yyextra->space_tokens = 0;
350 RETURN_TOKEN_NEVER_SKIP (IFDEF);
355 if (!yyextra->in_define) {
357 yyextra->lexing_directive = 1;
358 yyextra->space_tokens = 0;
359 RETURN_TOKEN_NEVER_SKIP (IFNDEF);
363 <HASH>if/[^_a-zA-Z0-9] {
364 if (!yyextra->in_define) {
366 yyextra->lexing_directive = 1;
367 yyextra->space_tokens = 0;
368 RETURN_TOKEN_NEVER_SKIP (IF);
372 <HASH>elif/[^_a-zA-Z0-9] {
373 if (!yyextra->in_define) {
375 yyextra->lexing_directive = 1;
376 yyextra->space_tokens = 0;
377 RETURN_TOKEN_NEVER_SKIP (ELIF);
382 if (!yyextra->in_define) {
384 yyextra->space_tokens = 0;
385 RETURN_TOKEN_NEVER_SKIP (ELSE);
390 if (!yyextra->in_define) {
392 yyextra->space_tokens = 0;
393 RETURN_TOKEN_NEVER_SKIP (ENDIF);
397 <HASH>error[^\r\n]* {
399 RETURN_STRING_TOKEN (ERROR_TOKEN);
402 /* After we see a "#define" we enter the <DEFINE> start state
403 * for the lexer. Within <DEFINE> we are looking for the first
404 * identifier and specifically checking whether the identifier
405 * is followed by a '(' or not, (to lex either a
406 * FUNC_IDENTIFIER or an OBJ_IDENITIFIER token).
408 * While in the <DEFINE> state we also need to explicitly
409 * handle a few other things that may appear before the
412 * * Comments, (handled above with the main support for
415 * * Whitespace (simply ignored)
417 * * Anything else, (not an identifier, not a comment,
418 * and not whitespace). This will generate an error.
420 <HASH>define{HSPACE}* {
421 yyextra->in_define = true;
422 if (!parser->skipping) {
424 yyextra->space_tokens = 0;
425 RETURN_TOKEN (DEFINE_TOKEN);
431 yyextra->space_tokens = 0;
432 RETURN_TOKEN (UNDEF);
436 /* Nothing to do here. Importantly, don't leave the <HASH>
437 * start condition, since it's legal to have space between the
438 * '#' and the directive.. */
441 /* This will catch any non-directive garbage after a HASH */
443 if (!parser->skipping) {
445 RETURN_TOKEN (GARBAGE);
449 /* An identifier immediately followed by '(' */
450 <DEFINE>{IDENTIFIER}/"(" {
452 RETURN_STRING_TOKEN (FUNC_IDENTIFIER);
455 /* An identifier not immediately followed by '(' */
456 <DEFINE>{IDENTIFIER} {
458 RETURN_STRING_TOKEN (OBJ_IDENTIFIER);
463 /* Just ignore it. Nothing to do here. */
466 /* '/' not followed by '*', so not a comment. This is an error. */
467 <DEFINE>[/][^*]{NONSPACE}* {
469 glcpp_error(yylloc, yyextra, "#define followed by a non-identifier: %s", yytext);
470 RETURN_STRING_TOKEN (INTEGER_STRING);
473 /* A character that can't start an identifier, comment, or
474 * space. This is an error. */
475 <DEFINE>[^_a-zA-Z/[:space:]]{NONSPACE}* {
477 glcpp_error(yylloc, yyextra, "#define followed by a non-identifier: %s", yytext);
478 RETURN_STRING_TOKEN (INTEGER_STRING);
482 RETURN_STRING_TOKEN (INTEGER_STRING);
486 RETURN_STRING_TOKEN (INTEGER_STRING);
489 {HEXADECIMAL_INTEGER} {
490 RETURN_STRING_TOKEN (INTEGER_STRING);
494 RETURN_TOKEN (LEFT_SHIFT);
498 RETURN_TOKEN (RIGHT_SHIFT);
502 RETURN_TOKEN (LESS_OR_EQUAL);
506 RETURN_TOKEN (GREATER_OR_EQUAL);
510 RETURN_TOKEN (EQUAL);
514 RETURN_TOKEN (NOT_EQUAL);
526 RETURN_TOKEN (PLUS_PLUS);
530 RETURN_TOKEN (MINUS_MINUS);
534 if (! parser->skipping) {
536 glcpp_error(yylloc, yyextra, "Token pasting (##) is illegal in GLES");
537 RETURN_TOKEN (PASTE);
542 RETURN_TOKEN (DEFINED);
546 RETURN_STRING_TOKEN (IDENTIFIER);
550 RETURN_STRING_TOKEN (OTHER);
554 RETURN_TOKEN (yytext[0]);
558 RETURN_STRING_TOKEN (OTHER);
562 if (yyextra->space_tokens) {
563 RETURN_TOKEN (SPACE);
568 RETURN_STRING_TOKEN (PATH);
571 /* We preserve all newlines, even between #if 0..#endif, so no
574 if (parser->commented_newlines) {
575 BEGIN NEWLINE_CATCHUP;
579 yyextra->space_tokens = 1;
580 yyextra->lexing_directive = 0;
581 yyextra->lexing_version_directive = 0;
584 RETURN_TOKEN_NEVER_SKIP (NEWLINE);
587 <INITIAL,COMMENT,DEFINE,HASH><<EOF>> {
588 if (YY_START == COMMENT)
589 glcpp_error(yylloc, yyextra, "Unterminated comment");
590 BEGIN DONE; /* Don't keep matching this rule forever. */
591 yyextra->lexing_directive = 0;
592 yyextra->lexing_version_directive = 0;
593 if (! parser->last_token_was_newline)
594 RETURN_TOKEN (NEWLINE);
597 /* This is a catch-all to avoid the annoying default flex action which
598 * matches any character and prints it. If any input ever matches this
599 * rule, then we have made a mistake above and need to fix one or more
600 * of the preceding patterns to match that input. */
603 glcpp_error(yylloc, yyextra, "Internal compiler error: Unexpected character: %s", yytext);
605 /* We don't actually use the UNREACHABLE start condition. We
606 only have this block here so that we can pretend to call some
607 generated functions, (to avoid "defined but not used"
609 if (YY_START == UNREACHABLE) {
611 yy_top_state(yyextra);
618 glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader)
620 yy_scan_string(shader, parser->scanner);