glsl2: Add the 1.30 reserved keywords.
[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 %}
50
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 *"
56
57 %x PP
58
59 DEC_INT [1-9][0-9]*
60 HEX_INT 0[xX][0-9a-fA-F]+
61 OCT_INT 0[0-7]*
62 INT ({DEC_INT}|{HEX_INT}|{OCT_INT})
63 SPC [ \t]*
64 SPCP [ \t]+
65 HASH ^{SPC}#{SPC}
66 %%
67
68 [ \r\t]+ ;
69
70 /* Preprocessor tokens. */
71 ^[ \t]*#[ \t]*$ ;
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
76 * encountered
77 */
78 char *ptr = yytext;
79 while (!isdigit(*ptr))
80 ptr++;
81
82 /* Subtract one from the line number because
83 * yylineno is zero-based instead of
84 * one-based.
85 */
86 yylineno = strtol(ptr, &ptr, 0) - 1;
87 yylloc->source = strtol(ptr, NULL, 0);
88 }
89 {HASH}line{SPCP}{INT}{SPC}$ {
90 /* Eat characters until the first digit is
91 * encountered
92 */
93 char *ptr = yytext;
94 while (!isdigit(*ptr))
95 ptr++;
96
97 /* Subtract one from the line number because
98 * yylineno is zero-based instead of
99 * one-based.
100 */
101 yylineno = strtol(ptr, &ptr, 0) - 1;
102 }
103 ^[ \t]*#[ \t]*pragma { BEGIN PP; return PRAGMA; }
104 <PP>\/\/[^\n]* { }
105 <PP>[ \t\r]* { }
106 <PP>: return COLON;
107 <PP>[_a-zA-Z][_a-zA-Z0-9]* {
108 yylval->identifier = strdup(yytext);
109 return IDENTIFIER;
110 }
111 <PP>[1-9][0-9]* {
112 yylval->n = strtol(yytext, NULL, 10);
113 return INTCONSTANT;
114 }
115 <PP>\n { BEGIN 0; yylineno++; yycolumn = 0; return EOL; }
116
117 \n { yylineno++; yycolumn = 0; }
118
119 attribute return ATTRIBUTE;
120 const return CONST_TOK;
121 bool return BOOL;
122 float return FLOAT;
123 int return INT;
124
125 break return BREAK;
126 continue return CONTINUE;
127 do return DO;
128 while return WHILE;
129 else return ELSE;
130 for return FOR;
131 if return IF;
132 discard return DISCARD;
133 return return RETURN;
134
135 bvec2 return BVEC2;
136 bvec3 return BVEC3;
137 bvec4 return BVEC4;
138 ivec2 return IVEC2;
139 ivec3 return IVEC3;
140 ivec4 return IVEC4;
141 vec2 return VEC2;
142 vec3 return VEC3;
143 vec4 return VEC4;
144 mat2 return MAT2;
145 mat3 return MAT3;
146 mat4 return MAT4;
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);
156
157 in return IN;
158 out return OUT;
159 inout return INOUT;
160 uniform return UNIFORM;
161 varying return VARYING;
162 centroid TOKEN_OR_IDENTIFIER(120, CENTROID);
163 invariant TOKEN_OR_IDENTIFIER(120, INVARIANT);
164
165 flat TOKEN_OR_IDENTIFIER(130, FLAT);
166 smooth TOKEN_OR_IDENTIFIER(130, SMOOTH);
167 noperspective TOKEN_OR_IDENTIFIER(130, NOPERSPECTIVE);
168
169 sampler1D return SAMPLER1D;
170 sampler2D return SAMPLER2D;
171 sampler3D return SAMPLER3D;
172 samplerCube return SAMPLERCUBE;
173 sampler1DShadow return SAMPLER1DSHADOW;
174 sampler2DShadow return SAMPLER2DSHADOW;
175
176 struct return STRUCT;
177 void return VOID;
178
179 layout {
180 if ((yyextra->language_version >= 140)
181 || (yyextra->ARB_fragment_coord_conventions_enable)){
182 return LAYOUT_TOK;
183 } else {
184 yylval->identifier = strdup(yytext);
185 return IDENTIFIER;
186 }
187 }
188
189 \+\+ return INC_OP;
190 -- return DEC_OP;
191 \<= return LE_OP;
192 >= return GE_OP;
193 == return EQ_OP;
194 != return NE_OP;
195 && return AND_OP;
196 \|\| return OR_OP;
197 "^^" return XOR_OP;
198
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;
209
210 [1-9][0-9]* {
211 yylval->n = strtol(yytext, NULL, 10);
212 return INTCONSTANT;
213 }
214 0[xX][0-9a-fA-F]+ {
215 yylval->n = strtol(yytext + 2, NULL, 16);
216 return INTCONSTANT;
217 }
218 0[0-7]* {
219 yylval->n = strtol(yytext, NULL, 8);
220 return INTCONSTANT;
221 }
222
223 [0-9]+\.[0-9]+([eE][+-]?[0-9]+)?[fF]? {
224 yylval->real = strtod(yytext, NULL);
225 return FLOATCONSTANT;
226 }
227 \.[0-9]+([eE][+-]?[0-9]+)?[fF]? {
228 yylval->real = strtod(yytext, NULL);
229 return FLOATCONSTANT;
230 }
231 [0-9]+\.([eE][+-]?[0-9]+)?[fF]? {
232 yylval->real = strtod(yytext, NULL);
233 return FLOATCONSTANT;
234 }
235 [0-9]+[eE][+-]?[0-9]+[fF]? {
236 yylval->real = strtod(yytext, NULL);
237 return FLOATCONSTANT;
238 }
239 [0-9]+[fF] {
240 yylval->real = strtod(yytext, NULL);
241 return FLOATCONSTANT;
242 }
243
244 true {
245 yylval->n = 1;
246 return BOOLCONSTANT;
247 }
248 false {
249 yylval->n = 0;
250 return BOOLCONSTANT;
251 }
252
253
254 /* Reserved words in GLSL 1.10. */
255 asm return ASM;
256 class return CLASS;
257 union return UNION;
258 enum return ENUM;
259 typedef return TYPEDEF;
260 template return TEMPLATE;
261 this return THIS;
262 packed return PACKED;
263 goto return GOTO;
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;
274 long return LONG;
275 short return SHORT;
276 double return DOUBLE;
277 half return HALF;
278 fixed return FIXED;
279 unsigned return UNSIGNED;
280 input return INPUT;
281 output return OUTPUT;
282 hvec2 return HVEC2;
283 hvec3 return HVEC3;
284 hvec4 return HVEC4;
285 dvec2 return DVEC2;
286 dvec3 return DVEC3;
287 dvec4 return DVEC4;
288 fvec2 return FVEC2;
289 fvec3 return FVEC3;
290 fvec4 return FVEC4;
291 sampler2DRect return SAMPLER2DRECT;
292 sampler3DRect return SAMPLER3DRECT;
293 sampler2DRectShadow return SAMPLER2DRECTSHADOW;
294 sizeof return SIZEOF;
295 cast return CAST;
296 namespace return NAMESPACE;
297 using return USING;
298
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);
304
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);
336
337 [_a-zA-Z][_a-zA-Z0-9]* {
338 struct _mesa_glsl_parse_state *state = yyextra;
339 void *ctx = state;
340 yylval->identifier = talloc_strdup(ctx, yytext);
341 return IDENTIFIER;
342 }
343
344 . { return yytext[0]; }
345
346 %%
347
348 void
349 _mesa_glsl_lexer_ctor(struct _mesa_glsl_parse_state *state, const char *string)
350 {
351 yylex_init_extra(state, & state->scanner);
352 yy_scan_string(string, state->scanner);
353 }
354
355 void
356 _mesa_glsl_lexer_dtor(struct _mesa_glsl_parse_state *state)
357 {
358 yylex_destroy(state->scanner);
359 }