5f0bb324990cdaa2eefcfeec3ec8dde9da6f6d24
[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 <DEFINE,INITIAL>"/*" { 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 yylval->str = ralloc_strdup (yyextra, yytext);
185 yylineno++;
186 yycolumn = 0;
187 return OTHER;
188 }
189
190 {HASH}line{HSPACE}+ {
191 return HASH_LINE;
192 }
193
194 <SKIP,INITIAL>{
195 {HASH}ifdef {
196 yyextra->lexing_directive = 1;
197 yyextra->space_tokens = 0;
198 return HASH_IFDEF;
199 }
200
201 {HASH}ifndef {
202 yyextra->lexing_directive = 1;
203 yyextra->space_tokens = 0;
204 return HASH_IFNDEF;
205 }
206
207 {HASH}if/[^_a-zA-Z0-9] {
208 yyextra->lexing_directive = 1;
209 yyextra->space_tokens = 0;
210 return HASH_IF;
211 }
212
213 {HASH}elif/[^_a-zA-Z0-9] {
214 yyextra->lexing_directive = 1;
215 yyextra->space_tokens = 0;
216 return HASH_ELIF;
217 }
218
219 {HASH}else {
220 yyextra->space_tokens = 0;
221 return HASH_ELSE;
222 }
223
224 {HASH}endif {
225 yyextra->space_tokens = 0;
226 return HASH_ENDIF;
227 }
228 }
229
230 <SKIP>[^\n] {
231 }
232
233 {HASH}error.* {
234 char *p;
235 for (p = yytext; !isalpha(p[0]); p++); /* skip " # " */
236 p += 5; /* skip "error" */
237 glcpp_error(yylloc, yyextra, "#error%s", p);
238 }
239
240 /* After we see a "#define" we enter the <DEFINE> start state
241 * for the lexer. Within <DEFINE> we are looking for the first
242 * identifier and specifically checking whether the identifier
243 * is followed by a '(' or not, (to lex either a
244 * FUNC_IDENTIFIER or an OBJ_IDENITIFIER token).
245 *
246 * While in the <DEFINE> state we also need to explicitly
247 * handle a few other things that may appear before the
248 * identifier:
249 *
250 * * Comments, (handled above with the main support for
251 * comments).
252 *
253 * * Whitespace (simply ignored)
254 *
255 * * Anything else, (not an identifier, not a comment,
256 * and not whitespace). This will generate an error.
257 */
258 {HASH}define{HSPACE}+ {
259 yyextra->space_tokens = 0;
260 yy_push_state(DEFINE, yyscanner);
261 return HASH_DEFINE;
262 }
263
264 /* An identifier immediately followed by '(' */
265 <DEFINE>{IDENTIFIER}/"(" {
266 yy_pop_state(yyscanner);
267 yylval->str = ralloc_strdup (yyextra, yytext);
268 return FUNC_IDENTIFIER;
269 }
270
271 /* An identifier not immediately followed by '(' */
272 <DEFINE>{IDENTIFIER} {
273 yy_pop_state(yyscanner);
274 yylval->str = ralloc_strdup (yyextra, yytext);
275 return OBJ_IDENTIFIER;
276 }
277
278 /* Whitespace */
279 <DEFINE>{HSPACE}+ {
280 /* Just ignore it. Nothing to do here. */
281 }
282
283 /* '/' not followed by '*', so not a comment. This is an error. */
284 <DEFINE>[/][^*]{NONSPACE}* {
285 BEGIN INITIAL;
286 glcpp_error(yylloc, yyextra, "#define followed by a non-identifier: %s", yytext);
287 return INTEGER_STRING;
288 }
289
290 /* A character that can't start an identifier, comment, or
291 * space. This is an error. */
292 <DEFINE>[^_a-zA-Z/[:space:]]{NONSPACE}* {
293 BEGIN INITIAL;
294 glcpp_error(yylloc, yyextra, "#define followed by a non-identifier: %s", yytext);
295 return INTEGER_STRING;
296 }
297
298 {HASH}undef {
299 yyextra->space_tokens = 0;
300 return HASH_UNDEF;
301 }
302
303 {HASH} {
304 yyextra->space_tokens = 0;
305 return HASH;
306 }
307
308 {DECIMAL_INTEGER} {
309 yylval->str = ralloc_strdup (yyextra, yytext);
310 return INTEGER_STRING;
311 }
312
313 {OCTAL_INTEGER} {
314 yylval->str = ralloc_strdup (yyextra, yytext);
315 return INTEGER_STRING;
316 }
317
318 {HEXADECIMAL_INTEGER} {
319 yylval->str = ralloc_strdup (yyextra, yytext);
320 return INTEGER_STRING;
321 }
322
323 "<<" {
324 return LEFT_SHIFT;
325 }
326
327 ">>" {
328 return RIGHT_SHIFT;
329 }
330
331 "<=" {
332 return LESS_OR_EQUAL;
333 }
334
335 ">=" {
336 return GREATER_OR_EQUAL;
337 }
338
339 "==" {
340 return EQUAL;
341 }
342
343 "!=" {
344 return NOT_EQUAL;
345 }
346
347 "&&" {
348 return AND;
349 }
350
351 "||" {
352 return OR;
353 }
354
355 "##" {
356 if (parser->is_gles)
357 glcpp_error(yylloc, yyextra, "Token pasting (##) is illegal in GLES");
358 return PASTE;
359 }
360
361 "defined" {
362 return DEFINED;
363 }
364
365 {IDENTIFIER} {
366 yylval->str = ralloc_strdup (yyextra, yytext);
367 return IDENTIFIER;
368 }
369
370 {PP_NUMBER} {
371 yylval->str = ralloc_strdup (yyextra, yytext);
372 return OTHER;
373 }
374
375 {PUNCTUATION} {
376 return yytext[0];
377 }
378
379 {OTHER}+ {
380 yylval->str = ralloc_strdup (yyextra, yytext);
381 return OTHER;
382 }
383
384 {HSPACE} {
385 if (yyextra->space_tokens) {
386 return SPACE;
387 }
388 }
389
390 <SKIP,INITIAL>\n {
391 if (parser->commented_newlines) {
392 BEGIN NEWLINE_CATCHUP;
393 }
394 yyextra->space_tokens = 1;
395 yyextra->lexing_directive = 0;
396 yylineno++;
397 yycolumn = 0;
398 return NEWLINE;
399 }
400
401 /* Handle missing newline at EOF. */
402 <INITIAL><<EOF>> {
403 BEGIN DONE; /* Don't keep matching this rule forever. */
404 yyextra->lexing_directive = 0;
405 return NEWLINE;
406 }
407
408 /* We don't actually use the UNREACHABLE start condition. We
409 only have this action here so that we can pretend to call some
410 generated functions, (to avoid "defined but not used"
411 warnings. */
412 <UNREACHABLE>. {
413 unput('.');
414 yy_top_state(yyextra);
415 }
416
417 %%
418
419 void
420 glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader)
421 {
422 yy_scan_string(shader, parser->scanner);
423 }