Merge branch 'master' into glsl2
[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 #define YY_NO_INPUT
38
39 #define YY_USER_ACTION \
40 do { \
41 yylloc->source = 0; \
42 yylloc->first_column = yycolumn + 1; \
43 yylloc->first_line = yylineno; \
44 yycolumn += yyleng; \
45 } while(0);
46 #define YY_USER_INIT yylineno = 1; yycolumn = 1;
47 %}
48
49 %option bison-bridge bison-locations reentrant noyywrap
50 %option extra-type="glcpp_parser_t *"
51 %option prefix="glcpp_"
52 %option stack
53 %option never-interactive
54
55 %x DONE COMMENT UNREACHABLE
56
57 SPACE [[:space:]]
58 NONSPACE [^[:space:]]
59 NEWLINE [\n]
60 HSPACE [ \t]
61 HASH ^{HSPACE}*#{HSPACE}*
62 IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]*
63 PUNCTUATION [][(){}.&*~!/%<>^|;,=+-]
64 OTHER [^][(){}.&*~!/%<>^|;,=#[:space:]+-]+
65
66 DECIMAL_INTEGER [1-9][0-9]*[uU]?
67 OCTAL_INTEGER 0[0-7]*[uU]?
68 HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]?
69
70 %%
71
72 /* Single-line comments */
73 "//"[^\n]*\n {
74 yylineno++;
75 yycolumn = 0;
76 return NEWLINE;
77 }
78
79 /* Multi-line comments */
80 "/*" { yy_push_state(COMMENT, yyscanner); }
81 <COMMENT>[^*\n]*
82 <COMMENT>[^*\n]*\n { yylineno++; yycolumn = 0; }
83 <COMMENT>"*"+[^*/\n]*
84 <COMMENT>"*"+[^*/\n]*\n { yylineno++; yycolumn = 0; }
85 <COMMENT>"*"+"/" {
86 yy_pop_state(yyscanner);
87 if (yyextra->space_tokens)
88 return SPACE;
89 }
90
91 {HASH}(version) {
92 yylval->str = talloc_strdup (yyextra, yytext);
93 yylineno++;
94 yycolumn = 0;
95 yyextra->space_tokens = 0;
96 return HASH_VERSION;
97 }
98
99 /* glcpp doesn't handle #extension, #version, or #pragma directives.
100 * Simply pass them through to the main compiler's lexer/parser. */
101 {HASH}(extension|pragma)[^\n]+ {
102 yylval->str = talloc_strdup (yyextra, yytext);
103 yylineno++;
104 yycolumn = 0;
105 return OTHER;
106 }
107
108 {HASH}ifdef/.*\n {
109 yyextra->lexing_if = 1;
110 yyextra->space_tokens = 0;
111 return HASH_IFDEF;
112 }
113
114 {HASH}ifndef/.*\n {
115 yyextra->lexing_if = 1;
116 yyextra->space_tokens = 0;
117 return HASH_IFNDEF;
118 }
119
120 {HASH}if/[^_a-zA-Z0-9].*\n {
121 yyextra->lexing_if = 1;
122 yyextra->space_tokens = 0;
123 return HASH_IF;
124 }
125
126 {HASH}elif/.*\n {
127 yyextra->lexing_if = 1;
128 yyextra->space_tokens = 0;
129 return HASH_ELIF;
130 }
131
132 {HASH}else/.*\n {
133 yyextra->space_tokens = 0;
134 return HASH_ELSE;
135 }
136
137 {HASH}endif/.*\n {
138 yyextra->space_tokens = 0;
139 return HASH_ENDIF;
140 }
141
142 /* When skipping (due to an #if 0 or similar) consume anything
143 * up to a newline. We do this with less priority than any
144 * #if-related directive (#if, #elif, #else, #endif), but with
145 * more priority than any other directive or token to avoid
146 * any side-effects from skipped content.
147 *
148 * We use the lexing_if flag to avoid skipping any part of an
149 * if conditional expression. */
150 [^\n]+/\n {
151 /* Since this rule always matches, YY_USER_ACTION gets called for it,
152 * wrongly incrementing yycolumn. We undo that effect here. */
153 yycolumn -= yyleng;
154 if (yyextra->lexing_if ||
155 yyextra->skip_stack == NULL ||
156 yyextra->skip_stack->type == SKIP_NO_SKIP)
157 {
158 REJECT;
159 }
160 }
161
162 {HASH}error.* {
163 char *p;
164 for (p = yytext; !isalpha(p[0]); p++); /* skip " # " */
165 p += 5; /* skip "error" */
166 glcpp_error(yylloc, yyextra, "#error%s", p);
167 }
168
169 {HASH}define{HSPACE}+/{IDENTIFIER}"(" {
170 yyextra->space_tokens = 0;
171 return HASH_DEFINE_FUNC;
172 }
173
174 {HASH}define {
175 yyextra->space_tokens = 0;
176 return HASH_DEFINE_OBJ;
177 }
178
179 {HASH}undef {
180 yyextra->space_tokens = 0;
181 return HASH_UNDEF;
182 }
183
184 {HASH} {
185 yyextra->space_tokens = 0;
186 return HASH;
187 }
188
189 {DECIMAL_INTEGER} {
190 yylval->str = talloc_strdup (yyextra, yytext);
191 return INTEGER_STRING;
192 }
193
194 {OCTAL_INTEGER} {
195 yylval->str = talloc_strdup (yyextra, yytext);
196 return INTEGER_STRING;
197 }
198
199 {HEXADECIMAL_INTEGER} {
200 yylval->str = talloc_strdup (yyextra, yytext);
201 return INTEGER_STRING;
202 }
203
204 "<<" {
205 return LEFT_SHIFT;
206 }
207
208 ">>" {
209 return RIGHT_SHIFT;
210 }
211
212 "<=" {
213 return LESS_OR_EQUAL;
214 }
215
216 ">=" {
217 return GREATER_OR_EQUAL;
218 }
219
220 "==" {
221 return EQUAL;
222 }
223
224 "!=" {
225 return NOT_EQUAL;
226 }
227
228 "&&" {
229 return AND;
230 }
231
232 "||" {
233 return OR;
234 }
235
236 "##" {
237 return PASTE;
238 }
239
240 "defined" {
241 return DEFINED;
242 }
243
244 {IDENTIFIER} {
245 yylval->str = talloc_strdup (yyextra, yytext);
246 return IDENTIFIER;
247 }
248
249 {PUNCTUATION} {
250 return yytext[0];
251 }
252
253 {OTHER}+ {
254 yylval->str = talloc_strdup (yyextra, yytext);
255 return OTHER;
256 }
257
258 {HSPACE}+ {
259 if (yyextra->space_tokens) {
260 return SPACE;
261 }
262 }
263
264 \n {
265 yyextra->lexing_if = 0;
266 yylineno++;
267 yycolumn = 0;
268 return NEWLINE;
269 }
270
271 /* Handle missing newline at EOF. */
272 <INITIAL><<EOF>> {
273 BEGIN DONE; /* Don't keep matching this rule forever. */
274 yyextra->lexing_if = 0;
275 return NEWLINE;
276 }
277
278 /* We don't actually use the UNREACHABLE start condition. We
279 only have this action here so that we can pretend to call some
280 generated functions, (to avoid "defined but not used"
281 warnings. */
282 <UNREACHABLE>. {
283 unput('.');
284 yy_top_state(yyextra);
285 }
286
287 %%
288
289 void
290 glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader)
291 {
292 yy_scan_string(shader, parser->scanner);
293 }