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 literal_integer(char *text, int len, struct _mesa_glsl_parse_state *state,
90 YYSTYPE *lval, YYLTYPE *lloc, int base)
92 bool is_uint = (text[len - 1] == 'u' ||
93 text[len - 1] == 'U');
94 const char *digits = text;
101 unsigned __int64 value = _strtoui64(digits, NULL, base);
103 unsigned long long value = strtoull(digits, NULL, base);
106 lval->n = (int)value;
108 if (value > UINT_MAX) {
109 /* Note that signed 0xffffffff is valid, not out of range! */
110 if (state->is_version(130, 0)) {
111 _mesa_glsl_error(lloc, state,
112 "Literal value `%s' out of range", text);
114 _mesa_glsl_warning(lloc, state,
115 "Literal value `%s' out of range", text);
117 } else if (base == 10 && !is_uint && (unsigned)value > (unsigned)INT_MAX + 1) {
118 /* Tries to catch unintentionally providing a negative value.
119 * Note that -2147483648 is parsed as -(2147483648), so we don't
120 * want to warn for INT_MAX.
122 _mesa_glsl_warning(lloc, state,
123 "Signed literal value `%s' is interpreted as %d",
126 return is_uint ? UINTCONSTANT : INTCONSTANT;
129 #define LITERAL_INTEGER(base) \
130 literal_integer(yytext, yyleng, yyextra, yylval, yylloc, base)
134 %option bison-bridge bison-locations reentrant noyywrap
135 %option nounput noyy_top_state
136 %option never-interactive
137 %option prefix="_mesa_glsl_"
138 %option extra-type="struct _mesa_glsl_parse_state *"
143 HEX_INT 0[xX][0-9a-fA-F]+
145 INT ({DEC_INT}|{HEX_INT}|{OCT_INT})
153 /* Preprocessor tokens. */
155 ^[ \t]*#[ \t]*version { BEGIN PP; return VERSION_TOK; }
156 ^[ \t]*#[ \t]*extension { BEGIN PP; return EXTENSION; }
157 {HASH}line{SPCP}{INT}{SPCP}{INT}{SPC}$ {
158 /* Eat characters until the first digit is
162 while (!isdigit(*ptr))
165 /* Subtract one from the line number because
166 * yylineno is zero-based instead of
169 yylineno = strtol(ptr, &ptr, 0) - 1;
170 yylloc->source = strtol(ptr, NULL, 0);
172 {HASH}line{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;
186 ^{SPC}#{SPC}pragma{SPCP}debug{SPC}\({SPC}on{SPC}\) {
188 return PRAGMA_DEBUG_ON;
190 ^{SPC}#{SPC}pragma{SPCP}debug{SPC}\({SPC}off{SPC}\) {
192 return PRAGMA_DEBUG_OFF;
194 ^{SPC}#{SPC}pragma{SPCP}optimize{SPC}\({SPC}on{SPC}\) {
196 return PRAGMA_OPTIMIZE_ON;
198 ^{SPC}#{SPC}pragma{SPCP}optimize{SPC}\({SPC}off{SPC}\) {
200 return PRAGMA_OPTIMIZE_OFF;
202 ^{SPC}#{SPC}pragma{SPCP}STDGL{SPCP}invariant{SPC}\({SPC}all{SPC}\) {
204 return PRAGMA_INVARIANT_ALL;
206 ^{SPC}#{SPC}pragma{SPCP} { BEGIN PRAGMA; }
208 <PRAGMA>\n { BEGIN 0; yylineno++; yycolumn = 0; }
214 <PP>[_a-zA-Z][_a-zA-Z0-9]* {
215 yylval->identifier = strdup(yytext);
219 yylval->n = strtol(yytext, NULL, 10);
222 <PP>\n { BEGIN 0; yylineno++; yycolumn = 0; return EOL; }
224 \n { yylineno++; yycolumn = 0; }
226 attribute return ATTRIBUTE;
227 const return CONST_TOK;
228 bool return BOOL_TOK;
229 float return FLOAT_TOK;
231 uint KEYWORD(130, 0, 130, 0, UINT_TOK);
234 continue return CONTINUE;
240 discard return DISCARD;
241 return return RETURN;
249 uvec2 KEYWORD(130, 0, 130, 0, UVEC2);
250 uvec3 KEYWORD(130, 0, 130, 0, UVEC3);
251 uvec4 KEYWORD(130, 0, 130, 0, UVEC4);
258 mat2x2 KEYWORD(120, 0, 120, 0, MAT2X2);
259 mat2x3 KEYWORD(120, 0, 120, 0, MAT2X3);
260 mat2x4 KEYWORD(120, 0, 120, 0, MAT2X4);
261 mat3x2 KEYWORD(120, 0, 120, 0, MAT3X2);
262 mat3x3 KEYWORD(120, 0, 120, 0, MAT3X3);
263 mat3x4 KEYWORD(120, 0, 120, 0, MAT3X4);
264 mat4x2 KEYWORD(120, 0, 120, 0, MAT4X2);
265 mat4x3 KEYWORD(120, 0, 120, 0, MAT4X3);
266 mat4x4 KEYWORD(120, 0, 120, 0, MAT4X4);
270 inout return INOUT_TOK;
271 uniform return UNIFORM;
272 varying return VARYING;
273 centroid KEYWORD(120, 0, 120, 0, CENTROID);
274 invariant KEYWORD(120, 100, 120, 100, INVARIANT);
275 flat KEYWORD(130, 100, 130, 0, FLAT);
276 smooth KEYWORD(130, 0, 130, 0, SMOOTH);
277 noperspective KEYWORD(130, 0, 130, 0, NOPERSPECTIVE);
279 sampler1D return SAMPLER1D;
280 sampler2D return SAMPLER2D;
281 sampler3D return SAMPLER3D;
282 samplerCube return SAMPLERCUBE;
283 sampler1DArray KEYWORD(130, 0, 130, 0, SAMPLER1DARRAY);
284 sampler2DArray KEYWORD(130, 0, 130, 0, SAMPLER2DARRAY);
285 sampler1DShadow return SAMPLER1DSHADOW;
286 sampler2DShadow return SAMPLER2DSHADOW;
287 samplerCubeShadow KEYWORD(130, 0, 130, 0, SAMPLERCUBESHADOW);
288 sampler1DArrayShadow KEYWORD(130, 0, 130, 0, SAMPLER1DARRAYSHADOW);
289 sampler2DArrayShadow KEYWORD(130, 0, 130, 0, SAMPLER2DARRAYSHADOW);
290 isampler1D KEYWORD(130, 0, 130, 0, ISAMPLER1D);
291 isampler2D KEYWORD(130, 0, 130, 0, ISAMPLER2D);
292 isampler3D KEYWORD(130, 0, 130, 0, ISAMPLER3D);
293 isamplerCube KEYWORD(130, 0, 130, 0, ISAMPLERCUBE);
294 isampler1DArray KEYWORD(130, 0, 130, 0, ISAMPLER1DARRAY);
295 isampler2DArray KEYWORD(130, 0, 130, 0, ISAMPLER2DARRAY);
296 usampler1D KEYWORD(130, 0, 130, 0, USAMPLER1D);
297 usampler2D KEYWORD(130, 0, 130, 0, USAMPLER2D);
298 usampler3D KEYWORD(130, 0, 130, 0, USAMPLER3D);
299 usamplerCube KEYWORD(130, 0, 130, 0, USAMPLERCUBE);
300 usampler1DArray KEYWORD(130, 0, 130, 0, USAMPLER1DARRAY);
301 usampler2DArray KEYWORD(130, 0, 130, 0, USAMPLER2DARRAY);
304 if (yyextra->ARB_texture_cube_map_array_enable)
305 return SAMPLERCUBEARRAY;
310 if (yyextra->ARB_texture_cube_map_array_enable)
311 return ISAMPLERCUBEARRAY;
316 if (yyextra->ARB_texture_cube_map_array_enable)
317 return USAMPLERCUBEARRAY;
321 samplerCubeArrayShadow {
322 if (yyextra->ARB_texture_cube_map_array_enable)
323 return SAMPLERCUBEARRAYSHADOW;
329 if (yyextra->OES_EGL_image_external_enable)
330 return SAMPLEREXTERNALOES;
336 struct return STRUCT;
337 void return VOID_TOK;
340 if ((yyextra->is_version(140, 0))
341 || yyextra->AMD_conservative_depth_enable
342 || yyextra->ARB_conservative_depth_enable
343 || yyextra->ARB_explicit_attrib_location_enable
344 || yyextra->ARB_uniform_buffer_object_enable
345 || yyextra->ARB_fragment_coord_conventions_enable) {
348 yylval->identifier = strdup(yytext);
363 ">>" return RIGHT_OP;
365 \*= return MUL_ASSIGN;
366 \/= return DIV_ASSIGN;
367 \+= return ADD_ASSIGN;
368 \%= return MOD_ASSIGN;
369 \<\<= return LEFT_ASSIGN;
370 >>= return RIGHT_ASSIGN;
371 &= return AND_ASSIGN;
372 "^=" return XOR_ASSIGN;
373 \|= return OR_ASSIGN;
374 -= return SUB_ASSIGN;
377 return LITERAL_INTEGER(10);
379 0[xX][0-9a-fA-F]+[uU]? {
380 return LITERAL_INTEGER(16);
383 return LITERAL_INTEGER(8);
386 [0-9]+\.[0-9]+([eE][+-]?[0-9]+)?[fF]? {
387 yylval->real = glsl_strtod(yytext, NULL);
388 return FLOATCONSTANT;
390 \.[0-9]+([eE][+-]?[0-9]+)?[fF]? {
391 yylval->real = glsl_strtod(yytext, NULL);
392 return FLOATCONSTANT;
394 [0-9]+\.([eE][+-]?[0-9]+)?[fF]? {
395 yylval->real = glsl_strtod(yytext, NULL);
396 return FLOATCONSTANT;
398 [0-9]+[eE][+-]?[0-9]+[fF]? {
399 yylval->real = glsl_strtod(yytext, NULL);
400 return FLOATCONSTANT;
403 yylval->real = glsl_strtod(yytext, NULL);
404 return FLOATCONSTANT;
417 /* Reserved words in GLSL 1.10. */
418 asm KEYWORD(110, 100, 0, 0, ASM);
419 class KEYWORD(110, 100, 0, 0, CLASS);
420 union KEYWORD(110, 100, 0, 0, UNION);
421 enum KEYWORD(110, 100, 0, 0, ENUM);
422 typedef KEYWORD(110, 100, 0, 0, TYPEDEF);
423 template KEYWORD(110, 100, 0, 0, TEMPLATE);
424 this KEYWORD(110, 100, 0, 0, THIS);
425 packed KEYWORD_WITH_ALT(110, 100, 140, 0, yyextra->ARB_uniform_buffer_object_enable, PACKED_TOK);
426 goto KEYWORD(110, 100, 0, 0, GOTO);
427 switch KEYWORD(110, 100, 130, 0, SWITCH);
428 default KEYWORD(110, 100, 130, 0, DEFAULT);
429 inline KEYWORD(110, 100, 0, 0, INLINE_TOK);
430 noinline KEYWORD(110, 100, 0, 0, NOINLINE);
431 volatile KEYWORD(110, 100, 0, 0, VOLATILE);
432 public KEYWORD(110, 100, 0, 0, PUBLIC_TOK);
433 static KEYWORD(110, 100, 0, 0, STATIC);
434 extern KEYWORD(110, 100, 0, 0, EXTERN);
435 external KEYWORD(110, 100, 0, 0, EXTERNAL);
436 interface KEYWORD(110, 100, 0, 0, INTERFACE);
437 long KEYWORD(110, 100, 0, 0, LONG_TOK);
438 short KEYWORD(110, 100, 0, 0, SHORT_TOK);
439 double KEYWORD(110, 100, 400, 0, DOUBLE_TOK);
440 half KEYWORD(110, 100, 0, 0, HALF);
441 fixed KEYWORD(110, 100, 0, 0, FIXED_TOK);
442 unsigned KEYWORD(110, 100, 0, 0, UNSIGNED);
443 input KEYWORD(110, 100, 0, 0, INPUT_TOK);
444 output KEYWORD(110, 100, 0, 0, OUTPUT);
445 hvec2 KEYWORD(110, 100, 0, 0, HVEC2);
446 hvec3 KEYWORD(110, 100, 0, 0, HVEC3);
447 hvec4 KEYWORD(110, 100, 0, 0, HVEC4);
448 dvec2 KEYWORD(110, 100, 400, 0, DVEC2);
449 dvec3 KEYWORD(110, 100, 400, 0, DVEC3);
450 dvec4 KEYWORD(110, 100, 400, 0, DVEC4);
451 fvec2 KEYWORD(110, 100, 0, 0, FVEC2);
452 fvec3 KEYWORD(110, 100, 0, 0, FVEC3);
453 fvec4 KEYWORD(110, 100, 0, 0, FVEC4);
454 sampler2DRect return SAMPLER2DRECT;
455 sampler3DRect KEYWORD(110, 100, 0, 0, SAMPLER3DRECT);
456 sampler2DRectShadow return SAMPLER2DRECTSHADOW;
457 sizeof KEYWORD(110, 100, 0, 0, SIZEOF);
458 cast KEYWORD(110, 100, 0, 0, CAST);
459 namespace KEYWORD(110, 100, 0, 0, NAMESPACE);
460 using KEYWORD(110, 100, 0, 0, USING);
462 /* Additional reserved words in GLSL 1.20. */
463 lowp KEYWORD(120, 100, 130, 100, LOWP);
464 mediump KEYWORD(120, 100, 130, 100, MEDIUMP);
465 highp KEYWORD(120, 100, 130, 100, HIGHP);
466 precision KEYWORD(120, 100, 130, 100, PRECISION);
468 /* Additional reserved words in GLSL 1.30. */
469 case KEYWORD(130, 0, 130, 0, CASE);
470 common KEYWORD(130, 0, 0, 0, COMMON);
471 partition KEYWORD(130, 0, 0, 0, PARTITION);
472 active KEYWORD(130, 0, 0, 0, ACTIVE);
473 superp KEYWORD(130, 100, 0, 0, SUPERP);
474 samplerBuffer KEYWORD(130, 0, 140, 0, SAMPLERBUFFER);
475 filter KEYWORD(130, 0, 0, 0, FILTER);
476 image1D KEYWORD(130, 0, 0, 0, IMAGE1D);
477 image2D KEYWORD(130, 0, 0, 0, IMAGE2D);
478 image3D KEYWORD(130, 0, 0, 0, IMAGE3D);
479 imageCube KEYWORD(130, 0, 0, 0, IMAGECUBE);
480 iimage1D KEYWORD(130, 0, 0, 0, IIMAGE1D);
481 iimage2D KEYWORD(130, 0, 0, 0, IIMAGE2D);
482 iimage3D KEYWORD(130, 0, 0, 0, IIMAGE3D);
483 iimageCube KEYWORD(130, 0, 0, 0, IIMAGECUBE);
484 uimage1D KEYWORD(130, 0, 0, 0, UIMAGE1D);
485 uimage2D KEYWORD(130, 0, 0, 0, UIMAGE2D);
486 uimage3D KEYWORD(130, 0, 0, 0, UIMAGE3D);
487 uimageCube KEYWORD(130, 0, 0, 0, UIMAGECUBE);
488 image1DArray KEYWORD(130, 0, 0, 0, IMAGE1DARRAY);
489 image2DArray KEYWORD(130, 0, 0, 0, IMAGE2DARRAY);
490 iimage1DArray KEYWORD(130, 0, 0, 0, IIMAGE1DARRAY);
491 iimage2DArray KEYWORD(130, 0, 0, 0, IIMAGE2DARRAY);
492 uimage1DArray KEYWORD(130, 0, 0, 0, UIMAGE1DARRAY);
493 uimage2DArray KEYWORD(130, 0, 0, 0, UIMAGE2DARRAY);
494 image1DShadow KEYWORD(130, 0, 0, 0, IMAGE1DSHADOW);
495 image2DShadow KEYWORD(130, 0, 0, 0, IMAGE2DSHADOW);
496 image1DArrayShadow KEYWORD(130, 0, 0, 0, IMAGE1DARRAYSHADOW);
497 image2DArrayShadow KEYWORD(130, 0, 0, 0, IMAGE2DARRAYSHADOW);
498 imageBuffer KEYWORD(130, 0, 0, 0, IMAGEBUFFER);
499 iimageBuffer KEYWORD(130, 0, 0, 0, IIMAGEBUFFER);
500 uimageBuffer KEYWORD(130, 0, 0, 0, UIMAGEBUFFER);
501 row_major KEYWORD_WITH_ALT(130, 0, 140, 0, yyextra->ARB_uniform_buffer_object_enable, ROW_MAJOR);
503 /* Additional reserved words in GLSL 1.40 */
504 isampler2DRect KEYWORD(140, 0, 140, 0, ISAMPLER2DRECT);
505 usampler2DRect KEYWORD(140, 0, 140, 0, USAMPLER2DRECT);
506 isamplerBuffer KEYWORD(140, 0, 140, 0, ISAMPLERBUFFER);
507 usamplerBuffer KEYWORD(140, 0, 140, 0, USAMPLERBUFFER);
509 [_a-zA-Z][_a-zA-Z0-9]* {
510 struct _mesa_glsl_parse_state *state = yyextra;
512 yylval->identifier = ralloc_strdup(ctx, yytext);
513 return classify_identifier(state, yytext);
516 . { return yytext[0]; }
521 classify_identifier(struct _mesa_glsl_parse_state *state, const char *name)
523 if (state->symbols->get_variable(name) || state->symbols->get_function(name))
525 else if (state->symbols->get_type(name))
526 return TYPE_IDENTIFIER;
528 return NEW_IDENTIFIER;
532 _mesa_glsl_lexer_ctor(struct _mesa_glsl_parse_state *state, const char *string)
534 yylex_init_extra(state, & state->scanner);
535 yy_scan_string(string, state->scanner);
539 _mesa_glsl_lexer_dtor(struct _mesa_glsl_parse_state *state)
541 yylex_destroy(state->scanner);