Merge branch 'llvm-cliptest-viewport'
[mesa.git] / src / glsl / glsl_lexer.lpp
1 %{
2 /*
3 * Copyright © 2008, 2009 Intel Corporation
4 *
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:
11 *
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
14 * Software.
15 *
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.
23 */
24 #include <ctype.h>
25 #include "ast.h"
26 #include "glsl_parser_extras.h"
27 #include "glsl_parser.h"
28
29 #define YY_USER_ACTION \
30 do { \
31 yylloc->source = 0; \
32 yylloc->first_column = yycolumn + 1; \
33 yylloc->first_line = yylineno + 1; \
34 yycolumn += yyleng; \
35 } while(0);
36
37 #define YY_USER_INIT yylineno = 0; yycolumn = 0;
38
39 #define TOKEN_OR_IDENTIFIER(version, token) \
40 do { \
41 if (yyextra->language_version >= version) { \
42 return token; \
43 } else { \
44 yylval->identifier = strdup(yytext); \
45 return IDENTIFIER; \
46 } \
47 } while (0)
48
49 /* Handle reserved words in GLSL ES (version 100) */
50 #define TOKEN_OR_IDENTIFIER_ES(version, token) \
51 do { \
52 if (yyextra->es_shader) { \
53 return token; \
54 } else { \
55 TOKEN_OR_IDENTIFIER(version, token); \
56 } \
57 } while (0)
58
59 #define RESERVED_WORD(version, token) \
60 do { \
61 if (yyextra->language_version >= version) { \
62 return token; \
63 } else { \
64 _mesa_glsl_error(yylloc, yyextra, \
65 "Illegal use of reserved word `%s'", yytext); \
66 return ERROR_TOK; \
67 } \
68 } while (0)
69 %}
70
71 %option bison-bridge bison-locations reentrant noyywrap
72 %option nounput noyy_top_state
73 %option never-interactive
74 %option prefix="_mesa_glsl_"
75 %option extra-type="struct _mesa_glsl_parse_state *"
76
77 %x PP PRAGMA
78
79 DEC_INT [1-9][0-9]*
80 HEX_INT 0[xX][0-9a-fA-F]+
81 OCT_INT 0[0-7]*
82 INT ({DEC_INT}|{HEX_INT}|{OCT_INT})
83 SPC [ \t]*
84 SPCP [ \t]+
85 HASH ^{SPC}#{SPC}
86 %%
87
88 [ \r\t]+ ;
89
90 /* Preprocessor tokens. */
91 ^[ \t]*#[ \t]*$ ;
92 ^[ \t]*#[ \t]*version { BEGIN PP; return VERSION; }
93 ^[ \t]*#[ \t]*extension { BEGIN PP; return EXTENSION; }
94 {HASH}line{SPCP}{INT}{SPCP}{INT}{SPC}$ {
95 /* Eat characters until the first digit is
96 * encountered
97 */
98 char *ptr = yytext;
99 while (!isdigit(*ptr))
100 ptr++;
101
102 /* Subtract one from the line number because
103 * yylineno is zero-based instead of
104 * one-based.
105 */
106 yylineno = strtol(ptr, &ptr, 0) - 1;
107 yylloc->source = strtol(ptr, NULL, 0);
108 }
109 {HASH}line{SPCP}{INT}{SPC}$ {
110 /* Eat characters until the first digit is
111 * encountered
112 */
113 char *ptr = yytext;
114 while (!isdigit(*ptr))
115 ptr++;
116
117 /* Subtract one from the line number because
118 * yylineno is zero-based instead of
119 * one-based.
120 */
121 yylineno = strtol(ptr, &ptr, 0) - 1;
122 }
123 ^{SPC}#{SPC}pragma{SPCP}debug{SPC}\({SPC}on{SPC}\) {
124 BEGIN PP;
125 return PRAGMA_DEBUG_ON;
126 }
127 ^{SPC}#{SPC}pragma{SPCP}debug{SPC}\({SPC}off{SPC}\) {
128 BEGIN PP;
129 return PRAGMA_DEBUG_OFF;
130 }
131 ^{SPC}#{SPC}pragma{SPCP}optimize{SPC}\({SPC}on{SPC}\) {
132 BEGIN PP;
133 return PRAGMA_OPTIMIZE_ON;
134 }
135 ^{SPC}#{SPC}pragma{SPCP}optimize{SPC}\({SPC}off{SPC}\) {
136 BEGIN PP;
137 return PRAGMA_OPTIMIZE_OFF;
138 }
139 ^{SPC}#{SPC}pragma{SPCP} { BEGIN PRAGMA; }
140
141 <PRAGMA>\n { BEGIN 0; yylineno++; yycolumn = 0; }
142 <PRAGMA>. { }
143
144 <PP>\/\/[^\n]* { }
145 <PP>[ \t\r]* { }
146 <PP>: return COLON;
147 <PP>[_a-zA-Z][_a-zA-Z0-9]* {
148 yylval->identifier = strdup(yytext);
149 return IDENTIFIER;
150 }
151 <PP>[1-9][0-9]* {
152 yylval->n = strtol(yytext, NULL, 10);
153 return INTCONSTANT;
154 }
155 <PP>\n { BEGIN 0; yylineno++; yycolumn = 0; return EOL; }
156
157 \n { yylineno++; yycolumn = 0; }
158
159 attribute return ATTRIBUTE;
160 const return CONST_TOK;
161 bool return BOOL_TOK;
162 float return FLOAT_TOK;
163 int return INT_TOK;
164 uint TOKEN_OR_IDENTIFIER(130, UINT_TOK);
165
166 break return BREAK;
167 continue return CONTINUE;
168 do return DO;
169 while return WHILE;
170 else return ELSE;
171 for return FOR;
172 if return IF;
173 discard return DISCARD;
174 return return RETURN;
175
176 bvec2 return BVEC2;
177 bvec3 return BVEC3;
178 bvec4 return BVEC4;
179 ivec2 return IVEC2;
180 ivec3 return IVEC3;
181 ivec4 return IVEC4;
182 uvec2 TOKEN_OR_IDENTIFIER(130, UVEC2);
183 uvec3 TOKEN_OR_IDENTIFIER(130, UVEC3);
184 uvec4 TOKEN_OR_IDENTIFIER(130, UVEC4);
185 vec2 return VEC2;
186 vec3 return VEC3;
187 vec4 return VEC4;
188 mat2 return MAT2X2;
189 mat3 return MAT3X3;
190 mat4 return MAT4X4;
191 mat2x2 TOKEN_OR_IDENTIFIER(120, MAT2X2);
192 mat2x3 TOKEN_OR_IDENTIFIER(120, MAT2X3);
193 mat2x4 TOKEN_OR_IDENTIFIER(120, MAT2X4);
194 mat3x2 TOKEN_OR_IDENTIFIER(120, MAT3X2);
195 mat3x3 TOKEN_OR_IDENTIFIER(120, MAT3X3);
196 mat3x4 TOKEN_OR_IDENTIFIER(120, MAT3X4);
197 mat4x2 TOKEN_OR_IDENTIFIER(120, MAT4X2);
198 mat4x3 TOKEN_OR_IDENTIFIER(120, MAT4X3);
199 mat4x4 TOKEN_OR_IDENTIFIER(120, MAT4X4);
200
201 in return IN_TOK;
202 out return OUT_TOK;
203 inout return INOUT_TOK;
204 uniform return UNIFORM;
205 varying return VARYING;
206 centroid TOKEN_OR_IDENTIFIER(120, CENTROID);
207 invariant TOKEN_OR_IDENTIFIER_ES(120, INVARIANT);
208
209 flat TOKEN_OR_IDENTIFIER_ES(130, FLAT);
210 smooth TOKEN_OR_IDENTIFIER(130, SMOOTH);
211 noperspective TOKEN_OR_IDENTIFIER(130, NOPERSPECTIVE);
212
213 sampler1D return SAMPLER1D;
214 sampler2D return SAMPLER2D;
215 sampler3D return SAMPLER3D;
216 samplerCube return SAMPLERCUBE;
217 sampler1DShadow return SAMPLER1DSHADOW;
218 sampler2DShadow return SAMPLER2DSHADOW;
219
220 struct return STRUCT;
221 void return VOID_TOK;
222
223 layout {
224 if ((yyextra->language_version >= 140)
225 || yyextra->ARB_explicit_attrib_location_enable
226 || (yyextra->ARB_fragment_coord_conventions_enable)){
227 return LAYOUT_TOK;
228 } else {
229 yylval->identifier = strdup(yytext);
230 return IDENTIFIER;
231 }
232 }
233
234 \+\+ return INC_OP;
235 -- return DEC_OP;
236 \<= return LE_OP;
237 >= return GE_OP;
238 == return EQ_OP;
239 != return NE_OP;
240 && return AND_OP;
241 \|\| return OR_OP;
242 "^^" return XOR_OP;
243 "<<" return LEFT_OP;
244 ">>" return RIGHT_OP;
245
246 \*= return MUL_ASSIGN;
247 \/= return DIV_ASSIGN;
248 \+= return ADD_ASSIGN;
249 \%= return MOD_ASSIGN;
250 \<\<= return LEFT_ASSIGN;
251 >>= return RIGHT_ASSIGN;
252 &= return AND_ASSIGN;
253 "^=" return XOR_ASSIGN;
254 \|= return OR_ASSIGN;
255 -= return SUB_ASSIGN;
256
257 [1-9][0-9]* {
258 yylval->n = strtol(yytext, NULL, 10);
259 return INTCONSTANT;
260 }
261 0[xX][0-9a-fA-F]+ {
262 yylval->n = strtol(yytext + 2, NULL, 16);
263 return INTCONSTANT;
264 }
265 0[0-7]* {
266 yylval->n = strtol(yytext, NULL, 8);
267 return INTCONSTANT;
268 }
269
270 [0-9]+\.[0-9]+([eE][+-]?[0-9]+)?[fF]? {
271 yylval->real = strtod(yytext, NULL);
272 return FLOATCONSTANT;
273 }
274 \.[0-9]+([eE][+-]?[0-9]+)?[fF]? {
275 yylval->real = strtod(yytext, NULL);
276 return FLOATCONSTANT;
277 }
278 [0-9]+\.([eE][+-]?[0-9]+)?[fF]? {
279 yylval->real = strtod(yytext, NULL);
280 return FLOATCONSTANT;
281 }
282 [0-9]+[eE][+-]?[0-9]+[fF]? {
283 yylval->real = strtod(yytext, NULL);
284 return FLOATCONSTANT;
285 }
286 [0-9]+[fF] {
287 yylval->real = strtod(yytext, NULL);
288 return FLOATCONSTANT;
289 }
290
291 true {
292 yylval->n = 1;
293 return BOOLCONSTANT;
294 }
295 false {
296 yylval->n = 0;
297 return BOOLCONSTANT;
298 }
299
300
301 /* Reserved words in GLSL 1.10. */
302 asm RESERVED_WORD(999, ASM);
303 class RESERVED_WORD(999, CLASS);
304 union RESERVED_WORD(999, UNION);
305 enum RESERVED_WORD(999, ENUM);
306 typedef RESERVED_WORD(999, TYPEDEF);
307 template RESERVED_WORD(999, TEMPLATE);
308 this RESERVED_WORD(999, THIS);
309 packed RESERVED_WORD(999, PACKED_TOK);
310 goto RESERVED_WORD(999, GOTO);
311 switch RESERVED_WORD(130, SWITCH);
312 default RESERVED_WORD(130, DEFAULT);
313 inline RESERVED_WORD(999, INLINE_TOK);
314 noinline RESERVED_WORD(999, NOINLINE);
315 volatile RESERVED_WORD(999, VOLATILE);
316 public RESERVED_WORD(999, PUBLIC_TOK);
317 static RESERVED_WORD(999, STATIC);
318 extern RESERVED_WORD(999, EXTERN);
319 external RESERVED_WORD(999, EXTERNAL);
320 interface RESERVED_WORD(999, INTERFACE);
321 long RESERVED_WORD(999, LONG_TOK);
322 short RESERVED_WORD(999, SHORT_TOK);
323 double RESERVED_WORD(999, DOUBLE_TOK);
324 half RESERVED_WORD(999, HALF);
325 fixed RESERVED_WORD(999, FIXED_TOK);
326 unsigned RESERVED_WORD(999, UNSIGNED);
327 input RESERVED_WORD(999, INPUT_TOK);
328 output RESERVED_WORD(999, OUTPUT);
329 hvec2 RESERVED_WORD(999, HVEC2);
330 hvec3 RESERVED_WORD(999, HVEC3);
331 hvec4 RESERVED_WORD(999, HVEC4);
332 dvec2 RESERVED_WORD(999, DVEC2);
333 dvec3 RESERVED_WORD(999, DVEC3);
334 dvec4 RESERVED_WORD(999, DVEC4);
335 fvec2 RESERVED_WORD(999, FVEC2);
336 fvec3 RESERVED_WORD(999, FVEC3);
337 fvec4 RESERVED_WORD(999, FVEC4);
338 sampler2DRect return SAMPLER2DRECT;
339 sampler3DRect RESERVED_WORD(999, SAMPLER3DRECT);
340 sampler2DRectShadow return SAMPLER2DRECTSHADOW;
341 sizeof RESERVED_WORD(999, SIZEOF);
342 cast RESERVED_WORD(999, CAST);
343 namespace RESERVED_WORD(999, NAMESPACE);
344 using RESERVED_WORD(999, USING);
345
346 /* Additional reserved words in GLSL 1.20. */
347 lowp TOKEN_OR_IDENTIFIER_ES(120, LOWP);
348 mediump TOKEN_OR_IDENTIFIER_ES(120, MEDIUMP);
349 highp TOKEN_OR_IDENTIFIER_ES(120, HIGHP);
350 precision TOKEN_OR_IDENTIFIER_ES(120, PRECISION);
351
352 /* Additional reserved words in GLSL 1.30. */
353 common TOKEN_OR_IDENTIFIER(130, COMMON);
354 partition TOKEN_OR_IDENTIFIER(130, PARTITION);
355 active TOKEN_OR_IDENTIFIER(130, ACTIVE);
356 superp TOKEN_OR_IDENTIFIER_ES(130, SUPERP);
357 samplerBuffer TOKEN_OR_IDENTIFIER(130, SAMPLERBUFFER);
358 filter TOKEN_OR_IDENTIFIER(130, FILTER);
359 image1D TOKEN_OR_IDENTIFIER(130, IMAGE1D);
360 image2D TOKEN_OR_IDENTIFIER(130, IMAGE2D);
361 image3D TOKEN_OR_IDENTIFIER(130, IMAGE3D);
362 imageCube TOKEN_OR_IDENTIFIER(130, IMAGECUBE);
363 iimage1D TOKEN_OR_IDENTIFIER(130, IIMAGE1D);
364 iimage2D TOKEN_OR_IDENTIFIER(130, IIMAGE2D);
365 iimage3D TOKEN_OR_IDENTIFIER(130, IIMAGE3D);
366 iimageCube TOKEN_OR_IDENTIFIER(130, IIMAGECUBE);
367 uimage1D TOKEN_OR_IDENTIFIER(130, UIMAGE1D);
368 uimage2D TOKEN_OR_IDENTIFIER(130, UIMAGE2D);
369 uimage3D TOKEN_OR_IDENTIFIER(130, UIMAGE3D);
370 uimageCube TOKEN_OR_IDENTIFIER(130, UIMAGECUBE);
371 image1DArray TOKEN_OR_IDENTIFIER(130, IMAGE1DARRAY);
372 image2DArray TOKEN_OR_IDENTIFIER(130, IMAGE2DARRAY);
373 iimage1DArray TOKEN_OR_IDENTIFIER(130, IIMAGE1DARRAY);
374 iimage2DArray TOKEN_OR_IDENTIFIER(130, IIMAGE2DARRAY);
375 uimage1DArray TOKEN_OR_IDENTIFIER(130, UIMAGE1DARRAY);
376 uimage2DArray TOKEN_OR_IDENTIFIER(130, UIMAGE2DARRAY);
377 image1DShadow TOKEN_OR_IDENTIFIER(130, IMAGE1DSHADOW);
378 image2DShadow TOKEN_OR_IDENTIFIER(130, IMAGE2DSHADOW);
379 imageBuffer TOKEN_OR_IDENTIFIER(130, IMAGEBUFFER);
380 iimageBuffer TOKEN_OR_IDENTIFIER(130, IIMAGEBUFFER);
381 uimageBuffer TOKEN_OR_IDENTIFIER(130, UIMAGEBUFFER);
382 row_major TOKEN_OR_IDENTIFIER(130, ROW_MAJOR);
383
384 [_a-zA-Z][_a-zA-Z0-9]* {
385 struct _mesa_glsl_parse_state *state = yyextra;
386 void *ctx = state;
387 yylval->identifier = talloc_strdup(ctx, yytext);
388 return IDENTIFIER;
389 }
390
391 . { return yytext[0]; }
392
393 %%
394
395 void
396 _mesa_glsl_lexer_ctor(struct _mesa_glsl_parse_state *state, const char *string)
397 {
398 yylex_init_extra(state, & state->scanner);
399 yy_scan_string(string, state->scanner);
400 }
401
402 void
403 _mesa_glsl_lexer_dtor(struct _mesa_glsl_parse_state *state)
404 {
405 yylex_destroy(state->scanner);
406 }