ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #ifndef V8_TOKEN_H_ |
| 29 | #define V8_TOKEN_H_ |
| 30 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 31 | #include "checks.h" |
| 32 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 33 | namespace v8 { |
| 34 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 35 | |
| 36 | // TOKEN_LIST takes a list of 3 macros M, all of which satisfy the |
| 37 | // same signature M(name, string, precedence), where name is the |
| 38 | // symbolic token name, string is the corresponding syntactic symbol |
| 39 | // (or NULL, for literals), and precedence is the precedence (or 0). |
| 40 | // The parameters are invoked for token categories as follows: |
| 41 | // |
| 42 | // T: Non-keyword tokens |
| 43 | // K: Keyword tokens |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 44 | |
| 45 | // IGNORE_TOKEN is a convenience macro that can be supplied as |
| 46 | // an argument (at any position) for a TOKEN_LIST call. It does |
| 47 | // nothing with tokens belonging to the respective category. |
| 48 | |
| 49 | #define IGNORE_TOKEN(name, string, precedence) |
| 50 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 51 | #define TOKEN_LIST(T, K) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 52 | /* End of source indicator. */ \ |
| 53 | T(EOS, "EOS", 0) \ |
| 54 | \ |
| 55 | /* Punctuators (ECMA-262, section 7.7, page 15). */ \ |
| 56 | T(LPAREN, "(", 0) \ |
| 57 | T(RPAREN, ")", 0) \ |
| 58 | T(LBRACK, "[", 0) \ |
| 59 | T(RBRACK, "]", 0) \ |
| 60 | T(LBRACE, "{", 0) \ |
| 61 | T(RBRACE, "}", 0) \ |
| 62 | T(COLON, ":", 0) \ |
| 63 | T(SEMICOLON, ";", 0) \ |
| 64 | T(PERIOD, ".", 0) \ |
| 65 | T(CONDITIONAL, "?", 3) \ |
| 66 | T(INC, "++", 0) \ |
| 67 | T(DEC, "--", 0) \ |
| 68 | \ |
| 69 | /* Assignment operators. */ \ |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 70 | /* IsAssignmentOp() and Assignment::is_compound() relies on */ \ |
| 71 | /* this block of enum values being contiguous and sorted in the */ \ |
| 72 | /* same order! */ \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 73 | T(INIT_VAR, "=init_var", 2) /* AST-use only. */ \ |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 74 | T(INIT_LET, "=init_let", 2) /* AST-use only. */ \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 75 | T(INIT_CONST, "=init_const", 2) /* AST-use only. */ \ |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 76 | T(INIT_CONST_HARMONY, "=init_const_harmony", 2) /* AST-use only. */ \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 77 | T(ASSIGN, "=", 2) \ |
| 78 | T(ASSIGN_BIT_OR, "|=", 2) \ |
| 79 | T(ASSIGN_BIT_XOR, "^=", 2) \ |
| 80 | T(ASSIGN_BIT_AND, "&=", 2) \ |
| 81 | T(ASSIGN_SHL, "<<=", 2) \ |
| 82 | T(ASSIGN_SAR, ">>=", 2) \ |
| 83 | T(ASSIGN_SHR, ">>>=", 2) \ |
| 84 | T(ASSIGN_ADD, "+=", 2) \ |
| 85 | T(ASSIGN_SUB, "-=", 2) \ |
| 86 | T(ASSIGN_MUL, "*=", 2) \ |
| 87 | T(ASSIGN_DIV, "/=", 2) \ |
| 88 | T(ASSIGN_MOD, "%=", 2) \ |
| 89 | \ |
| 90 | /* Binary operators sorted by precedence. */ \ |
| 91 | /* IsBinaryOp() relies on this block of enum values */ \ |
| 92 | /* being contiguous and sorted in the same order! */ \ |
| 93 | T(COMMA, ",", 1) \ |
| 94 | T(OR, "||", 4) \ |
| 95 | T(AND, "&&", 5) \ |
| 96 | T(BIT_OR, "|", 6) \ |
| 97 | T(BIT_XOR, "^", 7) \ |
| 98 | T(BIT_AND, "&", 8) \ |
| 99 | T(SHL, "<<", 11) \ |
| 100 | T(SAR, ">>", 11) \ |
| 101 | T(SHR, ">>>", 11) \ |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 102 | T(ROR, "rotate right", 11) /* only used by Crankshaft */ \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 103 | T(ADD, "+", 12) \ |
| 104 | T(SUB, "-", 12) \ |
| 105 | T(MUL, "*", 13) \ |
| 106 | T(DIV, "/", 13) \ |
| 107 | T(MOD, "%", 13) \ |
| 108 | \ |
| 109 | /* Compare operators sorted by precedence. */ \ |
| 110 | /* IsCompareOp() relies on this block of enum values */ \ |
| 111 | /* being contiguous and sorted in the same order! */ \ |
| 112 | T(EQ, "==", 9) \ |
| 113 | T(NE, "!=", 9) \ |
| 114 | T(EQ_STRICT, "===", 9) \ |
| 115 | T(NE_STRICT, "!==", 9) \ |
| 116 | T(LT, "<", 10) \ |
| 117 | T(GT, ">", 10) \ |
| 118 | T(LTE, "<=", 10) \ |
| 119 | T(GTE, ">=", 10) \ |
| 120 | K(INSTANCEOF, "instanceof", 10) \ |
| 121 | K(IN, "in", 10) \ |
| 122 | \ |
| 123 | /* Unary operators. */ \ |
| 124 | /* IsUnaryOp() relies on this block of enum values */ \ |
| 125 | /* being contiguous and sorted in the same order! */ \ |
| 126 | T(NOT, "!", 0) \ |
| 127 | T(BIT_NOT, "~", 0) \ |
| 128 | K(DELETE, "delete", 0) \ |
| 129 | K(TYPEOF, "typeof", 0) \ |
| 130 | K(VOID, "void", 0) \ |
| 131 | \ |
| 132 | /* Keywords (ECMA-262, section 7.5.2, page 13). */ \ |
| 133 | K(BREAK, "break", 0) \ |
| 134 | K(CASE, "case", 0) \ |
| 135 | K(CATCH, "catch", 0) \ |
| 136 | K(CONTINUE, "continue", 0) \ |
| 137 | K(DEBUGGER, "debugger", 0) \ |
| 138 | K(DEFAULT, "default", 0) \ |
| 139 | /* DELETE */ \ |
| 140 | K(DO, "do", 0) \ |
| 141 | K(ELSE, "else", 0) \ |
| 142 | K(FINALLY, "finally", 0) \ |
| 143 | K(FOR, "for", 0) \ |
| 144 | K(FUNCTION, "function", 0) \ |
| 145 | K(IF, "if", 0) \ |
| 146 | /* IN */ \ |
| 147 | /* INSTANCEOF */ \ |
| 148 | K(NEW, "new", 0) \ |
| 149 | K(RETURN, "return", 0) \ |
| 150 | K(SWITCH, "switch", 0) \ |
| 151 | K(THIS, "this", 0) \ |
| 152 | K(THROW, "throw", 0) \ |
| 153 | K(TRY, "try", 0) \ |
| 154 | /* TYPEOF */ \ |
| 155 | K(VAR, "var", 0) \ |
| 156 | /* VOID */ \ |
| 157 | K(WHILE, "while", 0) \ |
| 158 | K(WITH, "with", 0) \ |
| 159 | \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 160 | /* Literals (ECMA-262, section 7.8, page 16). */ \ |
| 161 | K(NULL_LITERAL, "null", 0) \ |
| 162 | K(TRUE_LITERAL, "true", 0) \ |
| 163 | K(FALSE_LITERAL, "false", 0) \ |
| 164 | T(NUMBER, NULL, 0) \ |
| 165 | T(STRING, NULL, 0) \ |
| 166 | \ |
| 167 | /* Identifiers (not keywords or future reserved words). */ \ |
| 168 | T(IDENTIFIER, NULL, 0) \ |
| 169 | \ |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 170 | /* Future reserved words (ECMA-262, section 7.6.1.2). */ \ |
| 171 | T(FUTURE_RESERVED_WORD, NULL, 0) \ |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 172 | T(FUTURE_STRICT_RESERVED_WORD, NULL, 0) \ |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 173 | K(CONST, "const", 0) \ |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 174 | K(EXPORT, "export", 0) \ |
| 175 | K(IMPORT, "import", 0) \ |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 176 | K(LET, "let", 0) \ |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 177 | K(YIELD, "yield", 0) \ |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 178 | \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 179 | /* Illegal token - not able to scan. */ \ |
| 180 | T(ILLEGAL, "ILLEGAL", 0) \ |
| 181 | \ |
| 182 | /* Scanner-internal use only. */ \ |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 183 | T(WHITESPACE, NULL, 0) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 184 | |
| 185 | |
| 186 | class Token { |
| 187 | public: |
| 188 | // All token values. |
| 189 | #define T(name, string, precedence) name, |
| 190 | enum Value { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 191 | TOKEN_LIST(T, T) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 192 | NUM_TOKENS |
| 193 | }; |
| 194 | #undef T |
| 195 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 196 | // Returns a string corresponding to the C++ token name |
| 197 | // (e.g. "LT" for the token LT). |
| 198 | static const char* Name(Value tok) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 199 | ASSERT(tok < NUM_TOKENS); // tok is unsigned |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 200 | return name_[tok]; |
| 201 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 202 | |
| 203 | // Predicates |
sgjesse@chromium.org | d3b3be0 | 2010-08-06 08:09:27 +0000 | [diff] [blame] | 204 | static bool IsKeyword(Value tok) { |
| 205 | return token_type[tok] == 'K'; |
| 206 | } |
| 207 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 208 | static bool IsAssignmentOp(Value tok) { |
| 209 | return INIT_VAR <= tok && tok <= ASSIGN_MOD; |
| 210 | } |
| 211 | |
| 212 | static bool IsBinaryOp(Value op) { |
| 213 | return COMMA <= op && op <= MOD; |
| 214 | } |
| 215 | |
| 216 | static bool IsCompareOp(Value op) { |
| 217 | return EQ <= op && op <= IN; |
| 218 | } |
| 219 | |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 220 | static bool IsOrderedRelationalCompareOp(Value op) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 221 | return op == LT || op == LTE || op == GT || op == GTE; |
| 222 | } |
| 223 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 224 | static bool IsEqualityOp(Value op) { |
| 225 | return op == EQ || op == EQ_STRICT; |
| 226 | } |
| 227 | |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 228 | static bool IsInequalityOp(Value op) { |
| 229 | return op == NE || op == NE_STRICT; |
| 230 | } |
| 231 | |
| 232 | static bool IsArithmeticCompareOp(Value op) { |
| 233 | return IsOrderedRelationalCompareOp(op) || |
| 234 | IsEqualityOp(op) || IsInequalityOp(op); |
| 235 | } |
| 236 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 237 | static Value NegateCompareOp(Value op) { |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 238 | ASSERT(IsArithmeticCompareOp(op)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 239 | switch (op) { |
| 240 | case EQ: return NE; |
| 241 | case NE: return EQ; |
| 242 | case EQ_STRICT: return NE_STRICT; |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 243 | case NE_STRICT: return EQ_STRICT; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 244 | case LT: return GTE; |
| 245 | case GT: return LTE; |
| 246 | case LTE: return GT; |
| 247 | case GTE: return LT; |
| 248 | default: |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 249 | UNREACHABLE(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 250 | return op; |
| 251 | } |
| 252 | } |
| 253 | |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 254 | static Value ReverseCompareOp(Value op) { |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 255 | ASSERT(IsArithmeticCompareOp(op)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 256 | switch (op) { |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 257 | case EQ: return EQ; |
| 258 | case NE: return NE; |
| 259 | case EQ_STRICT: return EQ_STRICT; |
| 260 | case NE_STRICT: return NE_STRICT; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 261 | case LT: return GT; |
| 262 | case GT: return LT; |
| 263 | case LTE: return GTE; |
| 264 | case GTE: return LTE; |
| 265 | default: |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 266 | UNREACHABLE(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 267 | return op; |
| 268 | } |
| 269 | } |
| 270 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 271 | static bool IsBitOp(Value op) { |
| 272 | return (BIT_OR <= op && op <= SHR) || op == BIT_NOT; |
| 273 | } |
| 274 | |
| 275 | static bool IsUnaryOp(Value op) { |
| 276 | return (NOT <= op && op <= VOID) || op == ADD || op == SUB; |
| 277 | } |
| 278 | |
| 279 | static bool IsCountOp(Value op) { |
| 280 | return op == INC || op == DEC; |
| 281 | } |
| 282 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 283 | static bool IsShiftOp(Value op) { |
| 284 | return (SHL <= op) && (op <= SHR); |
| 285 | } |
| 286 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 287 | // Returns a string corresponding to the JS token string |
| 288 | // (.e., "<" for the token LT) or NULL if the token doesn't |
| 289 | // have a (unique) string (e.g. an IDENTIFIER). |
| 290 | static const char* String(Value tok) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 291 | ASSERT(tok < NUM_TOKENS); // tok is unsigned. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 292 | return string_[tok]; |
| 293 | } |
| 294 | |
| 295 | // Returns the precedence > 0 for binary and compare |
| 296 | // operators; returns 0 otherwise. |
| 297 | static int Precedence(Value tok) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 298 | ASSERT(tok < NUM_TOKENS); // tok is unsigned. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 299 | return precedence_[tok]; |
| 300 | } |
| 301 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 302 | private: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 303 | static const char* const name_[NUM_TOKENS]; |
| 304 | static const char* const string_[NUM_TOKENS]; |
| 305 | static const int8_t precedence_[NUM_TOKENS]; |
sgjesse@chromium.org | d3b3be0 | 2010-08-06 08:09:27 +0000 | [diff] [blame] | 306 | static const char token_type[NUM_TOKENS]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 307 | }; |
| 308 | |
| 309 | } } // namespace v8::internal |
| 310 | |
| 311 | #endif // V8_TOKEN_H_ |