0d4bfc8d443ed11776a07458e783fe1880685c4c
[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 = 1; \
61 yylloc->source = 0; \
62 } while(0)
63 %}
64
65 %option bison-bridge bison-locations reentrant noyywrap
66 %option extra-type="glcpp_parser_t *"
67 %option prefix="glcpp_"
68 %option stack
69 %option never-interactive
70
71 %x DONE COMMENT UNREACHABLE SKIP DEFINE NEWLINE_CATCHUP
72
73 SPACE [[:space:]]
74 NONSPACE [^[:space:]]
75 NEWLINE [\n]
76 HSPACE [ \t]
77 HASH ^{HSPACE}*#{HSPACE}*
78 IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]*
79 PP_NUMBER [.]?[0-9]([._a-zA-Z0-9]|[eEpP][-+])*
80 PUNCTUATION [][(){}.&*~!/%<>^|;,=+-]
81
82 /* The OTHER class is simply a catch-all for things that the CPP
83 parser just doesn't care about. Since flex regular expressions that
84 match longer strings take priority over those matching shorter
85 strings, we have to be careful to avoid OTHER matching and hiding
86 something that CPP does care about. So we simply exclude all
87 characters that appear in any other expressions. */
88
89 OTHER [^][_#[:space:]#a-zA-Z0-9(){}.&*~!/%<>^|;,=+-]
90
91 DIGITS [0-9][0-9]*
92 DECIMAL_INTEGER [1-9][0-9]*[uU]?
93 OCTAL_INTEGER 0[0-7]*[uU]?
94 HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]?
95
96 %%
97
98 glcpp_parser_t *parser = yyextra;
99
100 /* When we lex a multi-line comment, we replace it (as
101 * specified) with a single space. But if the comment spanned
102 * multiple lines, then subsequent parsing stages will not
103 * count correct line numbers. To avoid this problem we keep
104 * track of all newlines that were commented out by a
105 * multi-line comment, and we emit a NEWLINE token for each at
106 * the next legal opportunity, (which is when the lexer would
107 * be emitting a NEWLINE token anyway).
108 */
109 if (YY_START == NEWLINE_CATCHUP) {
110 if (parser->commented_newlines)
111 parser->commented_newlines--;
112 if (parser->commented_newlines == 0)
113 BEGIN INITIAL;
114 return NEWLINE;
115 }
116
117 /* The handling of the SKIP vs INITIAL start states requires
118 * some special handling. Typically, a lexer would change
119 * start states with statements like "BEGIN SKIP" within the
120 * lexer rules. We can't get away with that here, since we
121 * need the parser to actually evaluate expressions for
122 * directives like "#if".
123 *
124 * So, here, in code that will be executed on every call to
125 * the lexer,and before any rules, we examine the skip_stack
126 * as set by the parser to know whether to change from INITIAL
127 * to SKIP or from SKIP back to INITIAL.
128 *
129 * Three cases cause us to switch out of the SKIP state and
130 * back to the INITIAL state:
131 *
132 * 1. The top of the skip_stack is of type SKIP_NO_SKIP
133 * This means we're still evaluating some #if
134 * hierarchy, but we're on a branch of it where
135 * content should not be skipped (such as "#if 1" or
136 * "#else" or so).
137 *
138 * 2. The skip_stack is NULL meaning that we've reached
139 * the last #endif.
140 *
141 * 3. The lexing_directive bit is set. This indicates that we are
142 * lexing a pre-processor directive, (such as #if, #elif, or
143 * #else). For the #if and #elif directives we always need to
144 * parse the conditions, (even if otherwise within an #if
145 * 0). And for #else, we want to be able to generate an error
146 * if any garbage follows #else.
147 */
148 if (YY_START == INITIAL || YY_START == SKIP) {
149 if (parser->lexing_directive ||
150 parser->skip_stack == NULL ||
151 parser->skip_stack->type == SKIP_NO_SKIP)
152 {
153 BEGIN INITIAL;
154 } else {
155 BEGIN SKIP;
156 }
157 }
158
159 /* Single-line comments */
160 "//"[^\n]* {
161 }
162
163 /* Multi-line comments */
164 "/*" { yy_push_state(COMMENT, yyscanner); }
165 <COMMENT>[^*\n]*
166 <COMMENT>[^*\n]*\n { yylineno++; yycolumn = 0; parser->commented_newlines++; }
167 <COMMENT>"*"+[^*/\n]*
168 <COMMENT>"*"+[^*/\n]*\n { yylineno++; yycolumn = 0; parser->commented_newlines++; }
169 <COMMENT>"*"+"/" {
170 yy_pop_state(yyscanner);
171 if (yyextra->space_tokens)
172 return SPACE;
173 }
174
175 {HASH}version{HSPACE}+ {
176 yylval->str = ralloc_strdup (yyextra, yytext);
177 yyextra->space_tokens = 0;
178 return HASH_VERSION;
179 }
180
181 /* glcpp doesn't handle #extension, #version, or #pragma directives.
182 * Simply pass them through to the main compiler's lexer/parser. */
183 {HASH}(extension|pragma)[^\n]* {
184 if (parser->commented_newlines)
185 BEGIN NEWLINE_CATCHUP;
186 yylval->str = ralloc_strdup (yyextra, yytext);
187 yylineno++;
188 yycolumn = 0;
189 return OTHER;
190 }
191
192 {HASH}line{HSPACE}+ {
193 return HASH_LINE;
194 }
195
196 <SKIP,INITIAL>{
197 {HASH}ifdef {
198 yyextra->lexing_directive = 1;
199 yyextra->space_tokens = 0;
200 return HASH_IFDEF;
201 }
202
203 {HASH}ifndef {
204 yyextra->lexing_directive = 1;
205 yyextra->space_tokens = 0;
206 return HASH_IFNDEF;
207 }
208
209 {HASH}if/[^_a-zA-Z0-9] {
210 yyextra->lexing_directive = 1;
211 yyextra->space_tokens = 0;
212 return HASH_IF;
213 }
214
215 {HASH}elif/[^_a-zA-Z0-9] {
216 yyextra->lexing_directive = 1;
217 yyextra->space_tokens = 0;
218 return HASH_ELIF;
219 }
220
221 {HASH}else {
222 yyextra->space_tokens = 0;
223 return HASH_ELSE;
224 }
225
226 {HASH}endif {
227 yyextra->space_tokens = 0;
228 return HASH_ENDIF;
229 }
230 }
231
232 <SKIP>[^\n] {
233 if (parser->commented_newlines)
234 BEGIN NEWLINE_CATCHUP;
235 }
236
237 {HASH}error.* {
238 char *p;
239 for (p = yytext; !isalpha(p[0]); p++); /* skip " # " */
240 p += 5; /* skip "error" */
241 glcpp_error(yylloc, yyextra, "#error%s", p);
242 }
243
244 {HASH}define{HSPACE}+ {
245 yyextra->space_tokens = 0;
246 yy_push_state(DEFINE, yyscanner);
247 return HASH_DEFINE;
248 }
249
250 <DEFINE>{IDENTIFIER}/"(" {
251 yy_pop_state(yyscanner);
252 yylval->str = ralloc_strdup (yyextra, yytext);
253 return FUNC_IDENTIFIER;
254 }
255
256 <DEFINE>{IDENTIFIER} {
257 yy_pop_state(yyscanner);
258 yylval->str = ralloc_strdup (yyextra, yytext);
259 return OBJ_IDENTIFIER;
260 }
261
262 <DEFINE>[^_a-zA-Z]{NONSPACE}* {
263 BEGIN INITIAL;
264 glcpp_error(yylloc, yyextra, "#define followed by a non-identifier: %s", yytext);
265 return INTEGER_STRING;
266 }
267
268 {HASH}undef {
269 yyextra->space_tokens = 0;
270 return HASH_UNDEF;
271 }
272
273 {HASH} {
274 yyextra->space_tokens = 0;
275 return HASH;
276 }
277
278 {DECIMAL_INTEGER} {
279 yylval->str = ralloc_strdup (yyextra, yytext);
280 return INTEGER_STRING;
281 }
282
283 {OCTAL_INTEGER} {
284 yylval->str = ralloc_strdup (yyextra, yytext);
285 return INTEGER_STRING;
286 }
287
288 {HEXADECIMAL_INTEGER} {
289 yylval->str = ralloc_strdup (yyextra, yytext);
290 return INTEGER_STRING;
291 }
292
293 "<<" {
294 return LEFT_SHIFT;
295 }
296
297 ">>" {
298 return RIGHT_SHIFT;
299 }
300
301 "<=" {
302 return LESS_OR_EQUAL;
303 }
304
305 ">=" {
306 return GREATER_OR_EQUAL;
307 }
308
309 "==" {
310 return EQUAL;
311 }
312
313 "!=" {
314 return NOT_EQUAL;
315 }
316
317 "&&" {
318 return AND;
319 }
320
321 "||" {
322 return OR;
323 }
324
325 "##" {
326 if (parser->is_gles)
327 glcpp_error(yylloc, yyextra, "Token pasting (##) is illegal in GLES");
328 return PASTE;
329 }
330
331 "defined" {
332 return DEFINED;
333 }
334
335 {IDENTIFIER} {
336 yylval->str = ralloc_strdup (yyextra, yytext);
337 return IDENTIFIER;
338 }
339
340 {PP_NUMBER} {
341 yylval->str = ralloc_strdup (yyextra, yytext);
342 return OTHER;
343 }
344
345 {PUNCTUATION} {
346 return yytext[0];
347 }
348
349 {OTHER}+ {
350 yylval->str = ralloc_strdup (yyextra, yytext);
351 return OTHER;
352 }
353
354 {HSPACE} {
355 if (yyextra->space_tokens) {
356 return SPACE;
357 }
358 }
359
360 <SKIP,INITIAL>\n {
361 if (parser->commented_newlines) {
362 BEGIN NEWLINE_CATCHUP;
363 }
364 yyextra->space_tokens = 1;
365 yyextra->lexing_directive = 0;
366 yylineno++;
367 yycolumn = 0;
368 return NEWLINE;
369 }
370
371 /* Handle missing newline at EOF. */
372 <INITIAL><<EOF>> {
373 BEGIN DONE; /* Don't keep matching this rule forever. */
374 yyextra->lexing_directive = 0;
375 return NEWLINE;
376 }
377
378 /* We don't actually use the UNREACHABLE start condition. We
379 only have this action here so that we can pretend to call some
380 generated functions, (to avoid "defined but not used"
381 warnings. */
382 <UNREACHABLE>. {
383 unput('.');
384 yy_top_state(yyextra);
385 }
386
387 %%
388
389 void
390 glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader)
391 {
392 yy_scan_string(shader, parser->scanner);
393 }