Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 /*
2 //
3 // Copyright (c) 2012 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 //
8 This file contains the Yacc grammar for GLSL ES preprocessor expression.
10 IF YOU MODIFY THIS FILE YOU ALSO NEED TO RUN generate_parser.sh,
11 WHICH GENERATES THE GLSL ES preprocessor expression parser.
12 */
14 %{
15 //
16 // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved.
17 // Use of this source code is governed by a BSD-style license that can be
18 // found in the LICENSE file.
19 //
21 // This file is auto-generated by generate_parser.sh. DO NOT EDIT!
23 #if defined(__GNUC__)
24 // Triggered by the auto-generated pplval variable.
25 #if !defined(__clang__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7))
26 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
27 #else
28 #pragma GCC diagnostic ignored "-Wuninitialized"
29 #endif
30 #elif defined(_MSC_VER)
31 #pragma warning(disable: 4065 4701)
32 #endif
34 #include "ExpressionParser.h"
36 #include <cassert>
37 #include <sstream>
39 #include "DiagnosticsBase.h"
40 #include "Lexer.h"
41 #include "Token.h"
43 #if defined(_MSC_VER)
44 typedef __int64 YYSTYPE;
45 #else
46 #include <stdint.h>
47 typedef intmax_t YYSTYPE;
48 #endif // _MSC_VER
49 #define YYENABLE_NLS 0
50 #define YYLTYPE_IS_TRIVIAL 1
51 #define YYSTYPE_IS_TRIVIAL 1
52 #define YYSTYPE_IS_DECLARED 1
54 namespace {
55 struct Context
56 {
57 pp::Diagnostics* diagnostics;
58 pp::Lexer* lexer;
59 pp::Token* token;
60 int* result;
61 };
62 } // namespace
63 %}
65 %pure-parser
66 %name-prefix="pp"
67 %parse-param {Context *context}
68 %lex-param {Context *context}
70 %{
71 static int yylex(YYSTYPE* lvalp, Context* context);
72 static void yyerror(Context* context, const char* reason);
73 %}
75 %token TOK_CONST_INT
76 %left TOK_OP_OR
77 %left TOK_OP_AND
78 %left '|'
79 %left '^'
80 %left '&'
81 %left TOK_OP_EQ TOK_OP_NE
82 %left '<' '>' TOK_OP_LE TOK_OP_GE
83 %left TOK_OP_LEFT TOK_OP_RIGHT
84 %left '+' '-'
85 %left '*' '/' '%'
86 %right TOK_UNARY
88 %%
90 input
91 : expression {
92 *(context->result) = static_cast<int>($1);
93 YYACCEPT;
94 }
95 ;
97 expression
98 : TOK_CONST_INT
99 | expression TOK_OP_OR expression {
100 $$ = $1 || $3;
101 }
102 | expression TOK_OP_AND expression {
103 $$ = $1 && $3;
104 }
105 | expression '|' expression {
106 $$ = $1 | $3;
107 }
108 | expression '^' expression {
109 $$ = $1 ^ $3;
110 }
111 | expression '&' expression {
112 $$ = $1 & $3;
113 }
114 | expression TOK_OP_NE expression {
115 $$ = $1 != $3;
116 }
117 | expression TOK_OP_EQ expression {
118 $$ = $1 == $3;
119 }
120 | expression TOK_OP_GE expression {
121 $$ = $1 >= $3;
122 }
123 | expression TOK_OP_LE expression {
124 $$ = $1 <= $3;
125 }
126 | expression '>' expression {
127 $$ = $1 > $3;
128 }
129 | expression '<' expression {
130 $$ = $1 < $3;
131 }
132 | expression TOK_OP_RIGHT expression {
133 $$ = $1 >> $3;
134 }
135 | expression TOK_OP_LEFT expression {
136 $$ = $1 << $3;
137 }
138 | expression '-' expression {
139 $$ = $1 - $3;
140 }
141 | expression '+' expression {
142 $$ = $1 + $3;
143 }
144 | expression '%' expression {
145 if ($3 == 0) {
146 std::ostringstream stream;
147 stream << $1 << " % " << $3;
148 std::string text = stream.str();
149 context->diagnostics->report(pp::Diagnostics::DIVISION_BY_ZERO,
150 context->token->location,
151 text.c_str());
152 YYABORT;
153 } else {
154 $$ = $1 % $3;
155 }
156 }
157 | expression '/' expression {
158 if ($3 == 0) {
159 std::ostringstream stream;
160 stream << $1 << " / " << $3;
161 std::string text = stream.str();
162 context->diagnostics->report(pp::Diagnostics::DIVISION_BY_ZERO,
163 context->token->location,
164 text.c_str());
165 YYABORT;
166 } else {
167 $$ = $1 / $3;
168 }
169 }
170 | expression '*' expression {
171 $$ = $1 * $3;
172 }
173 | '!' expression %prec TOK_UNARY {
174 $$ = ! $2;
175 }
176 | '~' expression %prec TOK_UNARY {
177 $$ = ~ $2;
178 }
179 | '-' expression %prec TOK_UNARY {
180 $$ = - $2;
181 }
182 | '+' expression %prec TOK_UNARY {
183 $$ = + $2;
184 }
185 | '(' expression ')' {
186 $$ = $2;
187 }
188 ;
190 %%
192 int yylex(YYSTYPE* lvalp, Context* context)
193 {
194 int type = 0;
196 pp::Token* token = context->token;
197 switch (token->type)
198 {
199 case pp::Token::CONST_INT:
200 {
201 unsigned int val = 0;
202 if (!token->uValue(&val))
203 {
204 context->diagnostics->report(pp::Diagnostics::INTEGER_OVERFLOW,
205 token->location, token->text);
206 }
207 *lvalp = static_cast<YYSTYPE>(val);
208 type = TOK_CONST_INT;
209 break;
210 }
211 case pp::Token::OP_OR: type = TOK_OP_OR; break;
212 case pp::Token::OP_AND: type = TOK_OP_AND; break;
213 case pp::Token::OP_NE: type = TOK_OP_NE; break;
214 case pp::Token::OP_EQ: type = TOK_OP_EQ; break;
215 case pp::Token::OP_GE: type = TOK_OP_GE; break;
216 case pp::Token::OP_LE: type = TOK_OP_LE; break;
217 case pp::Token::OP_RIGHT: type = TOK_OP_RIGHT; break;
218 case pp::Token::OP_LEFT: type = TOK_OP_LEFT; break;
219 case '|': type = '|'; break;
220 case '^': type = '^'; break;
221 case '&': type = '&'; break;
222 case '>': type = '>'; break;
223 case '<': type = '<'; break;
224 case '-': type = '-'; break;
225 case '+': type = '+'; break;
226 case '%': type = '%'; break;
227 case '/': type = '/'; break;
228 case '*': type = '*'; break;
229 case '!': type = '!'; break;
230 case '~': type = '~'; break;
231 case '(': type = '('; break;
232 case ')': type = ')'; break;
234 default: break;
235 }
237 // Advance to the next token if the current one is valid.
238 if (type != 0) context->lexer->lex(token);
240 return type;
241 }
243 void yyerror(Context* context, const char* reason)
244 {
245 context->diagnostics->report(pp::Diagnostics::INVALID_EXPRESSION,
246 context->token->location,
247 reason);
248 }
250 namespace pp {
252 ExpressionParser::ExpressionParser(Lexer* lexer, Diagnostics* diagnostics) :
253 mLexer(lexer),
254 mDiagnostics(diagnostics)
255 {
256 }
258 bool ExpressionParser::parse(Token* token, int* result)
259 {
260 Context context;
261 context.diagnostics = mDiagnostics;
262 context.lexer = mLexer;
263 context.token = token;
264 context.result = result;
265 int ret = yyparse(&context);
266 switch (ret)
267 {
268 case 0:
269 case 1:
270 break;
272 case 2:
273 mDiagnostics->report(Diagnostics::OUT_OF_MEMORY, token->location, "");
274 break;
276 default:
277 assert(false);
278 mDiagnostics->report(Diagnostics::INTERNAL_ERROR, token->location, "");
279 break;
280 }
282 return ret == 0;
283 }
285 } // namespace pp