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.
27 #include "glsl_parser_extras.h"
28 #include "glsl_parser.h"
30 static int classify_identifier(struct _mesa_glsl_parse_state *, const char *);
33 #define YY_NO_UNISTD_H
36 #define YY_USER_ACTION \
39 yylloc->first_column = yycolumn + 1; \
40 yylloc->first_line = yylineno + 1; \
44 #define YY_USER_INIT yylineno = 0; yycolumn = 0;
46 #define IS_UINT (yytext[yyleng - 1] == 'u' || yytext[yyleng - 1] == 'U')
48 /* A macro for handling reserved words and keywords across language versions.
50 * Certain words start out as identifiers, become reserved words in
51 * later language revisions, and finally become language keywords.
53 * For example, consider the following lexer rule:
54 * samplerBuffer KEYWORD(130, 140, 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
61 #define KEYWORD(reserved_version, allowed_version, token) \
63 if (yyextra->language_version >= allowed_version) { \
65 } else if (yyextra->language_version >= reserved_version) { \
66 _mesa_glsl_error(yylloc, yyextra, \
67 "Illegal use of reserved word `%s'", yytext); \
70 yylval->identifier = strdup(yytext); \
71 return classify_identifier(yyextra, yytext); \
75 /* The ES macro can be used in KEYWORD checks:
77 * word KEYWORD(110 || ES, 400, TOKEN)
78 * ...means the word is reserved in GLSL ES 1.00, while
80 * word KEYWORD(110, 130 || ES, TOKEN)
81 * ...means the word is a legal keyword in GLSL ES 1.00.
83 #define ES yyextra->es_shader
86 %option bison-bridge bison-locations reentrant noyywrap
87 %option nounput noyy_top_state
88 %option never-interactive
89 %option prefix="_mesa_glsl_"
90 %option extra-type="struct _mesa_glsl_parse_state *"
95 HEX_INT 0[xX][0-9a-fA-F]+
97 INT ({DEC_INT}|{HEX_INT}|{OCT_INT})
105 /* Preprocessor tokens. */
107 ^[ \t]*#[ \t]*version { BEGIN PP; return VERSION; }
108 ^[ \t]*#[ \t]*extension { BEGIN PP; return EXTENSION; }
109 {HASH}line{SPCP}{INT}{SPCP}{INT}{SPC}$ {
110 /* Eat characters until the first digit is
114 while (!isdigit(*ptr))
117 /* Subtract one from the line number because
118 * yylineno is zero-based instead of
121 yylineno = strtol(ptr, &ptr, 0) - 1;
122 yylloc->source = strtol(ptr, NULL, 0);
124 {HASH}line{SPCP}{INT}{SPC}$ {
125 /* Eat characters until the first digit is
129 while (!isdigit(*ptr))
132 /* Subtract one from the line number because
133 * yylineno is zero-based instead of
136 yylineno = strtol(ptr, &ptr, 0) - 1;
138 ^{SPC}#{SPC}pragma{SPCP}debug{SPC}\({SPC}on{SPC}\) {
140 return PRAGMA_DEBUG_ON;
142 ^{SPC}#{SPC}pragma{SPCP}debug{SPC}\({SPC}off{SPC}\) {
144 return PRAGMA_DEBUG_OFF;
146 ^{SPC}#{SPC}pragma{SPCP}optimize{SPC}\({SPC}on{SPC}\) {
148 return PRAGMA_OPTIMIZE_ON;
150 ^{SPC}#{SPC}pragma{SPCP}optimize{SPC}\({SPC}off{SPC}\) {
152 return PRAGMA_OPTIMIZE_OFF;
154 ^{SPC}#{SPC}pragma{SPCP}STDGL{SPCP}invariant{SPC}\({SPC}all{SPC}\) {
156 return PRAGMA_INVARIANT_ALL;
158 ^{SPC}#{SPC}pragma{SPCP} { BEGIN PRAGMA; }
160 <PRAGMA>\n { BEGIN 0; yylineno++; yycolumn = 0; }
166 <PP>[_a-zA-Z][_a-zA-Z0-9]* {
167 yylval->identifier = strdup(yytext);
171 yylval->n = strtol(yytext, NULL, 10);
174 <PP>\n { BEGIN 0; yylineno++; yycolumn = 0; return EOL; }
176 \n { yylineno++; yycolumn = 0; }
178 attribute return ATTRIBUTE;
179 const return CONST_TOK;
180 bool return BOOL_TOK;
181 float return FLOAT_TOK;
183 uint KEYWORD(130, 130, UINT_TOK);
186 continue return CONTINUE;
192 discard return DISCARD;
193 return return RETURN;
201 uvec2 KEYWORD(130, 130, UVEC2);
202 uvec3 KEYWORD(130, 130, UVEC3);
203 uvec4 KEYWORD(130, 130, UVEC4);
210 mat2x2 KEYWORD(120, 120, MAT2X2);
211 mat2x3 KEYWORD(120, 120, MAT2X3);
212 mat2x4 KEYWORD(120, 120, MAT2X4);
213 mat3x2 KEYWORD(120, 120, MAT3X2);
214 mat3x3 KEYWORD(120, 120, MAT3X3);
215 mat3x4 KEYWORD(120, 120, MAT3X4);
216 mat4x2 KEYWORD(120, 120, MAT4X2);
217 mat4x3 KEYWORD(120, 120, MAT4X3);
218 mat4x4 KEYWORD(120, 120, MAT4X4);
222 inout return INOUT_TOK;
223 uniform return UNIFORM;
224 varying return VARYING;
225 centroid KEYWORD(120, 120, CENTROID);
226 invariant KEYWORD(120 || ES, 120 || ES, INVARIANT);
227 flat KEYWORD(130 || ES, 130, FLAT);
228 smooth KEYWORD(130, 130, SMOOTH);
229 noperspective KEYWORD(130, 130, NOPERSPECTIVE);
231 sampler1D return SAMPLER1D;
232 sampler2D return SAMPLER2D;
233 sampler3D return SAMPLER3D;
234 samplerCube return SAMPLERCUBE;
235 sampler1DArray KEYWORD(130, 130, SAMPLER1DARRAY);
236 sampler2DArray KEYWORD(130, 130, SAMPLER2DARRAY);
237 sampler1DShadow return SAMPLER1DSHADOW;
238 sampler2DShadow return SAMPLER2DSHADOW;
239 samplerCubeShadow KEYWORD(130, 130, SAMPLERCUBESHADOW);
240 sampler1DArrayShadow KEYWORD(130, 130, SAMPLER1DARRAYSHADOW);
241 sampler2DArrayShadow KEYWORD(130, 130, SAMPLER2DARRAYSHADOW);
242 isampler1D KEYWORD(130, 130, ISAMPLER1D);
243 isampler2D KEYWORD(130, 130, ISAMPLER2D);
244 isampler3D KEYWORD(130, 130, ISAMPLER3D);
245 isamplerCube KEYWORD(130, 130, ISAMPLERCUBE);
246 isampler1DArray KEYWORD(130, 130, ISAMPLER1DARRAY);
247 isampler2DArray KEYWORD(130, 130, ISAMPLER2DARRAY);
248 usampler1D KEYWORD(130, 130, USAMPLER1D);
249 usampler2D KEYWORD(130, 130, USAMPLER2D);
250 usampler3D KEYWORD(130, 130, USAMPLER3D);
251 usamplerCube KEYWORD(130, 130, USAMPLERCUBE);
252 usampler1DArray KEYWORD(130, 130, USAMPLER1DARRAY);
253 usampler2DArray KEYWORD(130, 130, USAMPLER2DARRAY);
256 struct return STRUCT;
257 void return VOID_TOK;
260 if ((yyextra->language_version >= 140)
261 || yyextra->AMD_conservative_depth_enable
262 || yyextra->ARB_explicit_attrib_location_enable
263 || yyextra->ARB_fragment_coord_conventions_enable) {
266 yylval->identifier = strdup(yytext);
281 ">>" return RIGHT_OP;
283 \*= return MUL_ASSIGN;
284 \/= return DIV_ASSIGN;
285 \+= return ADD_ASSIGN;
286 \%= return MOD_ASSIGN;
287 \<\<= return LEFT_ASSIGN;
288 >>= return RIGHT_ASSIGN;
289 &= return AND_ASSIGN;
290 "^=" return XOR_ASSIGN;
291 \|= return OR_ASSIGN;
292 -= return SUB_ASSIGN;
295 yylval->n = strtol(yytext, NULL, 10);
296 return IS_UINT ? UINTCONSTANT : INTCONSTANT;
298 0[xX][0-9a-fA-F]+[uU]? {
299 yylval->n = strtol(yytext + 2, NULL, 16);
300 return IS_UINT ? UINTCONSTANT : INTCONSTANT;
303 yylval->n = strtol(yytext, NULL, 8);
304 return IS_UINT ? UINTCONSTANT : INTCONSTANT;
307 [0-9]+\.[0-9]+([eE][+-]?[0-9]+)?[fF]? {
308 yylval->real = glsl_strtod(yytext, NULL);
309 return FLOATCONSTANT;
311 \.[0-9]+([eE][+-]?[0-9]+)?[fF]? {
312 yylval->real = glsl_strtod(yytext, NULL);
313 return FLOATCONSTANT;
315 [0-9]+\.([eE][+-]?[0-9]+)?[fF]? {
316 yylval->real = glsl_strtod(yytext, NULL);
317 return FLOATCONSTANT;
319 [0-9]+[eE][+-]?[0-9]+[fF]? {
320 yylval->real = glsl_strtod(yytext, NULL);
321 return FLOATCONSTANT;
324 yylval->real = glsl_strtod(yytext, NULL);
325 return FLOATCONSTANT;
338 /* Reserved words in GLSL 1.10. */
339 asm KEYWORD(110 || ES, 999, ASM);
340 class KEYWORD(110 || ES, 999, CLASS);
341 union KEYWORD(110 || ES, 999, UNION);
342 enum KEYWORD(110 || ES, 999, ENUM);
343 typedef KEYWORD(110 || ES, 999, TYPEDEF);
344 template KEYWORD(110 || ES, 999, TEMPLATE);
345 this KEYWORD(110 || ES, 999, THIS);
346 packed KEYWORD(110 || ES, 999, PACKED_TOK);
347 goto KEYWORD(110 || ES, 999, GOTO);
348 switch KEYWORD(110 || ES, 130, SWITCH);
349 default KEYWORD(110 || ES, 130, DEFAULT);
350 inline KEYWORD(110 || ES, 999, INLINE_TOK);
351 noinline KEYWORD(110 || ES, 999, NOINLINE);
352 volatile KEYWORD(110 || ES, 999, VOLATILE);
353 public KEYWORD(110 || ES, 999, PUBLIC_TOK);
354 static KEYWORD(110 || ES, 999, STATIC);
355 extern KEYWORD(110 || ES, 999, EXTERN);
356 external KEYWORD(110 || ES, 999, EXTERNAL);
357 interface KEYWORD(110 || ES, 999, INTERFACE);
358 long KEYWORD(110 || ES, 999, LONG_TOK);
359 short KEYWORD(110 || ES, 999, SHORT_TOK);
360 double KEYWORD(110 || ES, 400, DOUBLE_TOK);
361 half KEYWORD(110 || ES, 999, HALF);
362 fixed KEYWORD(110 || ES, 999, FIXED_TOK);
363 unsigned KEYWORD(110 || ES, 999, UNSIGNED);
364 input KEYWORD(110 || ES, 999, INPUT_TOK);
365 output KEYWORD(110 || ES, 999, OUTPUT);
366 hvec2 KEYWORD(110 || ES, 999, HVEC2);
367 hvec3 KEYWORD(110 || ES, 999, HVEC3);
368 hvec4 KEYWORD(110 || ES, 999, HVEC4);
369 dvec2 KEYWORD(110 || ES, 400, DVEC2);
370 dvec3 KEYWORD(110 || ES, 400, DVEC3);
371 dvec4 KEYWORD(110 || ES, 400, DVEC4);
372 fvec2 KEYWORD(110 || ES, 999, FVEC2);
373 fvec3 KEYWORD(110 || ES, 999, FVEC3);
374 fvec4 KEYWORD(110 || ES, 999, FVEC4);
375 sampler2DRect return SAMPLER2DRECT;
376 sampler3DRect KEYWORD(110 || ES, 999, SAMPLER3DRECT);
377 sampler2DRectShadow return SAMPLER2DRECTSHADOW;
378 sizeof KEYWORD(110 || ES, 999, SIZEOF);
379 cast KEYWORD(110 || ES, 999, CAST);
380 namespace KEYWORD(110 || ES, 999, NAMESPACE);
381 using KEYWORD(110 || ES, 999, USING);
383 /* Additional reserved words in GLSL 1.20. */
384 lowp KEYWORD(120, 130 || ES, LOWP);
385 mediump KEYWORD(120, 130 || ES, MEDIUMP);
386 highp KEYWORD(120, 130 || ES, HIGHP);
387 precision KEYWORD(120, 130 || ES, PRECISION);
389 /* Additional reserved words in GLSL 1.30. */
390 case KEYWORD(130, 130, CASE);
391 common KEYWORD(130, 999, COMMON);
392 partition KEYWORD(130, 999, PARTITION);
393 active KEYWORD(130, 999, ACTIVE);
394 superp KEYWORD(130 || ES, 999, SUPERP);
395 samplerBuffer KEYWORD(130, 140, SAMPLERBUFFER);
396 filter KEYWORD(130, 999, FILTER);
397 image1D KEYWORD(130, 999, IMAGE1D);
398 image2D KEYWORD(130, 999, IMAGE2D);
399 image3D KEYWORD(130, 999, IMAGE3D);
400 imageCube KEYWORD(130, 999, IMAGECUBE);
401 iimage1D KEYWORD(130, 999, IIMAGE1D);
402 iimage2D KEYWORD(130, 999, IIMAGE2D);
403 iimage3D KEYWORD(130, 999, IIMAGE3D);
404 iimageCube KEYWORD(130, 999, IIMAGECUBE);
405 uimage1D KEYWORD(130, 999, UIMAGE1D);
406 uimage2D KEYWORD(130, 999, UIMAGE2D);
407 uimage3D KEYWORD(130, 999, UIMAGE3D);
408 uimageCube KEYWORD(130, 999, UIMAGECUBE);
409 image1DArray KEYWORD(130, 999, IMAGE1DARRAY);
410 image2DArray KEYWORD(130, 999, IMAGE2DARRAY);
411 iimage1DArray KEYWORD(130, 999, IIMAGE1DARRAY);
412 iimage2DArray KEYWORD(130, 999, IIMAGE2DARRAY);
413 uimage1DArray KEYWORD(130, 999, UIMAGE1DARRAY);
414 uimage2DArray KEYWORD(130, 999, UIMAGE2DARRAY);
415 image1DShadow KEYWORD(130, 999, IMAGE1DSHADOW);
416 image2DShadow KEYWORD(130, 999, IMAGE2DSHADOW);
417 image1DArrayShadow KEYWORD(130, 999, IMAGE1DARRAYSHADOW);
418 image2DArrayShadow KEYWORD(130, 999, IMAGE2DARRAYSHADOW);
419 imageBuffer KEYWORD(130, 999, IMAGEBUFFER);
420 iimageBuffer KEYWORD(130, 999, IIMAGEBUFFER);
421 uimageBuffer KEYWORD(130, 999, UIMAGEBUFFER);
422 row_major KEYWORD(130, 999, ROW_MAJOR);
424 [_a-zA-Z][_a-zA-Z0-9]* {
425 struct _mesa_glsl_parse_state *state = yyextra;
427 yylval->identifier = ralloc_strdup(ctx, yytext);
428 return classify_identifier(state, yytext);
431 . { return yytext[0]; }
436 classify_identifier(struct _mesa_glsl_parse_state *state, const char *name)
438 if (state->symbols->get_variable(name) || state->symbols->get_function(name))
440 else if (state->symbols->get_type(name))
441 return TYPE_IDENTIFIER;
443 return NEW_IDENTIFIER;
447 _mesa_glsl_lexer_ctor(struct _mesa_glsl_parse_state *state, const char *string)
449 yylex_init_extra(state, & state->scanner);
450 yy_scan_string(string, state->scanner);
454 _mesa_glsl_lexer_dtor(struct _mesa_glsl_parse_state *state)
456 yylex_destroy(state->scanner);