3 * Copyright © 2008, 2009 Intel Corporation
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:
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
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.
28 #include "glsl_parser_extras.h"
29 #include "glsl_parser.h"
31 static int classify_identifier(struct _mesa_glsl_parse_state *, const char *);
34 #define YY_NO_UNISTD_H
37 #define YY_USER_ACTION \
40 yylloc->first_column = yycolumn + 1; \
41 yylloc->first_line = yylineno + 1; \
45 #define YY_USER_INIT yylineno = 0; yycolumn = 0;
47 /* A macro for handling reserved words and keywords across language versions.
49 * Certain words start out as identifiers, become reserved words in
50 * later language revisions, and finally become language keywords.
51 * This may happen at different times in desktop GLSL and GLSL ES.
53 * For example, consider the following lexer rule:
54 * samplerBuffer KEYWORD(130, 0, 140, 0, SAMPLERBUFFER)
56 * This means that "samplerBuffer" will be treated as:
57 * - a keyword (SAMPLERBUFFER token) ...in GLSL >= 1.40
58 * - a reserved word - error ...in GLSL >= 1.30
59 * - an identifier ...in GLSL < 1.30 or GLSL ES
61 #define KEYWORD(reserved_glsl, reserved_glsl_es, \
62 allowed_glsl, allowed_glsl_es, token) \
63 KEYWORD_WITH_ALT(reserved_glsl, reserved_glsl_es, \
64 allowed_glsl, allowed_glsl_es, false, token)
67 * Like the KEYWORD macro, but the word is also treated as a keyword
68 * if the given boolean expression is true.
70 #define KEYWORD_WITH_ALT(reserved_glsl, reserved_glsl_es, \
71 allowed_glsl, allowed_glsl_es, \
74 if (yyextra->is_version(allowed_glsl, allowed_glsl_es) \
77 } else if (yyextra->is_version(reserved_glsl, \
78 reserved_glsl_es)) { \
79 _mesa_glsl_error(yylloc, yyextra, \
80 "illegal use of reserved word `%s'", yytext); \
83 yylval->identifier = strdup(yytext); \
84 return classify_identifier(yyextra, yytext); \
89 * A macro for handling keywords that have been present in GLSL since
90 * its origin, but were changed into reserved words in GLSL 3.00 ES.
92 #define DEPRECATED_ES_KEYWORD(token) \
94 if (yyextra->is_version(0, 300)) { \
95 _mesa_glsl_error(yylloc, yyextra, \
96 "illegal use of reserved word `%s'", yytext); \
104 literal_integer(char *text, int len, struct _mesa_glsl_parse_state *state,
105 YYSTYPE *lval, YYLTYPE *lloc, int base)
107 bool is_uint = (text[len - 1] == 'u' ||
108 text[len - 1] == 'U');
109 const char *digits = text;
116 unsigned __int64 value = _strtoui64(digits, NULL, base);
118 unsigned long long value = strtoull(digits, NULL, base);
121 lval->n = (int)value;
123 if (value > UINT_MAX) {
124 /* Note that signed 0xffffffff is valid, not out of range! */
125 if (state->is_version(130, 300)) {
126 _mesa_glsl_error(lloc, state,
127 "literal value `%s' out of range", text);
129 _mesa_glsl_warning(lloc, state,
130 "literal value `%s' out of range", text);
132 } else if (base == 10 && !is_uint && (unsigned)value > (unsigned)INT_MAX + 1) {
133 /* Tries to catch unintentionally providing a negative value.
134 * Note that -2147483648 is parsed as -(2147483648), so we don't
135 * want to warn for INT_MAX.
137 _mesa_glsl_warning(lloc, state,
138 "signed literal value `%s' is interpreted as %d",
141 return is_uint ? UINTCONSTANT : INTCONSTANT;
144 #define LITERAL_INTEGER(base) \
145 literal_integer(yytext, yyleng, yyextra, yylval, yylloc, base)
149 %option bison-bridge bison-locations reentrant noyywrap
150 %option nounput noyy_top_state
151 %option never-interactive
152 %option prefix="_mesa_glsl_lexer_"
153 %option extra-type="struct _mesa_glsl_parse_state *"
158 HEX_INT 0[xX][0-9a-fA-F]+
160 INT ({DEC_INT}|{HEX_INT}|{OCT_INT})
168 /* Preprocessor tokens. */
170 ^[ \t]*#[ \t]*version { BEGIN PP; return VERSION_TOK; }
171 ^[ \t]*#[ \t]*extension { BEGIN PP; return EXTENSION; }
172 {HASH}line{SPCP}{INT}{SPCP}{INT}{SPC}$ {
173 /* Eat characters until the first digit is
177 while (!isdigit(*ptr))
180 /* Subtract one from the line number because
181 * yylineno is zero-based instead of
184 yylineno = strtol(ptr, &ptr, 0) - 1;
185 yylloc->source = strtol(ptr, NULL, 0);
187 {HASH}line{SPCP}{INT}{SPC}$ {
188 /* Eat characters until the first digit is
192 while (!isdigit(*ptr))
195 /* Subtract one from the line number because
196 * yylineno is zero-based instead of
199 yylineno = strtol(ptr, &ptr, 0) - 1;
201 ^{SPC}#{SPC}pragma{SPCP}debug{SPC}\({SPC}on{SPC}\) {
203 return PRAGMA_DEBUG_ON;
205 ^{SPC}#{SPC}pragma{SPCP}debug{SPC}\({SPC}off{SPC}\) {
207 return PRAGMA_DEBUG_OFF;
209 ^{SPC}#{SPC}pragma{SPCP}optimize{SPC}\({SPC}on{SPC}\) {
211 return PRAGMA_OPTIMIZE_ON;
213 ^{SPC}#{SPC}pragma{SPCP}optimize{SPC}\({SPC}off{SPC}\) {
215 return PRAGMA_OPTIMIZE_OFF;
217 ^{SPC}#{SPC}pragma{SPCP}STDGL{SPCP}invariant{SPC}\({SPC}all{SPC}\) {
219 return PRAGMA_INVARIANT_ALL;
221 ^{SPC}#{SPC}pragma{SPCP} { BEGIN PRAGMA; }
223 <PRAGMA>\n { BEGIN 0; yylineno++; yycolumn = 0; }
229 <PP>[_a-zA-Z][_a-zA-Z0-9]* {
230 yylval->identifier = strdup(yytext);
234 yylval->n = strtol(yytext, NULL, 10);
237 <PP>\n { BEGIN 0; yylineno++; yycolumn = 0; return EOL; }
239 \n { yylineno++; yycolumn = 0; }
241 attribute DEPRECATED_ES_KEYWORD(ATTRIBUTE);
242 const return CONST_TOK;
243 bool return BOOL_TOK;
244 float return FLOAT_TOK;
246 uint KEYWORD(130, 300, 130, 300, UINT_TOK);
249 continue return CONTINUE;
255 discard return DISCARD;
256 return return RETURN;
264 uvec2 KEYWORD(130, 300, 130, 300, UVEC2);
265 uvec3 KEYWORD(130, 300, 130, 300, UVEC3);
266 uvec4 KEYWORD(130, 300, 130, 300, UVEC4);
273 mat2x2 KEYWORD(120, 300, 120, 300, MAT2X2);
274 mat2x3 KEYWORD(120, 300, 120, 300, MAT2X3);
275 mat2x4 KEYWORD(120, 300, 120, 300, MAT2X4);
276 mat3x2 KEYWORD(120, 300, 120, 300, MAT3X2);
277 mat3x3 KEYWORD(120, 300, 120, 300, MAT3X3);
278 mat3x4 KEYWORD(120, 300, 120, 300, MAT3X4);
279 mat4x2 KEYWORD(120, 300, 120, 300, MAT4X2);
280 mat4x3 KEYWORD(120, 300, 120, 300, MAT4X3);
281 mat4x4 KEYWORD(120, 300, 120, 300, MAT4X4);
285 inout return INOUT_TOK;
286 uniform return UNIFORM;
287 varying DEPRECATED_ES_KEYWORD(VARYING);
288 centroid KEYWORD(120, 300, 120, 300, CENTROID);
289 invariant KEYWORD(120, 100, 120, 100, INVARIANT);
290 flat KEYWORD(130, 100, 130, 300, FLAT);
291 smooth KEYWORD(130, 300, 130, 300, SMOOTH);
292 noperspective KEYWORD(130, 300, 130, 0, NOPERSPECTIVE);
294 sampler1D DEPRECATED_ES_KEYWORD(SAMPLER1D);
295 sampler2D return SAMPLER2D;
296 sampler3D return SAMPLER3D;
297 samplerCube return SAMPLERCUBE;
298 sampler1DArray KEYWORD(130, 300, 130, 0, SAMPLER1DARRAY);
299 sampler2DArray KEYWORD(130, 300, 130, 300, SAMPLER2DARRAY);
300 sampler1DShadow DEPRECATED_ES_KEYWORD(SAMPLER1DSHADOW);
301 sampler2DShadow return SAMPLER2DSHADOW;
302 samplerCubeShadow KEYWORD(130, 300, 130, 300, SAMPLERCUBESHADOW);
303 sampler1DArrayShadow KEYWORD(130, 300, 130, 0, SAMPLER1DARRAYSHADOW);
304 sampler2DArrayShadow KEYWORD(130, 300, 130, 300, SAMPLER2DARRAYSHADOW);
305 isampler1D KEYWORD(130, 300, 130, 0, ISAMPLER1D);
306 isampler2D KEYWORD(130, 300, 130, 300, ISAMPLER2D);
307 isampler3D KEYWORD(130, 300, 130, 300, ISAMPLER3D);
308 isamplerCube KEYWORD(130, 300, 130, 300, ISAMPLERCUBE);
309 isampler1DArray KEYWORD(130, 300, 130, 0, ISAMPLER1DARRAY);
310 isampler2DArray KEYWORD(130, 300, 130, 300, ISAMPLER2DARRAY);
311 usampler1D KEYWORD(130, 300, 130, 0, USAMPLER1D);
312 usampler2D KEYWORD(130, 300, 130, 300, USAMPLER2D);
313 usampler3D KEYWORD(130, 300, 130, 300, USAMPLER3D);
314 usamplerCube KEYWORD(130, 300, 130, 300, USAMPLERCUBE);
315 usampler1DArray KEYWORD(130, 300, 130, 0, USAMPLER1DARRAY);
316 usampler2DArray KEYWORD(130, 300, 130, 300, USAMPLER2DARRAY);
318 /* additional keywords in ARB_texture_multisample, included in GLSL 1.50 */
319 /* these are reserved but not defined in GLSL 3.00 */
320 sampler2DMS KEYWORD_WITH_ALT(150, 300, 150, 0, yyextra->ARB_texture_multisample_enable, SAMPLER2DMS);
321 isampler2DMS KEYWORD_WITH_ALT(150, 300, 150, 0, yyextra->ARB_texture_multisample_enable, ISAMPLER2DMS);
322 usampler2DMS KEYWORD_WITH_ALT(150, 300, 150, 0, yyextra->ARB_texture_multisample_enable, USAMPLER2DMS);
323 sampler2DMSArray KEYWORD_WITH_ALT(150, 300, 150, 0, yyextra->ARB_texture_multisample_enable, SAMPLER2DMSARRAY);
324 isampler2DMSArray KEYWORD_WITH_ALT(150, 300, 150, 0, yyextra->ARB_texture_multisample_enable, ISAMPLER2DMSARRAY);
325 usampler2DMSArray KEYWORD_WITH_ALT(150, 300, 150, 0, yyextra->ARB_texture_multisample_enable, USAMPLER2DMSARRAY);
327 /* keywords available with ARB_texture_cube_map_array_enable extension on desktop GLSL */
328 samplerCubeArray KEYWORD_WITH_ALT(400, 0, 400, 0, yyextra->ARB_texture_cube_map_array_enable, SAMPLERCUBEARRAY);
329 isamplerCubeArray KEYWORD_WITH_ALT(400, 0, 400, 0, yyextra->ARB_texture_cube_map_array_enable, ISAMPLERCUBEARRAY);
330 usamplerCubeArray KEYWORD_WITH_ALT(400, 0, 400, 0, yyextra->ARB_texture_cube_map_array_enable, USAMPLERCUBEARRAY);
331 samplerCubeArrayShadow KEYWORD_WITH_ALT(400, 0, 400, 0, yyextra->ARB_texture_cube_map_array_enable, SAMPLERCUBEARRAYSHADOW);
334 if (yyextra->OES_EGL_image_external_enable)
335 return SAMPLEREXTERNALOES;
341 struct return STRUCT;
342 void return VOID_TOK;
345 if ((yyextra->is_version(140, 300))
346 || yyextra->AMD_conservative_depth_enable
347 || yyextra->ARB_conservative_depth_enable
348 || yyextra->ARB_explicit_attrib_location_enable
349 || yyextra->ARB_uniform_buffer_object_enable
350 || yyextra->ARB_fragment_coord_conventions_enable
351 || yyextra->ARB_shading_language_420pack_enable) {
354 yylval->identifier = strdup(yytext);
355 return classify_identifier(yyextra, yytext);
369 ">>" return RIGHT_OP;
371 \*= return MUL_ASSIGN;
372 \/= return DIV_ASSIGN;
373 \+= return ADD_ASSIGN;
374 \%= return MOD_ASSIGN;
375 \<\<= return LEFT_ASSIGN;
376 >>= return RIGHT_ASSIGN;
377 &= return AND_ASSIGN;
378 "^=" return XOR_ASSIGN;
379 \|= return OR_ASSIGN;
380 -= return SUB_ASSIGN;
383 return LITERAL_INTEGER(10);
385 0[xX][0-9a-fA-F]+[uU]? {
386 return LITERAL_INTEGER(16);
389 return LITERAL_INTEGER(8);
392 [0-9]+\.[0-9]+([eE][+-]?[0-9]+)?[fF]? {
393 yylval->real = glsl_strtof(yytext, NULL);
394 return FLOATCONSTANT;
396 \.[0-9]+([eE][+-]?[0-9]+)?[fF]? {
397 yylval->real = glsl_strtof(yytext, NULL);
398 return FLOATCONSTANT;
400 [0-9]+\.([eE][+-]?[0-9]+)?[fF]? {
401 yylval->real = glsl_strtof(yytext, NULL);
402 return FLOATCONSTANT;
404 [0-9]+[eE][+-]?[0-9]+[fF]? {
405 yylval->real = glsl_strtof(yytext, NULL);
406 return FLOATCONSTANT;
409 yylval->real = glsl_strtof(yytext, NULL);
410 return FLOATCONSTANT;
423 /* Reserved words in GLSL 1.10. */
424 asm KEYWORD(110, 100, 0, 0, ASM);
425 class KEYWORD(110, 100, 0, 0, CLASS);
426 union KEYWORD(110, 100, 0, 0, UNION);
427 enum KEYWORD(110, 100, 0, 0, ENUM);
428 typedef KEYWORD(110, 100, 0, 0, TYPEDEF);
429 template KEYWORD(110, 100, 0, 0, TEMPLATE);
430 this KEYWORD(110, 100, 0, 0, THIS);
431 packed KEYWORD_WITH_ALT(110, 100, 140, 300, yyextra->ARB_uniform_buffer_object_enable, PACKED_TOK);
432 goto KEYWORD(110, 100, 0, 0, GOTO);
433 switch KEYWORD(110, 100, 130, 300, SWITCH);
434 default KEYWORD(110, 100, 130, 300, DEFAULT);
435 inline KEYWORD(110, 100, 0, 0, INLINE_TOK);
436 noinline KEYWORD(110, 100, 0, 0, NOINLINE);
437 volatile KEYWORD(110, 100, 0, 0, VOLATILE);
438 public KEYWORD(110, 100, 0, 0, PUBLIC_TOK);
439 static KEYWORD(110, 100, 0, 0, STATIC);
440 extern KEYWORD(110, 100, 0, 0, EXTERN);
441 external KEYWORD(110, 100, 0, 0, EXTERNAL);
442 interface KEYWORD(110, 100, 0, 0, INTERFACE);
443 long KEYWORD(110, 100, 0, 0, LONG_TOK);
444 short KEYWORD(110, 100, 0, 0, SHORT_TOK);
445 double KEYWORD(110, 100, 400, 0, DOUBLE_TOK);
446 half KEYWORD(110, 100, 0, 0, HALF);
447 fixed KEYWORD(110, 100, 0, 0, FIXED_TOK);
448 unsigned KEYWORD(110, 100, 0, 0, UNSIGNED);
449 input KEYWORD(110, 100, 0, 0, INPUT_TOK);
450 output KEYWORD(110, 100, 0, 0, OUTPUT);
451 hvec2 KEYWORD(110, 100, 0, 0, HVEC2);
452 hvec3 KEYWORD(110, 100, 0, 0, HVEC3);
453 hvec4 KEYWORD(110, 100, 0, 0, HVEC4);
454 dvec2 KEYWORD(110, 100, 400, 0, DVEC2);
455 dvec3 KEYWORD(110, 100, 400, 0, DVEC3);
456 dvec4 KEYWORD(110, 100, 400, 0, DVEC4);
457 fvec2 KEYWORD(110, 100, 0, 0, FVEC2);
458 fvec3 KEYWORD(110, 100, 0, 0, FVEC3);
459 fvec4 KEYWORD(110, 100, 0, 0, FVEC4);
460 sampler2DRect DEPRECATED_ES_KEYWORD(SAMPLER2DRECT);
461 sampler3DRect KEYWORD(110, 100, 0, 0, SAMPLER3DRECT);
462 sampler2DRectShadow DEPRECATED_ES_KEYWORD(SAMPLER2DRECTSHADOW);
463 sizeof KEYWORD(110, 100, 0, 0, SIZEOF);
464 cast KEYWORD(110, 100, 0, 0, CAST);
465 namespace KEYWORD(110, 100, 0, 0, NAMESPACE);
466 using KEYWORD(110, 100, 0, 0, USING);
468 /* Additional reserved words in GLSL 1.20. */
469 lowp KEYWORD(120, 100, 130, 100, LOWP);
470 mediump KEYWORD(120, 100, 130, 100, MEDIUMP);
471 highp KEYWORD(120, 100, 130, 100, HIGHP);
472 precision KEYWORD(120, 100, 130, 100, PRECISION);
474 /* Additional reserved words in GLSL 1.30. */
475 case KEYWORD(130, 300, 130, 300, CASE);
476 common KEYWORD(130, 300, 0, 0, COMMON);
477 partition KEYWORD(130, 300, 0, 0, PARTITION);
478 active KEYWORD(130, 300, 0, 0, ACTIVE);
479 superp KEYWORD(130, 100, 0, 0, SUPERP);
480 samplerBuffer KEYWORD(130, 300, 140, 0, SAMPLERBUFFER);
481 filter KEYWORD(130, 300, 0, 0, FILTER);
482 image1D KEYWORD(130, 300, 0, 0, IMAGE1D);
483 image2D KEYWORD(130, 300, 0, 0, IMAGE2D);
484 image3D KEYWORD(130, 300, 0, 0, IMAGE3D);
485 imageCube KEYWORD(130, 300, 0, 0, IMAGECUBE);
486 iimage1D KEYWORD(130, 300, 0, 0, IIMAGE1D);
487 iimage2D KEYWORD(130, 300, 0, 0, IIMAGE2D);
488 iimage3D KEYWORD(130, 300, 0, 0, IIMAGE3D);
489 iimageCube KEYWORD(130, 300, 0, 0, IIMAGECUBE);
490 uimage1D KEYWORD(130, 300, 0, 0, UIMAGE1D);
491 uimage2D KEYWORD(130, 300, 0, 0, UIMAGE2D);
492 uimage3D KEYWORD(130, 300, 0, 0, UIMAGE3D);
493 uimageCube KEYWORD(130, 300, 0, 0, UIMAGECUBE);
494 image1DArray KEYWORD(130, 300, 0, 0, IMAGE1DARRAY);
495 image2DArray KEYWORD(130, 300, 0, 0, IMAGE2DARRAY);
496 iimage1DArray KEYWORD(130, 300, 0, 0, IIMAGE1DARRAY);
497 iimage2DArray KEYWORD(130, 300, 0, 0, IIMAGE2DARRAY);
498 uimage1DArray KEYWORD(130, 300, 0, 0, UIMAGE1DARRAY);
499 uimage2DArray KEYWORD(130, 300, 0, 0, UIMAGE2DARRAY);
500 image1DShadow KEYWORD(130, 300, 0, 0, IMAGE1DSHADOW);
501 image2DShadow KEYWORD(130, 300, 0, 0, IMAGE2DSHADOW);
502 image1DArrayShadow KEYWORD(130, 300, 0, 0, IMAGE1DARRAYSHADOW);
503 image2DArrayShadow KEYWORD(130, 300, 0, 0, IMAGE2DARRAYSHADOW);
504 imageBuffer KEYWORD(130, 300, 0, 0, IMAGEBUFFER);
505 iimageBuffer KEYWORD(130, 300, 0, 0, IIMAGEBUFFER);
506 uimageBuffer KEYWORD(130, 300, 0, 0, UIMAGEBUFFER);
507 row_major KEYWORD_WITH_ALT(130, 0, 140, 0, yyextra->ARB_uniform_buffer_object_enable && !yyextra->es_shader, ROW_MAJOR);
509 /* Additional reserved words in GLSL 1.40 */
510 isampler2DRect KEYWORD(140, 300, 140, 0, ISAMPLER2DRECT);
511 usampler2DRect KEYWORD(140, 300, 140, 0, USAMPLER2DRECT);
512 isamplerBuffer KEYWORD(140, 300, 140, 0, ISAMPLERBUFFER);
513 usamplerBuffer KEYWORD(140, 300, 140, 0, USAMPLERBUFFER);
515 /* Additional reserved words in GLSL ES 3.00 */
516 coherent KEYWORD(0, 300, 0, 0, COHERENT);
517 restrict KEYWORD(0, 300, 0, 0, RESTRICT);
518 readonly KEYWORD(0, 300, 0, 0, READONLY);
519 writeonly KEYWORD(0, 300, 0, 0, WRITEONLY);
520 resource KEYWORD(0, 300, 0, 0, RESOURCE);
521 atomic_uint KEYWORD(0, 300, 0, 0, ATOMIC_UINT);
522 patch KEYWORD(0, 300, 0, 0, PATCH);
523 sample KEYWORD(0, 300, 0, 0, SAMPLE);
524 subroutine KEYWORD(0, 300, 0, 0, SUBROUTINE);
527 [_a-zA-Z][_a-zA-Z0-9]* {
528 struct _mesa_glsl_parse_state *state = yyextra;
530 yylval->identifier = ralloc_strdup(ctx, yytext);
531 return classify_identifier(state, yytext);
534 . { return yytext[0]; }
539 classify_identifier(struct _mesa_glsl_parse_state *state, const char *name)
541 if (state->symbols->get_variable(name) || state->symbols->get_function(name))
543 else if (state->symbols->get_type(name))
544 return TYPE_IDENTIFIER;
546 return NEW_IDENTIFIER;
550 _mesa_glsl_lexer_ctor(struct _mesa_glsl_parse_state *state, const char *string)
552 yylex_init_extra(state, & state->scanner);
553 yy_scan_string(string, state->scanner);
557 _mesa_glsl_lexer_dtor(struct _mesa_glsl_parse_state *state)
559 yylex_destroy(state->scanner);