grub2: bring back build of aros-side grub2 tools
[AROS.git] / workbench / libs / mesa / src / glsl / glsl_lexer.ll
blobcfd892617577f61b2e17f014cf8766c4f551227a
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 "strtod.h"
26 #include "ast.h"
27 #include "glsl_parser_extras.h"
28 #include "glsl_parser.h"
30 static int classify_identifier(struct _mesa_glsl_parse_state *, const char *);
32 #ifdef _MSC_VER
33 #define YY_NO_UNISTD_H
34 #endif
36 #define YY_USER_ACTION                                          \
37    do {                                                         \
38       yylloc->source = 0;                                       \
39       yylloc->first_column = yycolumn + 1;                      \
40       yylloc->first_line = yylineno + 1;                        \
41       yycolumn += yyleng;                                       \
42    } while(0);
44 #define YY_USER_INIT yylineno = 0; yycolumn = 0;
46 #define IS_UINT (yytext[yyleng - 1] == 'u' || yytext[yyleng - 1] == 'U')
48 /* A macro for handling reserved words and keywords across language versions.
49  *
50  * Certain words start out as identifiers, become reserved words in
51  * later language revisions, and finally become language keywords.
52  *
53  * For example, consider the following lexer rule:
54  * samplerBuffer       KEYWORD(130, 140, SAMPLERBUFFER)
55  *
56  * This means that "samplerBuffer" will be treated as:
57  * - a keyword (SAMPLERBUFFER token)         ...in GLSL >= 1.40
58  * - a reserved word - error                 ...in GLSL >= 1.30
59  * - an identifier                           ...in GLSL <  1.30
60  */
61 #define KEYWORD(reserved_version, allowed_version, token)               \
62    do {                                                                 \
63       if (yyextra->language_version >= allowed_version) {               \
64          return token;                                                  \
65       } else if (yyextra->language_version >= reserved_version) {       \
66          _mesa_glsl_error(yylloc, yyextra,                              \
67                           "Illegal use of reserved word `%s'", yytext); \
68          return ERROR_TOK;                                              \
69       } else {                                                          \
70          yylval->identifier = strdup(yytext);                           \
71          return classify_identifier(yyextra, yytext);                   \
72       }                                                                 \
73    } while (0)
75 /* The ES macro can be used in KEYWORD checks:
76  *
77  *    word      KEYWORD(110 || ES, 400, TOKEN)
78  * ...means the word is reserved in GLSL ES 1.00, while
79  *
80  *    word      KEYWORD(110, 130 || ES, TOKEN)
81  * ...means the word is a legal keyword in GLSL ES 1.00.
82  */
83 #define ES yyextra->es_shader
86 %option bison-bridge bison-locations reentrant noyywrap
87 %option nounput noyy_top_state
88 %option never-interactive
89 %option prefix="_mesa_glsl_"
90 %option extra-type="struct _mesa_glsl_parse_state *"
92 %x PP PRAGMA
94 DEC_INT         [1-9][0-9]*
95 HEX_INT         0[xX][0-9a-fA-F]+
96 OCT_INT         0[0-7]*
97 INT             ({DEC_INT}|{HEX_INT}|{OCT_INT})
98 SPC             [ \t]*
99 SPCP            [ \t]+
100 HASH            ^{SPC}#{SPC}
103 [ \r\t]+                ;
105     /* Preprocessor tokens. */ 
106 ^[ \t]*#[ \t]*$                 ;
107 ^[ \t]*#[ \t]*version           { BEGIN PP; return VERSION; }
108 ^[ \t]*#[ \t]*extension         { BEGIN PP; return EXTENSION; }
109 {HASH}line{SPCP}{INT}{SPCP}{INT}{SPC}$ {
110                                    /* Eat characters until the first digit is
111                                     * encountered
112                                     */
113                                    char *ptr = yytext;
114                                    while (!isdigit(*ptr))
115                                       ptr++;
117                                    /* Subtract one from the line number because
118                                     * yylineno is zero-based instead of
119                                     * one-based.
120                                     */
121                                    yylineno = strtol(ptr, &ptr, 0) - 1;
122                                    yylloc->source = strtol(ptr, NULL, 0);
123                                 }
124 {HASH}line{SPCP}{INT}{SPC}$     {
125                                    /* Eat characters until the first digit is
126                                     * encountered
127                                     */
128                                    char *ptr = yytext;
129                                    while (!isdigit(*ptr))
130                                       ptr++;
132                                    /* Subtract one from the line number because
133                                     * yylineno is zero-based instead of
134                                     * one-based.
135                                     */
136                                    yylineno = strtol(ptr, &ptr, 0) - 1;
137                                 }
138 ^{SPC}#{SPC}pragma{SPCP}debug{SPC}\({SPC}on{SPC}\) {
139                                   BEGIN PP;
140                                   return PRAGMA_DEBUG_ON;
141                                 }
142 ^{SPC}#{SPC}pragma{SPCP}debug{SPC}\({SPC}off{SPC}\) {
143                                   BEGIN PP;
144                                   return PRAGMA_DEBUG_OFF;
145                                 }
146 ^{SPC}#{SPC}pragma{SPCP}optimize{SPC}\({SPC}on{SPC}\) {
147                                   BEGIN PP;
148                                   return PRAGMA_OPTIMIZE_ON;
149                                 }
150 ^{SPC}#{SPC}pragma{SPCP}optimize{SPC}\({SPC}off{SPC}\) {
151                                   BEGIN PP;
152                                   return PRAGMA_OPTIMIZE_OFF;
153                                 }
154 ^{SPC}#{SPC}pragma{SPCP}STDGL{SPCP}invariant{SPC}\({SPC}all{SPC}\) {
155                                   BEGIN PP;
156                                   return PRAGMA_INVARIANT_ALL;
157                                 }
158 ^{SPC}#{SPC}pragma{SPCP}        { BEGIN PRAGMA; }
160 <PRAGMA>\n                      { BEGIN 0; yylineno++; yycolumn = 0; }
161 <PRAGMA>.                       { }
163 <PP>\/\/[^\n]*                  { }
164 <PP>[ \t\r]*                    { }
165 <PP>:                           return COLON;
166 <PP>[_a-zA-Z][_a-zA-Z0-9]*      {
167                                    yylval->identifier = strdup(yytext);
168                                    return IDENTIFIER;
169                                 }
170 <PP>[1-9][0-9]*                 {
171                                     yylval->n = strtol(yytext, NULL, 10);
172                                     return INTCONSTANT;
173                                 }
174 <PP>\n                          { BEGIN 0; yylineno++; yycolumn = 0; return EOL; }
176 \n              { yylineno++; yycolumn = 0; }
178 attribute       return ATTRIBUTE;
179 const           return CONST_TOK;
180 bool            return BOOL_TOK;
181 float           return FLOAT_TOK;
182 int             return INT_TOK;
183 uint            KEYWORD(130, 130, UINT_TOK);
185 break           return BREAK;
186 continue        return CONTINUE;
187 do              return DO;
188 while           return WHILE;
189 else            return ELSE;
190 for             return FOR;
191 if              return IF;
192 discard         return DISCARD;
193 return          return RETURN;
195 bvec2           return BVEC2;
196 bvec3           return BVEC3;
197 bvec4           return BVEC4;
198 ivec2           return IVEC2;
199 ivec3           return IVEC3;
200 ivec4           return IVEC4;
201 uvec2           KEYWORD(130, 130, UVEC2);
202 uvec3           KEYWORD(130, 130, UVEC3);
203 uvec4           KEYWORD(130, 130, UVEC4);
204 vec2            return VEC2;
205 vec3            return VEC3;
206 vec4            return VEC4;
207 mat2            return MAT2X2;
208 mat3            return MAT3X3;
209 mat4            return MAT4X4;
210 mat2x2          KEYWORD(120, 120, MAT2X2);
211 mat2x3          KEYWORD(120, 120, MAT2X3);
212 mat2x4          KEYWORD(120, 120, MAT2X4);
213 mat3x2          KEYWORD(120, 120, MAT3X2);
214 mat3x3          KEYWORD(120, 120, MAT3X3);
215 mat3x4          KEYWORD(120, 120, MAT3X4);
216 mat4x2          KEYWORD(120, 120, MAT4X2);
217 mat4x3          KEYWORD(120, 120, MAT4X3);
218 mat4x4          KEYWORD(120, 120, MAT4X4);
220 in              return IN_TOK;
221 out             return OUT_TOK;
222 inout           return INOUT_TOK;
223 uniform         return UNIFORM;
224 varying         return VARYING;
225 centroid        KEYWORD(120, 120, CENTROID);
226 invariant       KEYWORD(120 || ES, 120 || ES, INVARIANT);
227 flat            KEYWORD(130 || ES, 130, FLAT);
228 smooth          KEYWORD(130, 130, SMOOTH);
229 noperspective   KEYWORD(130, 130, NOPERSPECTIVE);
231 sampler1D       return SAMPLER1D;
232 sampler2D       return SAMPLER2D;
233 sampler3D       return SAMPLER3D;
234 samplerCube     return SAMPLERCUBE;
235 sampler1DArray  KEYWORD(130, 130, SAMPLER1DARRAY);
236 sampler2DArray  KEYWORD(130, 130, SAMPLER2DARRAY);
237 sampler1DShadow return SAMPLER1DSHADOW;
238 sampler2DShadow return SAMPLER2DSHADOW;
239 samplerCubeShadow       KEYWORD(130, 130, SAMPLERCUBESHADOW);
240 sampler1DArrayShadow    KEYWORD(130, 130, SAMPLER1DARRAYSHADOW);
241 sampler2DArrayShadow    KEYWORD(130, 130, SAMPLER2DARRAYSHADOW);
242 isampler1D              KEYWORD(130, 130, ISAMPLER1D);
243 isampler2D              KEYWORD(130, 130, ISAMPLER2D);
244 isampler3D              KEYWORD(130, 130, ISAMPLER3D);
245 isamplerCube            KEYWORD(130, 130, ISAMPLERCUBE);
246 isampler1DArray         KEYWORD(130, 130, ISAMPLER1DARRAY);
247 isampler2DArray         KEYWORD(130, 130, ISAMPLER2DARRAY);
248 usampler1D              KEYWORD(130, 130, USAMPLER1D);
249 usampler2D              KEYWORD(130, 130, USAMPLER2D);
250 usampler3D              KEYWORD(130, 130, USAMPLER3D);
251 usamplerCube            KEYWORD(130, 130, USAMPLERCUBE);
252 usampler1DArray         KEYWORD(130, 130, USAMPLER1DARRAY);
253 usampler2DArray         KEYWORD(130, 130, USAMPLER2DARRAY);
256 struct          return STRUCT;
257 void            return VOID_TOK;
259 layout          {
260                   if ((yyextra->language_version >= 140)
261                       || yyextra->AMD_conservative_depth_enable
262                       || yyextra->ARB_explicit_attrib_location_enable
263                       || yyextra->ARB_fragment_coord_conventions_enable) {
264                       return LAYOUT_TOK;
265                    } else {
266                       yylval->identifier = strdup(yytext);
267                       return IDENTIFIER;
268                    }
269                 }
271 \+\+            return INC_OP;
272 --              return DEC_OP;
273 \<=             return LE_OP;
274 >=              return GE_OP;
275 ==              return EQ_OP;
276 !=              return NE_OP;
277 &&              return AND_OP;
278 \|\|            return OR_OP;
279 "^^"            return XOR_OP;
280 "<<"            return LEFT_OP;
281 ">>"            return RIGHT_OP;
283 \*=             return MUL_ASSIGN;
284 \/=             return DIV_ASSIGN;
285 \+=             return ADD_ASSIGN;
286 \%=             return MOD_ASSIGN;
287 \<\<=           return LEFT_ASSIGN;
288 >>=             return RIGHT_ASSIGN;
289 &=              return AND_ASSIGN;
290 "^="            return XOR_ASSIGN;
291 \|=             return OR_ASSIGN;
292 -=              return SUB_ASSIGN;
294 [1-9][0-9]*[uU]?        {
295                             yylval->n = strtol(yytext, NULL, 10);
296                             return IS_UINT ? UINTCONSTANT : INTCONSTANT;
297                         }
298 0[xX][0-9a-fA-F]+[uU]?  {
299                             yylval->n = strtol(yytext + 2, NULL, 16);
300                             return IS_UINT ? UINTCONSTANT : INTCONSTANT;
301                         }
302 0[0-7]*[uU]?            {
303                             yylval->n = strtol(yytext, NULL, 8);
304                             return IS_UINT ? UINTCONSTANT : INTCONSTANT;
305                         }
307 [0-9]+\.[0-9]+([eE][+-]?[0-9]+)?[fF]?   {
308                             yylval->real = glsl_strtod(yytext, NULL);
309                             return FLOATCONSTANT;
310                         }
311 \.[0-9]+([eE][+-]?[0-9]+)?[fF]?         {
312                             yylval->real = glsl_strtod(yytext, NULL);
313                             return FLOATCONSTANT;
314                         }
315 [0-9]+\.([eE][+-]?[0-9]+)?[fF]?         {
316                             yylval->real = glsl_strtod(yytext, NULL);
317                             return FLOATCONSTANT;
318                         }
319 [0-9]+[eE][+-]?[0-9]+[fF]?              {
320                             yylval->real = glsl_strtod(yytext, NULL);
321                             return FLOATCONSTANT;
322                         }
323 [0-9]+[fF]              {
324                             yylval->real = glsl_strtod(yytext, NULL);
325                             return FLOATCONSTANT;
326                         }
328 true                    {
329                             yylval->n = 1;
330                             return BOOLCONSTANT;
331                         }
332 false                   {
333                             yylval->n = 0;
334                             return BOOLCONSTANT;
335                         }
338     /* Reserved words in GLSL 1.10. */
339 asm             KEYWORD(110 || ES, 999, ASM);
340 class           KEYWORD(110 || ES, 999, CLASS);
341 union           KEYWORD(110 || ES, 999, UNION);
342 enum            KEYWORD(110 || ES, 999, ENUM);
343 typedef         KEYWORD(110 || ES, 999, TYPEDEF);
344 template        KEYWORD(110 || ES, 999, TEMPLATE);
345 this            KEYWORD(110 || ES, 999, THIS);
346 packed          KEYWORD(110 || ES, 999, PACKED_TOK);
347 goto            KEYWORD(110 || ES, 999, GOTO);
348 switch          KEYWORD(110 || ES, 130, SWITCH);
349 default         KEYWORD(110 || ES, 130, DEFAULT);
350 inline          KEYWORD(110 || ES, 999, INLINE_TOK);
351 noinline        KEYWORD(110 || ES, 999, NOINLINE);
352 volatile        KEYWORD(110 || ES, 999, VOLATILE);
353 public          KEYWORD(110 || ES, 999, PUBLIC_TOK);
354 static          KEYWORD(110 || ES, 999, STATIC);
355 extern          KEYWORD(110 || ES, 999, EXTERN);
356 external        KEYWORD(110 || ES, 999, EXTERNAL);
357 interface       KEYWORD(110 || ES, 999, INTERFACE);
358 long            KEYWORD(110 || ES, 999, LONG_TOK);
359 short           KEYWORD(110 || ES, 999, SHORT_TOK);
360 double          KEYWORD(110 || ES, 400, DOUBLE_TOK);
361 half            KEYWORD(110 || ES, 999, HALF);
362 fixed           KEYWORD(110 || ES, 999, FIXED_TOK);
363 unsigned        KEYWORD(110 || ES, 999, UNSIGNED);
364 input           KEYWORD(110 || ES, 999, INPUT_TOK);
365 output          KEYWORD(110 || ES, 999, OUTPUT);
366 hvec2           KEYWORD(110 || ES, 999, HVEC2);
367 hvec3           KEYWORD(110 || ES, 999, HVEC3);
368 hvec4           KEYWORD(110 || ES, 999, HVEC4);
369 dvec2           KEYWORD(110 || ES, 400, DVEC2);
370 dvec3           KEYWORD(110 || ES, 400, DVEC3);
371 dvec4           KEYWORD(110 || ES, 400, DVEC4);
372 fvec2           KEYWORD(110 || ES, 999, FVEC2);
373 fvec3           KEYWORD(110 || ES, 999, FVEC3);
374 fvec4           KEYWORD(110 || ES, 999, FVEC4);
375 sampler2DRect           return SAMPLER2DRECT;
376 sampler3DRect           KEYWORD(110 || ES, 999, SAMPLER3DRECT);
377 sampler2DRectShadow     return SAMPLER2DRECTSHADOW;
378 sizeof          KEYWORD(110 || ES, 999, SIZEOF);
379 cast            KEYWORD(110 || ES, 999, CAST);
380 namespace       KEYWORD(110 || ES, 999, NAMESPACE);
381 using           KEYWORD(110 || ES, 999, USING);
383     /* Additional reserved words in GLSL 1.20. */
384 lowp            KEYWORD(120, 130 || ES, LOWP);
385 mediump         KEYWORD(120, 130 || ES, MEDIUMP);
386 highp           KEYWORD(120, 130 || ES, HIGHP);
387 precision       KEYWORD(120, 130 || ES, PRECISION);
389     /* Additional reserved words in GLSL 1.30. */
390 case            KEYWORD(130, 130, CASE);
391 common          KEYWORD(130, 999, COMMON);
392 partition       KEYWORD(130, 999, PARTITION);
393 active          KEYWORD(130, 999, ACTIVE);
394 superp          KEYWORD(130 || ES, 999, SUPERP);
395 samplerBuffer   KEYWORD(130, 140, SAMPLERBUFFER);
396 filter          KEYWORD(130, 999, FILTER);
397 image1D         KEYWORD(130, 999, IMAGE1D);
398 image2D         KEYWORD(130, 999, IMAGE2D);
399 image3D         KEYWORD(130, 999, IMAGE3D);
400 imageCube       KEYWORD(130, 999, IMAGECUBE);
401 iimage1D        KEYWORD(130, 999, IIMAGE1D);
402 iimage2D        KEYWORD(130, 999, IIMAGE2D);
403 iimage3D        KEYWORD(130, 999, IIMAGE3D);
404 iimageCube      KEYWORD(130, 999, IIMAGECUBE);
405 uimage1D        KEYWORD(130, 999, UIMAGE1D);
406 uimage2D        KEYWORD(130, 999, UIMAGE2D);
407 uimage3D        KEYWORD(130, 999, UIMAGE3D);
408 uimageCube      KEYWORD(130, 999, UIMAGECUBE);
409 image1DArray    KEYWORD(130, 999, IMAGE1DARRAY);
410 image2DArray    KEYWORD(130, 999, IMAGE2DARRAY);
411 iimage1DArray   KEYWORD(130, 999, IIMAGE1DARRAY);
412 iimage2DArray   KEYWORD(130, 999, IIMAGE2DARRAY);
413 uimage1DArray   KEYWORD(130, 999, UIMAGE1DARRAY);
414 uimage2DArray   KEYWORD(130, 999, UIMAGE2DARRAY);
415 image1DShadow   KEYWORD(130, 999, IMAGE1DSHADOW);
416 image2DShadow   KEYWORD(130, 999, IMAGE2DSHADOW);
417 image1DArrayShadow KEYWORD(130, 999, IMAGE1DARRAYSHADOW);
418 image2DArrayShadow KEYWORD(130, 999, IMAGE2DARRAYSHADOW);
419 imageBuffer     KEYWORD(130, 999, IMAGEBUFFER);
420 iimageBuffer    KEYWORD(130, 999, IIMAGEBUFFER);
421 uimageBuffer    KEYWORD(130, 999, UIMAGEBUFFER);
422 row_major       KEYWORD(130, 999, ROW_MAJOR);
424 [_a-zA-Z][_a-zA-Z0-9]*  {
425                             struct _mesa_glsl_parse_state *state = yyextra;
426                             void *ctx = state;  
427                             yylval->identifier = ralloc_strdup(ctx, yytext);
428                             return classify_identifier(state, yytext);
429                         }
431 .                       { return yytext[0]; }
436 classify_identifier(struct _mesa_glsl_parse_state *state, const char *name)
438    if (state->symbols->get_variable(name) || state->symbols->get_function(name))
439       return IDENTIFIER;
440    else if (state->symbols->get_type(name))
441       return TYPE_IDENTIFIER;
442    else
443       return NEW_IDENTIFIER;
446 void
447 _mesa_glsl_lexer_ctor(struct _mesa_glsl_parse_state *state, const char *string)
449    yylex_init_extra(state, & state->scanner);
450    yy_scan_string(string, state->scanner);
453 void
454 _mesa_glsl_lexer_dtor(struct _mesa_glsl_parse_state *state)
456    yylex_destroy(state->scanner);