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