alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 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 | // |
| 7 | |
| 8 | This file contains the Yacc grammar for GLSL ES preprocessor expression. |
| 9 | |
| 10 | IF YOU MODIFY THIS FILE YOU ALSO NEED TO RUN generate_parser.sh, |
| 11 | WHICH GENERATES THE GLSL ES preprocessor expression parser. |
| 12 | */ |
| 13 | |
| 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 | // |
| 20 | |
| 21 | // This file is auto-generated by generate_parser.sh. DO NOT EDIT! |
| 22 | |
| 23 | #if defined(__GNUC__) |
maxvujovic@gmail.com | c6b3b3c | 2012-06-27 22:49:39 +0000 | [diff] [blame] | 24 | // Triggered by the auto-generated pplval variable. |
shannon.woods@transgaming.com | eb68fd0 | 2013-02-28 23:20:01 +0000 | [diff] [blame] | 25 | #if !defined(__clang__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) |
daniel@transgaming.com | b3077d0 | 2013-01-11 04:12:09 +0000 | [diff] [blame] | 26 | #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" |
shannon.woods@transgaming.com | eb68fd0 | 2013-02-28 23:20:01 +0000 | [diff] [blame] | 27 | #else |
| 28 | #pragma GCC diagnostic ignored "-Wuninitialized" |
shannon.woods@transgaming.com | e36fddf | 2013-01-25 21:57:50 +0000 | [diff] [blame] | 29 | #endif |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 30 | #elif defined(_MSC_VER) |
Minmin Gong | 794e000 | 2015-04-07 18:31:54 -0700 | [diff] [blame] | 31 | #pragma warning(disable: 4065 4244 4701 4702) |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 32 | #endif |
| 33 | |
| 34 | #include "ExpressionParser.h" |
| 35 | |
Jamie Madill | a738f08 | 2013-11-01 17:45:04 -0400 | [diff] [blame] | 36 | #if defined(_MSC_VER) |
Jamie Madill | d7f2135 | 2013-10-30 17:53:15 -0400 | [diff] [blame] | 37 | #include <malloc.h> |
Jamie Madill | a738f08 | 2013-11-01 17:45:04 -0400 | [diff] [blame] | 38 | #else |
| 39 | #include <stdlib.h> |
| 40 | #endif |
| 41 | |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 42 | #include <cassert> |
| 43 | #include <sstream> |
| 44 | |
daniel@transgaming.com | b3077d0 | 2013-01-11 04:12:09 +0000 | [diff] [blame] | 45 | #include "DiagnosticsBase.h" |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 46 | #include "Lexer.h" |
| 47 | #include "Token.h" |
| 48 | |
alokp@chromium.org | d39ec4c | 2012-06-26 04:37:55 +0000 | [diff] [blame] | 49 | #if defined(_MSC_VER) |
| 50 | typedef __int64 YYSTYPE; |
alokp@chromium.org | d39ec4c | 2012-06-26 04:37:55 +0000 | [diff] [blame] | 51 | #else |
| 52 | #include <stdint.h> |
| 53 | typedef intmax_t YYSTYPE; |
| 54 | #endif // _MSC_VER |
Olli Etuaho | 3187a38 | 2015-09-09 12:00:12 +0300 | [diff] [blame] | 55 | |
daniel@transgaming.com | b3077d0 | 2013-01-11 04:12:09 +0000 | [diff] [blame] | 56 | #define YYENABLE_NLS 0 |
| 57 | #define YYLTYPE_IS_TRIVIAL 1 |
alokp@chromium.org | d39ec4c | 2012-06-26 04:37:55 +0000 | [diff] [blame] | 58 | #define YYSTYPE_IS_TRIVIAL 1 |
| 59 | #define YYSTYPE_IS_DECLARED 1 |
| 60 | |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 61 | namespace { |
| 62 | struct Context |
| 63 | { |
alokp@chromium.org | 2c958ee | 2012-05-17 20:35:42 +0000 | [diff] [blame] | 64 | pp::Diagnostics* diagnostics; |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 65 | pp::Lexer* lexer; |
| 66 | pp::Token* token; |
| 67 | int* result; |
Olli Etuaho | 247374c | 2015-09-09 15:07:24 +0300 | [diff] [blame] | 68 | bool parsePresetToken; |
| 69 | |
| 70 | pp::ExpressionParser::ErrorSettings errorSettings; |
| 71 | bool *valid; |
Olli Etuaho | 809ec54 | 2015-08-26 14:30:57 +0300 | [diff] [blame] | 72 | |
| 73 | void startIgnoreErrors() { ++ignoreErrors; } |
| 74 | void endIgnoreErrors() { --ignoreErrors; } |
| 75 | |
| 76 | bool isIgnoringErrors() { return ignoreErrors > 0; } |
| 77 | |
| 78 | int ignoreErrors; |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 79 | }; |
| 80 | } // namespace |
| 81 | %} |
| 82 | |
maxvujovic@gmail.com | e640ef8 | 2012-07-13 18:42:40 +0000 | [diff] [blame] | 83 | %pure-parser |
Jamie Madill | 185de88 | 2014-12-22 15:17:52 -0500 | [diff] [blame] | 84 | %name-prefix "pp" |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 85 | %parse-param {Context *context} |
| 86 | %lex-param {Context *context} |
| 87 | |
| 88 | %{ |
alokp@chromium.org | d39ec4c | 2012-06-26 04:37:55 +0000 | [diff] [blame] | 89 | static int yylex(YYSTYPE* lvalp, Context* context); |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 90 | static void yyerror(Context* context, const char* reason); |
| 91 | %} |
| 92 | |
maxvujovic@gmail.com | e640ef8 | 2012-07-13 18:42:40 +0000 | [diff] [blame] | 93 | %token TOK_CONST_INT |
Olli Etuaho | 809ec54 | 2015-08-26 14:30:57 +0300 | [diff] [blame] | 94 | %token TOK_IDENTIFIER |
maxvujovic@gmail.com | e640ef8 | 2012-07-13 18:42:40 +0000 | [diff] [blame] | 95 | %left TOK_OP_OR |
| 96 | %left TOK_OP_AND |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 97 | %left '|' |
| 98 | %left '^' |
| 99 | %left '&' |
maxvujovic@gmail.com | e640ef8 | 2012-07-13 18:42:40 +0000 | [diff] [blame] | 100 | %left TOK_OP_EQ TOK_OP_NE |
| 101 | %left '<' '>' TOK_OP_LE TOK_OP_GE |
| 102 | %left TOK_OP_LEFT TOK_OP_RIGHT |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 103 | %left '+' '-' |
| 104 | %left '*' '/' '%' |
maxvujovic@gmail.com | e640ef8 | 2012-07-13 18:42:40 +0000 | [diff] [blame] | 105 | %right TOK_UNARY |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 106 | |
| 107 | %% |
| 108 | |
| 109 | input |
| 110 | : expression { |
alokp@chromium.org | d39ec4c | 2012-06-26 04:37:55 +0000 | [diff] [blame] | 111 | *(context->result) = static_cast<int>($1); |
Olli Etuaho | 3187a38 | 2015-09-09 12:00:12 +0300 | [diff] [blame] | 112 | YYACCEPT; |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 113 | } |
| 114 | ; |
| 115 | |
| 116 | expression |
maxvujovic@gmail.com | e640ef8 | 2012-07-13 18:42:40 +0000 | [diff] [blame] | 117 | : TOK_CONST_INT |
Olli Etuaho | 809ec54 | 2015-08-26 14:30:57 +0300 | [diff] [blame] | 118 | | TOK_IDENTIFIER { |
| 119 | if (!context->isIgnoringErrors()) |
| 120 | { |
Olli Etuaho | 3187a38 | 2015-09-09 12:00:12 +0300 | [diff] [blame] | 121 | // This rule should be applied right after the token is lexed, so we can |
| 122 | // refer to context->token in the error message. |
Olli Etuaho | 247374c | 2015-09-09 15:07:24 +0300 | [diff] [blame] | 123 | context->diagnostics->report(context->errorSettings.unexpectedIdentifier, |
Olli Etuaho | 3187a38 | 2015-09-09 12:00:12 +0300 | [diff] [blame] | 124 | context->token->location, context->token->text); |
Olli Etuaho | 247374c | 2015-09-09 15:07:24 +0300 | [diff] [blame] | 125 | *(context->valid) = false; |
Olli Etuaho | 809ec54 | 2015-08-26 14:30:57 +0300 | [diff] [blame] | 126 | } |
Olli Etuaho | 3187a38 | 2015-09-09 12:00:12 +0300 | [diff] [blame] | 127 | $$ = $1; |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 128 | } |
Olli Etuaho | 809ec54 | 2015-08-26 14:30:57 +0300 | [diff] [blame] | 129 | | expression TOK_OP_OR { |
| 130 | if ($1 != 0) |
| 131 | { |
| 132 | // Ignore errors in the short-circuited part of the expression. |
| 133 | // ESSL3.00 section 3.4: |
| 134 | // If an operand is not evaluated, the presence of undefined identifiers |
| 135 | // in the operand will not cause an error. |
| 136 | // Unevaluated division by zero should not cause an error either. |
| 137 | context->startIgnoreErrors(); |
| 138 | } |
| 139 | } expression { |
| 140 | if ($1 != 0) |
| 141 | { |
| 142 | context->endIgnoreErrors(); |
| 143 | $$ = static_cast<YYSTYPE>(1); |
| 144 | } |
| 145 | else |
| 146 | { |
| 147 | $$ = $1 || $4; |
| 148 | } |
| 149 | } |
| 150 | | expression TOK_OP_AND { |
| 151 | if ($1 == 0) |
| 152 | { |
| 153 | // Ignore errors in the short-circuited part of the expression. |
| 154 | // ESSL3.00 section 3.4: |
| 155 | // If an operand is not evaluated, the presence of undefined identifiers |
| 156 | // in the operand will not cause an error. |
| 157 | // Unevaluated division by zero should not cause an error either. |
| 158 | context->startIgnoreErrors(); |
| 159 | } |
| 160 | } expression { |
| 161 | if ($1 == 0) |
| 162 | { |
| 163 | context->endIgnoreErrors(); |
| 164 | $$ = static_cast<YYSTYPE>(0); |
| 165 | } |
| 166 | else |
| 167 | { |
| 168 | $$ = $1 && $4; |
| 169 | } |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 170 | } |
| 171 | | expression '|' expression { |
| 172 | $$ = $1 | $3; |
| 173 | } |
| 174 | | expression '^' expression { |
| 175 | $$ = $1 ^ $3; |
| 176 | } |
| 177 | | expression '&' expression { |
| 178 | $$ = $1 & $3; |
| 179 | } |
maxvujovic@gmail.com | e640ef8 | 2012-07-13 18:42:40 +0000 | [diff] [blame] | 180 | | expression TOK_OP_NE expression { |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 181 | $$ = $1 != $3; |
| 182 | } |
maxvujovic@gmail.com | e640ef8 | 2012-07-13 18:42:40 +0000 | [diff] [blame] | 183 | | expression TOK_OP_EQ expression { |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 184 | $$ = $1 == $3; |
| 185 | } |
maxvujovic@gmail.com | e640ef8 | 2012-07-13 18:42:40 +0000 | [diff] [blame] | 186 | | expression TOK_OP_GE expression { |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 187 | $$ = $1 >= $3; |
| 188 | } |
maxvujovic@gmail.com | e640ef8 | 2012-07-13 18:42:40 +0000 | [diff] [blame] | 189 | | expression TOK_OP_LE expression { |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 190 | $$ = $1 <= $3; |
| 191 | } |
| 192 | | expression '>' expression { |
| 193 | $$ = $1 > $3; |
| 194 | } |
| 195 | | expression '<' expression { |
| 196 | $$ = $1 < $3; |
| 197 | } |
maxvujovic@gmail.com | e640ef8 | 2012-07-13 18:42:40 +0000 | [diff] [blame] | 198 | | expression TOK_OP_RIGHT expression { |
Geoff Lang | d4a07fb | 2016-09-20 10:24:23 -0400 | [diff] [blame] | 199 | if ($2 < 0 || $3 < 0) |
Jamie Madill | 461e3af | 2016-07-21 18:15:34 -0400 | [diff] [blame] | 200 | { |
| 201 | if (!context->isIgnoringErrors()) |
| 202 | { |
| 203 | std::ostringstream stream; |
| 204 | stream << $1 << " >> " << $3; |
| 205 | std::string text = stream.str(); |
| 206 | context->diagnostics->report(pp::Diagnostics::PP_UNDEFINED_SHIFT, |
| 207 | context->token->location, |
| 208 | text.c_str()); |
| 209 | *(context->valid) = false; |
| 210 | } |
| 211 | $$ = static_cast<YYSTYPE>(0); |
| 212 | } |
| 213 | else |
| 214 | { |
| 215 | $$ = $1 >> $3; |
| 216 | } |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 217 | } |
maxvujovic@gmail.com | e640ef8 | 2012-07-13 18:42:40 +0000 | [diff] [blame] | 218 | | expression TOK_OP_LEFT expression { |
Geoff Lang | d4a07fb | 2016-09-20 10:24:23 -0400 | [diff] [blame] | 219 | if ($2 < 0 || $3 < 0) |
Jamie Madill | 461e3af | 2016-07-21 18:15:34 -0400 | [diff] [blame] | 220 | { |
| 221 | if (!context->isIgnoringErrors()) |
| 222 | { |
| 223 | std::ostringstream stream; |
| 224 | stream << $1 << " << " << $3; |
| 225 | std::string text = stream.str(); |
| 226 | context->diagnostics->report(pp::Diagnostics::PP_UNDEFINED_SHIFT, |
| 227 | context->token->location, |
| 228 | text.c_str()); |
| 229 | *(context->valid) = false; |
| 230 | } |
| 231 | $$ = static_cast<YYSTYPE>(0); |
| 232 | } |
| 233 | else |
| 234 | { |
| 235 | $$ = $1 << $3; |
| 236 | } |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 237 | } |
| 238 | | expression '-' expression { |
| 239 | $$ = $1 - $3; |
| 240 | } |
| 241 | | expression '+' expression { |
| 242 | $$ = $1 + $3; |
| 243 | } |
| 244 | | expression '%' expression { |
Olli Etuaho | 809ec54 | 2015-08-26 14:30:57 +0300 | [diff] [blame] | 245 | if ($3 == 0) |
| 246 | { |
Olli Etuaho | 3187a38 | 2015-09-09 12:00:12 +0300 | [diff] [blame] | 247 | if (!context->isIgnoringErrors()) |
Olli Etuaho | 809ec54 | 2015-08-26 14:30:57 +0300 | [diff] [blame] | 248 | { |
| 249 | std::ostringstream stream; |
| 250 | stream << $1 << " % " << $3; |
| 251 | std::string text = stream.str(); |
| 252 | context->diagnostics->report(pp::Diagnostics::PP_DIVISION_BY_ZERO, |
| 253 | context->token->location, |
| 254 | text.c_str()); |
Olli Etuaho | 247374c | 2015-09-09 15:07:24 +0300 | [diff] [blame] | 255 | *(context->valid) = false; |
Olli Etuaho | 809ec54 | 2015-08-26 14:30:57 +0300 | [diff] [blame] | 256 | } |
Olli Etuaho | 3187a38 | 2015-09-09 12:00:12 +0300 | [diff] [blame] | 257 | $$ = static_cast<YYSTYPE>(0); |
Olli Etuaho | 809ec54 | 2015-08-26 14:30:57 +0300 | [diff] [blame] | 258 | } |
| 259 | else |
| 260 | { |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 261 | $$ = $1 % $3; |
| 262 | } |
| 263 | } |
| 264 | | expression '/' expression { |
Olli Etuaho | 809ec54 | 2015-08-26 14:30:57 +0300 | [diff] [blame] | 265 | if ($3 == 0) |
| 266 | { |
Olli Etuaho | 3187a38 | 2015-09-09 12:00:12 +0300 | [diff] [blame] | 267 | if (!context->isIgnoringErrors()) |
Olli Etuaho | 809ec54 | 2015-08-26 14:30:57 +0300 | [diff] [blame] | 268 | { |
| 269 | std::ostringstream stream; |
| 270 | stream << $1 << " / " << $3; |
| 271 | std::string text = stream.str(); |
| 272 | context->diagnostics->report(pp::Diagnostics::PP_DIVISION_BY_ZERO, |
| 273 | context->token->location, |
| 274 | text.c_str()); |
Olli Etuaho | 247374c | 2015-09-09 15:07:24 +0300 | [diff] [blame] | 275 | *(context->valid) = false; |
Olli Etuaho | 809ec54 | 2015-08-26 14:30:57 +0300 | [diff] [blame] | 276 | } |
Olli Etuaho | 3187a38 | 2015-09-09 12:00:12 +0300 | [diff] [blame] | 277 | $$ = static_cast<YYSTYPE>(0); |
Olli Etuaho | 809ec54 | 2015-08-26 14:30:57 +0300 | [diff] [blame] | 278 | } |
| 279 | else |
| 280 | { |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 281 | $$ = $1 / $3; |
| 282 | } |
| 283 | } |
| 284 | | expression '*' expression { |
| 285 | $$ = $1 * $3; |
| 286 | } |
maxvujovic@gmail.com | e640ef8 | 2012-07-13 18:42:40 +0000 | [diff] [blame] | 287 | | '!' expression %prec TOK_UNARY { |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 288 | $$ = ! $2; |
| 289 | } |
maxvujovic@gmail.com | e640ef8 | 2012-07-13 18:42:40 +0000 | [diff] [blame] | 290 | | '~' expression %prec TOK_UNARY { |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 291 | $$ = ~ $2; |
| 292 | } |
maxvujovic@gmail.com | e640ef8 | 2012-07-13 18:42:40 +0000 | [diff] [blame] | 293 | | '-' expression %prec TOK_UNARY { |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 294 | $$ = - $2; |
| 295 | } |
maxvujovic@gmail.com | e640ef8 | 2012-07-13 18:42:40 +0000 | [diff] [blame] | 296 | | '+' expression %prec TOK_UNARY { |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 297 | $$ = + $2; |
| 298 | } |
| 299 | | '(' expression ')' { |
| 300 | $$ = $2; |
| 301 | } |
| 302 | ; |
| 303 | |
| 304 | %% |
| 305 | |
Zhenyao Mo | d526f98 | 2014-05-13 14:51:19 -0700 | [diff] [blame] | 306 | int yylex(YYSTYPE *lvalp, Context *context) |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 307 | { |
Olli Etuaho | 3187a38 | 2015-09-09 12:00:12 +0300 | [diff] [blame] | 308 | pp::Token *token = context->token; |
Olli Etuaho | 247374c | 2015-09-09 15:07:24 +0300 | [diff] [blame] | 309 | if (!context->parsePresetToken) |
| 310 | { |
| 311 | context->lexer->lex(token); |
| 312 | } |
| 313 | context->parsePresetToken = false; |
Olli Etuaho | 3187a38 | 2015-09-09 12:00:12 +0300 | [diff] [blame] | 314 | |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 315 | int type = 0; |
| 316 | |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 317 | switch (token->type) |
| 318 | { |
Zhenyao Mo | d526f98 | 2014-05-13 14:51:19 -0700 | [diff] [blame] | 319 | case pp::Token::CONST_INT: { |
alokp@chromium.org | 2e81891 | 2012-06-29 21:26:03 +0000 | [diff] [blame] | 320 | unsigned int val = 0; |
Olli Etuaho | 247374c | 2015-09-09 15:07:24 +0300 | [diff] [blame] | 321 | int testVal = 0; |
| 322 | if (!token->uValue(&val) || (!token->iValue(&testVal) && |
| 323 | context->errorSettings.integerLiteralsMustFit32BitSignedRange)) |
alokp@chromium.org | 2e81891 | 2012-06-29 21:26:03 +0000 | [diff] [blame] | 324 | { |
Jamie Madill | c9f140d | 2014-02-18 15:27:21 -0500 | [diff] [blame] | 325 | context->diagnostics->report(pp::Diagnostics::PP_INTEGER_OVERFLOW, |
alokp@chromium.org | 2e81891 | 2012-06-29 21:26:03 +0000 | [diff] [blame] | 326 | token->location, token->text); |
Olli Etuaho | 247374c | 2015-09-09 15:07:24 +0300 | [diff] [blame] | 327 | *(context->valid) = false; |
alokp@chromium.org | 2e81891 | 2012-06-29 21:26:03 +0000 | [diff] [blame] | 328 | } |
| 329 | *lvalp = static_cast<YYSTYPE>(val); |
maxvujovic@gmail.com | e640ef8 | 2012-07-13 18:42:40 +0000 | [diff] [blame] | 330 | type = TOK_CONST_INT; |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 331 | break; |
alokp@chromium.org | 2e81891 | 2012-06-29 21:26:03 +0000 | [diff] [blame] | 332 | } |
Olli Etuaho | 809ec54 | 2015-08-26 14:30:57 +0300 | [diff] [blame] | 333 | case pp::Token::IDENTIFIER: |
Olli Etuaho | 809ec54 | 2015-08-26 14:30:57 +0300 | [diff] [blame] | 334 | *lvalp = static_cast<YYSTYPE>(-1); |
| 335 | type = TOK_IDENTIFIER; |
| 336 | break; |
Zhenyao Mo | d526f98 | 2014-05-13 14:51:19 -0700 | [diff] [blame] | 337 | case pp::Token::OP_OR: |
| 338 | type = TOK_OP_OR; |
| 339 | break; |
| 340 | case pp::Token::OP_AND: |
| 341 | type = TOK_OP_AND; |
| 342 | break; |
| 343 | case pp::Token::OP_NE: |
| 344 | type = TOK_OP_NE; |
| 345 | break; |
| 346 | case pp::Token::OP_EQ: |
| 347 | type = TOK_OP_EQ; |
| 348 | break; |
| 349 | case pp::Token::OP_GE: |
| 350 | type = TOK_OP_GE; |
| 351 | break; |
| 352 | case pp::Token::OP_LE: |
| 353 | type = TOK_OP_LE; |
| 354 | break; |
| 355 | case pp::Token::OP_RIGHT: |
| 356 | type = TOK_OP_RIGHT; |
| 357 | break; |
| 358 | case pp::Token::OP_LEFT: |
| 359 | type = TOK_OP_LEFT; |
| 360 | break; |
| 361 | case '|': |
| 362 | case '^': |
| 363 | case '&': |
| 364 | case '>': |
| 365 | case '<': |
| 366 | case '-': |
| 367 | case '+': |
| 368 | case '%': |
| 369 | case '/': |
| 370 | case '*': |
| 371 | case '!': |
| 372 | case '~': |
| 373 | case '(': |
| 374 | case ')': |
| 375 | type = token->type; |
| 376 | break; |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 377 | |
Zhenyao Mo | d526f98 | 2014-05-13 14:51:19 -0700 | [diff] [blame] | 378 | default: |
| 379 | break; |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 380 | } |
| 381 | |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 382 | return type; |
| 383 | } |
| 384 | |
Zhenyao Mo | d526f98 | 2014-05-13 14:51:19 -0700 | [diff] [blame] | 385 | void yyerror(Context *context, const char *reason) |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 386 | { |
Jamie Madill | c9f140d | 2014-02-18 15:27:21 -0500 | [diff] [blame] | 387 | context->diagnostics->report(pp::Diagnostics::PP_INVALID_EXPRESSION, |
alokp@chromium.org | 2c958ee | 2012-05-17 20:35:42 +0000 | [diff] [blame] | 388 | context->token->location, |
| 389 | reason); |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | namespace pp { |
| 393 | |
Zhenyao Mo | d526f98 | 2014-05-13 14:51:19 -0700 | [diff] [blame] | 394 | ExpressionParser::ExpressionParser(Lexer *lexer, Diagnostics *diagnostics) |
| 395 | : mLexer(lexer), |
| 396 | mDiagnostics(diagnostics) |
alokp@chromium.org | 2c958ee | 2012-05-17 20:35:42 +0000 | [diff] [blame] | 397 | { |
| 398 | } |
| 399 | |
Olli Etuaho | 247374c | 2015-09-09 15:07:24 +0300 | [diff] [blame] | 400 | bool ExpressionParser::parse(Token *token, |
| 401 | int *result, |
| 402 | bool parsePresetToken, |
| 403 | const ErrorSettings &errorSettings, |
| 404 | bool *valid) |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 405 | { |
| 406 | Context context; |
alokp@chromium.org | 2c958ee | 2012-05-17 20:35:42 +0000 | [diff] [blame] | 407 | context.diagnostics = mDiagnostics; |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 408 | context.lexer = mLexer; |
| 409 | context.token = token; |
| 410 | context.result = result; |
Olli Etuaho | 809ec54 | 2015-08-26 14:30:57 +0300 | [diff] [blame] | 411 | context.ignoreErrors = 0; |
Olli Etuaho | 247374c | 2015-09-09 15:07:24 +0300 | [diff] [blame] | 412 | context.parsePresetToken = parsePresetToken; |
| 413 | context.errorSettings = errorSettings; |
| 414 | context.valid = valid; |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 415 | int ret = yyparse(&context); |
| 416 | switch (ret) |
| 417 | { |
| 418 | case 0: |
| 419 | case 1: |
| 420 | break; |
| 421 | |
| 422 | case 2: |
Jamie Madill | c9f140d | 2014-02-18 15:27:21 -0500 | [diff] [blame] | 423 | mDiagnostics->report(Diagnostics::PP_OUT_OF_MEMORY, token->location, ""); |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 424 | break; |
| 425 | |
| 426 | default: |
| 427 | assert(false); |
Jamie Madill | c9f140d | 2014-02-18 15:27:21 -0500 | [diff] [blame] | 428 | mDiagnostics->report(Diagnostics::PP_INTERNAL_ERROR, token->location, ""); |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 429 | break; |
| 430 | } |
alokp@chromium.org | 2c958ee | 2012-05-17 20:35:42 +0000 | [diff] [blame] | 431 | |
alokp@chromium.org | 04d7d22 | 2012-05-16 19:24:07 +0000 | [diff] [blame] | 432 | return ret == 0; |
| 433 | } |
| 434 | |
| 435 | } // namespace pp |