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;
250 /* Reserved words in GLSL 1.10. */
255 typedef return TYPEDEF;
256 template return TEMPLATE;
258 packed return PACKED;
260 switch return SWITCH;
261 default return DEFAULT;
262 inline return INLINE_TOK;
263 noinline return NOINLINE;
264 volatile return VOLATILE;
265 public return PUBLIC_TOK;
266 static return STATIC;
267 extern return EXTERN;
268 external return EXTERNAL;
269 interface return INTERFACE;
272 double return DOUBLE;
275 unsigned return UNSIGNED;
277 output return OUTPUT;
287 sampler2DRect return SAMPLER2DRECT;
288 sampler3DRect return SAMPLER3DRECT;
289 sampler2DRectShadow return SAMPLER2DRECTSHADOW;
290 sizeof return SIZEOF;
292 namespace return NAMESPACE;
295 /* Additional reserved words in GLSL 1.20. */
296 lowp TOKEN_OR_IDENTIFIER(120, LOWP);
297 mediump TOKEN_OR_IDENTIFIER(120, MEDIUMP);
298 highp TOKEN_OR_IDENTIFIER(120, HIGHP);
299 precision TOKEN_OR_IDENTIFIER(120, PRECISION);
301 [_a-zA-Z][_a-zA-Z0-9]* {
302 struct _mesa_glsl_parse_state *state = yyextra;
304 yylval->identifier = talloc_strdup(ctx, yytext);
308 . { return yytext[0]; }
313 _mesa_glsl_lexer_ctor(struct _mesa_glsl_parse_state *state, const char *string)
315 yylex_init_extra(state, & state->scanner);
316 yy_scan_string(string, state->scanner);
320 _mesa_glsl_lexer_dtor(struct _mesa_glsl_parse_state *state)
322 yylex_destroy(state->scanner);