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 yylval->str = ralloc_strdup (yyextra, yytext); \
105 RETURN_TOKEN_NEVER_SKIP (token); \
110 /* Update all state necessary for each token being returned.
112 * Here we'll be tracking newlines and spaces so that the lexer can
113 * alter its behavior as necessary, (for example, '#' has special
114 * significance if it is the first non-whitespace, non-comment token
115 * in a line, but does not otherwise).
117 * NOTE: If this function returns FALSE, then no token should be
118 * returned at all. This is used to suprress duplicate SPACE tokens.
121 glcpp_lex_update_state_per_token (glcpp_parser_t *parser, int token)
123 /* After the first non-space token in a line, we won't
124 * allow any '#' to introduce a directive. */
125 if (token == NEWLINE) {
126 parser->first_non_space_token_this_line = 1;
127 } else if (token != SPACE) {
128 parser->first_non_space_token_this_line = 0;
131 /* Track newlines just to know whether a newline needs
132 * to be inserted if end-of-file comes early. */
133 if (token == NEWLINE) {
134 parser->last_token_was_newline = 1;
136 parser->last_token_was_newline = 0;
139 /* Track spaces to avoid emitting multiple SPACE
140 * tokens in a row. */
141 if (token == SPACE) {
142 if (! parser->last_token_was_space) {
143 parser->last_token_was_space = 1;
146 parser->last_token_was_space = 1;
150 parser->last_token_was_space = 0;
158 %option bison-bridge bison-locations reentrant noyywrap
159 %option extra-type="glcpp_parser_t *"
160 %option prefix="glcpp_"
162 %option never-interactive
164 %x DONE COMMENT HASH UNREACHABLE DEFINE NEWLINE_CATCHUP
167 NONSPACE [^[:space:]]
170 IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]*
171 PP_NUMBER [.]?[0-9]([._a-zA-Z0-9]|[eEpP][-+])*
172 PUNCTUATION [][(){}.&*~!/%<>^|;,=+-]
174 /* The OTHER class is simply a catch-all for things that the CPP
175 parser just doesn't care about. Since flex regular expressions that
176 match longer strings take priority over those matching shorter
177 strings, we have to be careful to avoid OTHER matching and hiding
178 something that CPP does care about. So we simply exclude all
179 characters that appear in any other expressions. */
181 OTHER [^][_#[:space:]#a-zA-Z0-9(){}.&*~!/%<>^|;,=+-]
184 DECIMAL_INTEGER [1-9][0-9]*[uU]?
185 OCTAL_INTEGER 0[0-7]*[uU]?
186 HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]?
190 glcpp_parser_t *parser = yyextra;
192 /* When we lex a multi-line comment, we replace it (as
193 * specified) with a single space. But if the comment spanned
194 * multiple lines, then subsequent parsing stages will not
195 * count correct line numbers. To avoid this problem we keep
196 * track of all newlines that were commented out by a
197 * multi-line comment, and we emit a NEWLINE token for each at
198 * the next legal opportunity, (which is when the lexer would
199 * be emitting a NEWLINE token anyway).
201 if (YY_START == NEWLINE_CATCHUP) {
202 if (parser->commented_newlines)
203 parser->commented_newlines--;
204 if (parser->commented_newlines == 0)
206 RETURN_TOKEN_NEVER_SKIP (NEWLINE);
209 /* Set up the parser->skipping bit here before doing any lexing.
211 * This bit controls whether tokens are skipped, (as implemented by
212 * RETURN_TOKEN), such as between "#if 0" and "#endif".
214 * The parser maintains a skip_stack indicating whether we should be
215 * skipping, (and nested levels of #if/#ifdef/#ifndef/#endif) will
216 * push and pop items from the stack.
218 * Here are the rules for determining whether we are skipping:
220 * 1. If the skip stack is NULL, we are outside of all #if blocks
221 * and we are not skipping.
223 * 2. If the skip stack is non-NULL, the type of the top node in
224 * the stack determines whether to skip. A type of
225 * SKIP_NO_SKIP is used for blocks wheere we are emitting
226 * tokens, (such as between #if 1 and #endif, or after the
227 * #else of an #if 0, etc.).
229 * 3. The lexing_directive bit overrides the skip stack. This bit
230 * is set when we are actively lexing the expression for a
231 * pre-processor condition, (such as #if, #elif, or #else). In
232 * this case, even if otherwise skipping, we need to emit the
233 * tokens for this condition so that the parser can evaluate
234 * the expression. (For, #else, there's no expression, but we
235 * emit tokens so the parser can generate a nice error message
236 * if there are any tokens here).
238 if (parser->skip_stack &&
239 parser->skip_stack->type != SKIP_NO_SKIP &&
240 ! parser->lexing_directive)
242 parser->skipping = 1;
244 parser->skipping = 0;
247 /* Single-line comments */
251 /* Multi-line comments */
252 <DEFINE,HASH,INITIAL>"/*" { yy_push_state(COMMENT, yyscanner); }
254 <COMMENT>[^*\r\n]*[\r\n] { yylineno++; yycolumn = 0; parser->commented_newlines++; }
255 <COMMENT>"*"+[^*/\r\n]*
256 <COMMENT>"*"+[^*/\r\n]*[\r\n] { yylineno++; yycolumn = 0; parser->commented_newlines++; }
258 yy_pop_state(yyscanner);
259 /* In the <HASH> start condition, we don't want any SPACE token. */
260 if (yyextra->space_tokens && YY_START != HASH)
261 RETURN_TOKEN (SPACE);
266 /* If the '#' is the first non-whitespace, non-comment token on this
267 * line, then it introduces a directive, switch to the <HASH> start
270 * Otherwise, this is just punctuation, so return the HASH_TOKEN
272 if (parser->first_non_space_token_this_line) {
276 RETURN_TOKEN_NEVER_SKIP (HASH_TOKEN);
279 <HASH>version{HSPACE}+ {
281 yyextra->space_tokens = 0;
282 RETURN_STRING_TOKEN (VERSION_TOKEN);
285 /* glcpp doesn't handle #extension, #version, or #pragma directives.
286 * Simply pass them through to the main compiler's lexer/parser. */
287 <HASH>(extension|pragma)[^\r\n]* {
291 RETURN_STRING_TOKEN (PRAGMA);
294 <HASH>line{HSPACE}+ {
301 RETURN_TOKEN_NEVER_SKIP (NEWLINE);
304 /* For the pre-processor directives, we return these tokens
305 * even when we are otherwise skipping. */
308 yyextra->lexing_directive = 1;
309 yyextra->space_tokens = 0;
310 RETURN_TOKEN_NEVER_SKIP (IFDEF);
315 yyextra->lexing_directive = 1;
316 yyextra->space_tokens = 0;
317 RETURN_TOKEN_NEVER_SKIP (IFNDEF);
320 <HASH>if/[^_a-zA-Z0-9] {
322 yyextra->lexing_directive = 1;
323 yyextra->space_tokens = 0;
324 RETURN_TOKEN_NEVER_SKIP (IF);
327 <HASH>elif/[^_a-zA-Z0-9] {
329 yyextra->lexing_directive = 1;
330 yyextra->space_tokens = 0;
331 RETURN_TOKEN_NEVER_SKIP (ELIF);
336 yyextra->space_tokens = 0;
337 RETURN_TOKEN_NEVER_SKIP (ELSE);
342 yyextra->space_tokens = 0;
343 RETURN_TOKEN_NEVER_SKIP (ENDIF);
348 RETURN_STRING_TOKEN (ERROR);
351 /* After we see a "#define" we enter the <DEFINE> start state
352 * for the lexer. Within <DEFINE> we are looking for the first
353 * identifier and specifically checking whether the identifier
354 * is followed by a '(' or not, (to lex either a
355 * FUNC_IDENTIFIER or an OBJ_IDENITIFIER token).
357 * While in the <DEFINE> state we also need to explicitly
358 * handle a few other things that may appear before the
361 * * Comments, (handled above with the main support for
364 * * Whitespace (simply ignored)
366 * * Anything else, (not an identifier, not a comment,
367 * and not whitespace). This will generate an error.
369 <HASH>define{HSPACE}+ {
370 if (! parser->skipping) {
372 yyextra->space_tokens = 0;
373 RETURN_TOKEN (DEFINE_TOKEN);
379 yyextra->space_tokens = 0;
380 RETURN_TOKEN (UNDEF);
384 /* Nothing to do here. Importantly, don't leave the <HASH>
385 * start condition, since it's legal to have space between the
386 * '#' and the directive.. */
389 /* This will catch any non-directive garbage after a HASH */
392 RETURN_TOKEN (GARBAGE);
395 /* An identifier immediately followed by '(' */
396 <DEFINE>{IDENTIFIER}/"(" {
398 RETURN_STRING_TOKEN (FUNC_IDENTIFIER);
401 /* An identifier not immediately followed by '(' */
402 <DEFINE>{IDENTIFIER} {
404 RETURN_STRING_TOKEN (OBJ_IDENTIFIER);
409 /* Just ignore it. Nothing to do here. */
412 /* '/' not followed by '*', so not a comment. This is an error. */
413 <DEFINE>[/][^*]{NONSPACE}* {
415 glcpp_error(yylloc, yyextra, "#define followed by a non-identifier: %s", yytext);
416 RETURN_STRING_TOKEN (INTEGER_STRING);
419 /* A character that can't start an identifier, comment, or
420 * space. This is an error. */
421 <DEFINE>[^_a-zA-Z/[:space:]]{NONSPACE}* {
423 glcpp_error(yylloc, yyextra, "#define followed by a non-identifier: %s", yytext);
424 RETURN_STRING_TOKEN (INTEGER_STRING);
428 RETURN_STRING_TOKEN (INTEGER_STRING);
432 RETURN_STRING_TOKEN (INTEGER_STRING);
435 {HEXADECIMAL_INTEGER} {
436 RETURN_STRING_TOKEN (INTEGER_STRING);
440 RETURN_TOKEN (LEFT_SHIFT);
444 RETURN_TOKEN (RIGHT_SHIFT);
448 RETURN_TOKEN (LESS_OR_EQUAL);
452 RETURN_TOKEN (GREATER_OR_EQUAL);
456 RETURN_TOKEN (EQUAL);
460 RETURN_TOKEN (NOT_EQUAL);
472 RETURN_TOKEN (PLUS_PLUS);
476 RETURN_TOKEN (MINUS_MINUS);
480 if (! parser->skipping) {
482 glcpp_error(yylloc, yyextra, "Token pasting (##) is illegal in GLES");
483 RETURN_TOKEN (PASTE);
488 RETURN_TOKEN (DEFINED);
492 RETURN_STRING_TOKEN (IDENTIFIER);
496 RETURN_STRING_TOKEN (OTHER);
500 RETURN_TOKEN (yytext[0]);
504 RETURN_STRING_TOKEN (OTHER);
508 if (yyextra->space_tokens) {
509 RETURN_TOKEN (SPACE);
513 /* We preserve all newlines, even between #if 0..#endif, so no
516 if (parser->commented_newlines) {
517 BEGIN NEWLINE_CATCHUP;
519 yyextra->space_tokens = 1;
520 yyextra->lexing_directive = 0;
523 RETURN_TOKEN_NEVER_SKIP (NEWLINE);
526 <INITIAL,COMMENT,DEFINE,HASH><<EOF>> {
527 if (YY_START == COMMENT)
528 glcpp_error(yylloc, yyextra, "Unterminated comment");
529 if (YY_START == DEFINE)
530 glcpp_error(yylloc, yyextra, "#define without macro name");
531 BEGIN DONE; /* Don't keep matching this rule forever. */
532 yyextra->lexing_directive = 0;
533 if (! parser->last_token_was_newline)
534 RETURN_TOKEN (NEWLINE);
537 /* We don't actually use the UNREACHABLE start condition. We
538 only have this action here so that we can pretend to call some
539 generated functions, (to avoid "defined but not used"
543 yy_top_state(yyextra);
549 glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader)
551 yy_scan_string(shader, parser->scanner);