glsl/glcpp: Add (non)-support for ++ and -- operators
[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
164 %x DONE COMMENT HASH UNREACHABLE DEFINE NEWLINE_CATCHUP
165
166 SPACE [[:space:]]
167 NONSPACE [^[:space:]]
168 NEWLINE [\n]
169 HSPACE [ \t]
170 HASH #
171 IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]*
172 PP_NUMBER [.]?[0-9]([._a-zA-Z0-9]|[eEpP][-+])*
173 PUNCTUATION [][(){}.&*~!/%<>^|;,=+-]
174
175 /* The OTHER class is simply a catch-all for things that the CPP
176 parser just doesn't care about. Since flex regular expressions that
177 match longer strings take priority over those matching shorter
178 strings, we have to be careful to avoid OTHER matching and hiding
179 something that CPP does care about. So we simply exclude all
180 characters that appear in any other expressions. */
181
182 OTHER [^][_#[:space:]#a-zA-Z0-9(){}.&*~!/%<>^|;,=+-]
183
184 DIGITS [0-9][0-9]*
185 DECIMAL_INTEGER [1-9][0-9]*[uU]?
186 OCTAL_INTEGER 0[0-7]*[uU]?
187 HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]?
188
189 %%
190
191 glcpp_parser_t *parser = yyextra;
192
193 /* When we lex a multi-line comment, we replace it (as
194 * specified) with a single space. But if the comment spanned
195 * multiple lines, then subsequent parsing stages will not
196 * count correct line numbers. To avoid this problem we keep
197 * track of all newlines that were commented out by a
198 * multi-line comment, and we emit a NEWLINE token for each at
199 * the next legal opportunity, (which is when the lexer would
200 * be emitting a NEWLINE token anyway).
201 */
202 if (YY_START == NEWLINE_CATCHUP) {
203 if (parser->commented_newlines)
204 parser->commented_newlines--;
205 if (parser->commented_newlines == 0)
206 BEGIN INITIAL;
207 RETURN_TOKEN_NEVER_SKIP (NEWLINE);
208 }
209
210 /* Set up the parser->skipping bit here before doing any lexing.
211 *
212 * This bit controls whether tokens are skipped, (as implemented by
213 * RETURN_TOKEN), such as between "#if 0" and "#endif".
214 *
215 * The parser maintains a skip_stack indicating whether we should be
216 * skipping, (and nested levels of #if/#ifdef/#ifndef/#endif) will
217 * push and pop items from the stack.
218 *
219 * Here are the rules for determining whether we are skipping:
220 *
221 * 1. If the skip stack is NULL, we are outside of all #if blocks
222 * and we are not skipping.
223 *
224 * 2. If the skip stack is non-NULL, the type of the top node in
225 * the stack determines whether to skip. A type of
226 * SKIP_NO_SKIP is used for blocks wheere we are emitting
227 * tokens, (such as between #if 1 and #endif, or after the
228 * #else of an #if 0, etc.).
229 *
230 * 3. The lexing_directive bit overrides the skip stack. This bit
231 * is set when we are actively lexing the expression for a
232 * pre-processor condition, (such as #if, #elif, or #else). In
233 * this case, even if otherwise skipping, we need to emit the
234 * tokens for this condition so that the parser can evaluate
235 * the expression. (For, #else, there's no expression, but we
236 * emit tokens so the parser can generate a nice error message
237 * if there are any tokens here).
238 */
239 if (parser->skip_stack &&
240 parser->skip_stack->type != SKIP_NO_SKIP &&
241 ! parser->lexing_directive)
242 {
243 parser->skipping = 1;
244 } else {
245 parser->skipping = 0;
246 }
247
248 /* Single-line comments */
249 "//"[^\n]* {
250 }
251
252 /* Multi-line comments */
253 <DEFINE,HASH,INITIAL>"/*" { yy_push_state(COMMENT, yyscanner); }
254 <COMMENT>[^*\n]*
255 <COMMENT>[^*\n]*\n { yylineno++; yycolumn = 0; parser->commented_newlines++; }
256 <COMMENT>"*"+[^*/\n]*
257 <COMMENT>"*"+[^*/\n]*\n { yylineno++; yycolumn = 0; parser->commented_newlines++; }
258 <COMMENT>"*"+"/" {
259 yy_pop_state(yyscanner);
260 /* In the <HASH> start condition, we don't want any SPACE token. */
261 if (yyextra->space_tokens && YY_START != HASH)
262 RETURN_TOKEN (SPACE);
263 }
264
265 {HASH} {
266
267 /* If the '#' is the first non-whitespace, non-comment token on this
268 * line, then it introduces a directive, switch to the <HASH> start
269 * condition.
270 *
271 * Otherwise, this is just punctuation, so return the HASH_TOKEN
272 * token. */
273 if (parser->first_non_space_token_this_line) {
274 BEGIN HASH;
275 }
276
277 RETURN_TOKEN_NEVER_SKIP (HASH_TOKEN);
278 }
279
280 <HASH>version{HSPACE}+ {
281 BEGIN INITIAL;
282 yyextra->space_tokens = 0;
283 RETURN_STRING_TOKEN (VERSION_TOKEN);
284 }
285
286 /* glcpp doesn't handle #extension, #version, or #pragma directives.
287 * Simply pass them through to the main compiler's lexer/parser. */
288 <HASH>(extension|pragma)[^\n]* {
289 BEGIN INITIAL;
290 yylineno++;
291 yycolumn = 0;
292 RETURN_STRING_TOKEN (PRAGMA);
293 }
294
295 <HASH>line{HSPACE}+ {
296 BEGIN INITIAL;
297 RETURN_TOKEN (LINE);
298 }
299
300 <HASH>\n {
301 BEGIN INITIAL;
302 RETURN_TOKEN_NEVER_SKIP (NEWLINE);
303 }
304
305 /* For the pre-processor directives, we return these tokens
306 * even when we are otherwise skipping. */
307 <HASH>ifdef {
308 BEGIN INITIAL;
309 yyextra->lexing_directive = 1;
310 yyextra->space_tokens = 0;
311 RETURN_TOKEN_NEVER_SKIP (IFDEF);
312 }
313
314 <HASH>ifndef {
315 BEGIN INITIAL;
316 yyextra->lexing_directive = 1;
317 yyextra->space_tokens = 0;
318 RETURN_TOKEN_NEVER_SKIP (IFNDEF);
319 }
320
321 <HASH>if/[^_a-zA-Z0-9] {
322 BEGIN INITIAL;
323 yyextra->lexing_directive = 1;
324 yyextra->space_tokens = 0;
325 RETURN_TOKEN_NEVER_SKIP (IF);
326 }
327
328 <HASH>elif/[^_a-zA-Z0-9] {
329 BEGIN INITIAL;
330 yyextra->lexing_directive = 1;
331 yyextra->space_tokens = 0;
332 RETURN_TOKEN_NEVER_SKIP (ELIF);
333 }
334
335 <HASH>else {
336 BEGIN INITIAL;
337 yyextra->space_tokens = 0;
338 RETURN_TOKEN_NEVER_SKIP (ELSE);
339 }
340
341 <HASH>endif {
342 BEGIN INITIAL;
343 yyextra->space_tokens = 0;
344 RETURN_TOKEN_NEVER_SKIP (ENDIF);
345 }
346
347 <HASH>error.* {
348 BEGIN INITIAL;
349 RETURN_STRING_TOKEN (ERROR);
350 }
351
352 /* After we see a "#define" we enter the <DEFINE> start state
353 * for the lexer. Within <DEFINE> we are looking for the first
354 * identifier and specifically checking whether the identifier
355 * is followed by a '(' or not, (to lex either a
356 * FUNC_IDENTIFIER or an OBJ_IDENITIFIER token).
357 *
358 * While in the <DEFINE> state we also need to explicitly
359 * handle a few other things that may appear before the
360 * identifier:
361 *
362 * * Comments, (handled above with the main support for
363 * comments).
364 *
365 * * Whitespace (simply ignored)
366 *
367 * * Anything else, (not an identifier, not a comment,
368 * and not whitespace). This will generate an error.
369 */
370 <HASH>define{HSPACE}+ {
371 if (! parser->skipping) {
372 BEGIN DEFINE;
373 yyextra->space_tokens = 0;
374 RETURN_TOKEN (DEFINE_TOKEN);
375 }
376 }
377
378 <HASH>undef {
379 BEGIN INITIAL;
380 yyextra->space_tokens = 0;
381 RETURN_TOKEN (UNDEF);
382 }
383
384 <HASH>{HSPACE}+ {
385 /* Nothing to do here. Importantly, don't leave the <HASH>
386 * start condition, since it's legal to have space between the
387 * '#' and the directive.. */
388 }
389
390 /* This will catch any non-directive garbage after a HASH */
391 <HASH>{NONSPACE} {
392 BEGIN INITIAL;
393 RETURN_TOKEN (GARBAGE);
394 }
395
396 /* An identifier immediately followed by '(' */
397 <DEFINE>{IDENTIFIER}/"(" {
398 BEGIN INITIAL;
399 RETURN_STRING_TOKEN (FUNC_IDENTIFIER);
400 }
401
402 /* An identifier not immediately followed by '(' */
403 <DEFINE>{IDENTIFIER} {
404 BEGIN INITIAL;
405 RETURN_STRING_TOKEN (OBJ_IDENTIFIER);
406 }
407
408 /* Whitespace */
409 <DEFINE>{HSPACE}+ {
410 /* Just ignore it. Nothing to do here. */
411 }
412
413 /* '/' not followed by '*', so not a comment. This is an error. */
414 <DEFINE>[/][^*]{NONSPACE}* {
415 BEGIN INITIAL;
416 glcpp_error(yylloc, yyextra, "#define followed by a non-identifier: %s", yytext);
417 RETURN_STRING_TOKEN (INTEGER_STRING);
418 }
419
420 /* A character that can't start an identifier, comment, or
421 * space. This is an error. */
422 <DEFINE>[^_a-zA-Z/[:space:]]{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 {DECIMAL_INTEGER} {
429 RETURN_STRING_TOKEN (INTEGER_STRING);
430 }
431
432 {OCTAL_INTEGER} {
433 RETURN_STRING_TOKEN (INTEGER_STRING);
434 }
435
436 {HEXADECIMAL_INTEGER} {
437 RETURN_STRING_TOKEN (INTEGER_STRING);
438 }
439
440 "<<" {
441 RETURN_TOKEN (LEFT_SHIFT);
442 }
443
444 ">>" {
445 RETURN_TOKEN (RIGHT_SHIFT);
446 }
447
448 "<=" {
449 RETURN_TOKEN (LESS_OR_EQUAL);
450 }
451
452 ">=" {
453 RETURN_TOKEN (GREATER_OR_EQUAL);
454 }
455
456 "==" {
457 RETURN_TOKEN (EQUAL);
458 }
459
460 "!=" {
461 RETURN_TOKEN (NOT_EQUAL);
462 }
463
464 "&&" {
465 RETURN_TOKEN (AND);
466 }
467
468 "||" {
469 RETURN_TOKEN (OR);
470 }
471
472 "++" {
473 RETURN_TOKEN (PLUS_PLUS);
474 }
475
476 "--" {
477 RETURN_TOKEN (MINUS_MINUS);
478 }
479
480 "##" {
481 if (! parser->skipping) {
482 if (parser->is_gles)
483 glcpp_error(yylloc, yyextra, "Token pasting (##) is illegal in GLES");
484 RETURN_TOKEN (PASTE);
485 }
486 }
487
488 "defined" {
489 RETURN_TOKEN (DEFINED);
490 }
491
492 {IDENTIFIER} {
493 RETURN_STRING_TOKEN (IDENTIFIER);
494 }
495
496 {PP_NUMBER} {
497 RETURN_STRING_TOKEN (OTHER);
498 }
499
500 {PUNCTUATION} {
501 RETURN_TOKEN (yytext[0]);
502 }
503
504 {OTHER}+ {
505 RETURN_STRING_TOKEN (OTHER);
506 }
507
508 {HSPACE} {
509 if (yyextra->space_tokens) {
510 RETURN_TOKEN (SPACE);
511 }
512 }
513
514 /* We preserve all newlines, even between #if 0..#endif, so no
515 skipping.. */
516 \n {
517 if (parser->commented_newlines) {
518 BEGIN NEWLINE_CATCHUP;
519 }
520 yyextra->space_tokens = 1;
521 yyextra->lexing_directive = 0;
522 yylineno++;
523 yycolumn = 0;
524 RETURN_TOKEN_NEVER_SKIP (NEWLINE);
525 }
526
527 <INITIAL,COMMENT,DEFINE,HASH><<EOF>> {
528 if (YY_START == COMMENT)
529 glcpp_error(yylloc, yyextra, "Unterminated comment");
530 if (YY_START == DEFINE)
531 glcpp_error(yylloc, yyextra, "#define without macro name");
532 BEGIN DONE; /* Don't keep matching this rule forever. */
533 yyextra->lexing_directive = 0;
534 if (! parser->last_token_was_newline)
535 RETURN_TOKEN (NEWLINE);
536 }
537
538 /* We don't actually use the UNREACHABLE start condition. We
539 only have this action here so that we can pretend to call some
540 generated functions, (to avoid "defined but not used"
541 warnings. */
542 <UNREACHABLE>. {
543 unput('.');
544 yy_top_state(yyextra);
545 }
546
547 %%
548
549 void
550 glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader)
551 {
552 yy_scan_string(shader, parser->scanner);
553 }