summaryrefslogtreecommitdiff
path: root/src/glsl/glsl_lexer.lpp
blob: 7ef537b2487678f6c226f9b0d82ea220e7dd4d41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
%{
/*
 * Copyright © 2008, 2009 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */
#include <ctype.h>
#include "ast.h"
#include "glsl_parser_extras.h"
#include "glsl_parser.h"

#define YY_USER_ACTION						\
   do {								\
      yylloc->source = 0;					\
      yylloc->first_column = yycolumn + 1;			\
      yylloc->first_line = yylineno + 1;			\
      yycolumn += yyleng;					\
   } while(0);

#define YY_USER_INIT yylineno = 0; yycolumn = 0;

#define TOKEN_OR_IDENTIFIER(version, token)			 \
   do {								 \
      if (yyextra->language_version >= version) {		 \
	 return token;						 \
      } else {							 \
	 yylval->identifier = strdup(yytext);			 \
	 return IDENTIFIER;					 \
      }								 \
   } while (0)

#define RESERVED_WORD(version, token)					\
   do {									\
      if (yyextra->language_version >= version) {			\
	 return token;							\
      } else {								\
	 _mesa_glsl_error(yylloc, yyextra,				\
			  "Illegal use of reserved word `%s'", yytext);	\
	 return ERROR_TOK;						\
      }									\
   } while (0)
%}

%option bison-bridge bison-locations reentrant noyywrap
%option nounput noyy_top_state
%option never-interactive
%option prefix="_mesa_glsl_"
%option extra-type="struct _mesa_glsl_parse_state *"

%x PP

DEC_INT		[1-9][0-9]*
HEX_INT		0[xX][0-9a-fA-F]+
OCT_INT		0[0-7]*
INT		({DEC_INT}|{HEX_INT}|{OCT_INT})
SPC		[ \t]*
SPCP		[ \t]+
HASH		^{SPC}#{SPC}
%%

[ \r\t]+		;

    /* Preprocessor tokens. */ 
^[ \t]*#[ \t]*$			;
^[ \t]*#[ \t]*version		{ BEGIN PP; return VERSION; }
^[ \t]*#[ \t]*extension		{ BEGIN PP; return EXTENSION; }
{HASH}line{SPCP}{INT}{SPCP}{INT}{SPC}$ {
				   /* Eat characters until the first digit is
				    * encountered
				    */
				   char *ptr = yytext;
				   while (!isdigit(*ptr))
				      ptr++;

				   /* Subtract one from the line number because
				    * yylineno is zero-based instead of
				    * one-based.
				    */
				   yylineno = strtol(ptr, &ptr, 0) - 1;
				   yylloc->source = strtol(ptr, NULL, 0);
				}
{HASH}line{SPCP}{INT}{SPC}$	{
				   /* Eat characters until the first digit is
				    * encountered
				    */
				   char *ptr = yytext;
				   while (!isdigit(*ptr))
				      ptr++;

				   /* Subtract one from the line number because
				    * yylineno is zero-based instead of
				    * one-based.
				    */
				   yylineno = strtol(ptr, &ptr, 0) - 1;
				}
^[ \t]*#[ \t]*pragma		{ BEGIN PP; return PRAGMA; }
<PP>\/\/[^\n]*			{ }
<PP>[ \t\r]*			{ }
<PP>:				return COLON;
<PP>[_a-zA-Z][_a-zA-Z0-9]*	{
				   yylval->identifier = strdup(yytext);
				   return IDENTIFIER;
				}
<PP>[1-9][0-9]*			{
				    yylval->n = strtol(yytext, NULL, 10);
				    return INTCONSTANT;
				}
<PP>\n				{ BEGIN 0; yylineno++; yycolumn = 0; return EOL; }

\n		{ yylineno++; yycolumn = 0; }

attribute	return ATTRIBUTE;
const		return CONST_TOK;
bool		return BOOL;
float		return FLOAT;
int		return INT;

break		return BREAK;
continue	return CONTINUE;
do		return DO;
while		return WHILE;
else		return ELSE;
for		return FOR;
if		return IF;
discard		return DISCARD;
return		return RETURN;

bvec2		return BVEC2;
bvec3		return BVEC3;
bvec4		return BVEC4;
ivec2		return IVEC2;
ivec3		return IVEC3;
ivec4		return IVEC4;
vec2		return VEC2;
vec3		return VEC3;
vec4		return VEC4;
mat2		return MAT2;
mat3		return MAT3;
mat4		return MAT4;
mat2x2		TOKEN_OR_IDENTIFIER(120, MAT2X2);
mat2x3		TOKEN_OR_IDENTIFIER(120, MAT2X3);
mat2x4		TOKEN_OR_IDENTIFIER(120, MAT2X4);
mat3x2		TOKEN_OR_IDENTIFIER(120, MAT3X2);
mat3x3		TOKEN_OR_IDENTIFIER(120, MAT3X3);
mat3x4		TOKEN_OR_IDENTIFIER(120, MAT3X4);
mat4x2		TOKEN_OR_IDENTIFIER(120, MAT4X2);
mat4x3		TOKEN_OR_IDENTIFIER(120, MAT4X3);
mat4x4		TOKEN_OR_IDENTIFIER(120, MAT4X4);

