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