Merge remote branch 'origin/master' into glsl2
[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 %}
40
41 %option bison-bridge bison-locations reentrant noyywrap
42 %option nounput noyy_top_state
43 %option never-interactive
44 %option prefix="_mesa_glsl_"
45 %option extra-type="struct _mesa_glsl_parse_state *"
46
47 %x PP
48
49 DEC_INT [1-9][0-9]*
50 HEX_INT 0[xX][0-9a-fA-F]+
51 OCT_INT 0[0-7]*
52 INT ({DEC_INT}|{HEX_INT}|{OCT_INT})
53 SPC [ \t]*
54 SPCP [ \t]+
55 HASH ^{SPC}#{SPC}
56 %%
57
58 [ \r\t]+ ;
59
60 /* Preprocessor tokens. */
61 ^[ \t]*#[ \t]*$ ;
62 ^[ \t]*#[ \t]*version { BEGIN PP; return VERSION; }
63 ^[ \t]*#[ \t]*extension { BEGIN PP; return EXTENSION; }
64 {HASH}line{SPCP}{INT}{SPCP}{INT}{SPC}$ {
65 /* Eat characters until the first digit is
66 * encountered
67 */
68 char *ptr = yytext;
69 while (!isdigit(*ptr))
70 ptr++;
71
72 /* Subtract one from the line number because
73 * yylineno is zero-based instead of
74 * one-based.
75 */
76 yylineno = strtol(ptr, &ptr, 0) - 1;
77 yylloc->source = strtol(ptr, NULL, 0);
78 }
79 {HASH}line{SPCP}{INT}{SPC}$ {
80 /* Eat characters until the first digit is
81 * encountered
82 */
83 char *ptr = yytext;
84 while (!isdigit(*ptr))
85 ptr++;
86
87 /* Subtract one from the line number because
88 * yylineno is zero-based instead of
89 * one-based.
90 */
91 yylineno = strtol(ptr, &ptr, 0) - 1;
92 }
93 ^[ \t]*#[ \t]*pragma { BEGIN PP; return PRAGMA; }
94 <PP>\/\/[^\n]* { }
95 <PP>[ \t\r]* { }
96 <PP>: return COLON;
97 <PP>[_a-zA-Z][_a-zA-Z0-9]* {
98 yylval->identifier = strdup(yytext);
99 return IDENTIFIER;
100 }
101 <PP>[1-9][0-9]* {
102 yylval->n = strtol(yytext, NULL, 10);
103 return INTCONSTANT;
104 }
105 <PP>\n { BEGIN 0; yylineno++; yycolumn = 0; return EOL; }
106
107 \n { yylineno++; yycolumn = 0; }
108
109 attribute return ATTRIBUTE;
110 const return CONST_TOK;
111 bool return BOOL;
112 float return FLOAT;
113 int return INT;
114
115 break return BREAK;
116 continue return CONTINUE;
117 do return DO;
118 while return WHILE;
119 else return ELSE;
120 for return FOR;
121 if return IF;
122 discard return DISCARD;
123 return return RETURN;
124
125 bvec2 return BVEC2;
126 bvec3 return BVEC3;
127 bvec4 return BVEC4;
128 ivec2 return IVEC2;
129 ivec3 return IVEC3;
130 ivec4 return IVEC4;
131 vec2 return VEC2;
132 vec3 return VEC3;
133 vec4 return VEC4;
134 mat2 return MAT2;
135 mat3 return MAT3;
136 mat4 return MAT4;
137 mat2x2 return MAT2X2;
138 mat2x3 return MAT2X3;
139 mat2x4 return MAT2X4;
140 mat3x2 return MAT3X2;
141 mat3x3 return MAT3X3;
142 mat3x4 return MAT3X4;
143 mat4x2 return MAT4X2;
144 mat4x3 return MAT4X3;
145 mat4x4 return MAT4X4;
146
147 in return IN;
148 out return OUT;
149 inout return INOUT;
150 uniform return UNIFORM;
151 varying return VARYING;
152 centroid {
153 if (yyextra->language_version >= 120) {
154 return CENTROID;
155 } else {
156 yylval->identifier = strdup(yytext);
157 return IDENTIFIER;
158 }
159 }
160 invariant {
161 if (yyextra->language_version >= 120) {
162 return INVARIANT;
163 } else {
164 yylval->identifier = strdup(yytext);
165 return IDENTIFIER;
166 }
167 }
168
169 flat {
170 if (yyextra->language_version >= 130) {
171 return FLAT;
172 } else {
173 yylval->identifier = strdup(yytext);
174 return IDENTIFIER;
175 }
176 }
177 smooth {
178 if (yyextra->language_version >= 130) {
179 return SMOOTH;
180 } else {
181 yylval->identifier = strdup(yytext);
182 return IDENTIFIER;
183 }
184 }
185 noperspective {
186 if (yyextra->language_version >= 130) {
187 return NOPERSPECTIVE;
188 } else {
189 yylval->identifier = strdup(yytext);
190 return IDENTIFIER;
191 }
192 }
193
194 sampler1D return SAMPLER1D;
195 sampler2D return SAMPLER2D;
196 sampler3D return SAMPLER3D;
197 samplerCube return SAMPLERCUBE;
198 sampler1DShadow return SAMPLER1DSHADOW;
199 sampler2DShadow return SAMPLER2DSHADOW;
200
201 struct return STRUCT;
202 void return VOID;
203
204 \+\+ return INC_OP;
205 -- return DEC_OP;
206 \<= return LE_OP;
207 >= return GE_OP;
208 == return EQ_OP;
209 != return NE_OP;
210 && return AND_OP;
211 \|\| return OR_OP;
212 "^^" return XOR_OP;
213
214 \*= return MUL_ASSIGN;
215 \/= return DIV_ASSIGN;
216 \+= return ADD_ASSIGN;
217 \%= return MOD_ASSIGN;
218 \<\<= return LEFT_ASSIGN;
219 >>= return RIGHT_ASSIGN;
220 &= return AND_ASSIGN;
221 ^= return XOR_ASSIGN;
222 \|= return OR_ASSIGN;
223 -= return SUB_ASSIGN;
224
225 [1-9][0-9]* {
226 yylval->n = strtol(yytext, NULL, 10);
227 return INTCONSTANT;
228 }
229 0[xX][0-9a-fA-F]+ {
230 yylval->n = strtol(yytext + 2, NULL, 16);
231 return INTCONSTANT;
232 }
233 0[0-7]* {
234 yylval->n = strtol(yytext, NULL, 8);
235 return INTCONSTANT;
236 }
237
238 [0-9]+\.[0-9]+([eE][+-]?[0-9]+)?[fF]? {
239 yylval->real = strtod(yytext, NULL);
240 return FLOATCONSTANT;
241 }
242 \.[0-9]+([eE][+-]?[0-9]+)?[fF]? {
243 yylval->real = strtod(yytext, NULL);
244 return FLOATCONSTANT;
245 }
246 [0-9]+\.([eE][+-]?[0-9]+)?[fF]? {
247 yylval->real = strtod(yytext, NULL);
248 return FLOATCONSTANT;
249 }
250 [0-9]+[eE][+-]?[0-9]+[fF]? {
251 yylval->real = strtod(yytext, NULL);
252 return FLOATCONSTANT;
253 }
254
255 true {
256 yylval->n = 1;
257 return BOOLCONSTANT;
258 }
259 false {
260 yylval->n = 0;
261 return BOOLCONSTANT;
262 }
263
264
265 /* Reserved words in GLSL 1.10. */
266 asm return ASM;
267 class return CLASS;
268 union return UNION;
269 enum return ENUM;
270 typedef return TYPEDEF;
271 template return TEMPLATE;
272 this return THIS;
273 packed return PACKED;
274 goto return GOTO;
275 switch return SWITCH;
276 default return DEFAULT;
277 inline return INLINE_TOK;
278 noinline return NOINLINE;
279 volatile return VOLATILE;
280 public return PUBLIC_TOK;
281 static return STATIC;
282 extern return EXTERN;
283 external return EXTERNAL;
284 interface return INTERFACE;
285 long return LONG;
286 short return SHORT;
287 double return DOUBLE;
288 half return HALF;
289 fixed return FIXED;
290 unsigned return UNSIGNED;
291 input return INPUT;
292 output return OUTPUT;
293 hvec2 return HVEC2;
294 hvec3 return HVEC3;
295 hvec4 return HVEC4;
296 dvec2 return DVEC2;
297 dvec3 return DVEC3;
298 dvec4 return DVEC4;
299 fvec2 return FVEC2;
300 fvec3 return FVEC3;
301 fvec4 return FVEC4;
302 sampler2DRect return SAMPLER2DRECT;
303 sampler3DRect return SAMPLER3DRECT;
304 sampler2DRectShadow return SAMPLER2DRECTSHADOW;
305 sizeof return SIZEOF;
306 cast return CAST;
307 namespace return NAMESPACE;
308 using return USING;
309
310 /* Additional reserved words in GLSL 1.20. */
311 lowp return LOWP;
312 mediump return MEDIUMP;
313 highp return HIGHP;
314 precision return PRECISION;
315
316 [_a-zA-Z][_a-zA-Z0-9]* {
317 struct _mesa_glsl_parse_state *state = yyextra;
318 void *ctx = state;
319 yylval->identifier = talloc_strdup(ctx, yytext);
320 return IDENTIFIER;
321 }
322
323 . { return yytext[0]; }
324
325 %%
326
327 void
328 _mesa_glsl_lexer_ctor(struct _mesa_glsl_parse_state *state, const char *string)
329 {
330 yylex_init_extra(state, & state->scanner);
331 yy_scan_string(string, state->scanner);
332 }
333
334 void
335 _mesa_glsl_lexer_dtor(struct _mesa_glsl_parse_state *state)
336 {
337 yylex_destroy(state->scanner);
338 }