glsl/glcpp: Treat carriage return as equivalent to line feed.
[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 HSPACE [ \t]
169 HASH #
170 IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]*
171 PP_NUMBER [.]?[0-9]([._a-zA-Z0-9]|[eEpP][-+])*
172 PUNCTUATION [][(){}.&*~!/%<>^|;,=+-]
173
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. */
180
181 OTHER [^][_#[:space:]#a-zA-Z0-9(){}.&*~!/%<>^|;,=+-]
182
183 DIGITS [0-9][0-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]?
187
188 %%
189
190 glcpp_parser_t *parser = yyextra;
191
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).
200 */
201 if (YY_START == NEWLINE_CATCHUP) {
202 if (parser->commented_newlines)
203 parser->commented_newlines--;
204 if (parser->commented_newlines == 0)
205 BEGIN INITIAL;
206 RETURN_TOKEN_NEVER_SKIP (NEWLINE);
207 }
208
209 /* Set up the parser->skipping bit here before doing any lexing.
210 *
211 * This bit controls whether tokens are skipped, (as implemented by
212 * RETURN_TOKEN), such as between "#if 0" and "#endif".
213 *
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.
217 *
218 * Here are the rules for determining whether we are skipping:
219 *
220 * 1. If the skip stack is NULL, we are outside of all #if blocks
221 * and we are not skipping.
222 *
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.).
228 *
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).
237 */
238 if (parser->skip_stack &&
239 parser->skip_stack->type != SKIP_NO_SKIP &&
240 ! parser->lexing_directive)
241 {
242 parser->skipping = 1;
243 } else {
244 parser->skipping = 0;
245 }
246
247 /* Single-line comments */
248 "//"[^\r\n]* {
249 }
250
251 /* Multi-line comments */
252 <DEFINE,HASH,INITIAL>"/*" { yy_push_state(COMMENT, yyscanner); }
253 <COMMENT>[^*\r\n]*
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++; }
257 <COMMENT>"*"+"/" {
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);
262 }
263
264 {HASH} {
265
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
268 * condition.
269 *
270 * Otherwise, this is just punctuation, so return the HASH_TOKEN
271 * token. */
272 if (parser->first_non_space_token_this_line) {
273 BEGIN HASH;
274 }
275
276 RETURN_TOKEN_NEVER_SKIP (HASH_TOKEN);
277 }
278
279 <HASH>version{HSPACE}+ {
280 BEGIN INITIAL;
281 yyextra->space_tokens = 0;
282 RETURN_STRING_TOKEN (VERSION_TOKEN);
283 }
284
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]* {
288 BEGIN INITIAL;
289 yylineno++;
290 yycolumn = 0;
291 RETURN_STRING_TOKEN (PRAGMA);
292 }
293
294 <HASH>line{HSPACE}+ {
295 BEGIN INITIAL;
296 RETURN_TOKEN (LINE);
297 }
298
299 <HASH>\n {
300 BEGIN INITIAL;
301 RETURN_TOKEN_NEVER_SKIP (NEWLINE);
302 }
303
304 /* For the pre-processor directives, we return these tokens
305 * even when we are otherwise skipping. */
306 <HASH>ifdef {
307 BEGIN INITIAL;
308 yyextra->lexing_directive = 1;
309 yyextra->space_tokens = 0;
310 RETURN_TOKEN_NEVER_SKIP (IFDEF);
311 }
312
313 <HASH>ifndef {
314 BEGIN INITIAL;
315 yyextra->lexing_directive = 1;
316 yyextra->space_tokens = 0;
317 RETURN_TOKEN_NEVER_SKIP (IFNDEF);
318 }
319
320 <HASH>if/[^_a-zA-Z0-9] {
321 BEGIN INITIAL;
322 yyextra->lexing_directive = 1;
323 yyextra->space_tokens = 0;
324 RETURN_TOKEN_NEVER_SKIP (IF);
325 }
326
327 <HASH>elif/[^_a-zA-Z0-9] {
328 BEGIN INITIAL;
329 yyextra->lexing_directive = 1;
330 yyextra->space_tokens = 0;
331 RETURN_TOKEN_NEVER_SKIP (ELIF);
332 }
333
334 <HASH>else {
335 BEGIN INITIAL;
336 yyextra->space_tokens = 0;
337 RETURN_TOKEN_NEVER_SKIP (ELSE);
338 }
339
340 <HASH>endif {
341 BEGIN INITIAL;
342 yyextra->space_tokens = 0;
343 RETURN_TOKEN_NEVER_SKIP (ENDIF);
344 }
345
346 <HASH>error.* {
347 BEGIN INITIAL;
348 RETURN_STRING_TOKEN (ERROR);
349 }
350
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).
356 *
357 * While in the <DEFINE> state we also need to explicitly
358 * handle a few other things that may appear before the
359 * identifier:
360 *
361 * * Comments, (handled above with the main support for
362 * comments).
363 *
364 * * Whitespace (simply ignored)
365 *
366 * * Anything else, (not an identifier, not a comment,
367 * and not whitespace). This will generate an error.
368 */
369 <HASH>define{HSPACE}+ {
370 if (! parser->skipping) {
371 BEGIN DEFINE;
372 yyextra->space_tokens = 0;
373 RETURN_TOKEN (DEFINE_TOKEN);
374 }
375 }
376
377 <HASH>undef {
378 BEGIN INITIAL;
379 yyextra->space_tokens = 0;
380 RETURN_TOKEN (UNDEF);
381 }
382
383 <HASH>{HSPACE}+ {
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.. */
387 }
388
389 /* This will catch any non-directive garbage after a HASH */
390 <HASH>{NONSPACE} {
391 BEGIN INITIAL;
392 RETURN_TOKEN (GARBAGE);
393 }
394
395 /* An identifier immediately followed by '(' */
396 <DEFINE>{IDENTIFIER}/"(" {
397 BEGIN INITIAL;
398 RETURN_STRING_TOKEN (FUNC_IDENTIFIER);
399 }
400
401 /* An identifier not immediately followed by '(' */
402 <DEFINE>{IDENTIFIER} {
403 BEGIN INITIAL;
404 RETURN_STRING_TOKEN (OBJ_IDENTIFIER);
405 }
406
407 /* Whitespace */
408 <DEFINE>{HSPACE}+ {
409 /* Just ignore it. Nothing to do here. */
410 }
411
412 /* '/' not followed by '*', so not a comment. This is an error. */
413 <DEFINE>[/][^*]{NONSPACE}* {
414 BEGIN INITIAL;
415 glcpp_error(yylloc, yyextra, "#define followed by a non-identifier: %s", yytext);
416 RETURN_STRING_TOKEN (INTEGER_STRING);
417 }
418
419 /* A character that can't start an identifier, comment, or
420 * space. This is an error. */
421 <DEFINE>[^_a-zA-Z/[:space:]]{NONSPACE}* {
422 BEGIN INITIAL;
423 glcpp_error(yylloc, yyextra, "#define followed by a non-identifier: %s", yytext);
424 RETURN_STRING_TOKEN (INTEGER_STRING);
425 }
426
427 {DECIMAL_INTEGER} {
428 RETURN_STRING_TOKEN (INTEGER_STRING);
429 }
430
431 {OCTAL_INTEGER} {
432 RETURN_STRING_TOKEN (INTEGER_STRING);
433 }
434
435 {HEXADECIMAL_INTEGER} {
436 RETURN_STRING_TOKEN (INTEGER_STRING);
437 }
438
439 "<<" {
440 RETURN_TOKEN (LEFT_SHIFT);
441 }
442
443 ">>" {
444 RETURN_TOKEN (RIGHT_SHIFT);
445 }
446
447 "<=" {
448 RETURN_TOKEN (LESS_OR_EQUAL);
449 }
450
451 ">=" {
452 RETURN_TOKEN (GREATER_OR_EQUAL);
453 }
454
455 "==" {
456 RETURN_TOKEN (EQUAL);
457 }
458
459 "!=" {
460 RETURN_TOKEN (NOT_EQUAL);
461 }
462
463 "&&" {
464 RETURN_TOKEN (AND);
465 }
466
467 "||" {
468 RETURN_TOKEN (OR);
469 }
470
471 "++" {
472 RETURN_TOKEN (PLUS_PLUS);
473 }
474
475 "--" {
476 RETURN_TOKEN (MINUS_MINUS);
477 }
478
479 "##" {
480 if (! parser->skipping) {
481 if (parser->is_gles)
482 glcpp_error(yylloc, yyextra, "Token pasting (##) is illegal in GLES");
483 RETURN_TOKEN (PASTE);
484 }
485 }
486
487 "defined" {
488 RETURN_TOKEN (DEFINED);
489 }
490
491 {IDENTIFIER} {
492 RETURN_STRING_TOKEN (IDENTIFIER);
493 }
494
495 {PP_NUMBER} {
496 RETURN_STRING_TOKEN (OTHER);
497 }
498
499 {PUNCTUATION} {
500 RETURN_TOKEN (yytext[0]);
501 }
502
503 {OTHER}+ {
504 RETURN_STRING_TOKEN (OTHER);
505 }
506
507 {HSPACE} {
508 if (yyextra->space_tokens) {
509 RETURN_TOKEN (SPACE);
510 }
511 }
512
513 /* We preserve all newlines, even between #if 0..#endif, so no
514 skipping.. */
515 [\r\n] {
516 if (parser->commented_newlines) {
517 BEGIN NEWLINE_CATCHUP;
518 }
519 yyextra->space_tokens = 1;
520 yyextra->lexing_directive = 0;
521 yylineno++;
522 yycolumn = 0;
523 RETURN_TOKEN_NEVER_SKIP (NEWLINE);
524 }
525
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);
535 }
536
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"
540 warnings. */
541 <UNREACHABLE>. {
542 unput('.');
543 yy_top_state(yyextra);
544 }
545
546 %%
547
548 void
549 glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader)
550 {
551 yy_scan_string(shader, parser->scanner);
552 }