• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 //
3 // Copyright 2002 The ANGLE Project Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE file.
6 //
7 
8 This file contains the Lex specification for GLSL ES.
9 Based on ANSI C grammar, Lex specification:
10 http://www.lysator.liu.se/c/ANSI-C-grammar-l.html
11 
12 IF YOU MODIFY THIS FILE YOU ALSO NEED TO RUN scripts/run_code_generation.py
13 WHICH GENERATES THE GLSL ES LEXER (glslang_lex_autogen.cpp).
14 */
15 
16 %top{
17 // GENERATED FILE - DO NOT EDIT.
18 // Generated by generate_parser.py from glslang.l
19 //
20 // Copyright 2019 The ANGLE Project Authors. All rights reserved.
21 // Use of this source code is governed by a BSD-style license that can be
22 // found in the LICENSE file.
23 //
24 // glslang.l:
25 //   Lexer for the OpenGL shading language.
26 
27 // Ignore errors in auto-generated code.
28 #if defined(__GNUC__)
29 #pragma GCC diagnostic ignored "-Wswitch-enum"
30 #pragma GCC diagnostic ignored "-Wunused-function"
31 #pragma GCC diagnostic ignored "-Wunused-variable"
32 #elif defined(_MSC_VER)
33 #pragma warning(disable: 4005)
34 #pragma warning(disable: 4065)
35 #pragma warning(disable: 4189)
36 #pragma warning(disable: 4244)
37 #pragma warning(disable: 4505)
38 #pragma warning(disable: 4701)
39 #pragma warning(disable: 4702)
40 #endif
41 #if defined(__clang__)
42 #pragma clang diagnostic ignored "-Wimplicit-fallthrough"
43 #if defined(__APPLE__)
44 // Older clang versions don't have -Wextra-semi-stmt, and detecting Apple clang versions is
45 // difficult because they use different yet overlapping version numbers vs. regular clang.
46 #pragma clang diagnostic ignored "-Wunknown-warning-option"
47 #endif
48 // Flex isn't semi-colon clean.
49 #pragma clang diagnostic ignored "-Wextra-semi-stmt"
50 #pragma clang diagnostic ignored "-Wunreachable-code"
51 #endif
52 }
53 
54 %{
55 #include "compiler/translator/glslang.h"
56 #include "compiler/translator/ParseContext.h"
57 #include "compiler/preprocessor/Token.h"
58 #include "compiler/translator/util.h"
59 #include "compiler/translator/length_limits.h"
60 
61 using namespace sh;
62 
63 #include "glslang_tab_autogen.h"
64 
65 /* windows only pragma */
66 #ifdef _MSC_VER
67 #pragma warning(disable : 4102)
68 #endif
69 
70 // Workaround for flex using the register keyword, deprecated in C++11.
71 #ifdef __cplusplus
72 #if __cplusplus > 199711L
73 #define register
74 #endif
75 #endif
76 
77 #define YY_NO_INPUT
78 #define YY_USER_ACTION                                 \
79     yylloc->first_file = yylloc->last_file = yycolumn; \
80     yylloc->first_line = yylloc->last_line = yylineno;
81 
82 #define YY_INPUT(buf, result, max_size) \
83     result = string_input(buf, max_size, yyscanner);
84 
85 static yy_size_t string_input(char* buf, yy_size_t max_size, yyscan_t yyscanner);
86 static int check_type(yyscan_t yyscanner);
87 static int reserved_word(yyscan_t yyscanner);
88 // Tests if an extension is enabled.  If the extension is promoted to core, this function returns true.
89 static bool is_extension_enabled_or_is_core(TParseContext *context,
90         int extension_version, TExtension extension, int promotion_version);
91 // Helpers to determine if a symbol is reserved, keyword in extension or core, or identifier.
92 // Formatted as:
93 //
94 //    [V1_reserved_][V2_extension_][V3_keyword]
95 //
96 // which means in version V1, the symbol is reserved, and remains reserved until V3.  From versions
97 // V2 until V3, it's a keyword if the extension is enabled.  From version V3 on, it's a keyword in
98 // the spec itself.  Prior to V1, the symbol can be used as identifier.
99 static int ES2_extensions_ES3_keyword(TParseContext *context, TExtension extension1, TExtension extension2, TExtension extension3, int token);
100 static int ES2_reserved_ES3_keyword(TParseContext *context, int token);
101 static int ES2_keyword_ES3_reserved(TParseContext *context, int token);
102 static int ES3_keyword(TParseContext *context, int token);
103 static int ES3_reserved_ES3_1_keyword(TParseContext *context, int token);
104 static int ES2_reserved_ES3_1_keyword(TParseContext *context, int token);
105 static int ES3_1_keyword(TParseContext *context, int token);
106 static int ES2_reserved_ES2_extension_ES3_keyword(TParseContext *context, TExtension extension, int token);
107 static int ES3_extension(TParseContext *context, TExtension extension, int token);
108 static int ES3_reserved_ES3_1_extension_ES3_2_keyword(TParseContext *context, TExtension extension, int token);
109 static int ES3_reserved_ES3_extension(TParseContext *context, TExtension extension, int token);
110 static int ES3_reserved_ES3_extension_ES3_1_keyword(TParseContext *context, TExtension extension, int token);
111 static int ES3_reserved_ES3_extension_ES3_2_keyword(TParseContext *context, TExtension extension, int token);
112 static int ES3_1_reserved_ES3_1_extension_ES3_2_keyword(TParseContext *context, TExtension extension, int token);
113 static int ES3_1_reserved_ES3_1_extension_ES3_2_keyword_2(TParseContext *context, TExtension extension1, TExtension extension2, int token1, int token2);
114 static int ES3_and_3_1_reserved_ES3_1_extension_ES3_2_keyword_2(TParseContext *context, TExtension extension1, TExtension extension2, int token1, int token2);
115 static int WEBGL_video_texture_extension(TParseContext *context, int token);
116 static int uint_constant(TParseContext *context);
117 static int int_constant(TParseContext *context);
118 static int float_constant(yyscan_t yyscanner);
119 static int floatsuffix_check(TParseContext* context);
120 static int yuvcscstandardext_constant(TParseContext *context);
121 %}
122 
123 %option noyywrap nounput never-interactive
124 %option yylineno reentrant bison-bridge bison-locations
125 %option extra-type="TParseContext*"
126 %x FIELDS
127 
128 D           [0-9]
129 L           [a-zA-Z_]
130 H           [a-fA-F0-9]
131 E           [Ee][+-]?{D}+
132 O           [0-7]
133 
134 %%
135 
136 %{
137     TParseContext* context = yyextra;
138 %}
139 
140 "invariant"    { return INVARIANT; }
141 "highp"        { return HIGH_PRECISION; }
142 "mediump"      { return MEDIUM_PRECISION; }
143 "lowp"         { return LOW_PRECISION; }
144 "precision"    { return PRECISION; }
145 
146 "attribute"    { return ES2_keyword_ES3_reserved(context, ATTRIBUTE); }
147 "const"        { return CONST_QUAL; }
148 "uniform"      { return UNIFORM; }
149 "buffer"       { return ES3_1_keyword(context, BUFFER); }
150 "varying"      { return ES2_keyword_ES3_reserved(context, VARYING); }
151 
152 "break"        { return BREAK; }
153 "continue"     { return CONTINUE; }
154 "do"           { return DO; }
155 "for"          { return FOR; }
156 "while"        { return WHILE; }
157 
158 "if"           { return IF; }
159 "else"         { return ELSE; }
160 "switch"       { return ES2_reserved_ES3_keyword(context, SWITCH); }
161 "case"         { return ES3_keyword(context, CASE); }
162 "default"      { return ES2_reserved_ES3_keyword(context, DEFAULT); }
163 
164 "centroid"      { return ES3_keyword(context, CENTROID); }
165 "flat"          { return ES2_reserved_ES3_keyword(context, FLAT); }
166 "smooth"        { return ES3_keyword(context, SMOOTH); }
167 "noperspective" { return ES3_reserved_ES3_extension(context, TExtension::NV_shader_noperspective_interpolation, NOPERSPECTIVE); }
168 
169 "patch"        { return ES3_reserved_ES3_1_extension_ES3_2_keyword(context, TExtension::EXT_tessellation_shader, PATCH); }
170 
171 "in"           { return IN_QUAL; }
172 "out"          { return OUT_QUAL; }
173 "inout"        { return INOUT_QUAL; }
174 "shared"       { return ES3_1_keyword(context, SHARED); }
175 
176 "float"        { return FLOAT_TYPE; }
177 "int"          { return INT_TYPE; }
178 "uint"         { return ES3_keyword(context, UINT_TYPE); }
179 "void"         { return VOID_TYPE; }
180 "bool"         { return BOOL_TYPE; }
181 "true"         { yylval->lex.b = true;  return BOOLCONSTANT; }
182 "false"        { yylval->lex.b = false; return BOOLCONSTANT; }
183 
184 "discard"      { return DISCARD; }
185 "return"       { return RETURN; }
186 
187 "mat2"         { return MATRIX2; }
188 "mat3"         { return MATRIX3; }
189 "mat4"         { return MATRIX4; }
190 
191 "mat2x2"         { return ES3_keyword(context, MATRIX2); }
192 "mat3x3"         { return ES3_keyword(context, MATRIX3); }
193 "mat4x4"         { return ES3_keyword(context, MATRIX4); }
194 
195 "mat2x3"         { return ES3_keyword(context, MATRIX2x3); }
196 "mat3x2"         { return ES3_keyword(context, MATRIX3x2); }
197 "mat2x4"         { return ES3_keyword(context, MATRIX2x4); }
198 "mat4x2"         { return ES3_keyword(context, MATRIX4x2); }
199 "mat3x4"         { return ES3_keyword(context, MATRIX3x4); }
200 "mat4x3"         { return ES3_keyword(context, MATRIX4x3); }
201 
202 "vec2"         { return VEC2; }
203 "vec3"         { return VEC3; }
204 "vec4"         { return VEC4; }
205 "ivec2"        { return IVEC2; }
206 "ivec3"        { return IVEC3; }
207 "ivec4"        { return IVEC4; }
208 "bvec2"        { return BVEC2; }
209 "bvec3"        { return BVEC3; }
210 "bvec4"        { return BVEC4; }
211 "uvec2"        { return ES3_keyword(context, UVEC2); }
212 "uvec3"        { return ES3_keyword(context, UVEC3); }
213 "uvec4"        { return ES3_keyword(context, UVEC4); }
214 
215 "sampler2D"            { return SAMPLER2D; }
216 "samplerCube"          { return SAMPLERCUBE; }
217 "samplerExternalOES"   { return SAMPLER_EXTERNAL_OES; }
218 "sampler3D"            { return ES2_reserved_ES2_extension_ES3_keyword(context, TExtension::OES_texture_3D, SAMPLER3D); }
219 "sampler3DRect"        { return ES2_reserved_ES3_keyword(context, SAMPLER3DRECT); }
220 "sampler2DRect"        { return SAMPLER2DRECT; }
221 "sampler2DArray"       { return ES3_keyword(context, SAMPLER2DARRAY); }
222 "sampler2DMS"          { return ES3_reserved_ES3_extension_ES3_1_keyword(context, TExtension::ANGLE_texture_multisample, SAMPLER2DMS); }
223 "isampler2D"           { return ES3_keyword(context, ISAMPLER2D); }
224 "isampler3D"           { return ES3_keyword(context, ISAMPLER3D); }
225 "isamplerCube"         { return ES3_keyword(context, ISAMPLERCUBE); }
226 "isampler2DArray"      { return ES3_keyword(context, ISAMPLER2DARRAY); }
227 "isampler2DMS"         { return ES3_reserved_ES3_extension_ES3_1_keyword(context, TExtension::ANGLE_texture_multisample, ISAMPLER2DMS); }
228 "usampler2D"           { return ES3_keyword(context, USAMPLER2D); }
229 "usampler3D"           { return ES3_keyword(context, USAMPLER3D); }
230 "usamplerCube"         { return ES3_keyword(context, USAMPLERCUBE); }
231 "usampler2DArray"      { return ES3_keyword(context, USAMPLER2DARRAY); }
232 "usampler2DMS"         { return ES3_reserved_ES3_extension_ES3_1_keyword(context, TExtension::ANGLE_texture_multisample, USAMPLER2DMS); }
233 "sampler2DShadow"      { return ES2_reserved_ES2_extension_ES3_keyword(context, TExtension::EXT_shadow_samplers, SAMPLER2DSHADOW); }
234 "samplerCubeShadow"    { return ES3_keyword(context, SAMPLERCUBESHADOW); }
235 "sampler2DArrayShadow" { return ES3_keyword(context, SAMPLER2DARRAYSHADOW); }
236 "__samplerExternal2DY2YEXT"   { return ES3_extension(context, TExtension::EXT_YUV_target, SAMPLEREXTERNAL2DY2YEXT); }
237 "sampler2DMSArray"     { return ES3_reserved_ES3_1_extension_ES3_2_keyword(context, TExtension::OES_texture_storage_multisample_2d_array, SAMPLER2DMSARRAY); }
238 "isampler2DMSArray"    { return ES3_reserved_ES3_1_extension_ES3_2_keyword(context, TExtension::OES_texture_storage_multisample_2d_array, ISAMPLER2DMSARRAY); }
239 "usampler2DMSArray"    { return ES3_reserved_ES3_1_extension_ES3_2_keyword(context, TExtension::OES_texture_storage_multisample_2d_array, USAMPLER2DMSARRAY); }
240 "samplerCubeArray"     { return ES3_1_reserved_ES3_1_extension_ES3_2_keyword_2(context, TExtension::OES_texture_cube_map_array, TExtension::EXT_texture_cube_map_array, SAMPLERCUBEARRAYOES, SAMPLERCUBEARRAYEXT); }
241 "samplerBuffer"        { return ES3_and_3_1_reserved_ES3_1_extension_ES3_2_keyword_2(context, TExtension::OES_texture_buffer, TExtension::EXT_texture_buffer, SAMPLERBUFFER, SAMPLERBUFFER); }
242 "samplerCubeArrayShadow"    { return ES3_1_reserved_ES3_1_extension_ES3_2_keyword_2(context, TExtension::OES_texture_cube_map_array, TExtension::EXT_texture_cube_map_array, SAMPLERCUBEARRAYSHADOWOES, SAMPLERCUBEARRAYSHADOWEXT); }
243 "isamplerCubeArray"    { return ES3_1_reserved_ES3_1_extension_ES3_2_keyword_2(context, TExtension::OES_texture_cube_map_array, TExtension::EXT_texture_cube_map_array, ISAMPLERCUBEARRAYOES, ISAMPLERCUBEARRAYEXT); }
244 "isamplerBuffer"       { return ES3_and_3_1_reserved_ES3_1_extension_ES3_2_keyword_2(context, TExtension::OES_texture_buffer, TExtension::EXT_texture_buffer, ISAMPLERBUFFER, ISAMPLERBUFFER); }
245 "usamplerCubeArray"    { return ES3_1_reserved_ES3_1_extension_ES3_2_keyword_2(context, TExtension::OES_texture_cube_map_array, TExtension::EXT_texture_cube_map_array, USAMPLERCUBEARRAYOES, USAMPLERCUBEARRAYEXT); }
246 "usamplerBuffer"       { return ES3_and_3_1_reserved_ES3_1_extension_ES3_2_keyword_2(context, TExtension::OES_texture_buffer, TExtension::EXT_texture_buffer, USAMPLERBUFFER, USAMPLERBUFFER); }
247 "samplerVideoWEBGL"    { return WEBGL_video_texture_extension(context, SAMPLERVIDEOWEBGL); }
248 "struct"       { return STRUCT; }
249 
250 "layout"  { return ES2_extensions_ES3_keyword(context, TExtension::EXT_shader_framebuffer_fetch, TExtension::EXT_shader_framebuffer_fetch_non_coherent, TExtension::KHR_blend_equation_advanced, LAYOUT); }
251 
252 "yuvCscStandardEXT"    { return ES3_extension(context, TExtension::EXT_YUV_target, YUVCSCSTANDARDEXT); }
253 "itu_601"              { return yuvcscstandardext_constant(context); }
254 "itu_601_full_range"   { return yuvcscstandardext_constant(context); }
255 "itu_709"              { return yuvcscstandardext_constant(context); }
256 
257 "image2D" { return ES3_reserved_ES3_1_keyword(context, IMAGE2D); }
258 "iimage2D" { return ES3_reserved_ES3_1_keyword(context, IIMAGE2D); }
259 "uimage2D" { return ES3_reserved_ES3_1_keyword(context, UIMAGE2D); }
260 "image2DArray" { return ES3_reserved_ES3_1_keyword(context, IMAGE2DARRAY); }
261 "iimage2DArray" { return ES3_reserved_ES3_1_keyword(context, IIMAGE2DARRAY); }
262 "uimage2DArray" { return ES3_reserved_ES3_1_keyword(context, UIMAGE2DARRAY); }
263 "image3D"  { return ES3_reserved_ES3_1_keyword(context, IMAGE3D); }
264 "uimage3D" { return ES3_reserved_ES3_1_keyword(context, UIMAGE3D); }
265 "iimage3D" { return ES3_reserved_ES3_1_keyword(context, IIMAGE3D); }
266 "iimageCube" { return ES3_reserved_ES3_1_keyword(context, IIMAGECUBE); }
267 "uimageCube" { return ES3_reserved_ES3_1_keyword(context, UIMAGECUBE); }
268 "imageCube" { return ES3_reserved_ES3_1_keyword(context, IMAGECUBE); }
269 "imageCubeArray"    { return ES3_1_reserved_ES3_1_extension_ES3_2_keyword_2(context, TExtension::OES_texture_cube_map_array, TExtension::EXT_texture_cube_map_array, IMAGECUBEARRAYOES, IMAGECUBEARRAYEXT); }
270 "iimageCubeArray"    { return ES3_1_reserved_ES3_1_extension_ES3_2_keyword_2(context, TExtension::OES_texture_cube_map_array, TExtension::EXT_texture_cube_map_array, IIMAGECUBEARRAYOES, IIMAGECUBEARRAYEXT); }
271 "uimageCubeArray"    { return ES3_1_reserved_ES3_1_extension_ES3_2_keyword_2(context, TExtension::OES_texture_cube_map_array, TExtension::EXT_texture_cube_map_array, UIMAGECUBEARRAYOES, UIMAGECUBEARRAYEXT); }
272 "imageBuffer"     { return ES3_and_3_1_reserved_ES3_1_extension_ES3_2_keyword_2(context, TExtension::OES_texture_buffer, TExtension::EXT_texture_buffer, IMAGEBUFFER, IMAGEBUFFER); }
273 "iimageBuffer"    { return ES3_and_3_1_reserved_ES3_1_extension_ES3_2_keyword_2(context, TExtension::OES_texture_buffer, TExtension::EXT_texture_buffer, IIMAGEBUFFER, IIMAGEBUFFER); }
274 "uimageBuffer"    { return ES3_and_3_1_reserved_ES3_1_extension_ES3_2_keyword_2(context, TExtension::OES_texture_buffer, TExtension::EXT_texture_buffer, UIMAGEBUFFER, UIMAGEBUFFER); }
275 "readonly" { return ES3_reserved_ES3_1_keyword(context, READONLY); }
276 "writeonly" { return ES3_reserved_ES3_1_keyword(context, WRITEONLY); }
277 "coherent" { return ES3_reserved_ES3_1_keyword(context, COHERENT); }
278 "restrict" { return ES3_reserved_ES3_1_keyword(context, RESTRICT); }
279 "volatile" { return ES2_reserved_ES3_1_keyword(context, VOLATILE); }
280 "atomic_uint" { return ES3_reserved_ES3_1_keyword(context, ATOMICUINT); }
281 "sample" { return ES3_reserved_ES3_extension_ES3_2_keyword(context, TExtension::OES_shader_multisample_interpolation, SAMPLE); }
282 "precise" { return ES3_1_reserved_ES3_1_extension_ES3_2_keyword(context, TExtension::EXT_gpu_shader5, PRECISE); }
283 
284     /* Reserved keywords for GLSL ES 3.00 that are not reserved for GLSL ES 1.00 */
285 "resource"          |
286 "subroutine"        |
287 "common"            |
288 "partition"         |
289 "active"            |
290 
291 "filter"            |
292 "image1D"           |
293 "iimage1D"          |
294 "uimage1D"          |
295 "image1DArray"      |
296 "iimage1DArray"     |
297 "uimage1DArray"     |
298 "image1DShadow"     |
299 "image2DShadow"     |
300 "image1DArrayShadow" |
301 "image2DArrayShadow" |
302 
303 "sampler1DArray"    |
304 "sampler1DArrayShadow" |
305 "isampler1D"        |
306 "isampler1DArray"   |
307 "usampler1D"        |
308 "usampler1DArray"   |
309 "isampler2DRect"    |
310 "usampler2DRect"    {
311     if (context->getShaderVersion() < 300) {
312         yylval->lex.string = AllocatePoolCharArray(yytext, yyleng);
313         return check_type(yyscanner);
314     }
315     return reserved_word(yyscanner);
316 }
317 
318     /* Reserved keywords in GLSL ES 1.00 that are not reserved in GLSL ES 3.00 */
319 "packed"  {
320     if (context->getShaderVersion() >= 300)
321     {
322         yylval->lex.string = AllocatePoolCharArray(yytext, yyleng);
323         return check_type(yyscanner);
324     }
325 
326     return reserved_word(yyscanner);
327 }
328 
329     /* Reserved keywords */
330 "asm"          |
331 
332 "class"        |
333 "union"        |
334 "enum"         |
335 "typedef"      |
336 "template"     |
337 "this"         |
338 
339 "goto"         |
340 
341 "inline"       |
342 "noinline"     |
343 "public"       |
344 "static"       |
345 "extern"       |
346 "external"     |
347 "interface"    |
348 
349 "long"         |
350 "short"        |
351 "double"       |
352 "half"         |
353 "fixed"        |
354 "unsigned"     |
355 "superp"       |
356 
357 "input"        |
358 "output"       |
359 
360 "hvec2"        |
361 "hvec3"        |
362 "hvec4"        |
363 "dvec2"        |
364 "dvec3"        |
365 "dvec4"        |
366 "fvec2"        |
367 "fvec3"        |
368 "fvec4"        |
369 
370 "sampler1D"    |
371 "sampler1DShadow" |
372 "sampler2DRectShadow" |
373 
374 "sizeof"       |
375 "cast"         |
376 
377 "namespace"    |
378 "using"        { return reserved_word(yyscanner); }
379 
380 {L}({L}|{D})*       {
381    yylval->lex.string = AllocatePoolCharArray(yytext, yyleng);
382    return check_type(yyscanner);
383 }
384 
385 0[xX]{H}+         { return int_constant(context); }
386 0{O}+             { return int_constant(context); }
387 {D}+              { return int_constant(context); }
388 
389 0[xX]{H}+[uU]     { return uint_constant(context); }
390 0{O}+[uU]         { return uint_constant(context); }
391 {D}+[uU]          { return uint_constant(context); }
392 
393 {D}+{E}           { return float_constant(yyscanner); }
394 {D}+"."{D}*({E})? { return float_constant(yyscanner); }
395 "."{D}+({E})?     { return float_constant(yyscanner); }
396 
397 {D}+{E}[fF]           { return floatsuffix_check(context); }
398 {D}+"."{D}*({E})?[fF] { return floatsuffix_check(context); }
399 "."{D}+({E})?[fF]     { return floatsuffix_check(context); }
400 
401 "+="            { return ADD_ASSIGN; }
402 "-="            { return SUB_ASSIGN; }
403 "*="            { return MUL_ASSIGN; }
404 "/="            { return DIV_ASSIGN; }
405 "%="            { return MOD_ASSIGN; }
406 "<<="           { return LEFT_ASSIGN; }
407 ">>="           { return RIGHT_ASSIGN; }
408 "&="            { return AND_ASSIGN; }
409 "^="            { return XOR_ASSIGN; }
410 "|="            { return OR_ASSIGN; }
411 
412 "++"            { return INC_OP; }
413 "--"            { return DEC_OP; }
414 "&&"            { return AND_OP; }
415 "||"            { return OR_OP; }
416 "^^"            { return XOR_OP; }
417 "<="            { return LE_OP; }
418 ">="            { return GE_OP; }
419 "=="            { return EQ_OP; }
420 "!="            { return NE_OP; }
421 "<<"            { return LEFT_OP; }
422 ">>"            { return RIGHT_OP; }
423 ";"             { return SEMICOLON; }
424 ("{"|"<%")      { return LEFT_BRACE; }
425 ("}"|"%>")      { return RIGHT_BRACE; }
426 ","             { return COMMA; }
427 ":"             { return COLON; }
428 "="             { return EQUAL; }
429 "("             { return LEFT_PAREN; }
430 ")"             { return RIGHT_PAREN; }
431 ("["|"<:")      { return LEFT_BRACKET; }
432 ("]"|":>")      { return RIGHT_BRACKET; }
433 "."             { BEGIN(FIELDS); return DOT; }
434 "!"             { return BANG; }
435 "-"             { return DASH; }
436 "~"             { return TILDE; }
437 "+"             { return PLUS; }
438 "*"             { return STAR; }
439 "/"             { return SLASH; }
440 "%"             { return PERCENT; }
441 "<"             { return LEFT_ANGLE; }
442 ">"             { return RIGHT_ANGLE; }
443 "|"             { return VERTICAL_BAR; }
444 "^"             { return CARET; }
445 "&"             { return AMPERSAND; }
446 "?"             { return QUESTION; }
447 
448 <FIELDS>{L}({L}|{D})* {
449     BEGIN(INITIAL);
450     yylval->lex.string = AllocatePoolCharArray(yytext, yyleng);
451     return FIELD_SELECTION;
452 }
453 <FIELDS>[ \t\v\f\r] {}
454 <FIELDS>. {
455     yyextra->error(*yylloc, "Illegal character at fieldname start", yytext);
456     return 0;
457 }
458 
459 [ \t\v\n\f\r] { }
460 <*><<EOF>>    { yyterminate(); }
461 <*>.          { assert(false); return 0; }
462 
463 %%
464 
465 yy_size_t string_input(char* buf, yy_size_t max_size, yyscan_t yyscanner) {
466     angle::pp::Token token;
467     yyget_extra(yyscanner)->getPreprocessor().lex(&token);
468     yy_size_t len = token.type == angle::pp::Token::LAST ? 0 : token.text.size();
469     if (len < max_size)
470         memcpy(buf, token.text.c_str(), len);
471     yyset_column(token.location.file, yyscanner);
472     yyset_lineno(token.location.line, yyscanner);
473 
474     if (len >= max_size)
475         YY_FATAL_ERROR("Input buffer overflow");
476     else if (len > 0)
477         buf[len++] = ' ';
478     return len;
479 }
480 
check_type(yyscan_t yyscanner)481 int check_type(yyscan_t yyscanner) {
482     struct yyguts_t* yyg = (struct yyguts_t*) yyscanner;
483 
484     int token = IDENTIFIER;
485     // Note that the ImmutableString used here isn't static or pool allocated - but it's fine since yytext is valid for the duration of its use.
486     const TSymbol* symbol = yyextra->symbolTable.find(ImmutableString(yytext, yyleng), yyextra->getShaderVersion());
487     if (symbol && symbol->isStruct())
488     {
489         token = TYPE_NAME;
490     }
491     yylval->lex.symbol = symbol;
492     return token;
493 }
494 
reserved_word(yyscan_t yyscanner)495 int reserved_word(yyscan_t yyscanner) {
496     struct yyguts_t* yyg = (struct yyguts_t*) yyscanner;
497 
498     yyextra->error(*yylloc, "Illegal use of reserved word", yytext);
499     return 0;
500 }
501 
is_extension_enabled_or_is_core(TParseContext * context,int extension_version,TExtension extension,int promotion_version)502 static bool is_extension_enabled_or_is_core(TParseContext *context,
503         int extension_version, TExtension extension, int promotion_version)
504 {
505     int version = context->getShaderVersion();
506 
507     // If version is at least promotion_version, symbol is definitely keyword.  Otherwise it's a
508     // keyword if version is at least extension_version (where the extension was introduced) and
509     // the extension is enabled.
510     return version >= promotion_version ||
511         (version >= extension_version && context->isExtensionEnabled(extension));
512 }
513 
ES2_reserved_ES3_keyword(TParseContext * context,int token)514 int ES2_reserved_ES3_keyword(TParseContext *context, int token)
515 {
516     yyscan_t yyscanner = (yyscan_t) context->getScanner();
517 
518     if (context->getShaderVersion() < 300)
519     {
520         return reserved_word(yyscanner);
521     }
522 
523     return token;
524 }
525 
ES2_keyword_ES3_reserved(TParseContext * context,int token)526 int ES2_keyword_ES3_reserved(TParseContext *context, int token)
527 {
528     yyscan_t yyscanner = (yyscan_t) context->getScanner();
529 
530     if (context->getShaderVersion() >= 300)
531     {
532         return reserved_word(yyscanner);
533     }
534 
535     return token;
536 }
537 
ES3_reserved_ES3_1_keyword(TParseContext * context,int token)538 int ES3_reserved_ES3_1_keyword(TParseContext *context, int token)
539 {
540     struct yyguts_t* yyg = (struct yyguts_t*) context->getScanner();
541     yyscan_t yyscanner = (yyscan_t) context->getScanner();
542 
543     if (context->getShaderVersion() < 300)
544     {
545         yylval->lex.string = AllocatePoolCharArray(yytext, yyleng);
546         return check_type(yyscanner);
547     }
548     else if (context->getShaderVersion() == 300)
549     {
550         return reserved_word(yyscanner);
551     }
552 
553     return token;
554 }
555 
ES3_keyword(TParseContext * context,int token)556 int ES3_keyword(TParseContext *context, int token)
557 {
558     struct yyguts_t* yyg = (struct yyguts_t*) context->getScanner();
559     yyscan_t yyscanner = (yyscan_t) context->getScanner();
560 
561     // not a reserved word in GLSL ES 1.00, so could be used as an identifier/type name
562     if (context->getShaderVersion() < 300)
563     {
564         yylval->lex.string = AllocatePoolCharArray(yytext, yyleng);
565         return check_type(yyscanner);
566     }
567 
568     return token;
569 }
570 
ES2_reserved_ES3_1_keyword(TParseContext * context,int token)571 int ES2_reserved_ES3_1_keyword(TParseContext *context, int token)
572 {
573     yyscan_t yyscanner = (yyscan_t) context->getScanner();
574 
575     if (context->getShaderVersion() < 310)
576     {
577         return reserved_word(yyscanner);
578     }
579 
580     return token;
581 }
582 
ES3_1_keyword(TParseContext * context,int token)583 int ES3_1_keyword(TParseContext *context, int token)
584 {
585     struct yyguts_t* yyg = (struct yyguts_t*) context->getScanner();
586     yyscan_t yyscanner = (yyscan_t) context->getScanner();
587 
588     // A keyword in GLSL ES 3.10.
589     if (context->getShaderVersion() >= 310)
590     {
591         return token;
592     }
593 
594     // Otherwise can be used as an identifier/type name
595     yylval->lex.string = AllocatePoolCharArray(yytext, yyleng);
596     return check_type(yyscanner);
597 }
598 
WEBGL_video_texture_extension(TParseContext * context,int token)599 int WEBGL_video_texture_extension(TParseContext *context, int token)
600 {
601     // Available with WEBGL_video_texture_extension
602     if (context->isExtensionEnabled(TExtension::WEBGL_video_texture))
603     {
604         return token;
605     }
606 
607     // Otherwise can be used as an identifier/type name
608     struct yyguts_t* yyg = (struct yyguts_t*) context->getScanner();
609     yyscan_t yyscanner = (yyscan_t) context->getScanner();
610 
611     yylval->lex.string = AllocatePoolCharArray(yytext, yyleng);
612     return check_type(yyscanner);
613 }
614 
ES2_extensions_ES3_keyword(TParseContext * context,TExtension extension1,TExtension extension2,TExtension extension3,int token)615 int ES2_extensions_ES3_keyword(TParseContext *context, TExtension extension1, TExtension extension2, TExtension extension3, int token)
616 {
617     struct yyguts_t* yyg = (struct yyguts_t*) context->getScanner();
618     yyscan_t yyscanner = (yyscan_t) context->getScanner();
619 
620     // A keyword in GLSL ES 3.00 or GLSL ES 1.00 with enabled extension.
621     if (is_extension_enabled_or_is_core(context, 100, extension1, 300))
622     {
623         return token;
624     }
625     else if (is_extension_enabled_or_is_core(context, 100, extension2, 300))
626     {
627         return token;
628     }
629     else if (is_extension_enabled_or_is_core(context, 100, extension3, 300))
630     {
631         return token;
632     }
633 
634     // not a reserved word in GLSL ES 1.00, so could be used as an identifier/type name
635     yylval->lex.string = AllocatePoolCharArray(yytext, yyleng);
636     return check_type(yyscanner);
637 }
638 
ES2_reserved_ES2_extension_ES3_keyword(TParseContext * context,TExtension extension,int token)639 int ES2_reserved_ES2_extension_ES3_keyword(TParseContext *context, TExtension extension, int token)
640 {
641     yyscan_t yyscanner = (yyscan_t) context->getScanner();
642 
643     // A keyword in GLSL ES 3.00 or GLSL ES 1.00 with enabled extension.
644     if (is_extension_enabled_or_is_core(context, 100, extension, 300))
645     {
646         return token;
647     }
648 
649     // Reserved otherwise.
650     return reserved_word(yyscanner);
651 }
652 
ES3_extension(TParseContext * context,TExtension extension,int token)653 int ES3_extension(TParseContext *context, TExtension extension, int token)
654 {
655     struct yyguts_t* yyg = (struct yyguts_t*) context->getScanner();
656     yyscan_t yyscanner = (yyscan_t) context->getScanner();
657 
658     // a keyword word in GLSL ES 3.00 with enabled extension.
659     if (context->getShaderVersion() >= 300 && context->isExtensionEnabled(extension))
660     {
661         return token;
662     }
663 
664     // Otherwise can be used as an identifier/type name
665     yylval->lex.string = AllocatePoolCharArray(yytext, yyleng);
666     return check_type(yyscanner);
667 }
668 
ES3_reserved_ES3_1_extension_ES3_2_keyword(TParseContext * context,TExtension extension,int token)669 int ES3_reserved_ES3_1_extension_ES3_2_keyword(TParseContext *context, TExtension extension, int token)
670 {
671     struct yyguts_t* yyg = (struct yyguts_t*) context->getScanner();
672     yyscan_t yyscanner = (yyscan_t) context->getScanner();
673 
674     // a keyword in GLSL ES 3.10 with enabled extension
675     if (is_extension_enabled_or_is_core(context, 310, extension, 320))
676     {
677         return token;
678     }
679     // a reserved word in GLSL ES 3.00+
680     if (context->getShaderVersion() >= 300)
681     {
682         return reserved_word(yyscanner);
683     }
684 
685     // Otherwise can be used as an identifier/type name
686     yylval->lex.string = AllocatePoolCharArray(yytext, yyleng);
687     return check_type(yyscanner);
688 }
689 
ES3_reserved_ES3_extension(TParseContext * context,TExtension extension,int token)690 int ES3_reserved_ES3_extension(TParseContext *context, TExtension extension, int token)
691 {
692     struct yyguts_t* yyg = (struct yyguts_t*) context->getScanner();
693     yyscan_t yyscanner = (yyscan_t) context->getScanner();
694 
695     if(context->getShaderVersion() >= 300)
696     {
697         if (context->isExtensionEnabled(extension)) {
698             return token;
699         } else {
700             return reserved_word(yyscanner);
701         }
702     }
703 
704     yylval->lex.string = AllocatePoolCharArray(yytext, yyleng);
705     return check_type(yyscanner);
706 }
707 
ES3_reserved_ES3_extension_ES3_1_keyword(TParseContext * context,TExtension extension,int token)708 int ES3_reserved_ES3_extension_ES3_1_keyword(TParseContext *context, TExtension extension, int token)
709 {
710     struct yyguts_t* yyg = (struct yyguts_t*) context->getScanner();
711     yyscan_t yyscanner = (yyscan_t) context->getScanner();
712 
713     // A keyword in GLSL ES 3.00 with enabled extension or in GLSL ES 3.10
714     if (is_extension_enabled_or_is_core(context, 300, extension, 310))
715     {
716         return token;
717     }
718 
719     if(context->getShaderVersion() == 300)
720     {
721         return reserved_word(yyscanner);
722     }
723 
724     yylval->lex.string = AllocatePoolCharArray(yytext, yyleng);
725     return check_type(yyscanner);
726 }
727 
ES3_reserved_ES3_extension_ES3_2_keyword(TParseContext * context,TExtension extension,int token)728 int ES3_reserved_ES3_extension_ES3_2_keyword(TParseContext *context, TExtension extension, int token)
729 {
730     struct yyguts_t* yyg = (struct yyguts_t*) context->getScanner();
731     yyscan_t yyscanner = (yyscan_t) context->getScanner();
732 
733     // A keyword in GLSL ES 3.00 with enabled extension or in GLSL ES 3.20
734     if (is_extension_enabled_or_is_core(context, 300, extension, 320))
735     {
736         return token;
737     }
738 
739     if(context->getShaderVersion() == 300 ||context->getShaderVersion() == 310)
740     {
741         return reserved_word(yyscanner);
742     }
743 
744     yylval->lex.string = AllocatePoolCharArray(yytext, yyleng);
745     return check_type(yyscanner);
746 }
747 
ES3_1_reserved_ES3_1_extension_ES3_2_keyword(TParseContext * context,TExtension extension,int token)748 static int ES3_1_reserved_ES3_1_extension_ES3_2_keyword(TParseContext *context, TExtension extension, int token)
749 {
750     struct yyguts_t* yyg = (struct yyguts_t*) context->getScanner();
751     yyscan_t yyscanner = (yyscan_t) context->getScanner();
752 
753     // A keyword in GLSL ES 3.20 or GLSL ES 3.10 with enabled extension.
754     if (is_extension_enabled_or_is_core(context, 310, extension, 320))
755     {
756         return token;
757     }
758 
759     // A reserved word in GLSL ES 3.10
760     if (context->getShaderVersion() == 310)
761     {
762         return reserved_word(yyscanner);
763     }
764 
765     yylval->lex.string = AllocatePoolCharArray(yytext, yyleng);
766     return check_type(yyscanner);
767 }
768 
ES3_1_reserved_ES3_1_extension_ES3_2_keyword_2(TParseContext * context,TExtension extension1,TExtension extension2,int token1,int token2)769 static int ES3_1_reserved_ES3_1_extension_ES3_2_keyword_2(TParseContext *context, TExtension extension1, TExtension extension2, int token1, int token2)
770 {
771    struct yyguts_t* yyg = (struct yyguts_t*) context->getScanner();
772     yyscan_t yyscanner = (yyscan_t) context->getScanner();
773 
774     // A keyword in GLSL ES 3.20 or GLSL ES 3.10 with enabled extension.
775     if (is_extension_enabled_or_is_core(context, 310, extension1, 320))
776     {
777         return token1;
778     }
779     else if (is_extension_enabled_or_is_core(context, 310, extension2, 320))
780     {
781         return token2;
782     }
783 
784     // A reserved word in GLSL ES 3.10
785     if (context->getShaderVersion() == 310)
786     {
787         return reserved_word(yyscanner);
788     }
789 
790     yylval->lex.string = AllocatePoolCharArray(yytext, yyleng);
791     return check_type(yyscanner);
792 }
793 
ES3_and_3_1_reserved_ES3_1_extension_ES3_2_keyword_2(TParseContext * context,TExtension extension1,TExtension extension2,int token1,int token2)794 static int ES3_and_3_1_reserved_ES3_1_extension_ES3_2_keyword_2(TParseContext *context, TExtension extension1, TExtension extension2, int token1, int token2)
795 {
796    struct yyguts_t* yyg = (struct yyguts_t*) context->getScanner();
797     yyscan_t yyscanner = (yyscan_t) context->getScanner();
798 
799     // A keyword in GLSL ES 3.20 or GLSL ES 3.10 with enabled extension.
800     if (is_extension_enabled_or_is_core(context, 310, extension1, 320))
801     {
802         return token1;
803     }
804     else if (is_extension_enabled_or_is_core(context, 310, extension2, 320))
805     {
806         return token2;
807     }
808 
809     // A reserved word in GLSL ES 3.00 and 3.10
810     if (context->getShaderVersion() >= 300)
811     {
812         return reserved_word(yyscanner);
813     }
814 
815     yylval->lex.string = AllocatePoolCharArray(yytext, yyleng);
816     return check_type(yyscanner);
817 }
818 
uint_constant(TParseContext * context)819 int uint_constant(TParseContext *context)
820 {
821     struct yyguts_t* yyg = (struct yyguts_t*) context->getScanner();
822 
823     if (context->getShaderVersion() < 300)
824     {
825         context->error(*yylloc, "Unsigned integers are unsupported prior to GLSL ES 3.00", yytext);
826         return 0;
827     }
828 
829     if (!atoi_clamp(yytext, &(yylval->lex.u)))
830         yyextra->error(*yylloc, "Integer overflow", yytext);
831 
832     return UINTCONSTANT;
833 }
834 
floatsuffix_check(TParseContext * context)835 int floatsuffix_check(TParseContext* context)
836 {
837     struct yyguts_t* yyg = (struct yyguts_t*) context->getScanner();
838 
839     if (context->getShaderVersion() < 300)
840     {
841         context->error(*yylloc, "Floating-point suffix unsupported prior to GLSL ES 3.00", yytext);
842         return 0;
843     }
844 
845     std::string text = yytext;
846     text.resize(text.size() - 1);
847     if (!strtof_clamp(text, &(yylval->lex.f)))
848         yyextra->warning(*yylloc, "Float overflow", yytext);
849 
850     return(FLOATCONSTANT);
851 }
852 
yyerror(YYLTYPE * lloc,TParseContext * context,void * scanner,const char * reason)853 void yyerror(YYLTYPE* lloc, TParseContext* context, void *scanner, const char* reason) {
854     context->error(*lloc, reason, yyget_text(scanner));
855 }
856 
int_constant(TParseContext * context)857 int int_constant(TParseContext *context) {
858     struct yyguts_t* yyg = (struct yyguts_t*) context->getScanner();
859 
860     unsigned int u;
861     if (!atoi_clamp(yytext, &u))
862     {
863         if (context->getShaderVersion() >= 300)
864             yyextra->error(*yylloc, "Integer overflow", yytext);
865         else
866             yyextra->warning(*yylloc, "Integer overflow", yytext);
867     }
868     yylval->lex.i = static_cast<int>(u);
869     return INTCONSTANT;
870 }
871 
float_constant(yyscan_t yyscanner)872 int float_constant(yyscan_t yyscanner) {
873     struct yyguts_t* yyg = (struct yyguts_t*) yyscanner;
874 
875     if (!strtof_clamp(yytext, &(yylval->lex.f)))
876         yyextra->warning(*yylloc, "Float overflow", yytext);
877     return FLOATCONSTANT;
878 }
879 
yuvcscstandardext_constant(TParseContext * context)880 int yuvcscstandardext_constant(TParseContext *context)
881 {
882     struct yyguts_t* yyg = (struct yyguts_t*) context->getScanner();
883     yyscan_t yyscanner = (yyscan_t) context->getScanner();
884 
885     // a reserved word in GLSL ES 3.00 with enabled extension, otherwise could be used as an identifier/type name
886     if (context->getShaderVersion() >= 300 && context->isExtensionEnabled(TExtension::EXT_YUV_target))
887     {
888         yylval->lex.string = AllocatePoolCharArray(yytext, yyleng);
889         return YUVCSCSTANDARDEXTCONSTANT;
890     }
891 
892     yylval->lex.string = AllocatePoolCharArray(yytext, yyleng);
893     return check_type(yyscanner);
894 }
895 
glslang_initialize(TParseContext * context)896 int glslang_initialize(TParseContext* context) {
897     yyscan_t scanner = NULL;
898     if (yylex_init_extra(context, &scanner))
899         return 1;
900 
901     context->setScanner(scanner);
902     return 0;
903 }
904 
glslang_finalize(TParseContext * context)905 int glslang_finalize(TParseContext* context) {
906     yyscan_t scanner = context->getScanner();
907     if (scanner == NULL) return 0;
908 
909     context->setScanner(NULL);
910     yylex_destroy(scanner);
911 
912     return 0;
913 }
914 
glslang_scan(size_t count,const char * const string[],const int length[],TParseContext * context)915 int glslang_scan(size_t count, const char* const string[], const int length[],
916                  TParseContext* context) {
917     yyrestart(NULL, context->getScanner());
918     yyset_column(0, context->getScanner());
919     yyset_lineno(1, context->getScanner());
920 
921     // Initialize preprocessor.
922     angle::pp::Preprocessor *preprocessor = &context->getPreprocessor();
923 
924     if (!preprocessor->init(count, string, length))
925         return 1;
926 
927     // Define extension macros.
928     const TExtensionBehavior& extBehavior = context->extensionBehavior();
929     for (TExtensionBehavior::const_iterator iter = extBehavior.begin();
930          iter != extBehavior.end(); ++iter) {
931         // OVR_multiview should not be defined for WebGL spec'ed shaders.
932         if (sh::IsWebGLBasedSpec(context->getShaderSpec()) &&
933             iter->first == TExtension::OVR_multiview) {
934             continue;
935         }
936         preprocessor->predefineMacro(GetExtensionNameString(iter->first), 1);
937     }
938     if (context->getFragmentPrecisionHigh())
939         preprocessor->predefineMacro("GL_FRAGMENT_PRECISION_HIGH", 1);
940 
941     preprocessor->setMaxTokenSize(sh::GetGlobalMaxTokenSize(context->getShaderSpec()));
942 
943     return 0;
944 }
945