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.
26 #include "glsl_parser_extras.h"
27 #include "glsl_parser.h"
29 #define YY_USER_ACTION \
32 yylloc->first_column = yycolumn + 1; \
33 yylloc->first_line = yylineno + 1; \
37 #define YY_USER_INIT yylineno = 0; yycolumn = 0;
39 #define TOKEN_OR_IDENTIFIER(version, token) \
41 if (yyextra->language_version >= version) { \
44 yylval->identifier = strdup(yytext); \
51 %option bison-bridge bison-locations reentrant noyywrap
52 %option nounput noyy_top_state
53 %option never-interactive
54 %option prefix="_mesa_glsl_"
55 %option extra-type="struct _mesa_glsl_parse_state *"
60 HEX_INT 0[xX][0-9a-fA-F]+
62 INT ({DEC_INT}|{HEX_INT}|{OCT_INT})
70 /* Preprocessor tokens. */
72 ^[ \t]*#[ \t]*version { BEGIN PP; return VERSION; }
73 ^[ \t]*#[ \t]*extension { BEGIN PP; return EXTENSION; }
74 {HASH}line{SPCP}{INT}{SPCP}{INT}{SPC}$ {
75 /* Eat characters until the first digit is
79 while (!isdigit(*ptr))
82 /* Subtract one from the line number because
83 * yylineno is zero-based instead of
86 yylineno = strtol(ptr, &ptr, 0) - 1;
87 yylloc->source = strtol(ptr, NULL, 0);
89 {HASH}line{SPCP}{INT}{SPC}$ {
90 /* Eat characters until the first digit is
94 while (!isdigit(*ptr))
97 /* Subtract one from the line number because
98 * yylineno is zero-based instead of
101 yylineno = strtol(ptr, &ptr, 0) - 1;
103 ^[ \t]*#[ \t]*pragma { BEGIN PP; return PRAGMA; }
107 <PP>[_a-zA-Z][_a-zA-Z0-9]* {
108 yylval->identifier = strdup(yytext);
112 yylval->n = strtol(yytext, NULL, 10);
115 <PP>\n { BEGIN 0; yylineno++; yycolumn = 0; return EOL; }
117 \n { yylineno++; yycolumn = 0; }
119 attribute return ATTRIBUTE;
120 const return CONST_TOK;
126 continue return CONTINUE;
132 discard return DISCARD;
133 return return RETURN;
147 mat2x2 TOKEN_OR_IDENTIFIER(120, MAT2X2);
148 mat2x3 TOKEN_OR_IDENTIFIER(120, MAT2X3);
149 mat2x4 TOKEN_OR_IDENTIFIER(120, MAT2X4);
150 mat3x2 TOKEN_OR_IDENTIFIER(120, MAT3X2);
151 mat3x3 TOKEN_OR_IDENTIFIER(120, MAT3X3);
152 mat3x4 TOKEN_OR_IDENTIFIER(120, MAT3X4);
153 mat4x2 TOKEN_OR_IDENTIFIER(120, MAT4X2);
154 mat4x3 TOKEN_OR_IDENTIFIER(120, MAT4X3);
155 mat4x4 TOKEN_OR_IDENTIFIER(120, MAT4X4);
160 uniform return UNIFORM;
161 varying return VARYING;
162 centroid TOKEN_OR_IDENTIFIER(120, CENTROID);
163 invariant TOKEN_OR_IDENTIFIER(120, INVARIANT);
165 flat TOKEN_OR_IDENTIFIER(130, FLAT);
166 smooth TOKEN_OR_IDENTIFIER(130, SMOOTH);
167 noperspective TOKEN_OR_IDENTIFIER(130, NOPERSPECTIVE);
169 sampler1D return SAMPLER1D;
170 sampler2D return SAMPLER2D;
171 sampler3D return SAMPLER3D;
172 samplerCube return SAMPLERCUBE;
173 sampler1DShadow return SAMPLER1DSHADOW;
174 sampler2DShadow return SAMPLER2DSHADOW;
176 struct return STRUCT;
180 if ((yyextra->language_version >= 140)
181 || (yyextra->ARB_fragment_coord_conventions_enable)){
184 yylval->identifier = strdup(yytext);
199 \*= return MUL_ASSIGN;
200 \/= return DIV_ASSIGN;
201 \+= return ADD_ASSIGN;
202 \%= return MOD_ASSIGN;
203 \<\<= return LEFT_ASSIGN;
204 >>= return RIGHT_ASSIGN;
205 &= return AND_ASSIGN;
206 ^= return XOR_ASSIGN;
207 \|= return OR_ASSIGN;
208 -= return SUB_ASSIGN;
211 yylval->n = strtol(yytext, NULL, 10);
215 yylval->n = strtol(yytext + 2, NULL, 16);
219 yylval->n = strtol(yytext, NULL, 8);
223 [0-9]+\.[0-9]+([eE][+-]?[0-9]+)?[fF]? {
224 yylval->real = strtod(yytext, NULL);
225 return FLOATCONSTANT;
227 \.[0-9]+([eE][+-]?[0-9]+)?[fF]? {
228 yylval->real = strtod(yytext, NULL);
229 return FLOATCONSTANT;
231 [0-9]+\.([eE][+-]?[0-9]+)?[fF]? {
232 yylval->real = strtod(yytext, NULL);
233 return FLOATCONSTANT;
235 [0-9]+[eE][+-]?[0-9]+[fF]? {
236 yylval->real = strtod(yytext, NULL);
237 return FLOATCONSTANT;
240 yylval->real = strtod(yytext, NULL);
241 return FLOATCONSTANT;
254 /* Reserved words in GLSL 1.10. */
259 typedef return TYPEDEF;
260 template return TEMPLATE;
262 packed return PACKED;
264 switch return SWITCH;
265 default return DEFAULT;
266 inline return INLINE_TOK;
267 noinline return NOINLINE;
268 volatile return VOLATILE;
269 public return PUBLIC_TOK;
270 static return STATIC;
271 extern return EXTERN;
272 external return EXTERNAL;
273 interface return INTERFACE;
276 double return DOUBLE;
279 unsigned return UNSIGNED;
281 output return OUTPUT;
291 sampler2DRect return SAMPLER2DRECT;
292 sampler3DRect return SAMPLER3DRECT;
293 sampler2DRectShadow return SAMPLER2DRECTSHADOW;
294 sizeof return SIZEOF;
296 namespace return NAMESPACE;
299 /* Additional reserved words in GLSL 1.20. */
300 lowp TOKEN_OR_IDENTIFIER(120, LOWP);
301 mediump TOKEN_OR_IDENTIFIER(120, MEDIUMP);
302 highp TOKEN_OR_IDENTIFIER(120, HIGHP);
303 precision TOKEN_OR_IDENTIFIER(120, PRECISION);
305 /* Additional reserved words in GLSL 1.30. */
306 common TOKEN_OR_IDENTIFIER(130, COMMON);
307 partition TOKEN_OR_IDENTIFIER(130, PARTITION);
308 active TOKEN_OR_IDENTIFIER(130, ACTIVE);
309 superp TOKEN_OR_IDENTIFIER(130, SUPERP);
310 samplerBuffer TOKEN_OR_IDENTIFIER(130, SAMPLERBUFFER);
311 filter TOKEN_OR_IDENTIFIER(130, FILTER);
312 image1D TOKEN_OR_IDENTIFIER(130, IMAGE1D);
313 image2D TOKEN_OR_IDENTIFIER(130, IMAGE2D);
314 image3D TOKEN_OR_IDENTIFIER(130, IMAGE3D);
315 imageCube TOKEN_OR_IDENTIFIER(130, IMAGECUBE);
316 iimage1D TOKEN_OR_IDENTIFIER(130, IIMAGE1D);
317 iimage2D TOKEN_OR_IDENTIFIER(130, IIMAGE2D);
318 iimage3D TOKEN_OR_IDENTIFIER(130, IIMAGE3D);
319 iimageCube TOKEN_OR_IDENTIFIER(130, IIMAGECUBE);
320 uimage1D TOKEN_OR_IDENTIFIER(130, UIMAGE1D);
321 uimage2D TOKEN_OR_IDENTIFIER(130, UIMAGE2D);
322 uimage3D TOKEN_OR_IDENTIFIER(130, UIMAGE3D);
323 uimageCube TOKEN_OR_IDENTIFIER(130, UIMAGECUBE);
324 image1DArray TOKEN_OR_IDENTIFIER(130, IMAGE1DARRAY);
325 image2DArray TOKEN_OR_IDENTIFIER(130, IMAGE2DARRAY);
326 iimage1DArray TOKEN_OR_IDENTIFIER(130, IIMAGE1DARRAY);
327 iimage2DArray TOKEN_OR_IDENTIFIER(130, IIMAGE2DARRAY);
328 uimage1DArray TOKEN_OR_IDENTIFIER(130, UIMAGE1DARRAY);
329 uimage2DArray TOKEN_OR_IDENTIFIER(130, UIMAGE2DARRAY);
330 image1DShadow TOKEN_OR_IDENTIFIER(130, IMAGE1DSHADOW);
331 image2DShadow TOKEN_OR_IDENTIFIER(130, IMAGE2DSHADOW);
332 imageBuffer TOKEN_OR_IDENTIFIER(130, IMAGEBUFFER);
333 iimageBuffer TOKEN_OR_IDENTIFIER(130, IIMAGEBUFFER);
334 uimageBuffer TOKEN_OR_IDENTIFIER(130, UIMAGEBUFFER);
335 row_major TOKEN_OR_IDENTIFIER(130, ROW_MAJOR);
337 [_a-zA-Z][_a-zA-Z0-9]* {
338 struct _mesa_glsl_parse_state *state = yyextra;
340 yylval->identifier = talloc_strdup(ctx, yytext);
344 . { return yytext[0]; }
349 _mesa_glsl_lexer_ctor(struct _mesa_glsl_parse_state *state, const char *string)
351 yylex_init_extra(state, & state->scanner);
352 yy_scan_string(string, state->scanner);
356 _mesa_glsl_lexer_dtor(struct _mesa_glsl_parse_state *state)
358 yylex_destroy(state->scanner);