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