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.
52 * For example, consider the following lexer rule:
53 * samplerBuffer KEYWORD(130, 140, SAMPLERBUFFER)
55 * This means that "samplerBuffer" will be treated as:
56 * - a keyword (SAMPLERBUFFER token) ...in GLSL >= 1.40
57 * - a reserved word - error ...in GLSL >= 1.30
58 * - an identifier ...in GLSL < 1.30
60 #define KEYWORD(reserved_version, allowed_version, token) \
62 if (yyextra->language_version >= allowed_version) { \
64 } else if (yyextra->language_version >= reserved_version) { \
65 _mesa_glsl_error(yylloc, yyextra, \
66 "Illegal use of reserved word `%s'", yytext); \
69 yylval->identifier = strdup(yytext); \
70 return classify_identifier(yyextra, yytext); \
74 /* The ES macro can be used in KEYWORD checks:
76 * word KEYWORD(110 || ES, 400, TOKEN)
77 * ...means the word is reserved in GLSL ES 1.00, while
79 * word KEYWORD(110, 130 || ES, TOKEN)
80 * ...means the word is a legal keyword in GLSL ES 1.00.
82 #define ES yyextra->es_shader
85 literal_integer(char *text, int len, struct _mesa_glsl_parse_state *state,
86 YYSTYPE *lval, YYLTYPE *lloc, int base)
88 bool is_uint = (text[len - 1] == 'u' ||
89 text[len - 1] == 'U');
90 const char *digits = text;
97 unsigned __int64 value = _strtoui64(digits, NULL, base);
99 unsigned long long value = strtoull(digits, NULL, base);
102 lval->n = (int)value;
104 if (value > UINT_MAX) {
105 /* Note that signed 0xffffffff is valid, not out of range! */
106 if (state->language_version >= 130) {
107 _mesa_glsl_error(lloc, state,
108 "Literal value `%s' out of range", text);
110 _mesa_glsl_warning(lloc, state,
111 "Literal value `%s' out of range", text);
113 } else if (base == 10 && !is_uint && (unsigned)value > (unsigned)INT_MAX + 1) {
114 /* Tries to catch unintentionally providing a negative value.
115 * Note that -2147483648 is parsed as -(2147483648), so we don't
116 * want to warn for INT_MAX.
118 _mesa_glsl_warning(lloc, state,
119 "Signed literal value `%s' is interpreted as %d",
122 return is_uint ? UINTCONSTANT : INTCONSTANT;
125 #define LITERAL_INTEGER(base) \
126 literal_integer(yytext, yyleng, yyextra, yylval, yylloc, base)
130 %option bison-bridge bison-locations reentrant noyywrap
131 %option nounput noyy_top_state
132 %option never-interactive
133 %option prefix="_mesa_glsl_"
134 %option extra-type="struct _mesa_glsl_parse_state *"
139 HEX_INT 0[xX][0-9a-fA-F]+
141 INT ({DEC_INT}|{HEX_INT}|{OCT_INT})
149 /* Preprocessor tokens. */
151 ^[ \t]*#[ \t]*version { BEGIN PP; return VERSION_TOK; }
152 ^[ \t]*#[ \t]*extension { BEGIN PP; return EXTENSION; }
153 {HASH}line{SPCP}{INT}{SPCP}{INT}{SPC}$ {
154 /* Eat characters until the first digit is
158 while (!isdigit(*ptr))
161 /* Subtract one from the line number because
162 * yylineno is zero-based instead of
165 yylineno = strtol(ptr, &ptr, 0) - 1;
166 yylloc->source = strtol(ptr, NULL, 0);
168 {HASH}line{SPCP}{INT}{SPC}$ {
169 /* Eat characters until the first digit is
173 while (!isdigit(*ptr))
176 /* Subtract one from the line number because
177 * yylineno is zero-based instead of
180 yylineno = strtol(ptr, &ptr, 0) - 1;
182 ^{SPC}#{SPC}pragma{SPCP}debug{SPC}\({SPC}on{SPC}\) {
184 return PRAGMA_DEBUG_ON;
186 ^{SPC}#{SPC}pragma{SPCP}debug{SPC}\({SPC}off{SPC}\) {
188 return PRAGMA_DEBUG_OFF;
190 ^{SPC}#{SPC}pragma{SPCP}optimize{SPC}\({SPC}on{SPC}\) {
192 return PRAGMA_OPTIMIZE_ON;
194 ^{SPC}#{SPC}pragma{SPCP}optimize{SPC}\({SPC}off{SPC}\) {
196 return PRAGMA_OPTIMIZE_OFF;
198 ^{SPC}#{SPC}pragma{SPCP}STDGL{SPCP}invariant{SPC}\({SPC}all{SPC}\) {
200 return PRAGMA_INVARIANT_ALL;
202 ^{SPC}#{SPC}pragma{SPCP} { BEGIN PRAGMA; }
204 <PRAGMA>\n { BEGIN 0; yylineno++; yycolumn = 0; }
210 <PP>[_a-zA-Z][_a-zA-Z0-9]* {
211 yylval->identifier = strdup(yytext);
215 yylval->n = strtol(yytext, NULL, 10);
218 <PP>\n { BEGIN 0; yylineno++; yycolumn = 0; return EOL; }
220 \n { yylineno++; yycolumn = 0; }
222 attribute return ATTRIBUTE;
223 const return CONST_TOK;
224 bool return BOOL_TOK;
225 float return FLOAT_TOK;
227 uint KEYWORD(130, 130, UINT_TOK);
230 continue return CONTINUE;
236 discard return DISCARD;
237 return return RETURN;
245 uvec2 KEYWORD(130, 130, UVEC2);
246 uvec3 KEYWORD(130, 130, UVEC3);
247 uvec4 KEYWORD(130, 130, UVEC4);
254 mat2x2 KEYWORD(120, 120, MAT2X2);
255 mat2x3 KEYWORD(120, 120, MAT2X3);
256 mat2x4 KEYWORD(120, 120, MAT2X4);
257 mat3x2 KEYWORD(120, 120, MAT3X2);
258 mat3x3 KEYWORD(120, 120, MAT3X3);
259 mat3x4 KEYWORD(120, 120, MAT3X4);
260 mat4x2 KEYWORD(120, 120, MAT4X2);
261 mat4x3 KEYWORD(120, 120, MAT4X3);
262 mat4x4 KEYWORD(120, 120, MAT4X4);
266 inout return INOUT_TOK;
267 uniform return UNIFORM;
268 varying return VARYING;
269 centroid KEYWORD(120, 120, CENTROID);
270 invariant KEYWORD(120 || ES, 120 || ES, INVARIANT);
271 flat KEYWORD(130 || ES, 130, FLAT);
272 smooth KEYWORD(130, 130, SMOOTH);
273 noperspective KEYWORD(130, 130, NOPERSPECTIVE);
275 sampler1D return SAMPLER1D;
276 sampler2D return SAMPLER2D;
277 sampler3D return SAMPLER3D;
278 samplerCube return SAMPLERCUBE;
279 sampler1DArray KEYWORD(130, 130, SAMPLER1DARRAY);
280 sampler2DArray KEYWORD(130, 130, SAMPLER2DARRAY);
281 sampler1DShadow return SAMPLER1DSHADOW;
282 sampler2DShadow return SAMPLER2DSHADOW;
283 samplerCubeShadow KEYWORD(130, 130, SAMPLERCUBESHADOW);
284 sampler1DArrayShadow KEYWORD(130, 130, SAMPLER1DARRAYSHADOW);
285 sampler2DArrayShadow KEYWORD(130, 130, SAMPLER2DARRAYSHADOW);
286 isampler1D KEYWORD(130, 130, ISAMPLER1D);
287 isampler2D KEYWORD(130, 130, ISAMPLER2D);
288 isampler3D KEYWORD(130, 130, ISAMPLER3D);
289 isamplerCube KEYWORD(130, 130, ISAMPLERCUBE);
290 isampler1DArray KEYWORD(130, 130, ISAMPLER1DARRAY);
291 isampler2DArray KEYWORD(130, 130, ISAMPLER2DARRAY);
292 usampler1D KEYWORD(130, 130, USAMPLER1D);
293 usampler2D KEYWORD(130, 130, USAMPLER2D);
294 usampler3D KEYWORD(130, 130, USAMPLER3D);
295 usamplerCube KEYWORD(130, 130, USAMPLERCUBE);
296 usampler1DArray KEYWORD(130, 130, USAMPLER1DARRAY);
297 usampler2DArray KEYWORD(130, 130, USAMPLER2DARRAY);
300 if (yyextra->ARB_texture_cube_map_array_enable)
301 return SAMPLERCUBEARRAY;
306 if (yyextra->ARB_texture_cube_map_array_enable)
307 return ISAMPLERCUBEARRAY;
312 if (yyextra->ARB_texture_cube_map_array_enable)
313 return USAMPLERCUBEARRAY;
317 samplerCubeArrayShadow {
318 if (yyextra->ARB_texture_cube_map_array_enable)
319 return SAMPLERCUBEARRAYSHADOW;
325 if (yyextra->OES_EGL_image_external_enable)
326 return SAMPLEREXTERNALOES;
332 struct return STRUCT;
333 void return VOID_TOK;
336 if ((yyextra->language_version >= 140)
337 || yyextra->AMD_conservative_depth_enable
338 || yyextra->ARB_conservative_depth_enable
339 || yyextra->ARB_explicit_attrib_location_enable
340 || yyextra->ARB_uniform_buffer_object_enable
341 || yyextra->ARB_fragment_coord_conventions_enable) {
344 yylval->identifier = strdup(yytext);
359 ">>" return RIGHT_OP;
361 \*= return MUL_ASSIGN;
362 \/= return DIV_ASSIGN;
363 \+= return ADD_ASSIGN;
364 \%= return MOD_ASSIGN;
365 \<\<= return LEFT_ASSIGN;
366 >>= return RIGHT_ASSIGN;
367 &= return AND_ASSIGN;
368 "^=" return XOR_ASSIGN;
369 \|= return OR_ASSIGN;
370 -= return SUB_ASSIGN;
373 return LITERAL_INTEGER(10);
375 0[xX][0-9a-fA-F]+[uU]? {
376 return LITERAL_INTEGER(16);
379 return LITERAL_INTEGER(8);
382 [0-9]+\.[0-9]+([eE][+-]?[0-9]+)?[fF]? {
383 yylval->real = glsl_strtod(yytext, NULL);
384 return FLOATCONSTANT;
386 \.[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;
399 yylval->real = glsl_strtod(yytext, NULL);
400 return FLOATCONSTANT;
413 /* Reserved words in GLSL 1.10. */
414 asm KEYWORD(110 || ES, 999, ASM);
415 class KEYWORD(110 || ES, 999, CLASS);
416 union KEYWORD(110 || ES, 999, UNION);
417 enum KEYWORD(110 || ES, 999, ENUM);
418 typedef KEYWORD(110 || ES, 999, TYPEDEF);
419 template KEYWORD(110 || ES, 999, TEMPLATE);
420 this KEYWORD(110 || ES, 999, THIS);
421 packed KEYWORD(110 || ES, 140 || yyextra->ARB_uniform_buffer_object_enable, PACKED_TOK);
422 goto KEYWORD(110 || ES, 999, GOTO);
423 switch KEYWORD(110 || ES, 130, SWITCH);
424 default KEYWORD(110 || ES, 130, DEFAULT);
425 inline KEYWORD(110 || ES, 999, INLINE_TOK);
426 noinline KEYWORD(110 || ES, 999, NOINLINE);
427 volatile KEYWORD(110 || ES, 999, VOLATILE);
428 public KEYWORD(110 || ES, 999, PUBLIC_TOK);
429 static KEYWORD(110 || ES, 999, STATIC);
430 extern KEYWORD(110 || ES, 999, EXTERN);
431 external KEYWORD(110 || ES, 999, EXTERNAL);
432 interface KEYWORD(110 || ES, 999, INTERFACE);
433 long KEYWORD(110 || ES, 999, LONG_TOK);
434 short KEYWORD(110 || ES, 999, SHORT_TOK);
435 double KEYWORD(110 || ES, 400, DOUBLE_TOK);
436 half KEYWORD(110 || ES, 999, HALF);
437 fixed KEYWORD(110 || ES, 999, FIXED_TOK);
438 unsigned KEYWORD(110 || ES, 999, UNSIGNED);
439 input KEYWORD(110 || ES, 999, INPUT_TOK);
440 output KEYWORD(110 || ES, 999, OUTPUT);
441 hvec2 KEYWORD(110 || ES, 999, HVEC2);
442 hvec3 KEYWORD(110 || ES, 999, HVEC3);
443 hvec4 KEYWORD(110 || ES, 999, HVEC4);
444 dvec2 KEYWORD(110 || ES, 400, DVEC2);
445 dvec3 KEYWORD(110 || ES, 400, DVEC3);
446 dvec4 KEYWORD(110 || ES, 400, DVEC4);
447 fvec2 KEYWORD(110 || ES, 999, FVEC2);
448 fvec3 KEYWORD(110 || ES, 999, FVEC3);
449 fvec4 KEYWORD(110 || ES, 999, FVEC4);
450 sampler2DRect return SAMPLER2DRECT;
451 sampler3DRect KEYWORD(110 || ES, 999, SAMPLER3DRECT);
452 sampler2DRectShadow return SAMPLER2DRECTSHADOW;
453 sizeof KEYWORD(110 || ES, 999, SIZEOF);
454 cast KEYWORD(110 || ES, 999, CAST);
455 namespace KEYWORD(110 || ES, 999, NAMESPACE);
456 using KEYWORD(110 || ES, 999, USING);
458 /* Additional reserved words in GLSL 1.20. */
459 lowp KEYWORD(120, 130 || ES, LOWP);
460 mediump KEYWORD(120, 130 || ES, MEDIUMP);
461 highp KEYWORD(120, 130 || ES, HIGHP);
462 precision KEYWORD(120, 130 || ES, PRECISION);
464 /* Additional reserved words in GLSL 1.30. */
465 case KEYWORD(130, 130, CASE);
466 common KEYWORD(130, 999, COMMON);
467 partition KEYWORD(130, 999, PARTITION);
468 active KEYWORD(130, 999, ACTIVE);
469 superp KEYWORD(130 || ES, 999, SUPERP);
470 samplerBuffer KEYWORD(130, 140, SAMPLERBUFFER);
471 filter KEYWORD(130, 999, FILTER);
472 image1D KEYWORD(130, 999, IMAGE1D);
473 image2D KEYWORD(130, 999, IMAGE2D);
474 image3D KEYWORD(130, 999, IMAGE3D);
475 imageCube KEYWORD(130, 999, IMAGECUBE);
476 iimage1D KEYWORD(130, 999, IIMAGE1D);
477 iimage2D KEYWORD(130, 999, IIMAGE2D);
478 iimage3D KEYWORD(130, 999, IIMAGE3D);
479 iimageCube KEYWORD(130, 999, IIMAGECUBE);
480 uimage1D KEYWORD(130, 999, UIMAGE1D);
481 uimage2D KEYWORD(130, 999, UIMAGE2D);
482 uimage3D KEYWORD(130, 999, UIMAGE3D);
483 uimageCube KEYWORD(130, 999, UIMAGECUBE);
484 image1DArray KEYWORD(130, 999, IMAGE1DARRAY);
485 image2DArray KEYWORD(130, 999, IMAGE2DARRAY);
486 iimage1DArray KEYWORD(130, 999, IIMAGE1DARRAY);
487 iimage2DArray KEYWORD(130, 999, IIMAGE2DARRAY);
488 uimage1DArray KEYWORD(130, 999, UIMAGE1DARRAY);
489 uimage2DArray KEYWORD(130, 999, UIMAGE2DARRAY);
490 image1DShadow KEYWORD(130, 999, IMAGE1DSHADOW);
491 image2DShadow KEYWORD(130, 999, IMAGE2DSHADOW);
492 image1DArrayShadow KEYWORD(130, 999, IMAGE1DARRAYSHADOW);
493 image2DArrayShadow KEYWORD(130, 999, IMAGE2DARRAYSHADOW);
494 imageBuffer KEYWORD(130, 999, IMAGEBUFFER);
495 iimageBuffer KEYWORD(130, 999, IIMAGEBUFFER);
496 uimageBuffer KEYWORD(130, 999, UIMAGEBUFFER);
497 row_major KEYWORD(130, 140 || yyextra->ARB_uniform_buffer_object_enable, ROW_MAJOR);
499 /* Additional reserved words in GLSL 1.40 */
500 isampler2DRect KEYWORD(140, 140, ISAMPLER2DRECT);
501 usampler2DRect KEYWORD(140, 140, USAMPLER2DRECT);
502 isamplerBuffer KEYWORD(140, 140, ISAMPLERBUFFER);
503 usamplerBuffer KEYWORD(140, 140, USAMPLERBUFFER);
505 [_a-zA-Z][_a-zA-Z0-9]* {
506 struct _mesa_glsl_parse_state *state = yyextra;
508 yylval->identifier = ralloc_strdup(ctx, yytext);
509 return classify_identifier(state, yytext);
512 . { return yytext[0]; }
517 classify_identifier(struct _mesa_glsl_parse_state *state, const char *name)
519 if (state->symbols->get_variable(name) || state->symbols->get_function(name))
521 else if (state->symbols->get_type(name))
522 return TYPE_IDENTIFIER;
524 return NEW_IDENTIFIER;
528 _mesa_glsl_lexer_ctor(struct _mesa_glsl_parse_state *state, const char *string)
530 yylex_init_extra(state, & state->scanner);
531 yy_scan_string(string, state->scanner);
535 _mesa_glsl_lexer_dtor(struct _mesa_glsl_parse_state *state)
537 yylex_destroy(state->scanner);