glsl: Properly lex extra tokens when handling # directives.
[mesa.git] / src / glsl / glsl_lexer.ll
1 %{
2 /*
3 * Copyright © 2008, 2009 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 #include <ctype.h>
25 #include <limits.h>
26 #include "strtod.h"
27 #include "ast.h"
28 #include "glsl_parser_extras.h"
29 #include "glsl_parser.h"
30
31 static int classify_identifier(struct _mesa_glsl_parse_state *, const char *);
32
33 #ifdef _MSC_VER
34 #define YY_NO_UNISTD_H
35 #endif
36
37 #define YY_USER_ACTION \
38 do { \
39 yylloc->source = 0; \
40 yylloc->first_column = yycolumn + 1; \
41 yylloc->first_line = yylloc->last_line = yylineno + 1; \
42 yycolumn += yyleng; \
43 yylloc->last_column = yycolumn + 1; \
44 } while(0);
45
46 #define YY_USER_INIT yylineno = 0; yycolumn = 0;
47
48 /* A macro for handling reserved words and keywords across language versions.
49 *
50 * Certain words start out as identifiers, become reserved words in
51 * later language revisions, and finally become language keywords.
52 * This may happen at different times in desktop GLSL and GLSL ES.
53 *
54 * For example, consider the following lexer rule:
55 * samplerBuffer KEYWORD(130, 0, 140, 0, SAMPLERBUFFER)
56 *
57 * This means that "samplerBuffer" will be treated as:
58 * - a keyword (SAMPLERBUFFER token) ...in GLSL >= 1.40
59 * - a reserved word - error ...in GLSL >= 1.30
60 * - an identifier ...in GLSL < 1.30 or GLSL ES
61 */
62 #define KEYWORD(reserved_glsl, reserved_glsl_es, \
63 allowed_glsl, allowed_glsl_es, token) \
64 KEYWORD_WITH_ALT(reserved_glsl, reserved_glsl_es, \
65 allowed_glsl, allowed_glsl_es, false, token)
66
67 /**
68 * Like the KEYWORD macro, but the word is also treated as a keyword
69 * if the given boolean expression is true.
70 */
71 #define KEYWORD_WITH_ALT(reserved_glsl, reserved_glsl_es, \
72 allowed_glsl, allowed_glsl_es, \
73 alt_expr, token) \
74 do { \
75 if (yyextra->is_version(allowed_glsl, allowed_glsl_es) \
76 || (alt_expr)) { \
77 return token; \
78 } else if (yyextra->is_version(reserved_glsl, \
79 reserved_glsl_es)) { \
80 _mesa_glsl_error(yylloc, yyextra, \
81 "illegal use of reserved word `%s'", yytext); \
82 return ERROR_TOK; \
83 } else { \
84 yylval->identifier = strdup(yytext); \
85 return classify_identifier(yyextra, yytext); \
86 } \
87 } while (0)
88
89 /**
90 * A macro for handling keywords that have been present in GLSL since
91 * its origin, but were changed into reserved words in GLSL 3.00 ES.
92 */
93 #define DEPRECATED_ES_KEYWORD(token) \
94 do { \
95 if (yyextra->is_version(0, 300)) { \
96 _mesa_glsl_error(yylloc, yyextra, \
97 "illegal use of reserved word `%s'", yytext); \
98 return ERROR_TOK; \
99 } else { \
100 return token; \
101 } \
102 } while (0)
103
104 static int
105 literal_integer(char *text, int len, struct _mesa_glsl_parse_state *state,
106 YYSTYPE *lval, YYLTYPE *lloc, int base)
107 {
108 bool is_uint = (text[len - 1] == 'u' ||
109 text[len - 1] == 'U');
110 const char *digits = text;
111
112 /* Skip "0x" */
113 if (base == 16)
114 digits += 2;
115
116 #ifdef _MSC_VER
117 unsigned __int64 value = _strtoui64(digits, NULL, base);
118 #else
119 unsigned long long value = strtoull(digits, NULL, base);
120 #endif
121
122 lval->n = (int)value;
123
124 if (value > UINT_MAX) {
125 /* Note that signed 0xffffffff is valid, not out of range! */
126 if (state->is_version(130, 300)) {
127 _mesa_glsl_error(lloc, state,
128 "literal value `%s' out of range", text);
129 } else {
130 _mesa_glsl_warning(lloc, state,
131 "literal value `%s' out of range", text);
132 }
133 } else if (base == 10 && !is_uint && (unsigned)value > (unsigned)INT_MAX + 1) {
134 /* Tries to catch unintentionally providing a negative value.
135 * Note that -2147483648 is parsed as -(2147483648), so we don't
136 * want to warn for INT_MAX.
137 */
138 _mesa_glsl_warning(lloc, state,
139 "signed literal value `%s' is interpreted as %d",
140 text, lval->n);
141 }
142 return is_uint ? UINTCONSTANT : INTCONSTANT;
143 }
144
145 #define LITERAL_INTEGER(base) \
146 literal_integer(yytext, yyleng, yyextra, yylval, yylloc, base)
147
148 %}
149
150 %option bison-bridge bison-locations reentrant noyywrap
151 %option nounput noyy_top_state
152 %option never-interactive
153 %option prefix="_mesa_glsl_lexer_"
154 %option extra-type="struct _mesa_glsl_parse_state *"
155
156 /* Note: When adding any start conditions to this list, you must also
157 * update the "Internal compiler error" catch-all rule near the end of
158 * this file. */
159 %x PP PRAGMA
160
161 DEC_INT [1-9][0-9]*
162 HEX_INT 0[xX][0-9a-fA-F]+
163 OCT_INT 0[0-7]*
164 INT ({DEC_INT}|{HEX_INT}|{OCT_INT})
165 SPC [ \t]*
166 SPCP [ \t]+
167 HASH ^{SPC}#{SPC}
168 %%
169
170 [ \r\t]+ ;
171
172 /* Preprocessor tokens. */
173 ^[ \t]*#[ \t]*$ ;
174 ^[ \t]*#[ \t]*version { BEGIN PP; return VERSION_TOK; }
175 ^[ \t]*#[ \t]*extension { BEGIN PP; return EXTENSION; }
176 {HASH}line{SPCP}{INT}{SPCP}{INT}{SPC}$ {
177 /* Eat characters until the first digit is
178 * encountered
179 */
180 char *ptr = yytext;
181 while (!isdigit(*ptr))
182 ptr++;
183
184 /* Subtract one from the line number because
185 * yylineno is zero-based instead of
186 * one-based.
187 */
188 yylineno = strtol(ptr, &ptr, 0) - 1;
189 yylloc->source = strtol(ptr, NULL, 0);
190 }
191 {HASH}line{SPCP}{INT}{SPC}$ {
192 /* Eat characters until the first digit is
193 * encountered
194 */
195 char *ptr = yytext;
196 while (!isdigit(*ptr))
197 ptr++;
198
199 /* Subtract one from the line number because
200 * yylineno is zero-based instead of
201 * one-based.
202 */
203 yylineno = strtol(ptr, &ptr, 0) - 1;
204 }
205 ^{SPC}#{SPC}pragma{SPCP}debug{SPC}\({SPC}on{SPC}\) {
206 BEGIN PP;
207 return PRAGMA_DEBUG_ON;
208 }
209 ^{SPC}#{SPC}pragma{SPCP}debug{SPC}\({SPC}off{SPC}\) {
210 BEGIN PP;
211 return PRAGMA_DEBUG_OFF;
212 }
213 ^{SPC}#{SPC}pragma{SPCP}optimize{SPC}\({SPC}on{SPC}\) {
214 BEGIN PP;
215 return PRAGMA_OPTIMIZE_ON;
216 }
217 ^{SPC}#{SPC}pragma{SPCP}optimize{SPC}\({SPC}off{SPC}\) {
218 BEGIN PP;
219 return PRAGMA_OPTIMIZE_OFF;
220 }
221 ^{SPC}#{SPC}pragma{SPCP}STDGL{SPCP}invariant{SPC}\({SPC}all{SPC}\) {
222 BEGIN PP;
223 return PRAGMA_INVARIANT_ALL;
224 }
225 ^{SPC}#{SPC}pragma{SPCP} { BEGIN PRAGMA; }
226
227 <PRAGMA>\n { BEGIN 0; yylineno++; yycolumn = 0; }
228 <PRAGMA>. { }
229
230 <PP>\/\/[^\n]* { }
231 <PP>[ \t\r]* { }
232 <PP>: return COLON;
233 <PP>[_a-zA-Z][_a-zA-Z0-9]* {
234 yylval->identifier = strdup(yytext);
235 return IDENTIFIER;
236 }
237 <PP>[1-9][0-9]* {
238 yylval->n = strtol(yytext, NULL, 10);
239 return INTCONSTANT;
240 }
241 <PP>\n { BEGIN 0; yylineno++; yycolumn = 0; return EOL; }
242 <PP>. { return yytext[0]; }
243
244 \n { yylineno++; yycolumn = 0; }
245
246 attribute DEPRECATED_ES_KEYWORD(ATTRIBUTE);
247 const return CONST_TOK;
248 bool return BOOL_TOK;
249 float return FLOAT_TOK;
250 int return INT_TOK;
251 uint KEYWORD(130, 300, 130, 300, UINT_TOK);
252
253 break return BREAK;
254 continue return CONTINUE;
255 do return DO;
256 while return WHILE;
257 else return ELSE;
258 for return FOR;
259 if return IF;
260 discard return DISCARD;
261 return return RETURN;
262
263 bvec2 return BVEC2;
264 bvec3 return BVEC3;
265 bvec4 return BVEC4;
266 ivec2 return IVEC2;
267 ivec3 return IVEC3;
268 ivec4 return IVEC4;
269 uvec2 KEYWORD(130, 300, 130, 300, UVEC2);
270 uvec3 KEYWORD(130, 300, 130, 300, UVEC3);
271 uvec4 KEYWORD(130, 300, 130, 300, UVEC4);
272 vec2 return VEC2;
273 vec3 return VEC3;
274 vec4 return VEC4;
275 mat2 return MAT2X2;
276 mat3 return MAT3X3;
277 mat4 return MAT4X4;
278 mat2x2 KEYWORD(120, 300, 120, 300, MAT2X2);
279 mat2x3 KEYWORD(120, 300, 120, 300, MAT2X3);
280 mat2x4 KEYWORD(120, 300, 120, 300, MAT2X4);
281 mat3x2 KEYWORD(120, 300, 120, 300, MAT3X2);
282 mat3x3 KEYWORD(120, 300, 120, 300, MAT3X3);
283 mat3x4 KEYWORD(120, 300, 120, 300, MAT3X4);
284 mat4x2 KEYWORD(120, 300, 120, 300, MAT4X2);
285 mat4x3 KEYWORD(120, 300, 120, 300, MAT4X3);
286 mat4x4 KEYWORD(120, 300, 120, 300, MAT4X4);
287
288 in return IN_TOK;
289 out return OUT_TOK;
290 inout return INOUT_TOK;
291 uniform return UNIFORM;
292 varying DEPRECATED_ES_KEYWORD(VARYING);
293 centroid KEYWORD(120, 300, 120, 300, CENTROID);
294 invariant KEYWORD(120, 100, 120, 100, INVARIANT);
295 flat KEYWORD(130, 100, 130, 300, FLAT);
296 smooth KEYWORD(130, 300, 130, 300, SMOOTH);
297 noperspective KEYWORD(130, 300, 130, 0, NOPERSPECTIVE);
298
299 sampler1D DEPRECATED_ES_KEYWORD(SAMPLER1D);
300 sampler2D return SAMPLER2D;
301 sampler3D return SAMPLER3D;
302 samplerCube return SAMPLERCUBE;
303 sampler1DArray KEYWORD(130, 300, 130, 0, SAMPLER1DARRAY);
304 sampler2DArray KEYWORD(130, 300, 130, 300, SAMPLER2DARRAY);
305 sampler1DShadow DEPRECATED_ES_KEYWORD(SAMPLER1DSHADOW);
306 sampler2DShadow return SAMPLER2DSHADOW;
307 samplerCubeShadow KEYWORD(130, 300, 130, 300, SAMPLERCUBESHADOW);
308 sampler1DArrayShadow KEYWORD(130, 300, 130, 0, SAMPLER1DARRAYSHADOW);
309 sampler2DArrayShadow KEYWORD(130, 300, 130, 300, SAMPLER2DARRAYSHADOW);
310 isampler1D KEYWORD(130, 300, 130, 0, ISAMPLER1D);
311 isampler2D KEYWORD(130, 300, 130, 300, ISAMPLER2D);
312 isampler3D KEYWORD(130, 300, 130, 300, ISAMPLER3D);
313 isamplerCube KEYWORD(130, 300, 130, 300, ISAMPLERCUBE);
314 isampler1DArray KEYWORD(130, 300, 130, 0, ISAMPLER1DARRAY);
315 isampler2DArray KEYWORD(130, 300, 130, 300, ISAMPLER2DARRAY);
316 usampler1D KEYWORD(130, 300, 130, 0, USAMPLER1D);
317 usampler2D KEYWORD(130, 300, 130, 300, USAMPLER2D);
318 usampler3D KEYWORD(130, 300, 130, 300, USAMPLER3D);
319 usamplerCube KEYWORD(130, 300, 130, 300, USAMPLERCUBE);
320 usampler1DArray KEYWORD(130, 300, 130, 0, USAMPLER1DARRAY);
321 usampler2DArray KEYWORD(130, 300, 130, 300, USAMPLER2DARRAY);
322
323 /* additional keywords in ARB_texture_multisample, included in GLSL 1.50 */
324 /* these are reserved but not defined in GLSL 3.00 */
325 sampler2DMS KEYWORD_WITH_ALT(150, 300, 150, 0, yyextra->ARB_texture_multisample_enable, SAMPLER2DMS);
326 isampler2DMS KEYWORD_WITH_ALT(150, 300, 150, 0, yyextra->ARB_texture_multisample_enable, ISAMPLER2DMS);
327 usampler2DMS KEYWORD_WITH_ALT(150, 300, 150, 0, yyextra->ARB_texture_multisample_enable, USAMPLER2DMS);
328 sampler2DMSArray KEYWORD_WITH_ALT(150, 300, 150, 0, yyextra->ARB_texture_multisample_enable, SAMPLER2DMSARRAY);
329 isampler2DMSArray KEYWORD_WITH_ALT(150, 300, 150, 0, yyextra->ARB_texture_multisample_enable, ISAMPLER2DMSARRAY);
330 usampler2DMSArray KEYWORD_WITH_ALT(150, 300, 150, 0, yyextra->ARB_texture_multisample_enable, USAMPLER2DMSARRAY);
331
332 /* keywords available with ARB_texture_cube_map_array_enable extension on desktop GLSL */
333 samplerCubeArray KEYWORD_WITH_ALT(400, 0, 400, 0, yyextra->ARB_texture_cube_map_array_enable, SAMPLERCUBEARRAY);
334 isamplerCubeArray KEYWORD_WITH_ALT(400, 0, 400, 0, yyextra->ARB_texture_cube_map_array_enable, ISAMPLERCUBEARRAY);
335 usamplerCubeArray KEYWORD_WITH_ALT(400, 0, 400, 0, yyextra->ARB_texture_cube_map_array_enable, USAMPLERCUBEARRAY);
336 samplerCubeArrayShadow KEYWORD_WITH_ALT(400, 0, 400, 0, yyextra->ARB_texture_cube_map_array_enable, SAMPLERCUBEARRAYSHADOW);
337
338 samplerExternalOES {
339 if (yyextra->OES_EGL_image_external_enable)
340 return SAMPLEREXTERNALOES;
341 else
342 return IDENTIFIER;
343 }
344
345 /* keywords available with ARB_gpu_shader5 */
346 precise KEYWORD_WITH_ALT(400, 0, 400, 0, yyextra->ARB_gpu_shader5_enable, PRECISE);
347
348 /* keywords available with ARB_shader_image_load_store */
349 image1D KEYWORD_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, IMAGE1D);
350 image2D KEYWORD_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, IMAGE2D);
351 image3D KEYWORD_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, IMAGE3D);
352 image2DRect KEYWORD_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, IMAGE2DRECT);
353 imageCube KEYWORD_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, IMAGECUBE);
354 imageBuffer KEYWORD_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, IMAGEBUFFER);
355 image1DArray KEYWORD_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, IMAGE1DARRAY);
356 image2DArray KEYWORD_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, IMAGE2DARRAY);
357 imageCubeArray KEYWORD_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, IMAGECUBEARRAY);
358 image2DMS KEYWORD_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, IMAGE2DMS);
359 image2DMSArray KEYWORD_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, IMAGE2DMSARRAY);
360 iimage1D KEYWORD_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, IIMAGE1D);
361 iimage2D KEYWORD_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, IIMAGE2D);
362 iimage3D KEYWORD_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, IIMAGE3D);
363 iimage2DRect KEYWORD_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, IIMAGE2DRECT);
364 iimageCube KEYWORD_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, IIMAGECUBE);
365 iimageBuffer KEYWORD_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, IIMAGEBUFFER);
366 iimage1DArray KEYWORD_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, IIMAGE1DARRAY);
367 iimage2DArray KEYWORD_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, IIMAGE2DARRAY);
368 iimageCubeArray KEYWORD_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, IIMAGECUBEARRAY);
369 iimage2DMS KEYWORD_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, IIMAGE2DMS);
370 iimage2DMSArray KEYWORD_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, IIMAGE2DMSARRAY);
371 uimage1D KEYWORD_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, UIMAGE1D);
372 uimage2D KEYWORD_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, UIMAGE2D);
373 uimage3D KEYWORD_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, UIMAGE3D);
374 uimage2DRect KEYWORD_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, UIMAGE2DRECT);
375 uimageCube KEYWORD_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, UIMAGECUBE);
376 uimageBuffer KEYWORD_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, UIMAGEBUFFER);
377 uimage1DArray KEYWORD_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, UIMAGE1DARRAY);
378 uimage2DArray KEYWORD_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, UIMAGE2DARRAY);
379 uimageCubeArray KEYWORD_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, UIMAGECUBEARRAY);
380 uimage2DMS KEYWORD_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, UIMAGE2DMS);
381 uimage2DMSArray KEYWORD_WITH_ALT(130, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, UIMAGE2DMSARRAY);
382 image1DShadow KEYWORD(130, 300, 0, 0, IMAGE1DSHADOW);
383 image2DShadow KEYWORD(130, 300, 0, 0, IMAGE2DSHADOW);
384 image1DArrayShadow KEYWORD(130, 300, 0, 0, IMAGE1DARRAYSHADOW);
385 image2DArrayShadow KEYWORD(130, 300, 0, 0, IMAGE2DARRAYSHADOW);
386
387 coherent KEYWORD_WITH_ALT(420, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, COHERENT);
388 volatile KEYWORD_WITH_ALT(110, 100, 420, 0, yyextra->ARB_shader_image_load_store_enable, VOLATILE);
389 restrict KEYWORD_WITH_ALT(420, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, RESTRICT);
390 readonly KEYWORD_WITH_ALT(420, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, READONLY);
391 writeonly KEYWORD_WITH_ALT(420, 300, 420, 0, yyextra->ARB_shader_image_load_store_enable, WRITEONLY);
392
393 atomic_uint KEYWORD_WITH_ALT(420, 300, 420, 0, yyextra->ARB_shader_atomic_counters_enable, ATOMIC_UINT);
394
395 struct return STRUCT;
396 void return VOID_TOK;
397
398 layout {
399 if ((yyextra->is_version(140, 300))
400 || yyextra->AMD_conservative_depth_enable
401 || yyextra->ARB_conservative_depth_enable
402 || yyextra->ARB_explicit_attrib_location_enable
403 || yyextra->ARB_explicit_uniform_location_enable
404 || yyextra->has_separate_shader_objects()
405 || yyextra->ARB_uniform_buffer_object_enable
406 || yyextra->ARB_fragment_coord_conventions_enable
407 || yyextra->ARB_shading_language_420pack_enable
408 || yyextra->ARB_compute_shader_enable) {
409 return LAYOUT_TOK;
410 } else {
411 yylval->identifier = strdup(yytext);
412 return classify_identifier(yyextra, yytext);
413 }
414 }
415
416 \+\+ return INC_OP;
417 -- return DEC_OP;
418 \<= return LE_OP;
419 >= return GE_OP;
420 == return EQ_OP;
421 != return NE_OP;
422 && return AND_OP;
423 \|\| return OR_OP;
424 "^^" return XOR_OP;
425 "<<" return LEFT_OP;
426 ">>" return RIGHT_OP;
427
428 \*= return MUL_ASSIGN;
429 \/= return DIV_ASSIGN;
430 \+= return ADD_ASSIGN;
431 \%= return MOD_ASSIGN;
432 \<\<= return LEFT_ASSIGN;
433 >>= return RIGHT_ASSIGN;
434 &= return AND_ASSIGN;
435 "^=" return XOR_ASSIGN;
436 \|= return OR_ASSIGN;
437 -= return SUB_ASSIGN;
438
439 [1-9][0-9]*[uU]? {
440 return LITERAL_INTEGER(10);
441 }
442 0[xX][0-9a-fA-F]+[uU]? {
443 return LITERAL_INTEGER(16);
444 }
445 0[0-7]*[uU]? {
446 return LITERAL_INTEGER(8);
447 }
448
449 [0-9]+\.[0-9]+([eE][+-]?[0-9]+)?[fF]? {
450 yylval->real = glsl_strtof(yytext, NULL);
451 return FLOATCONSTANT;
452 }
453 \.[0-9]+([eE][+-]?[0-9]+)?[fF]? {
454 yylval->real = glsl_strtof(yytext, NULL);
455 return FLOATCONSTANT;
456 }
457 [0-9]+\.([eE][+-]?[0-9]+)?[fF]? {
458 yylval->real = glsl_strtof(yytext, NULL);
459 return FLOATCONSTANT;
460 }
461 [0-9]+[eE][+-]?[0-9]+[fF]? {
462 yylval->real = glsl_strtof(yytext, NULL);
463 return FLOATCONSTANT;
464 }
465 [0-9]+[fF] {
466 yylval->real = glsl_strtof(yytext, NULL);
467 return FLOATCONSTANT;
468 }
469
470 true {
471 yylval->n = 1;
472 return BOOLCONSTANT;
473 }
474 false {
475 yylval->n = 0;
476 return BOOLCONSTANT;
477 }
478
479
480 /* Reserved words in GLSL 1.10. */
481 asm KEYWORD(110, 100, 0, 0, ASM);
482 class KEYWORD(110, 100, 0, 0, CLASS);
483 union KEYWORD(110, 100, 0, 0, UNION);
484 enum KEYWORD(110, 100, 0, 0, ENUM);
485 typedef KEYWORD(110, 100, 0, 0, TYPEDEF);
486 template KEYWORD(110, 100, 0, 0, TEMPLATE);
487 this KEYWORD(110, 100, 0, 0, THIS);
488 packed KEYWORD_WITH_ALT(110, 100, 140, 300, yyextra->ARB_uniform_buffer_object_enable, PACKED_TOK);
489 goto KEYWORD(110, 100, 0, 0, GOTO);
490 switch KEYWORD(110, 100, 130, 300, SWITCH);
491 default KEYWORD(110, 100, 130, 300, DEFAULT);
492 inline KEYWORD(110, 100, 0, 0, INLINE_TOK);
493 noinline KEYWORD(110, 100, 0, 0, NOINLINE);
494 public KEYWORD(110, 100, 0, 0, PUBLIC_TOK);
495 static KEYWORD(110, 100, 0, 0, STATIC);
496 extern KEYWORD(110, 100, 0, 0, EXTERN);
497 external KEYWORD(110, 100, 0, 0, EXTERNAL);
498 interface KEYWORD(110, 100, 0, 0, INTERFACE);
499 long KEYWORD(110, 100, 0, 0, LONG_TOK);
500 short KEYWORD(110, 100, 0, 0, SHORT_TOK);
501 double KEYWORD(110, 100, 400, 0, DOUBLE_TOK);
502 half KEYWORD(110, 100, 0, 0, HALF);
503 fixed KEYWORD(110, 100, 0, 0, FIXED_TOK);
504 unsigned KEYWORD(110, 100, 0, 0, UNSIGNED);
505 input KEYWORD(110, 100, 0, 0, INPUT_TOK);
506 output KEYWORD(110, 100, 0, 0, OUTPUT);
507 hvec2 KEYWORD(110, 100, 0, 0, HVEC2);
508 hvec3 KEYWORD(110, 100, 0, 0, HVEC3);
509 hvec4 KEYWORD(110, 100, 0, 0, HVEC4);
510 dvec2 KEYWORD(110, 100, 400, 0, DVEC2);
511 dvec3 KEYWORD(110, 100, 400, 0, DVEC3);
512 dvec4 KEYWORD(110, 100, 400, 0, DVEC4);
513 fvec2 KEYWORD(110, 100, 0, 0, FVEC2);
514 fvec3 KEYWORD(110, 100, 0, 0, FVEC3);
515 fvec4 KEYWORD(110, 100, 0, 0, FVEC4);
516 sampler2DRect DEPRECATED_ES_KEYWORD(SAMPLER2DRECT);
517 sampler3DRect KEYWORD(110, 100, 0, 0, SAMPLER3DRECT);
518 sampler2DRectShadow DEPRECATED_ES_KEYWORD(SAMPLER2DRECTSHADOW);
519 sizeof KEYWORD(110, 100, 0, 0, SIZEOF);
520 cast KEYWORD(110, 100, 0, 0, CAST);
521 namespace KEYWORD(110, 100, 0, 0, NAMESPACE);
522 using KEYWORD(110, 100, 0, 0, USING);
523
524 /* Additional reserved words in GLSL 1.20. */
525 lowp KEYWORD(120, 100, 130, 100, LOWP);
526 mediump KEYWORD(120, 100, 130, 100, MEDIUMP);
527 highp KEYWORD(120, 100, 130, 100, HIGHP);
528 precision KEYWORD(120, 100, 130, 100, PRECISION);
529
530 /* Additional reserved words in GLSL 1.30. */
531 case KEYWORD(130, 300, 130, 300, CASE);
532 common KEYWORD(130, 300, 0, 0, COMMON);
533 partition KEYWORD(130, 300, 0, 0, PARTITION);
534 active KEYWORD(130, 300, 0, 0, ACTIVE);
535 superp KEYWORD(130, 100, 0, 0, SUPERP);
536 samplerBuffer KEYWORD(130, 300, 140, 0, SAMPLERBUFFER);
537 filter KEYWORD(130, 300, 0, 0, FILTER);
538 row_major KEYWORD_WITH_ALT(130, 0, 140, 0, yyextra->ARB_uniform_buffer_object_enable && !yyextra->es_shader, ROW_MAJOR);
539
540 /* Additional reserved words in GLSL 1.40 */
541 isampler2DRect KEYWORD(140, 300, 140, 0, ISAMPLER2DRECT);
542 usampler2DRect KEYWORD(140, 300, 140, 0, USAMPLER2DRECT);
543 isamplerBuffer KEYWORD(140, 300, 140, 0, ISAMPLERBUFFER);
544 usamplerBuffer KEYWORD(140, 300, 140, 0, USAMPLERBUFFER);
545
546 /* Additional reserved words in GLSL ES 3.00 */
547 resource KEYWORD(0, 300, 0, 0, RESOURCE);
548 patch KEYWORD(0, 300, 0, 0, PATCH);
549 sample KEYWORD_WITH_ALT(400, 300, 400, 0, yyextra->ARB_gpu_shader5_enable, SAMPLE);
550 subroutine KEYWORD(0, 300, 0, 0, SUBROUTINE);
551
552
553 [_a-zA-Z][_a-zA-Z0-9]* {
554 struct _mesa_glsl_parse_state *state = yyextra;
555 void *ctx = state;
556 yylval->identifier = ralloc_strdup(ctx, yytext);
557 return classify_identifier(state, yytext);
558 }
559
560 . { return yytext[0]; }
561
562 /* This is a catch-all to avoid the annoying default flex action which
563 * matches any character and prints it. If any input ever matches this
564 * rule, then we have made a mistake above and need to fix one or more
565 * of the preceding patterns to match that input. */
566 <PP,PRAGMA>. {
567 _mesa_glsl_error(yylloc, yyextra,
568 "Internal compiler error: Unexpected character: %s", yytext);
569 }
570
571
572 %%
573
574 int
575 classify_identifier(struct _mesa_glsl_parse_state *state, const char *name)
576 {
577 if (state->symbols->get_variable(name) || state->symbols->get_function(name))
578 return IDENTIFIER;
579 else if (state->symbols->get_type(name))
580 return TYPE_IDENTIFIER;
581 else
582 return NEW_IDENTIFIER;
583 }
584
585 void
586 _mesa_glsl_lexer_ctor(struct _mesa_glsl_parse_state *state, const char *string)
587 {
588 yylex_init_extra(state, & state->scanner);
589 yy_scan_string(string, state->scanner);
590 }
591
592 void
593 _mesa_glsl_lexer_dtor(struct _mesa_glsl_parse_state *state)
594 {
595 yylex_destroy(state->scanner);
596 }