glsl/glcpp: Swallow empty #pragma directives.
[mesa.git] / src / glsl / glcpp / glcpp-lex.l
1 %{
2 /*
3 * Copyright © 2010 Intel Corporation
4 *
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:
11 *
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
14 * Software.
15 *
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.
23 */
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <ctype.h>
28
29 #include "glcpp.h"
30 #include "glcpp-parse.h"
31
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);
36
37 #ifdef _MSC_VER
38 #define YY_NO_UNISTD_H
39 #endif
40
41 #define YY_NO_INPUT
42
43 #define YY_USER_ACTION \
44 do { \
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; \
51 yycolumn += yyleng; \
52 yylloc->last_column = yycolumn + 1; \
53 parser->has_new_line_number = 0; \
54 parser->has_new_source_number = 0; \
55 } while(0);
56
57 #define YY_USER_INIT \
58 do { \
59 yylineno = 1; \
60 yycolumn = 0; \
61 yylloc->source = 0; \
62 } while(0)
63
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
66 * return statement.
67 *
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.
72 *
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
77 * 0...#else).
78 *
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).
83 *
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
86 * return.
87 */
88 #define RETURN_TOKEN_NEVER_SKIP(token) \
89 do { \
90 if (glcpp_lex_update_state_per_token (parser, token)) \
91 return token; \
92 } while (0)
93
94 #define RETURN_TOKEN(token) \
95 do { \
96 if (! parser->skipping) { \
97 RETURN_TOKEN_NEVER_SKIP(token); \
98 } \
99 } while(0)
100
101 #define RETURN_STRING_TOKEN(token) \
102 do { \
103 if (! parser->skipping) { \
104 yylval->str = ralloc_strdup (yyextra, yytext); \
105 RETURN_TOKEN_NEVER_SKIP (token); \
106 } \
107 } while(0)
108
109
110 /* Update all state necessary for each token being returned.
111 *
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).
116 *
117 * NOTE: If this function returns FALSE, then no token should be
118 * returned at all. This is used to suprress duplicate SPACE tokens.
119 */
120 static int
121 glcpp_lex_update_state_per_token (glcpp_parser_t *parser, int token)
122 {
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;
129 }
130
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;
135 } else {
136 parser->last_token_was_newline = 0;
137 }
138
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;
144 return 1;
145 } else {
146 parser->last_token_was_space = 1;
147 return 0;
148 }
149 } else {
150 parser->last_token_was_space = 0;
151 return 1;
152 }
153 }
154
155
156 %}
157
158 %option bison-bridge bison-locations reentrant noyywrap
159 %option extra-type="glcpp_parser_t *"
160 %option prefix="glcpp_"
161 %option stack
162 %option never-interactive
163 %option warn nodefault
164
165 /* Note: When adding any start conditions to this list, you must also
166 * update the "Internal compiler error" catch-all rule near the end of
167 * this file. */
168
169 %x COMMENT DEFINE DONE HASH NEWLINE_CATCHUP UNREACHABLE
170
171 SPACE [[:space:]]
172 NONSPACE [^[:space:]]
173 HSPACE [ \t]
174 HASH #
175 IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]*
176 PP_NUMBER [.]?[0-9]([._a-zA-Z0-9]|[eEpP][-+])*
177 PUNCTUATION [][(){}.&*~!/%<>^|;,=+-]
178
179 /* The OTHER class is simply a catch-all for things that the CPP
180 parser just doesn't care about. Since flex regular expressions that
181 match longer strings take priority over those matching shorter
182 strings, we have to be careful to avoid OTHER matching and hiding
183 something that CPP does care about. So we simply exclude all
184 characters that appear in any other expressions. */
185
186 OTHER [^][_#[:space:]#a-zA-Z0-9(){}.&*~!/%<>^|;,=+-]
187
188 DIGITS [0-9][0-9]*
189 DECIMAL_INTEGER [1-9][0-9]*[uU]?
190 OCTAL_INTEGER 0[0-7]*[uU]?
191 HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]?
192
193 %%
194
195 glcpp_parser_t *parser = yyextra;
196
197 /* When we lex a multi-line comment, we replace it (as
198 * specified) with a single space. But if the comment spanned
199 * multiple lines, then subsequent parsing stages will not
200 * count correct line numbers. To avoid this problem we keep
201 * track of all newlines that were commented out by a
202 * multi-line comment, and we emit a NEWLINE token for each at
203 * the next legal opportunity, (which is when the lexer would
204 * be emitting a NEWLINE token anyway).
205 */
206 if (YY_START == NEWLINE_CATCHUP) {
207 if (parser->commented_newlines)
208 parser->commented_newlines--;
209 if (parser->commented_newlines == 0)
210 BEGIN INITIAL;
211 RETURN_TOKEN_NEVER_SKIP (NEWLINE);
212 }
213
214 /* Set up the parser->skipping bit here before doing any lexing.
215 *
216 * This bit controls whether tokens are skipped, (as implemented by
217 * RETURN_TOKEN), such as between "#if 0" and "#endif".
218 *
219 * The parser maintains a skip_stack indicating whether we should be
220 * skipping, (and nested levels of #if/#ifdef/#ifndef/#endif) will
221 * push and pop items from the stack.
222 *
223 * Here are the rules for determining whether we are skipping:
224 *
225 * 1. If the skip stack is NULL, we are outside of all #if blocks
226 * and we are not skipping.
227 *
228 * 2. If the skip stack is non-NULL, the type of the top node in
229 * the stack determines whether to skip. A type of
230 * SKIP_NO_SKIP is used for blocks wheere we are emitting
231 * tokens, (such as between #if 1 and #endif, or after the
232 * #else of an #if 0, etc.).
233 *
234 * 3. The lexing_directive bit overrides the skip stack. This bit
235 * is set when we are actively lexing the expression for a
236 * pre-processor condition, (such as #if, #elif, or #else). In
237 * this case, even if otherwise skipping, we need to emit the
238 * tokens for this condition so that the parser can evaluate
239 * the expression. (For, #else, there's no expression, but we
240 * emit tokens so the parser can generate a nice error message
241 * if there are any tokens here).
242 */
243 if (parser->skip_stack &&
244 parser->skip_stack->type != SKIP_NO_SKIP &&
245 ! parser->lexing_directive)
246 {
247 parser->skipping = 1;
248 } else {
249 parser->skipping = 0;
250 }
251
252 /* Single-line comments */
253 <INITIAL,DEFINE,HASH>"//"[^\r\n]* {
254 }
255
256 /* Multi-line comments */
257 <INITIAL,DEFINE,HASH>"/*" { yy_push_state(COMMENT, yyscanner); }
258 <COMMENT>[^*\r\n]*
259 <COMMENT>[^*\r\n]*[\r\n] { yylineno++; yycolumn = 0; parser->commented_newlines++; }
260 <COMMENT>"*"+[^*/\r\n]*
261 <COMMENT>"*"+[^*/\r\n]*[\r\n] { yylineno++; yycolumn = 0; parser->commented_newlines++; }
262 <COMMENT>"*"+"/" {
263 yy_pop_state(yyscanner);
264 /* In the <HASH> start condition, we don't want any SPACE token. */
265 if (yyextra->space_tokens && YY_START != HASH)
266 RETURN_TOKEN (SPACE);
267 }
268
269 {HASH} {
270
271 /* If the '#' is the first non-whitespace, non-comment token on this
272 * line, then it introduces a directive, switch to the <HASH> start
273 * condition.
274 *
275 * Otherwise, this is just punctuation, so return the HASH_TOKEN
276 * token. */
277 if (parser->first_non_space_token_this_line) {
278 BEGIN HASH;
279 }
280
281 RETURN_TOKEN_NEVER_SKIP (HASH_TOKEN);
282 }
283
284 <HASH>version{HSPACE}+ {
285 BEGIN INITIAL;
286 yyextra->space_tokens = 0;
287 RETURN_STRING_TOKEN (VERSION_TOKEN);
288 }
289
290 /* Swallow empty #pragma directives, (to avoid confusing the
291 * downstream compiler). */
292 <HASH>pragma{HSPACE}*/[\r\n] {
293 BEGIN INITIAL;
294 }
295
296 /* glcpp doesn't handle #extension, #version, or #pragma directives.
297 * Simply pass them through to the main compiler's lexer/parser. */
298 <HASH>(extension|pragma)[^\r\n]* {
299 BEGIN INITIAL;
300 RETURN_STRING_TOKEN (PRAGMA);
301 }
302
303 <HASH>line{HSPACE}+ {
304 BEGIN INITIAL;
305 RETURN_TOKEN (LINE);
306 }
307
308 <HASH>\n {
309 BEGIN INITIAL;
310 RETURN_TOKEN_NEVER_SKIP (NEWLINE);
311 }
312
313 /* For the pre-processor directives, we return these tokens
314 * even when we are otherwise skipping. */
315 <HASH>ifdef {
316 BEGIN INITIAL;
317 yyextra->lexing_directive = 1;
318 yyextra->space_tokens = 0;
319 RETURN_TOKEN_NEVER_SKIP (IFDEF);
320 }
321
322 <HASH>ifndef {
323 BEGIN INITIAL;
324 yyextra->lexing_directive = 1;
325 yyextra->space_tokens = 0;
326 RETURN_TOKEN_NEVER_SKIP (IFNDEF);
327 }
328
329 <HASH>if/[^_a-zA-Z0-9] {
330 BEGIN INITIAL;
331 yyextra->lexing_directive = 1;
332 yyextra->space_tokens = 0;
333 RETURN_TOKEN_NEVER_SKIP (IF);
334 }
335
336 <HASH>elif/[^_a-zA-Z0-9] {
337 BEGIN INITIAL;
338 yyextra->lexing_directive = 1;
339 yyextra->space_tokens = 0;
340 RETURN_TOKEN_NEVER_SKIP (ELIF);
341 }
342
343 <HASH>else {
344 BEGIN INITIAL;
345 yyextra->space_tokens = 0;
346 RETURN_TOKEN_NEVER_SKIP (ELSE);
347 }
348
349 <HASH>endif {
350 BEGIN INITIAL;
351 yyextra->space_tokens = 0;
352 RETURN_TOKEN_NEVER_SKIP (ENDIF);
353 }
354
355 <HASH>error.* {
356 BEGIN INITIAL;
357 RETURN_STRING_TOKEN (ERROR_TOKEN);
358 }
359
360 /* After we see a "#define" we enter the <DEFINE> start state
361 * for the lexer. Within <DEFINE> we are looking for the first
362 * identifier and specifically checking whether the identifier
363 * is followed by a '(' or not, (to lex either a
364 * FUNC_IDENTIFIER or an OBJ_IDENITIFIER token).
365 *
366 * While in the <DEFINE> state we also need to explicitly
367 * handle a few other things that may appear before the
368 * identifier:
369 *
370 * * Comments, (handled above with the main support for
371 * comments).
372 *
373 * * Whitespace (simply ignored)
374 *
375 * * Anything else, (not an identifier, not a comment,
376 * and not whitespace). This will generate an error.
377 */
378 <HASH>define{HSPACE}* {
379 if (! parser->skipping) {
380 BEGIN DEFINE;
381 yyextra->space_tokens = 0;
382 RETURN_TOKEN (DEFINE_TOKEN);
383 }
384 }
385
386 <HASH>undef {
387 BEGIN INITIAL;
388 yyextra->space_tokens = 0;
389 RETURN_TOKEN (UNDEF);
390 }
391
392 <HASH>{HSPACE}+ {
393 /* Nothing to do here. Importantly, don't leave the <HASH>
394 * start condition, since it's legal to have space between the
395 * '#' and the directive.. */
396 }
397
398 /* This will catch any non-directive garbage after a HASH */
399 <HASH>{NONSPACE} {
400 BEGIN INITIAL;
401 RETURN_TOKEN (GARBAGE);
402 }
403
404 /* An identifier immediately followed by '(' */
405 <DEFINE>{IDENTIFIER}/"(" {
406 BEGIN INITIAL;
407 RETURN_STRING_TOKEN (FUNC_IDENTIFIER);
408 }
409
410 /* An identifier not immediately followed by '(' */
411 <DEFINE>{IDENTIFIER} {
412 BEGIN INITIAL;
413 RETURN_STRING_TOKEN (OBJ_IDENTIFIER);
414 }
415
416 /* Whitespace */
417 <DEFINE>{HSPACE}+ {
418 /* Just ignore it. Nothing to do here. */
419 }
420
421 /* '/' not followed by '*', so not a comment. This is an error. */
422 <DEFINE>[/][^*]{NONSPACE}* {
423 BEGIN INITIAL;
424 glcpp_error(yylloc, yyextra, "#define followed by a non-identifier: %s", yytext);
425 RETURN_STRING_TOKEN (INTEGER_STRING);
426 }
427
428 /* A character that can't start an identifier, comment, or
429 * space. This is an error. */
430 <DEFINE>[^_a-zA-Z/[:space:]]{NONSPACE}* {
431 BEGIN INITIAL;
432 glcpp_error(yylloc, yyextra, "#define followed by a non-identifier: %s", yytext);
433 RETURN_STRING_TOKEN (INTEGER_STRING);
434 }
435
436 {DECIMAL_INTEGER} {
437 RETURN_STRING_TOKEN (INTEGER_STRING);
438 }
439
440 {OCTAL_INTEGER} {
441 RETURN_STRING_TOKEN (INTEGER_STRING);
442 }
443
444 {HEXADECIMAL_INTEGER} {
445 RETURN_STRING_TOKEN (INTEGER_STRING);
446 }
447
448 "<<" {
449 RETURN_TOKEN (LEFT_SHIFT);
450 }
451
452 ">>" {
453 RETURN_TOKEN (RIGHT_SHIFT);
454 }
455
456 "<=" {
457 RETURN_TOKEN (LESS_OR_EQUAL);
458 }
459
460 ">=" {
461 RETURN_TOKEN (GREATER_OR_EQUAL);
462 }
463
464 "==" {
465 RETURN_TOKEN (EQUAL);
466 }
467
468 "!=" {
469 RETURN_TOKEN (NOT_EQUAL);
470 }
471
472 "&&" {
473 RETURN_TOKEN (AND);
474 }
475
476 "||" {
477 RETURN_TOKEN (OR);
478 }
479
480 "++" {
481 RETURN_TOKEN (PLUS_PLUS);
482 }
483
484 "--" {
485 RETURN_TOKEN (MINUS_MINUS);
486 }
487
488 "##" {
489 if (! parser->skipping) {
490 if (parser->is_gles)
491 glcpp_error(yylloc, yyextra, "Token pasting (##) is illegal in GLES");
492 RETURN_TOKEN (PASTE);
493 }
494 }
495
496 "defined" {
497 RETURN_TOKEN (DEFINED);
498 }
499
500 {IDENTIFIER} {
501 RETURN_STRING_TOKEN (IDENTIFIER);
502 }
503
504 {PP_NUMBER} {
505 RETURN_STRING_TOKEN (OTHER);
506 }
507
508 {PUNCTUATION} {
509 RETURN_TOKEN (yytext[0]);
510 }
511
512 {OTHER}+ {
513 RETURN_STRING_TOKEN (OTHER);
514 }
515
516 {HSPACE} {
517 if (yyextra->space_tokens) {
518 RETURN_TOKEN (SPACE);
519 }
520 }
521
522 /* We preserve all newlines, even between #if 0..#endif, so no
523 skipping.. */
524 <*>[\r\n] {
525 if (parser->commented_newlines) {
526 BEGIN NEWLINE_CATCHUP;
527 } else {
528 BEGIN INITIAL;
529 }
530 yyextra->space_tokens = 1;
531 yyextra->lexing_directive = 0;
532 yylineno++;
533 yycolumn = 0;
534 RETURN_TOKEN_NEVER_SKIP (NEWLINE);
535 }
536
537 <INITIAL,COMMENT,DEFINE,HASH><<EOF>> {
538 if (YY_START == COMMENT)
539 glcpp_error(yylloc, yyextra, "Unterminated comment");
540 BEGIN DONE; /* Don't keep matching this rule forever. */
541 yyextra->lexing_directive = 0;
542 if (! parser->last_token_was_newline)
543 RETURN_TOKEN (NEWLINE);
544 }
545
546 /* This is a catch-all to avoid the annoying default flex action which
547 * matches any character and prints it. If any input ever matches this
548 * rule, then we have made a mistake above and need to fix one or more
549 * of the preceding patterns to match that input. */
550
551 <*>. {
552 glcpp_error(yylloc, yyextra, "Internal compiler error: Unexpected character: %s", yytext);
553
554 /* We don't actually use the UNREACHABLE start condition. We
555 only have this block here so that we can pretend to call some
556 generated functions, (to avoid "defined but not used"
557 warnings. */
558 if (YY_START == UNREACHABLE) {
559 unput('.');
560 yy_top_state(yyextra);
561 }
562 }
563
564 %%
565
566 void
567 glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader)
568 {
569 yy_scan_string(shader, parser->scanner);
570 }