Include stdio.h and stdlib.h everywhere, and don't cook our own #define NULL.
[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 {
160 if (yyextra->language_version >= 120) {
161 return CENTROID;
162 } else {
163 yylval->identifier = strdup(yytext);
164 return IDENTIFIER;
165 }
166 }
167 invariant {
168 if (yyextra->language_version >= 120) {
169 return INVARIANT;
170 } else {
171 yylval->identifier = strdup(yytext);
172 return IDENTIFIER;
173 }
174 }
175
176 flat {
177 if (yyextra->language_version >= 130) {
178 return FLAT;
179 } else {
180 yylval->identifier = strdup(yytext);
181 return IDENTIFIER;
182 }
183 }
184 smooth {
185 if (yyextra->language_version >= 130) {
186 return SMOOTH;
187 } else {
188 yylval->identifier = strdup(yytext);
189 return IDENTIFIER;
190 }
191 }
192 noperspective {
193 if (yyextra->language_version >= 130) {
194 return NOPERSPECTIVE;
195 } else {
196 yylval->identifier = strdup(yytext);
197 return IDENTIFIER;
198 }
199 }
200
201 sampler1D return SAMPLER1D;
202 sampler2D return SAMPLER2D;
203 sampler3D return SAMPLER3D;
204 samplerCube return SAMPLERCUBE;
205 sampler1DShadow return SAMPLER1DSHADOW;
206 sampler2DShadow return SAMPLER2DSHADOW;
207
208 struct return STRUCT;
209 void return VOID;
210
211 \+\+ return INC_OP;
212 -- return DEC_OP;
213 \<= return LE_OP;
214 >= return GE_OP;
215 == return EQ_OP;
216 != return NE_OP;
217 && return AND_OP;
218 \|\| return OR_OP;
219 "^^" return XOR_OP;
220
221 \*= return MUL_ASSIGN;
222 \/= return DIV_ASSIGN;
223 \+= return ADD_ASSIGN;
224 \%= return MOD_ASSIGN;
225 \<\<= return LEFT_ASSIGN;
226 >>= return RIGHT_ASSIGN;
227 &= return AND_ASSIGN;
228 ^= return XOR_ASSIGN;
229 \|= return OR_ASSIGN;
230 -= return SUB_ASSIGN;
231
232 [1-9][0-9]* {
233 yylval->n = strtol(yytext, NULL, 10);
234 return INTCONSTANT;
235 }
236 0[xX][0-9a-fA-F]+ {
237 yylval->n = strtol(yytext + 2, NULL, 16);
238 return INTCONSTANT;
239 }
240 0[0-7]* {
241 yylval->n = strtol(yytext + 2, NULL, 8);
242 return INTCONSTANT;
243 }
244
245 [0-9]+\.[0-9]+([eE][+-]?[0-9]+)?[fF]? {
246 yylval->real = strtod(yytext, NULL);
247 return FLOATCONSTANT;
248 }
249 \.[0-9]+([eE][+-]?[0-9]+)?[fF]? {
250 yylval->real = strtod(yytext, NULL);
251 return FLOATCONSTANT;
252 }
253 [0-9]+\.([eE][+-]?[0-9]+)?[fF]? {
254 yylval->real = strtod(yytext, NULL);
255 return FLOATCONSTANT;
256 }
257 [0-9]+[eE][+-]?[0-9]+[fF]? {
258 yylval->real = strtod(yytext, NULL);
259 return FLOATCONSTANT;
260 }
261
262 true {
263 yylval->n = 1;
264 return BOOLCONSTANT;
265 }
266 false {
267 yylval->n = 0;
268 return BOOLCONSTANT;
269 }
270
271
272 /* Reserved words in GLSL 1.10. */
273 asm return ASM;
274 class return CLASS;
275 union return UNION;
276 enum return ENUM;
277 typedef return TYPEDEF;
278 template return TEMPLATE;
279 this return THIS;
280 packed return PACKED;
281 goto return GOTO;
282 switch return SWITCH;
283 default return DEFAULT;
284 inline return INLINE;
285 noinline return NOINLINE;
286 volatile return VOLATILE;
287 public return PUBLIC;
288 static return STATIC;
289 extern return EXTERN;
290 external return EXTERNAL;
291 interface return INTERFACE;
292 long return LONG;
293 short return SHORT;
294 double return DOUBLE;
295 half return HALF;
296 fixed return FIXED;
297 unsigned return UNSIGNED;
298 input return INPUT;
299 output return OUTPUT;
300 hvec2 return HVEC2;
301 hvec3 return HVEC3;
302 hvec4 return HVEC4;
303 dvec2 return DVEC2;
304 dvec3 return DVEC3;
305 dvec4 return DVEC4;
306 fvec2 return FVEC2;
307 fvec3 return FVEC3;
308 fvec4 return FVEC4;
309 sampler2DRect return SAMPLER2DRECT;
310 sampler3DRect return SAMPLER3DRECT;
311 sampler2DRectShadow return SAMPLER2DRECTSHADOW;
312 sizeof return SIZEOF;
313 cast return CAST;
314 namespace return NAMESPACE;
315 using return USING;
316
317 /* Additional reserved words in GLSL 1.20. */
318 lowp return LOWP;
319 mediump return MEDIUMP;
320 highp return HIGHP;
321 precision return PRECISION;
322
323 [_a-zA-Z][_a-zA-Z0-9]* {
324 yylval->identifier = strdup(yytext);
325 return IDENTIFIER;
326 }
327
328 . { return yytext[0]; }
329
330 %%
331
332 void
333 _mesa_glsl_lexer_ctor(struct _mesa_glsl_parse_state *state, const char *string)
334 {
335 yylex_init_extra(state, & state->scanner);
336 yy_scan_string(string, state->scanner);
337 }
338
339 void
340 _mesa_glsl_lexer_dtor(struct _mesa_glsl_parse_state *state)
341 {
342 yylex_destroy(state->scanner);
343 }