glsl2: Make non-square matrix keywords not keywords pre-120.
[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
240 true {
241 yylval->n = 1;
242 return BOOLCONSTANT;
243 }
244 false {
245 yylval->n = 0;
246 return BOOLCONSTANT;
247 }
248
249
250 /* Reserved words in GLSL 1.10. */
251 asm return ASM;
252 class return CLASS;
253 union return UNION;
254 enum return ENUM;
255 typedef return TYPEDEF;
256 template return TEMPLATE;
257 this return THIS;
258 packed return PACKED;
259 goto return GOTO;
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;
270 long return LONG;
271 short return SHORT;
272 double return DOUBLE;
273 half return HALF;
274 fixed return FIXED;
275 unsigned return UNSIGNED;
276 input return INPUT;
277 output return OUTPUT;
278 hvec2 return HVEC2;
279 hvec3 return HVEC3;
280 hvec4 return HVEC4;
281 dvec2 return DVEC2;
282 dvec3 return DVEC3;
283 dvec4 return DVEC4;
284 fvec2 return FVEC2;
285 fvec3 return FVEC3;
286 fvec4 return FVEC4;
287 sampler2DRect return SAMPLER2DRECT;
288 sampler3DRect return SAMPLER3DRECT;
289 sampler2DRectShadow return SAMPLER2DRECTSHADOW;
290 sizeof return SIZEOF;
291 cast return CAST;
292 namespace return NAMESPACE;
293 using return USING;
294
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);
300
301 [_a-zA-Z][_a-zA-Z0-9]* {
302 struct _mesa_glsl_parse_state *state = yyextra;
303 void *ctx = state;
304 yylval->identifier = talloc_strdup(ctx, yytext);
305 return IDENTIFIER;
306 }
307
308 . { return yytext[0]; }
309
310 %%
311
312 void
313 _mesa_glsl_lexer_ctor(struct _mesa_glsl_parse_state *state, const char *string)
314 {
315 yylex_init_extra(state, & state->scanner);
316 yy_scan_string(string, state->scanner);
317 }
318
319 void
320 _mesa_glsl_lexer_dtor(struct _mesa_glsl_parse_state *state)
321 {
322 yylex_destroy(state->scanner);
323 }