Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 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 | |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 28 | #include "glcpp.h" |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 29 | #include "glcpp-parse.h" |
| 30 | %} |
| 31 | |
Kenneth Graunke | 465e03e | 2010-06-16 16:35:57 -0700 | [diff] [blame^] | 32 | %option bison-bridge bison-locations reentrant noyywrap |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 33 | %option extra-type="glcpp_parser_t *" |
Kenneth Graunke | 254a485 | 2010-06-16 11:51:43 -0700 | [diff] [blame] | 34 | %option prefix="glcpp_" |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 35 | |
Kenneth Graunke | f82d673 | 2010-06-16 12:53:19 -0700 | [diff] [blame] | 36 | %x DONE |
| 37 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 38 | SPACE [[:space:]] |
| 39 | NONSPACE [^[:space:]] |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 40 | NEWLINE [\n] |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 41 | HSPACE [ \t] |
Carl Worth | e36a4d5 | 2010-05-14 17:29:24 -0700 | [diff] [blame] | 42 | HASH ^{HSPACE}*#{HSPACE}* |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 43 | IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]* |
Carl Worth | 6310169 | 2010-05-29 05:07:24 -0700 | [diff] [blame] | 44 | PUNCTUATION [][(){}.&*~!/%<>^|;,=+-] |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 45 | OTHER [^][(){}.&*~!/%<>^|;,=#[:space:]+-]+ |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 46 | |
Carl Worth | 03f6d5d | 2010-05-24 11:29:02 -0700 | [diff] [blame] | 47 | DECIMAL_INTEGER [1-9][0-9]*[uU]? |
| 48 | OCTAL_INTEGER 0[0-7]*[uU]? |
| 49 | HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]? |
| 50 | |
Carl Worth | 111e25b | 2010-06-02 12:54:15 -0700 | [diff] [blame] | 51 | NON_STARS_THEN_STARS [^*]*[*]+ |
| 52 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 53 | %% |
| 54 | |
Carl Worth | 2571415 | 2010-06-01 12:18:43 -0700 | [diff] [blame] | 55 | /* Single-line comments */ |
| 56 | "//"[^\n]+\n { |
| 57 | return NEWLINE; |
| 58 | } |
| 59 | |
| 60 | /* Multi-line comments */ |
Carl Worth | 111e25b | 2010-06-02 12:54:15 -0700 | [diff] [blame] | 61 | "/*"({NON_STARS_THEN_STARS}[^*/])*{NON_STARS_THEN_STARS}"/" { |
Carl Worth | 2571415 | 2010-06-01 12:18:43 -0700 | [diff] [blame] | 62 | if (yyextra->space_tokens) |
| 63 | return SPACE; |
| 64 | } |
| 65 | |
Kenneth Graunke | 3b73ea3 | 2010-06-16 12:21:17 -0700 | [diff] [blame] | 66 | /* glcpp doesn't handle #extension, #version, or #pragma directives. |
| 67 | * Simply pass them through to the main compiler's lexer/parser. */ |
| 68 | {HASH}(extension|version|pragma).*\n { |
Kenneth Graunke | e0e429f | 2010-06-16 16:26:28 -0700 | [diff] [blame] | 69 | yylval->str = xtalloc_strdup (yyextra, yytext); |
Kenneth Graunke | 3b73ea3 | 2010-06-16 12:21:17 -0700 | [diff] [blame] | 70 | return OTHER; |
| 71 | } |
| 72 | |
Kenneth Graunke | 77260fc | 2010-06-17 14:36:34 -0700 | [diff] [blame] | 73 | {HASH}ifdef/.*\n { |
| 74 | yyextra->space_tokens = 0; |
| 75 | return HASH_IFDEF; |
| 76 | } |
| 77 | |
| 78 | {HASH}ifndef/.*\n { |
| 79 | yyextra->space_tokens = 0; |
| 80 | return HASH_IFNDEF; |
| 81 | } |
| 82 | |
Carl Worth | a771a40 | 2010-06-01 11:20:18 -0700 | [diff] [blame] | 83 | {HASH}if/.*\n { |
| 84 | yyextra->lexing_if = 1; |
| 85 | yyextra->space_tokens = 0; |
| 86 | return HASH_IF; |
| 87 | } |
| 88 | |
| 89 | {HASH}elif/.*\n { |
| 90 | yyextra->lexing_if = 1; |
| 91 | yyextra->space_tokens = 0; |
| 92 | return HASH_ELIF; |
| 93 | } |
| 94 | |
| 95 | {HASH}else/.*\n { |
| 96 | yyextra->space_tokens = 0; |
| 97 | return HASH_ELSE; |
| 98 | } |
| 99 | |
| 100 | {HASH}endif/.*\n { |
| 101 | yyextra->space_tokens = 0; |
| 102 | return HASH_ENDIF; |
| 103 | } |
| 104 | |
| 105 | /* When skipping (due to an #if 0 or similar) consume anything |
| 106 | * up to a newline. We do this less priroty than any |
| 107 | * #if-related directive (#if, #elif, #else, #endif), but with |
| 108 | * more priority than any other directive or token to avoid |
| 109 | * any side-effects from skipped content. |
| 110 | * |
| 111 | * We use the lexing_if flag to avoid skipping any part of an |
| 112 | * if conditional expression. */ |
| 113 | [^\n]+/\n { |
| 114 | if (yyextra->lexing_if || |
| 115 | yyextra->skip_stack == NULL || |
| 116 | yyextra->skip_stack->type == SKIP_NO_SKIP) |
| 117 | { |
| 118 | REJECT; |
| 119 | } |
| 120 | } |
| 121 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 122 | {HASH}define{HSPACE}+/{IDENTIFIER}"(" { |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 123 | yyextra->space_tokens = 0; |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 124 | return HASH_DEFINE_FUNC; |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 127 | {HASH}define { |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 128 | yyextra->space_tokens = 0; |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 129 | return HASH_DEFINE_OBJ; |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 132 | {HASH}undef { |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 133 | yyextra->space_tokens = 0; |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 134 | return HASH_UNDEF; |
Carl Worth | 03f6d5d | 2010-05-24 11:29:02 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 137 | {HASH} { |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 138 | yyextra->space_tokens = 0; |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 139 | return HASH; |
Carl Worth | 81f0143 | 2010-05-14 17:08:45 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 142 | {DECIMAL_INTEGER} { |
Kenneth Graunke | e0e429f | 2010-06-16 16:26:28 -0700 | [diff] [blame] | 143 | yylval->str = xtalloc_strdup (yyextra, yytext); |
Carl Worth | 050e3de | 2010-05-27 14:36:29 -0700 | [diff] [blame] | 144 | return INTEGER_STRING; |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | {OCTAL_INTEGER} { |
Kenneth Graunke | e0e429f | 2010-06-16 16:26:28 -0700 | [diff] [blame] | 148 | yylval->str = xtalloc_strdup (yyextra, yytext); |
Carl Worth | 050e3de | 2010-05-27 14:36:29 -0700 | [diff] [blame] | 149 | return INTEGER_STRING; |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | {HEXADECIMAL_INTEGER} { |
Kenneth Graunke | e0e429f | 2010-06-16 16:26:28 -0700 | [diff] [blame] | 153 | yylval->str = xtalloc_strdup (yyextra, yytext); |
Carl Worth | 050e3de | 2010-05-27 14:36:29 -0700 | [diff] [blame] | 154 | return INTEGER_STRING; |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 157 | "<<" { |
| 158 | return LEFT_SHIFT; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 161 | ">>" { |
| 162 | return RIGHT_SHIFT; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 165 | "<=" { |
| 166 | return LESS_OR_EQUAL; |
| 167 | } |
| 168 | |
| 169 | ">=" { |
| 170 | return GREATER_OR_EQUAL; |
| 171 | } |
| 172 | |
| 173 | "==" { |
| 174 | return EQUAL; |
| 175 | } |
| 176 | |
| 177 | "!=" { |
| 178 | return NOT_EQUAL; |
| 179 | } |
| 180 | |
| 181 | "&&" { |
| 182 | return AND; |
| 183 | } |
| 184 | |
| 185 | "||" { |
| 186 | return OR; |
| 187 | } |
| 188 | |
| 189 | "##" { |
| 190 | return PASTE; |
| 191 | } |
| 192 | |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 193 | "defined" { |
| 194 | return DEFINED; |
| 195 | } |
| 196 | |
Carl Worth | 16c1e98 | 2010-05-26 09:35:34 -0700 | [diff] [blame] | 197 | {IDENTIFIER} { |
Kenneth Graunke | e0e429f | 2010-06-16 16:26:28 -0700 | [diff] [blame] | 198 | yylval->str = xtalloc_strdup (yyextra, yytext); |
Carl Worth | 16c1e98 | 2010-05-26 09:35:34 -0700 | [diff] [blame] | 199 | return IDENTIFIER; |
| 200 | } |
| 201 | |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 202 | {PUNCTUATION} { |
| 203 | return yytext[0]; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Carl Worth | 9fb8b7a | 2010-05-25 15:04:32 -0700 | [diff] [blame] | 206 | {OTHER}+ { |
Kenneth Graunke | e0e429f | 2010-06-16 16:26:28 -0700 | [diff] [blame] | 207 | yylval->str = xtalloc_strdup (yyextra, yytext); |
Carl Worth | 9fb8b7a | 2010-05-25 15:04:32 -0700 | [diff] [blame] | 208 | return OTHER; |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Carl Worth | 9fb8b7a | 2010-05-25 15:04:32 -0700 | [diff] [blame] | 211 | {HSPACE}+ { |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 212 | if (yyextra->space_tokens) { |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 213 | return SPACE; |
| 214 | } |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 215 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 216 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 217 | \n { |
Carl Worth | a771a40 | 2010-06-01 11:20:18 -0700 | [diff] [blame] | 218 | yyextra->lexing_if = 0; |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 219 | return NEWLINE; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 220 | } |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 221 | |
Kenneth Graunke | f82d673 | 2010-06-16 12:53:19 -0700 | [diff] [blame] | 222 | /* Handle missing newline at EOF. */ |
| 223 | <INITIAL><<EOF>> { |
| 224 | BEGIN DONE; /* Don't keep matching this rule forever. */ |
| 225 | yyextra->lexing_if = 0; |
| 226 | return NEWLINE; |
| 227 | } |
| 228 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 229 | %% |
Kenneth Graunke | 1b1f43e | 2010-06-16 12:01:17 -0700 | [diff] [blame] | 230 | |
| 231 | void |
| 232 | glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader) |
| 233 | { |
| 234 | yy_scan_string(shader, parser->scanner); |
| 235 | } |