blob: 1eaa30c1c3732f9ed7335163fffc711c6d976696 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001/*
2//
3// Copyright (c) 2002-2010 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//
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//
18// Copyright (c) 2010 The ANGLE Project Authors. All rights reserved.
19// 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!
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000024}
25
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000026%{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000027#include "compiler/glslang.h"
daniel@transgaming.come6842292010-04-20 18:52:50 +000028#include "compiler/ParseHelper.h"
daniel@transgaming.com91ed1492010-10-29 03:11:43 +000029#include "compiler/util.h"
alokp@chromium.orgeab1ef12010-04-23 17:33:49 +000030#include "glslang_tab.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000031
32/* windows only pragma */
33#ifdef _MSC_VER
34#pragma warning(disable : 4102)
35#endif
36
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000037#define YY_USER_ACTION yylval->lex.line = yylineno;
38#define YY_INPUT(buf, result, max_size) \
39 result = string_input(buf, max_size, yyscanner);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000040
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000041static int string_input(char* buf, int max_size, yyscan_t yyscanner);
42static int check_type(yyscan_t yyscanner);
43static int reserved_word(yyscan_t yyscanner);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000044%}
45
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000046%option noyywrap nounput never-interactive
47%option yylineno reentrant bison-bridge
48%option stack
49%option extra-type="TParseContext*"
50%x COMMENT FIELDS
alokp@chromium.org29d56fb2010-04-06 15:42:22 +000051
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000052D [0-9]
53L [a-zA-Z_]
54H [a-fA-F0-9]
55E [Ee][+-]?{D}+
56O [0-7]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000057
58%%
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000059
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000060%{
61 TParseContext* context = yyextra;
62%}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000063
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000064 /* Single-line comments */
65"//"[^\n]* ;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000066
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000067 /* Multi-line comments */
68"/*" { yy_push_state(COMMENT, yyscanner); }
69<COMMENT>. |
70<COMMENT>\n ;
71<COMMENT>"*/" { yy_pop_state(yyscanner); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000072
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000073"invariant" { return(INVARIANT); }
74"highp" { return(HIGH_PRECISION); }
75"mediump" { return(MEDIUM_PRECISION); }
76"lowp" { return(LOW_PRECISION); }
77"precision" { return(PRECISION); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000078
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000079"attribute" { return(ATTRIBUTE); }
80"const" { return(CONST_QUAL); }
81"uniform" { return(UNIFORM); }
82"varying" { return(VARYING); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000083
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000084"break" { return(BREAK); }
85"continue" { return(CONTINUE); }
86"do" { return(DO); }
87"for" { return(FOR); }
88"while" { return(WHILE); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000089
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000090"if" { return(IF); }
91"else" { return(ELSE); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000092
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000093"in" { return(IN_QUAL); }
94"out" { return(OUT_QUAL); }
95"inout" { return(INOUT_QUAL); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000096
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000097"float" { context->lexAfterType = true; return(FLOAT_TYPE); }
98"int" { context->lexAfterType = true; return(INT_TYPE); }
99"void" { context->lexAfterType = true; return(VOID_TYPE); }
100"bool" { context->lexAfterType = true; return(BOOL_TYPE); }
101"true" { yylval->lex.b = true; return(BOOLCONSTANT); }
102"false" { yylval->lex.b = false; return(BOOLCONSTANT); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000103
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000104"discard" { return(DISCARD); }
105"return" { return(RETURN); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000107"mat2" { context->lexAfterType = true; return(MATRIX2); }
108"mat3" { context->lexAfterType = true; return(MATRIX3); }
109"mat4" { context->lexAfterType = true; return(MATRIX4); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000110
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000111"vec2" { context->lexAfterType = true; return (VEC2); }
112"vec3" { context->lexAfterType = true; return (VEC3); }
113"vec4" { context->lexAfterType = true; return (VEC4); }
114"ivec2" { context->lexAfterType = true; return (IVEC2); }
115"ivec3" { context->lexAfterType = true; return (IVEC3); }
116"ivec4" { context->lexAfterType = true; return (IVEC4); }
117"bvec2" { context->lexAfterType = true; return (BVEC2); }
118"bvec3" { context->lexAfterType = true; return (BVEC3); }
119"bvec4" { context->lexAfterType = true; return (BVEC4); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000120
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000121"sampler2D" { context->lexAfterType = true; return SAMPLER2D; }
122"samplerCube" { context->lexAfterType = true; return SAMPLERCUBE; }
zmo@google.com09c323a2011-08-12 18:22:25 +0000123"samplerExternalOES" { context->lexAfterType = true; return SAMPLER_EXTERNAL_OES; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000124
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000125"struct" { context->lexAfterType = true; return(STRUCT); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000126
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000127"asm" { return reserved_word(yyscanner); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000128
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000129"class" { return reserved_word(yyscanner); }
130"union" { return reserved_word(yyscanner); }
131"enum" { return reserved_word(yyscanner); }
132"typedef" { return reserved_word(yyscanner); }
133"template" { return reserved_word(yyscanner); }
134"this" { return reserved_word(yyscanner); }
135"packed" { return reserved_word(yyscanner); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000136
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000137"goto" { return reserved_word(yyscanner); }
138"switch" { return reserved_word(yyscanner); }
139"default" { return reserved_word(yyscanner); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000140
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000141"inline" { return reserved_word(yyscanner); }
142"noinline" { return reserved_word(yyscanner); }
143"volatile" { return reserved_word(yyscanner); }
144"public" { return reserved_word(yyscanner); }
145"static" { return reserved_word(yyscanner); }
146"extern" { return reserved_word(yyscanner); }
147"external" { return reserved_word(yyscanner); }
148"interface" { return reserved_word(yyscanner); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000149
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000150"long" { return reserved_word(yyscanner); }
151"short" { return reserved_word(yyscanner); }
152"double" { return reserved_word(yyscanner); }
153"half" { return reserved_word(yyscanner); }
154"fixed" { return reserved_word(yyscanner); }
155"unsigned" { return reserved_word(yyscanner); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000156
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000157"input" { return reserved_word(yyscanner); }
158"output" { return reserved_word(yyscanner); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000159
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000160"hvec2" { return reserved_word(yyscanner); }
161"hvec3" { return reserved_word(yyscanner); }
162"hvec4" { return reserved_word(yyscanner); }
163"fvec2" { return reserved_word(yyscanner); }
164"fvec3" { return reserved_word(yyscanner); }
165"fvec4" { return reserved_word(yyscanner); }
166"dvec2" { return reserved_word(yyscanner); }
167"dvec3" { return reserved_word(yyscanner); }
168"dvec4" { return reserved_word(yyscanner); }
169
170"sizeof" { return reserved_word(yyscanner); }
171"cast" { return reserved_word(yyscanner); }
172
173"namespace" { return reserved_word(yyscanner); }
174"using" { return reserved_word(yyscanner); }
175
176{L}({L}|{D})* {
177 yylval->lex.string = NewPoolTString(yytext);
178 return check_type(yyscanner);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000179}
180
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00001810[xX]{H}+ { yylval->lex.i = strtol(yytext, 0, 0); return(INTCONSTANT); }
1820{O}+ { yylval->lex.i = strtol(yytext, 0, 0); return(INTCONSTANT); }
1830{D}+ { context->error(yylineno, "Invalid Octal number.", yytext, "", ""); context->recover(); return 0;}
184{D}+ { yylval->lex.i = strtol(yytext, 0, 0); return(INTCONSTANT); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000185
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000186{D}+{E} { yylval->lex.f = static_cast<float>(atof_dot(yytext)); return(FLOATCONSTANT); }
187{D}+"."{D}*({E})? { yylval->lex.f = static_cast<float>(atof_dot(yytext)); return(FLOATCONSTANT); }
188"."{D}+({E})? { yylval->lex.f = static_cast<float>(atof_dot(yytext)); return(FLOATCONSTANT); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000189
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000190"+=" { return(ADD_ASSIGN); }
191"-=" { return(SUB_ASSIGN); }
192"*=" { return(MUL_ASSIGN); }
193"/=" { return(DIV_ASSIGN); }
194"%=" { return(MOD_ASSIGN); }
195"<<=" { return(LEFT_ASSIGN); }
196">>=" { return(RIGHT_ASSIGN); }
197"&=" { return(AND_ASSIGN); }
198"^=" { return(XOR_ASSIGN); }
199"|=" { return(OR_ASSIGN); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000200
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000201"++" { return(INC_OP); }
202"--" { return(DEC_OP); }
203"&&" { return(AND_OP); }
204"||" { return(OR_OP); }
205"^^" { return(XOR_OP); }
206"<=" { return(LE_OP); }
207">=" { return(GE_OP); }
208"==" { return(EQ_OP); }
209"!=" { return(NE_OP); }
210"<<" { return(LEFT_OP); }
211">>" { return(RIGHT_OP); }
212";" { context->lexAfterType = false; return(SEMICOLON); }
213("{"|"<%") { context->lexAfterType = false; return(LEFT_BRACE); }
214("}"|"%>") { return(RIGHT_BRACE); }
215"," { if (context->inTypeParen) context->lexAfterType = false; return(COMMA); }
216":" { return(COLON); }
217"=" { context->lexAfterType = false; return(EQUAL); }
218"(" { context->lexAfterType = false; context->inTypeParen = true; return(LEFT_PAREN); }
219")" { context->inTypeParen = false; return(RIGHT_PAREN); }
220("["|"<:") { return(LEFT_BRACKET); }
221("]"|":>") { return(RIGHT_BRACKET); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000222"." { BEGIN(FIELDS); return(DOT); }
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000223"!" { return(BANG); }
224"-" { return(DASH); }
225"~" { return(TILDE); }
226"+" { return(PLUS); }
227"*" { return(STAR); }
228"/" { return(SLASH); }
229"%" { return(PERCENT); }
230"<" { return(LEFT_ANGLE); }
231">" { return(RIGHT_ANGLE); }
232"|" { return(VERTICAL_BAR); }
233"^" { return(CARET); }
234"&" { return(AMPERSAND); }
235"?" { return(QUESTION); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000236
237<FIELDS>{L}({L}|{D})* {
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000238 BEGIN(INITIAL);
239 yylval->lex.string = NewPoolTString(yytext);
240 return FIELD_SELECTION;
241}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000242<FIELDS>[ \t\v\f\r] {}
243
244[ \t\v\n\f\r] { }
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000245<*><<EOF>> { context->AfterEOF = true; yyterminate(); }
246<*>. { context->warning(yylineno, "Unknown char", yytext, ""); return 0; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000247
248%%
249
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000250extern "C" {
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000251// Preprocessor interface.
252#include "compiler/preprocessor/preprocess.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000253
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000254#define SETUP_CONTEXT(pp) \
255 TParseContext* context = (TParseContext*) pp->pC; \
256 struct yyguts_t* yyg = (struct yyguts_t*) context->scanner;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000257
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000258// Preprocessor callbacks.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000259void CPPDebugLogMsg(const char *msg)
260{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000261 SETUP_CONTEXT(cpp);
262 context->infoSink.debug.message(EPrefixNone, msg);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000263}
264
265void CPPWarningToInfoLog(const char *msg)
266{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000267 SETUP_CONTEXT(cpp);
268 context->warning(yylineno, msg, "", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000269}
270
271void CPPShInfoLogMsg(const char *msg)
272{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000273 SETUP_CONTEXT(cpp);
274 context->error(yylineno, msg, "", "");
275 context->recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000276}
277
278void CPPErrorToInfoLog(char *msg)
279{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000280 SETUP_CONTEXT(cpp);
281 context->error(yylineno, msg, "", "");
282 context->recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000283}
284
285void SetLineNumber(int line)
286{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000287 SETUP_CONTEXT(cpp);
288 int string = 0;
289 DecodeSourceLoc(yylineno, &string, NULL);
290 yylineno = EncodeSourceLoc(string, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000291}
292
293void SetStringNumber(int string)
294{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000295 SETUP_CONTEXT(cpp);
296 int line = 0;
297 DecodeSourceLoc(yylineno, NULL, &line);
298 yylineno = EncodeSourceLoc(string, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000299}
300
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000301int GetStringNumber()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000302{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000303 SETUP_CONTEXT(cpp);
304 int string = 0;
305 DecodeSourceLoc(yylineno, &string, NULL);
306 return string;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000307}
308
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000309int GetLineNumber()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000310{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000311 SETUP_CONTEXT(cpp);
312 int line = 0;
313 DecodeSourceLoc(yylineno, NULL, &line);
314 return line;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000315}
316
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000317void IncLineNumber()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000318{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000319 SETUP_CONTEXT(cpp);
320 int string = 0, line = 0;
321 DecodeSourceLoc(yylineno, &string, &line);
322 yylineno = EncodeSourceLoc(string, ++line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000323}
324
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000325void DecLineNumber()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000326{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000327 SETUP_CONTEXT(cpp);
328 int string = 0, line = 0;
329 DecodeSourceLoc(yylineno, &string, &line);
330 yylineno = EncodeSourceLoc(string, --line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000331}
332
333void HandlePragma(const char **tokens, int numTokens)
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000334{
335 SETUP_CONTEXT(cpp);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000336 if (!strcmp(tokens[0], "optimize")) {
337 if (numTokens != 4) {
338 CPPShInfoLogMsg("optimize pragma syntax is incorrect");
339 return;
340 }
341
342 if (strcmp(tokens[1], "(")) {
343 CPPShInfoLogMsg("\"(\" expected after 'optimize' keyword");
344 return;
345 }
346
347 if (!strcmp(tokens[2], "on"))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000348 context->contextPragma.optimize = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000349 else if (!strcmp(tokens[2], "off"))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000350 context->contextPragma.optimize = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000351 else {
352 CPPShInfoLogMsg("\"on\" or \"off\" expected after '(' for 'optimize' pragma");
353 return;
354 }
355
356 if (strcmp(tokens[3], ")")) {
357 CPPShInfoLogMsg("\")\" expected to end 'optimize' pragma");
358 return;
359 }
360 } else if (!strcmp(tokens[0], "debug")) {
361 if (numTokens != 4) {
362 CPPShInfoLogMsg("debug pragma syntax is incorrect");
363 return;
364 }
365
366 if (strcmp(tokens[1], "(")) {
367 CPPShInfoLogMsg("\"(\" expected after 'debug' keyword");
368 return;
369 }
370
371 if (!strcmp(tokens[2], "on"))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000372 context->contextPragma.debug = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000373 else if (!strcmp(tokens[2], "off"))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000374 context->contextPragma.debug = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000375 else {
376 CPPShInfoLogMsg("\"on\" or \"off\" expected after '(' for 'debug' pragma");
377 return;
378 }
379
380 if (strcmp(tokens[3], ")")) {
381 CPPShInfoLogMsg("\")\" expected to end 'debug' pragma");
382 return;
383 }
384 } else {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000385#ifdef PRAGMA_TABLE
386 //
387 // implementation specific pragma
388 // use ((TParseContext *)cpp->pC)->contextPragma.pragmaTable to store the information about pragma
389 // For now, just ignore the pragma that the implementation cannot recognize
390 // An Example of one such implementation for a pragma that has a syntax like
391 // #pragma pragmaname(pragmavalue)
392 // This implementation stores the current pragmavalue against the pragma name in pragmaTable.
393 //
394 if (numTokens == 4 && !strcmp(tokens[1], "(") && !strcmp(tokens[3], ")")) {
395 TPragmaTable& pragmaTable = ((TParseContext *)cpp->pC)->contextPragma.pragmaTable;
396 TPragmaTable::iterator iter;
397 iter = pragmaTable.find(TString(tokens[0]));
398 if (iter != pragmaTable.end()) {
399 iter->second = tokens[2];
400 } else {
alokp@chromium.org7e0ed772010-05-12 21:16:46 +0000401 pragmaTable[ tokens[0] ] = tokens[2];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000402 }
403 } else if (numTokens >= 2) {
404 TPragmaTable& pragmaTable = ((TParseContext *)cpp->pC)->contextPragma.pragmaTable;
405 TPragmaTable::iterator iter;
406 iter = pragmaTable.find(TString(tokens[0]));
407 if (iter != pragmaTable.end()) {
408 iter->second = tokens[1];
409 } else {
alokp@chromium.org7e0ed772010-05-12 21:16:46 +0000410 pragmaTable[ tokens[0] ] = tokens[1];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000411 }
412 }
413#endif // PRAGMA_TABLE
414 }
415}
416
417void StoreStr(char *string)
418{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000419 SETUP_CONTEXT(cpp);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000420 TString strSrc;
421 strSrc = TString(string);
422
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000423 context->HashErrMsg = context->HashErrMsg + " " + strSrc;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000424}
425
426const char* GetStrfromTStr(void)
427{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000428 SETUP_CONTEXT(cpp);
429 cpp->ErrMsg = context->HashErrMsg.c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000430 return cpp->ErrMsg;
431}
432
433void ResetTString(void)
434{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000435 SETUP_CONTEXT(cpp);
436 context->HashErrMsg = "";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000437}
438
439TBehavior GetBehavior(const char* behavior)
440{
441 if (!strcmp("require", behavior))
442 return EBhRequire;
443 else if (!strcmp("enable", behavior))
444 return EBhEnable;
445 else if (!strcmp("disable", behavior))
446 return EBhDisable;
447 else if (!strcmp("warn", behavior))
448 return EBhWarn;
449 else {
450 CPPShInfoLogMsg((TString("behavior '") + behavior + "' is not supported").c_str());
451 return EBhDisable;
452 }
453}
454
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000455void updateExtensionBehavior(const char* extName, const char* behavior)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000456{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000457 SETUP_CONTEXT(cpp);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000458 TBehavior behaviorVal = GetBehavior(behavior);
459 TMap<TString, TBehavior>:: iterator iter;
460 TString msg;
461
462 // special cased for all extension
463 if (!strcmp(extName, "all")) {
464 if (behaviorVal == EBhRequire || behaviorVal == EBhEnable) {
465 CPPShInfoLogMsg("extension 'all' cannot have 'require' or 'enable' behavior");
466 return;
467 } else {
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000468 for (iter = context->extensionBehavior.begin(); iter != context->extensionBehavior.end(); ++iter)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000469 iter->second = behaviorVal;
470 }
471 } else {
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000472 iter = context->extensionBehavior.find(TString(extName));
473 if (iter == context->extensionBehavior.end()) {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000474 switch (behaviorVal) {
475 case EBhRequire:
476 CPPShInfoLogMsg((TString("extension '") + extName + "' is not supported").c_str());
477 break;
478 case EBhEnable:
479 case EBhWarn:
480 case EBhDisable:
481 msg = TString("extension '") + extName + "' is not supported";
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000482 context->infoSink.info.message(EPrefixWarning, msg.c_str(), yylineno);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000483 break;
484 }
485 return;
486 } else
487 iter->second = behaviorVal;
488 }
489}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000490} // extern "C"
491
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000492int string_input(char* buf, int max_size, yyscan_t yyscanner) {
493 int len;
494
495 if ((len = yylex_CPP(buf, max_size)) == 0)
496 return 0;
497 if (len >= max_size)
498 YY_FATAL_ERROR("input buffer overflow, can't enlarge buffer because scanner uses REJECT");
499
500 buf[len] = ' ';
501 return len+1;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000502}
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000503
504int check_type(yyscan_t yyscanner) {
505 struct yyguts_t* yyg = (struct yyguts_t*) yyscanner;
506
507 int token = IDENTIFIER;
508 TSymbol* symbol = yyextra->symbolTable.find(yytext);
509 if (yyextra->lexAfterType == false && symbol && symbol->isVariable()) {
510 TVariable* variable = static_cast<TVariable*>(symbol);
511 if (variable->isUserType()) {
512 yyextra->lexAfterType = true;
513 token = TYPE_NAME;
514 }
515 }
516 yylval->lex.symbol = symbol;
517 return token;
518}
519
520int reserved_word(yyscan_t yyscanner) {
521 struct yyguts_t* yyg = (struct yyguts_t*) yyscanner;
522
523 yyextra->error(yylineno, "Illegal use of reserved word", yytext, "");
524 yyextra->recover();
525 return 0;
526}
527
528void yyerror(TParseContext* context, const char* reason) {
529 struct yyguts_t* yyg = (struct yyguts_t*) context->scanner;
530
531 if (context->AfterEOF) {
532 context->error(yylineno, reason, "unexpected EOF", "");
533 } else {
534 context->error(yylineno, reason, yytext, "");
535 }
536 context->recover();
537}
538
539int glslang_initialize(TParseContext* context) {
540 yyscan_t scanner = NULL;
541 if (yylex_init_extra(context, &scanner))
542 return 1;
543
544 context->scanner = scanner;
545 return 0;
546}
547
548int glslang_finalize(TParseContext* context) {
549 yyscan_t scanner = context->scanner;
550 if (scanner == NULL) return 0;
551
552 context->scanner = NULL;
553 return yylex_destroy(scanner);
554}
555
556void glslang_scan(int count, const char* const string[], const int length[],
557 TParseContext* context) {
558 yyrestart(NULL, context->scanner);
559 yyset_lineno(EncodeSourceLoc(0, 1), context->scanner);
560 context->AfterEOF = false;
561
562 // Init preprocessor.
563 cpp->pC = context;
564 cpp->PaWhichStr = 0;
565 cpp->PaArgv = string;
566 cpp->PaArgc = count;
567 cpp->PaStrLen = length;
568 cpp->pastFirstStatement = 0;
569 ScanFromString(string[0]);
570}
571