in		return IN;
out		return OUT;
inout		return INOUT;
uniform		return UNIFORM;
varying		return VARYING;
centroid	TOKEN_OR_IDENTIFIER(120, CENTROID);
invariant	TOKEN_OR_IDENTIFIER(120, INVARIANT);

flat		TOKEN_OR_IDENTIFIER(130, FLAT);
smooth		TOKEN_OR_IDENTIFIER(130, SMOOTH);
noperspective	TOKEN_OR_IDENTIFIER(130, NOPERSPECTIVE);

sampler1D	return SAMPLER1D;
sampler2D	return SAMPLER2D;
sampler3D	return SAMPLER3D;
samplerCube	return SAMPLERCUBE;
sampler1DShadow	return SAMPLER1DSHADOW;
sampler2DShadow	return SAMPLER2DSHADOW;

struct		return STRUCT;
void		return VOID;

layout		{
		  if ((yyextra->language_version >= 140)
		      || (yyextra->ARB_fragment_coord_conventions_enable)){
		      return LAYOUT_TOK;
		   } else {
		      yylval->identifier = strdup(yytext);
		      return IDENTIFIER;
		   }
		}

\+\+		return INC_OP;
--		return DEC_OP;
\<=		return LE_OP;
>=		return GE_OP;
==		return EQ_OP;
!=		return NE_OP;
&&		return AND_OP;
\|\|		return OR_OP;
"^^"		return XOR_OP;

\*=		return MUL_ASSIGN;
\/=		return DIV_ASSIGN;
\+=		return ADD_ASSIGN;
\%=		return MOD_ASSIGN;
\<\<=		return LEFT_ASSIGN;
>>=		return RIGHT_ASSIGN;
&=		return AND_ASSIGN;
^=		return XOR_ASSIGN;
\|=		return OR_ASSIGN;
-=		return SUB_ASSIGN;

[1-9][0-9]*		{
			    yylval->n = strtol(yytext, NULL, 10);
			    return INTCONSTANT;
			}
0[xX][0-9a-fA-F]+	{
			    yylval->n = strtol(yytext + 2, NULL, 16);
			    return INTCONSTANT;
			}
0[0-7]*			{
			    yylval->n = strtol(yytext, NULL, 8);
			    return INTCONSTANT;
			}

[0-9]+\.[0-9]+([eE][+-]?[0-9]+)?[fF]?	{
			    yylval->real = strtod(yytext, NULL);
			    return FLOATCONSTANT;
			}
\.[0-9]+([eE][+-]?[0-9]+)?[fF]?		{
			    yylval->real = strtod(yytext, NULL);
			    return FLOATCONSTANT;
			}
[0-9]+\.([eE][+-]?[0-9]+)?[fF]?		{
			    yylval->real = strtod(yytext, NULL);
			    return FLOATCONSTANT;
			}
[0-9]+[eE][+-]?[0-9]+[fF]?		{
			    yylval->real = strtod(yytext, NULL);
			    return FLOATCONSTANT;
			}
[0-9]+[fF]		{
			    yylval->real = strtod(yytext, NULL);
			    return FLOATCONSTANT;
			}

true			{
			    yylval->n = 1;
			    return BOOLCONSTANT;
			}
false			{
			    yylval->n = 0;
			    return BOOLCONSTANT;
			}


    /* Reserved words in GLSL 1.10. */
asm		RESERVED_WORD(999, ASM);
class		RESERVED_WORD(999, CLASS);
union		RESERVED_WORD(999, UNION);
enum		RESERVED_WORD(999, ENUM);
typedef		RESERVED_WORD(999, TYPEDEF);
template	RESERVED_WORD(999, TEMPLATE);
this		RESERVED_WORD(999, THIS);
packed		RESERVED_WORD(999, PACKED);
goto		RESERVED_WORD(999, GOTO);
switch		RESERVED_WORD(130, SWITCH);
default		RESERVED_WORD(130, DEFAULT);
inline		RESERVED_WORD(999, INLINE_TOK);
noinline	RESERVED_WORD(999, NOINLINE);
volatile	RESERVED_WORD(999, VOLATILE);
public		RESERVED_WORD(999, PUBLIC_TOK);
static		RESERVED_WORD(999, STATIC);
extern		RESERVED_WORD(999, EXTERN);
external	RESERVED_WORD(999, EXTERNAL);
interface	RESERVED_WORD(999, INTERFACE);
long		RESERVED_WORD(999, LONG);
short		RESERVED_WORD(999, SHORT);
double		RESERVED_WORD(999, DOUBLE);
half		RESERVED_WORD(999, HALF);
fixed		RESERVED_WORD(999, FIXED);
unsigned	RESERVED_WORD(999, UNSIGNED);
input		RESERVED_WORD(999, INPUT);
output		RESERVED_WORD(999, OUTPUT);
hvec2		RESERVED_WORD(999, HVEC2);
hvec3		RESERVED_WORD(999, HVEC3);
hvec4		RESERVED_WORD(999, HVEC4);
dvec2		RESERVED_WORD(999, DVEC2);
dvec3		RESERVED_WORD(999, DVEC3);
dvec4		RESERVED_WORD(999, DVEC4);
fvec2		RESERVED_WORD(999, FVEC2);
fvec3		RESERVED_WORD(999, FVEC3);
fvec4		RESERVED_WORD(999, FVEC4);
sampler2DRect		return SAMPLER2DRECT;
sampler3DRect		RESERVED_WORD(999, SAMPLER3DRECT);
sampler2DRectShadow	return SAMPLER2DRECTSHADOW;
sizeof		RESERVED_WORD(999, SIZEOF);
cast		RESERVED_WORD(999, CAST);
namespace	RESERVED_WORD(999, NAMESPACE);
using		RESERVED_WORD(999, USING);

    /* Additional reserved words in GLSL 1.20. */
