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