blob: 8300ff63f7045b015d71d16f0ecb576f13d064fa [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001/*
2//
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +00003// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004// Use of this source code is governed by a BSD-style license that can be
5// found in the LICENSE file.
6//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00007
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00008This file contains the Lex specification for GLSL ES.
9Based on ANSI C grammar, Lex specification:
10http://www.lysator.liu.se/c/ANSI-C-grammar-l.html
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000011
alokp@chromium.org75fe6b72011-08-14 05:31:22 +000012IF YOU MODIFY THIS FILE YOU ALSO NEED TO RUN generate_parser.sh,
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000013WHICH GENERATES THE GLSL ES LEXER (glslang_lex.cpp).
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000014*/
15
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000016%top{
17//
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +000018// Copyright (c) 2012 The ANGLE Project Authors. All rights reserved.
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000019// Use of this source code is governed by a BSD-style license that can be
20// found in the LICENSE file.
21//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000022
alokp@chromium.org75fe6b72011-08-14 05:31:22 +000023// This file is auto-generated by generate_parser.sh. DO NOT EDIT!
apatrick@chromium.org536888b2012-01-25 02:10:25 +000024
25// Ignore errors in auto-generated code.
26#if defined(__GNUC__)
apatrick@chromium.orga1d80592012-01-25 21:52:10 +000027#pragma GCC diagnostic ignored "-Wunused-function"
apatrick@chromium.org536888b2012-01-25 02:10:25 +000028#pragma GCC diagnostic ignored "-Wunused-variable"
apatrick@chromium.orge057c5d2012-01-26 19:18:24 +000029#pragma GCC diagnostic ignored "-Wswitch-enum"
apatrick@chromium.org536888b2012-01-25 02:10:25 +000030#elif defined(_MSC_VER)
31#pragma warning(disable: 4065)
apatrick@chromium.orga1d80592012-01-25 21:52:10 +000032#pragma warning(disable: 4189)
33#pragma warning(disable: 4505)
34#pragma warning(disable: 4701)
apatrick@chromium.org536888b2012-01-25 02:10:25 +000035#endif
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000036}
37
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000038%{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000039#include "compiler/glslang.h"
daniel@transgaming.come6842292010-04-20 18:52:50 +000040#include "compiler/ParseHelper.h"
daniel@transgaming.comb401a922012-10-26 18:58:24 +000041#include "compiler/preprocessor/Token.h"
daniel@transgaming.com91ed1492010-10-29 03:11:43 +000042#include "compiler/util.h"
alokp@chromium.orgeab1ef12010-04-23 17:33:49 +000043#include "glslang_tab.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000044
45/* windows only pragma */
46#ifdef _MSC_VER
47#pragma warning(disable : 4102)
48#endif
49
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000050#define YY_USER_ACTION yylval->lex.line = yylineno;
51#define YY_INPUT(buf, result, max_size) \
52 result = string_input(buf, max_size, yyscanner);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000053
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000054static int string_input(char* buf, int max_size, yyscan_t yyscanner);
55static int check_type(yyscan_t yyscanner);
56static int reserved_word(yyscan_t yyscanner);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000057%}
58
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000059%option noyywrap nounput never-interactive
60%option yylineno reentrant bison-bridge
61%option stack
62%option extra-type="TParseContext*"
63%x COMMENT FIELDS
alokp@chromium.org29d56fb2010-04-06 15:42:22 +000064
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000065D [0-9]
66L [a-zA-Z_]
67H [a-fA-F0-9]
68E [Ee][+-]?{D}+
69O [0-7]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000070
71%%
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000072
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000073%{
74 TParseContext* context = yyextra;
75%}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000076
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000077 /* Single-line comments */
78"//"[^\n]* ;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000079
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000080 /* Multi-line comments */
81"/*" { yy_push_state(COMMENT, yyscanner); }
82<COMMENT>. |
83<COMMENT>\n ;
84<COMMENT>"*/" { yy_pop_state(yyscanner); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000085
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000086"invariant" { return(INVARIANT); }
87"highp" { return(HIGH_PRECISION); }
88"mediump" { return(MEDIUM_PRECISION); }
89"lowp" { return(LOW_PRECISION); }
90"precision" { return(PRECISION); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000091
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000092"attribute" { return(ATTRIBUTE); }
93"const" { return(CONST_QUAL); }
94"uniform" { return(UNIFORM); }
95"varying" { return(VARYING); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000096
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000097"break" { return(BREAK); }
98"continue" { return(CONTINUE); }
99"do" { return(DO); }
100"for" { return(FOR); }
101"while" { return(WHILE); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000102
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000103"if" { return(IF); }
104"else" { return(ELSE); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000105
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000106"in" { return(IN_QUAL); }
107"out" { return(OUT_QUAL); }
108"inout" { return(INOUT_QUAL); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000109
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000110"float" { context->lexAfterType = true; return(FLOAT_TYPE); }
111"int" { context->lexAfterType = true; return(INT_TYPE); }
112"void" { context->lexAfterType = true; return(VOID_TYPE); }
113"bool" { context->lexAfterType = true; return(BOOL_TYPE); }
114"true" { yylval->lex.b = true; return(BOOLCONSTANT); }
115"false" { yylval->lex.b = false; return(BOOLCONSTANT); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000116
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000117"discard" { return(DISCARD); }
118"return" { return(RETURN); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000119
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000120"mat2" { context->lexAfterType = true; return(MATRIX2); }
121"mat3" { context->lexAfterType = true; return(MATRIX3); }
122"mat4" { context->lexAfterType = true; return(MATRIX4); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000123
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000124"vec2" { context->lexAfterType = true; return (VEC2); }
125"vec3" { context->lexAfterType = true; return (VEC3); }
126"vec4" { context->lexAfterType = true; return (VEC4); }
127"ivec2" { context->lexAfterType = true; return (IVEC2); }
128"ivec3" { context->lexAfterType = true; return (IVEC3); }
129"ivec4" { context->lexAfterType = true; return (IVEC4); }
130"bvec2" { context->lexAfterType = true; return (BVEC2); }
131"bvec3" { context->lexAfterType = true; return (BVEC3); }
132"bvec4" { context->lexAfterType = true; return (BVEC4); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000133
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000134"sampler2D" { context->lexAfterType = true; return SAMPLER2D; }
135"samplerCube" { context->lexAfterType = true; return SAMPLERCUBE; }
zmo@google.com09c323a2011-08-12 18:22:25 +0000136"samplerExternalOES" { context->lexAfterType = true; return SAMPLER_EXTERNAL_OES; }
kbr@chromium.org205fef32011-11-22 20:50:02 +0000137"sampler2DRect" { context->lexAfterType = true; return SAMPLER2DRECT; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000138
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000139"struct" { context->lexAfterType = true; return(STRUCT); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000140
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000141"asm" { return reserved_word(yyscanner); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000142
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000143"class" { return reserved_word(yyscanner); }
144"union" { return reserved_word(yyscanner); }
145"enum" { return reserved_word(yyscanner); }
146"typedef" { return reserved_word(yyscanner); }
147"template" { return reserved_word(yyscanner); }
148"this" { return reserved_word(yyscanner); }
149"packed" { return reserved_word(yyscanner); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000150
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000151"goto" { return reserved_word(yyscanner); }
152"switch" { return reserved_word(yyscanner); }
153"default" { return reserved_word(yyscanner); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000154
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000155"inline" { return reserved_word(yyscanner); }
156"noinline" { return reserved_word(yyscanner); }
157"volatile" { return reserved_word(yyscanner); }
158"public" { return reserved_word(yyscanner); }
159"static" { return reserved_word(yyscanner); }
160"extern" { return reserved_word(yyscanner); }
161"external" { return reserved_word(yyscanner); }
162"interface" { return reserved_word(yyscanner); }
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +0000163"flat" { return reserved_word(yyscanner); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000164
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000165"long" { return reserved_word(yyscanner); }
166"short" { return reserved_word(yyscanner); }
167"double" { return reserved_word(yyscanner); }
168"half" { return reserved_word(yyscanner); }
169"fixed" { return reserved_word(yyscanner); }
170"unsigned" { return reserved_word(yyscanner); }
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +0000171"superp" { return reserved_word(yyscanner); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000172
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000173"input" { return reserved_word(yyscanner); }
174"output" { return reserved_word(yyscanner); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000175
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000176"hvec2" { return reserved_word(yyscanner); }
177"hvec3" { return reserved_word(yyscanner); }
178"hvec4" { return reserved_word(yyscanner); }
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000179"dvec2" { return reserved_word(yyscanner); }
180"dvec3" { return reserved_word(yyscanner); }
181"dvec4" { return reserved_word(yyscanner); }
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +0000182"fvec2" { return reserved_word(yyscanner); }
183"fvec3" { return reserved_word(yyscanner); }
184"fvec4" { return reserved_word(yyscanner); }
185
186"sampler1D" { return reserved_word(yyscanner); }
187"sampler3D" { return reserved_word(yyscanner); }
188
189"sampler1DShadow" { return reserved_word(yyscanner); }
190"sampler2DShadow" { return reserved_word(yyscanner); }
191
192"sampler3DRect" { return reserved_word(yyscanner); }
193"sampler2DRectShadow" { return reserved_word(yyscanner); }
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000194
195"sizeof" { return reserved_word(yyscanner); }
196"cast" { return reserved_word(yyscanner); }
197
198"namespace" { return reserved_word(yyscanner); }
199"using" { return reserved_word(yyscanner); }
200
201{L}({L}|{D})* {
202 yylval->lex.string = NewPoolTString(yytext);
203 return check_type(yyscanner);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000204}
205
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002060[xX]{H}+ { yylval->lex.i = strtol(yytext, 0, 0); return(INTCONSTANT); }
2070{O}+ { yylval->lex.i = strtol(yytext, 0, 0); return(INTCONSTANT); }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002080{D}+ { context->error(yylineno, "Invalid Octal number.", yytext); context->recover(); return 0;}
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000209{D}+ { yylval->lex.i = strtol(yytext, 0, 0); return(INTCONSTANT); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000210
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000211{D}+{E} { yylval->lex.f = static_cast<float>(atof_dot(yytext)); return(FLOATCONSTANT); }
212{D}+"."{D}*({E})? { yylval->lex.f = static_cast<float>(atof_dot(yytext)); return(FLOATCONSTANT); }
213"."{D}+({E})? { yylval->lex.f = static_cast<float>(atof_dot(yytext)); return(FLOATCONSTANT); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000214
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000215"+=" { return(ADD_ASSIGN); }
216"-=" { return(SUB_ASSIGN); }
217"*=" { return(MUL_ASSIGN); }
218"/=" { return(DIV_ASSIGN); }
219"%=" { return(MOD_ASSIGN); }
220"<<=" { return(LEFT_ASSIGN); }
221">>=" { return(RIGHT_ASSIGN); }
222"&=" { return(AND_ASSIGN); }
223"^=" { return(XOR_ASSIGN); }
224"|=" { return(OR_ASSIGN); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000225
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000226"++" { return(INC_OP); }
227"--" { return(DEC_OP); }
228"&&" { return(AND_OP); }
229"||" { return(OR_OP); }
230"^^" { return(XOR_OP); }
231"<=" { return(LE_OP); }
232">=" { return(GE_OP); }
233"==" { return(EQ_OP); }
234"!=" { return(NE_OP); }
235"<<" { return(LEFT_OP); }
236">>" { return(RIGHT_OP); }
237";" { context->lexAfterType = false; return(SEMICOLON); }
238("{"|"<%") { context->lexAfterType = false; return(LEFT_BRACE); }
239("}"|"%>") { return(RIGHT_BRACE); }
240"," { if (context->inTypeParen) context->lexAfterType = false; return(COMMA); }
241":" { return(COLON); }
242"=" { context->lexAfterType = false; return(EQUAL); }
243"(" { context->lexAfterType = false; context->inTypeParen = true; return(LEFT_PAREN); }
244")" { context->inTypeParen = false; return(RIGHT_PAREN); }
245("["|"<:") { return(LEFT_BRACKET); }
246("]"|":>") { return(RIGHT_BRACKET); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000247"." { BEGIN(FIELDS); return(DOT); }
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000248"!" { return(BANG); }
249"-" { return(DASH); }
250"~" { return(TILDE); }
251"+" { return(PLUS); }
252"*" { return(STAR); }
253"/" { return(SLASH); }
254"%" { return(PERCENT); }
255"<" { return(LEFT_ANGLE); }
256">" { return(RIGHT_ANGLE); }
257"|" { return(VERTICAL_BAR); }
258"^" { return(CARET); }
259"&" { return(AMPERSAND); }
260"?" { return(QUESTION); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000261
262<FIELDS>{L}({L}|{D})* {
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000263 BEGIN(INITIAL);
264 yylval->lex.string = NewPoolTString(yytext);
265 return FIELD_SELECTION;
266}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000267<FIELDS>[ \t\v\f\r] {}
268
269[ \t\v\n\f\r] { }
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000270<*><<EOF>> { context->AfterEOF = true; yyterminate(); }
271<*>. { context->warning(yylineno, "Unknown char", yytext, ""); return 0; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000272
273%%
274
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000275int string_input(char* buf, int max_size, yyscan_t yyscanner) {
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000276 pp::Token token;
277 yyget_extra(yyscanner)->preprocessor.lex(&token);
daniel@transgaming.coma646c912012-10-26 18:58:12 +0000278 int len = token.type == pp::Token::LAST ? 0 : token.text.size();
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000279 if ((len > 0) && (len < max_size))
alokp@chromium.org5b6a68e2012-06-28 20:29:13 +0000280 memcpy(buf, token.text.c_str(), len);
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000281 yyset_lineno(EncodeSourceLoc(token.location.file, token.location.line), yyscanner);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000282
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000283 if (len >= max_size)
284 YY_FATAL_ERROR("Input buffer overflow");
285 else if (len > 0)
286 buf[len++] = ' ';
287 return len;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000288}
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000289
290int check_type(yyscan_t yyscanner) {
291 struct yyguts_t* yyg = (struct yyguts_t*) yyscanner;
292
293 int token = IDENTIFIER;
294 TSymbol* symbol = yyextra->symbolTable.find(yytext);
295 if (yyextra->lexAfterType == false && symbol && symbol->isVariable()) {
296 TVariable* variable = static_cast<TVariable*>(symbol);
297 if (variable->isUserType()) {
298 yyextra->lexAfterType = true;
299 token = TYPE_NAME;
300 }
301 }
302 yylval->lex.symbol = symbol;
303 return token;
304}
305
306int reserved_word(yyscan_t yyscanner) {
307 struct yyguts_t* yyg = (struct yyguts_t*) yyscanner;
308
309 yyextra->error(yylineno, "Illegal use of reserved word", yytext, "");
310 yyextra->recover();
311 return 0;
312}
313
314void yyerror(TParseContext* context, const char* reason) {
315 struct yyguts_t* yyg = (struct yyguts_t*) context->scanner;
316
317 if (context->AfterEOF) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000318 context->error(yylineno, reason, "unexpected EOF");
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000319 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000320 context->error(yylineno, reason, yytext);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000321 }
322 context->recover();
323}
324
325int glslang_initialize(TParseContext* context) {
326 yyscan_t scanner = NULL;
327 if (yylex_init_extra(context, &scanner))
328 return 1;
329
330 context->scanner = scanner;
331 return 0;
332}
333
334int glslang_finalize(TParseContext* context) {
335 yyscan_t scanner = context->scanner;
336 if (scanner == NULL) return 0;
337
338 context->scanner = NULL;
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000339 yylex_destroy(scanner);
340
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000341 return 0;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000342}
343
alokp@chromium.org408c45e2012-04-05 15:54:43 +0000344int glslang_scan(int count, const char* const string[], const int length[],
345 TParseContext* context) {
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000346 yyrestart(NULL, context->scanner);
347 yyset_lineno(EncodeSourceLoc(0, 1), context->scanner);
348 context->AfterEOF = false;
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000349
350 // Initialize preprocessor.
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000351 if (!context->preprocessor.init(count, string, length))
352 return 1;
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000353
354 // Define extension macros.
355 const TExtensionBehavior& extBehavior = context->extensionBehavior();
356 for (TExtensionBehavior::const_iterator iter = extBehavior.begin();
357 iter != extBehavior.end(); ++iter) {
alokp@chromium.orge3043b12012-06-19 19:40:52 +0000358 context->preprocessor.predefineMacro(iter->first.c_str(), 1);
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000359 }
360 return 0;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000361}
362