lowp		TOKEN_OR_IDENTIFIER(120, LOWP);
mediump		TOKEN_OR_IDENTIFIER(120, MEDIUMP);
highp		TOKEN_OR_IDENTIFIER(120, HIGHP);
precision	TOKEN_OR_IDENTIFIER(120, PRECISION);

    /* Additional reserved words in GLSL 1.30. */
common		TOKEN_OR_IDENTIFIER(130, COMMON);
partition	TOKEN_OR_IDENTIFIER(130, PARTITION);
active		TOKEN_OR_IDENTIFIER(130, ACTIVE);
superp		TOKEN_OR_IDENTIFIER(130, SUPERP);
samplerBuffer	TOKEN_OR_IDENTIFIER(130, SAMPLERBUFFER);
filter		TOKEN_OR_IDENTIFIER(130, FILTER);
image1D		TOKEN_OR_IDENTIFIER(130, IMAGE1D);
image2D		TOKEN_OR_IDENTIFIER(130, IMAGE2D);
image3D		TOKEN_OR_IDENTIFIER(130, IMAGE3D);
imageCube	TOKEN_OR_IDENTIFIER(130, IMAGECUBE);
iimage1D	TOKEN_OR_IDENTIFIER(130, IIMAGE1D);
iimage2D	TOKEN_OR_IDENTIFIER(130, IIMAGE2D);
iimage3D	TOKEN_OR_IDENTIFIER(130, IIMAGE3D);
iimageCube	TOKEN_OR_IDENTIFIER(130, IIMAGECUBE);
uimage1D	TOKEN_OR_IDENTIFIER(130, UIMAGE1D);
uimage2D	TOKEN_OR_IDENTIFIER(130, UIMAGE2D);
uimage3D	TOKEN_OR_IDENTIFIER(130, UIMAGE3D);
uimageCube	TOKEN_OR_IDENTIFIER(130, UIMAGECUBE);
image1DArray	TOKEN_OR_IDENTIFIER(130, IMAGE1DARRAY);
image2DArray	TOKEN_OR_IDENTIFIER(130, IMAGE2DARRAY);
iimage1DArray	TOKEN_OR_IDENTIFIER(130, IIMAGE1DARRAY);
iimage2DArray	TOKEN_OR_IDENTIFIER(130, IIMAGE2DARRAY);
uimage1DArray	TOKEN_OR_IDENTIFIER(130, UIMAGE1DARRAY);
uimage2DArray	TOKEN_OR_IDENTIFIER(130, UIMAGE2DARRAY);
image1DShadow	TOKEN_OR_IDENTIFIER(130, IMAGE1DSHADOW);
image2DShadow	TOKEN_OR_IDENTIFIER(130, IMAGE2DSHADOW);
imageBuffer	TOKEN_OR_IDENTIFIER(130, IMAGEBUFFER);
iimageBuffer	TOKEN_OR_IDENTIFIER(130, IIMAGEBUFFER);
uimageBuffer	TOKEN_OR_IDENTIFIER(130, UIMAGEBUFFER);
row_major	TOKEN_OR_IDENTIFIER(130, ROW_MAJOR);

[_a-zA-Z][_a-zA-Z0-9]*	{
			    struct _mesa_glsl_parse_state *state = yyextra;
			    void *ctx = state;	
			    yylval->identifier = talloc_strdup(ctx, yytext);
			    return IDENTIFIER;
			}

.			{ return yytext[0]; }

%%

void
_mesa_glsl_lexer_ctor(struct _mesa_glsl_parse_state *state, const char *string)
{
   yylex_init_extra(state, & state->scanner);
   yy_scan_string(string, state->scanner);
}

void
_mesa_glsl_lexer_dtor(struct _mesa_glsl_parse_state *state)
{
   yylex_destroy(state->scanner);
}