| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#ifndef frontend_TokenKind_h |
| 8 |
#define frontend_TokenKind_h |
| 9 |
|
| 10 |
#include <stdint.h> |
| 11 |
|
| 12 |
#include "js/TypeDecls.h" // IF_DECORATORS |
| 13 |
|
| 14 |
/* |
| 15 |
* List of token kinds and their ranges. |
| 16 |
* |
| 17 |
* The format for each line is: |
| 18 |
* |
| 19 |
* MACRO(<TOKEN_KIND_NAME>, <DESCRIPTION>) |
| 20 |
* |
| 21 |
* or |
| 22 |
* |
| 23 |
* RANGE(<TOKEN_RANGE_NAME>, <TOKEN_KIND_NAME>) |
| 24 |
* |
| 25 |
* where ; |
| 26 |
* <TOKEN_KIND_NAME> is a legal C identifier of the token, that will be used in |
| 27 |
* the JS engine source. |
| 28 |
* |
| 29 |
* <DESCRIPTION> is a string that describe about the token, and will be used in |
| 30 |
* error message. |
| 31 |
* |
| 32 |
* <TOKEN_RANGE_NAME> is a legal C identifier of the range that will be used to |
| 33 |
* JS engine source. It should end with `First` or `Last`. This is used to check |
| 34 |
* TokenKind by range-testing: |
| 35 |
* BinOpFirst <= tt && tt <= BinOpLast |
| 36 |
* |
| 37 |
* Second argument of `RANGE` is the actual value of the <TOKEN_RANGE_NAME>, |
| 38 |
* should be same as one of <TOKEN_KIND_NAME> in other `MACRO`s. |
| 39 |
* |
| 40 |
* To use this macro, define two macros for `MACRO` and `RANGE`, and pass them |
| 41 |
* as arguments. |
| 42 |
* |
| 43 |
* #define EMIT_TOKEN(name, desc) ... |
| 44 |
* #define EMIT_RANGE(name, value) ... |
| 45 |
* FOR_EACH_TOKEN_KIND_WITH_RANGE(EMIT_TOKEN, EMIT_RANGE) |
| 46 |
* #undef EMIT_TOKEN |
| 47 |
* #undef EMIT_RANGE |
| 48 |
* |
| 49 |
* If you don't need range data, use FOR_EACH_TOKEN_KIND instead. |
| 50 |
* |
| 51 |
* #define EMIT_TOKEN(name, desc) ... |
| 52 |
* FOR_EACH_TOKEN_KIND(EMIT_TOKEN) |
| 53 |
* #undef EMIT_TOKEN |
| 54 |
* |
| 55 |
* Note that this list does not contain Limit. |
| 56 |
*/ |
| 57 |
#define FOR_EACH_TOKEN_KIND_WITH_RANGE(MACRO, RANGE) \ |
| 58 |
MACRO(Eof, "end of script") \ |
| 59 |
\ |
| 60 |
/* only returned by peekTokenSameLine() */ \ |
| 61 |
MACRO(Eol, "line terminator") \ |
| 62 |
\ |
| 63 |
MACRO(Semi, "';'") \ |
| 64 |
MACRO(Comma, "','") \ |
| 65 |
MACRO(Hook, "'?'") /* conditional */ \ |
| 66 |
MACRO(Colon, "':'") /* conditional */ \ |
| 67 |
MACRO(Inc, "'++'") /* increment */ \ |
| 68 |
MACRO(Dec, "'--'") /* decrement */ \ |
| 69 |
MACRO(Dot, "'.'") /* member operator */ \ |
| 70 |
MACRO(TripleDot, "'...'") /* rest arguments and spread operator */ \ |
| 71 |
MACRO(OptionalChain, "'?.'") \ |
| 72 |
MACRO(LeftBracket, "'['") \ |
| 73 |
MACRO(RightBracket, "']'") \ |
| 74 |
MACRO(LeftCurly, "'{'") \ |
| 75 |
MACRO(RightCurly, "'}'") \ |
| 76 |
MACRO(LeftParen, "'('") \ |
| 77 |
MACRO(RightParen, "')'") \ |
| 78 |
MACRO(Name, "identifier") \ |
| 79 |
MACRO(PrivateName, "private identifier") \ |
| 80 |
MACRO(Number, "numeric literal") \ |
| 81 |
MACRO(String, "string literal") \ |
| 82 |
MACRO(BigInt, "bigint literal") \ |
| 83 |
IF_DECORATORS(MACRO(At, "'@'")) \ |
| 84 |
\ |
| 85 |
/* start of template literal with substitutions */ \ |
| 86 |
MACRO(TemplateHead, "'${'") \ |
| 87 |
/* template literal without substitutions */ \ |
| 88 |
MACRO(NoSubsTemplate, "template literal") \ |
| 89 |
\ |
| 90 |
MACRO(RegExp, "regular expression literal") \ |
| 91 |
MACRO(True, "boolean literal 'true'") \ |
| 92 |
RANGE(ReservedWordLiteralFirst, True) \ |
| 93 |
MACRO(False, "boolean literal 'false'") \ |
| 94 |
MACRO(Null, "null literal") \ |
| 95 |
RANGE(ReservedWordLiteralLast, Null) \ |
| 96 |
MACRO(This, "keyword 'this'") \ |
| 97 |
RANGE(KeywordFirst, This) \ |
| 98 |
MACRO(Function, "keyword 'function'") \ |
| 99 |
MACRO(If, "keyword 'if'") \ |
| 100 |
MACRO(Else, "keyword 'else'") \ |
| 101 |
MACRO(Switch, "keyword 'switch'") \ |
| 102 |
MACRO(Case, "keyword 'case'") \ |
| 103 |
MACRO(Default, "keyword 'default'") \ |
| 104 |
MACRO(While, "keyword 'while'") \ |
| 105 |
MACRO(Do, "keyword 'do'") \ |
| 106 |
MACRO(For, "keyword 'for'") \ |
| 107 |
MACRO(Break, "keyword 'break'") \ |
| 108 |
MACRO(Continue, "keyword 'continue'") \ |
| 109 |
MACRO(Var, "keyword 'var'") \ |
| 110 |
MACRO(Const, "keyword 'const'") \ |
| 111 |
MACRO(With, "keyword 'with'") \ |
| 112 |
MACRO(Return, "keyword 'return'") \ |
| 113 |
MACRO(New, "keyword 'new'") \ |
| 114 |
MACRO(Delete, "keyword 'delete'") \ |
| 115 |
MACRO(Try, "keyword 'try'") \ |
| 116 |
MACRO(Catch, "keyword 'catch'") \ |
| 117 |
MACRO(Finally, "keyword 'finally'") \ |
| 118 |
MACRO(Throw, "keyword 'throw'") \ |
| 119 |
MACRO(Debugger, "keyword 'debugger'") \ |
| 120 |
MACRO(Export, "keyword 'export'") \ |
| 121 |
MACRO(Import, "keyword 'import'") \ |
| 122 |
MACRO(Class, "keyword 'class'") \ |
| 123 |
MACRO(Extends, "keyword 'extends'") \ |
| 124 |
MACRO(Super, "keyword 'super'") \ |
| 125 |
RANGE(KeywordLast, Super) \ |
| 126 |
\ |
| 127 |
/* contextual keywords */ \ |
| 128 |
MACRO(As, "'as'") \ |
| 129 |
RANGE(ContextualKeywordFirst, As) \ |
| 130 |
/* TODO: Move to alphabetical order when IF_DECORATORS is removed */ \ |
| 131 |
IF_DECORATORS(MACRO(Accessor, "'accessor'")) \ |
| 132 |
MACRO(Assert, "'assert'") \ |
| 133 |
MACRO(Async, "'async'") \ |
| 134 |
MACRO(Await, "'await'") \ |
| 135 |
MACRO(Each, "'each'") \ |
| 136 |
MACRO(From, "'from'") \ |
| 137 |
MACRO(Get, "'get'") \ |
| 138 |
MACRO(Let, "'let'") \ |
| 139 |
MACRO(Meta, "'meta'") \ |
| 140 |
MACRO(Of, "'of'") \ |
| 141 |
MACRO(Set, "'set'") \ |
| 142 |
MACRO(Static, "'static'") \ |
| 143 |
MACRO(Target, "'target'") \ |
| 144 |
IF_EXPLICIT_RESOURCE_MANAGEMENT(MACRO(Using, "'using'")) \ |
| 145 |
MACRO(Yield, "'yield'") \ |
| 146 |
RANGE(ContextualKeywordLast, Yield) \ |
| 147 |
\ |
| 148 |
/* future reserved words */ \ |
| 149 |
MACRO(Enum, "reserved word 'enum'") \ |
| 150 |
RANGE(FutureReservedKeywordFirst, Enum) \ |
| 151 |
RANGE(FutureReservedKeywordLast, Enum) \ |
| 152 |
\ |
| 153 |
/* reserved words in strict mode */ \ |
| 154 |
MACRO(Implements, "reserved word 'implements'") \ |
| 155 |
RANGE(StrictReservedKeywordFirst, Implements) \ |
| 156 |
MACRO(Interface, "reserved word 'interface'") \ |
| 157 |
MACRO(Package, "reserved word 'package'") \ |
| 158 |
MACRO(Private, "reserved word 'private'") \ |
| 159 |
MACRO(Protected, "reserved word 'protected'") \ |
| 160 |
MACRO(Public, "reserved word 'public'") \ |
| 161 |
RANGE(StrictReservedKeywordLast, Public) \ |
| 162 |
\ |
| 163 |
/* \ |
| 164 |
* The following token types occupy contiguous ranges to enable easy \ |
| 165 |
* range-testing. \ |
| 166 |
*/ \ |
| 167 |
/* \ |
| 168 |
* Binary operators. \ |
| 169 |
* This list must be kept in the same order in several places: \ |
| 170 |
* - the binary operators in ParseNode.h \ |
| 171 |
* - the precedence list in Parser.cpp \ |
| 172 |
* - the JSOp code list in BytecodeEmitter.cpp \ |
| 173 |
*/ \ |
| 174 |
MACRO(Coalesce, "'\?\?'") /* escapes to avoid trigraphs warning */ \ |
| 175 |
RANGE(BinOpFirst, Coalesce) \ |
| 176 |
MACRO(Or, "'||'") /* logical or */ \ |
| 177 |
MACRO(And, "'&&'") /* logical and */ \ |
| 178 |
MACRO(BitOr, "'|'") /* bitwise-or */ \ |
| 179 |
MACRO(BitXor, "'^'") /* bitwise-xor */ \ |
| 180 |
MACRO(BitAnd, "'&'") /* bitwise-and */ \ |
| 181 |
\ |
| 182 |
/* Equality operation tokens, per TokenKindIsEquality. */ \ |
| 183 |
MACRO(StrictEq, "'==='") \ |
| 184 |
RANGE(EqualityStart, StrictEq) \ |
| 185 |
MACRO(Eq, "'=='") \ |
| 186 |
MACRO(StrictNe, "'!=='") \ |
| 187 |
MACRO(Ne, "'!='") \ |
| 188 |
RANGE(EqualityLast, Ne) \ |
| 189 |
\ |
| 190 |
/* Relational ops, per TokenKindIsRelational. */ \ |
| 191 |
MACRO(Lt, "'<'") \ |
| 192 |
RANGE(RelOpStart, Lt) \ |
| 193 |
MACRO(Le, "'<='") \ |
| 194 |
MACRO(Gt, "'>'") \ |
| 195 |
MACRO(Ge, "'>='") \ |
| 196 |
RANGE(RelOpLast, Ge) \ |
| 197 |
\ |
| 198 |
MACRO(InstanceOf, "keyword 'instanceof'") \ |
| 199 |
RANGE(KeywordBinOpFirst, InstanceOf) \ |
| 200 |
MACRO(In, "keyword 'in'") \ |
| 201 |
MACRO(PrivateIn, "keyword 'in' (private)") \ |
| 202 |
RANGE(KeywordBinOpLast, PrivateIn) \ |
| 203 |
\ |
| 204 |
/* Shift ops, per TokenKindIsShift. */ \ |
| 205 |
MACRO(Lsh, "'<<'") \ |
| 206 |
RANGE(ShiftOpStart, Lsh) \ |
| 207 |
MACRO(Rsh, "'>>'") \ |
| 208 |
MACRO(Ursh, "'>>>'") \ |
| 209 |
RANGE(ShiftOpLast, Ursh) \ |
| 210 |
\ |
| 211 |
MACRO(Add, "'+'") \ |
| 212 |
MACRO(Sub, "'-'") \ |
| 213 |
MACRO(Mul, "'*'") \ |
| 214 |
MACRO(Div, "'/'") \ |
| 215 |
MACRO(Mod, "'%'") \ |
| 216 |
MACRO(Pow, "'**'") \ |
| 217 |
RANGE(BinOpLast, Pow) \ |
| 218 |
\ |
| 219 |
/* Unary operation tokens. */ \ |
| 220 |
MACRO(TypeOf, "keyword 'typeof'") \ |
| 221 |
RANGE(KeywordUnOpFirst, TypeOf) \ |
| 222 |
MACRO(Void, "keyword 'void'") \ |
| 223 |
RANGE(KeywordUnOpLast, Void) \ |
| 224 |
MACRO(Not, "'!'") \ |
| 225 |
MACRO(BitNot, "'~'") \ |
| 226 |
\ |
| 227 |
MACRO(Arrow, "'=>'") /* function arrow */ \ |
| 228 |
\ |
| 229 |
/* Assignment ops, per TokenKindIsAssignment */ \ |
| 230 |
MACRO(Assign, "'='") \ |
| 231 |
RANGE(AssignmentStart, Assign) \ |
| 232 |
MACRO(AddAssign, "'+='") \ |
| 233 |
MACRO(SubAssign, "'-='") \ |
| 234 |
MACRO(CoalesceAssign, "'\?\?='") /* avoid trigraphs warning */ \ |
| 235 |
MACRO(OrAssign, "'||='") \ |
| 236 |
MACRO(AndAssign, "'&&='") \ |
| 237 |
MACRO(BitOrAssign, "'|='") \ |
| 238 |
MACRO(BitXorAssign, "'^='") \ |
| 239 |
MACRO(BitAndAssign, "'&='") \ |
| 240 |
MACRO(LshAssign, "'<<='") \ |
| 241 |
MACRO(RshAssign, "'>>='") \ |
| 242 |
MACRO(UrshAssign, "'>>>='") \ |
| 243 |
MACRO(MulAssign, "'*='") \ |
| 244 |
MACRO(DivAssign, "'/='") \ |
| 245 |
MACRO(ModAssign, "'%='") \ |
| 246 |
MACRO(PowAssign, "'**='") \ |
| 247 |
RANGE(AssignmentLast, PowAssign) |
| 248 |
|
| 249 |
#define TOKEN_KIND_RANGE_EMIT_NONE(name, value) |
| 250 |
#define FOR_EACH_TOKEN_KIND(MACRO) \ |
| 251 |
FOR_EACH_TOKEN_KIND_WITH_RANGE(MACRO, TOKEN_KIND_RANGE_EMIT_NONE) |
| 252 |
|
| 253 |
namespace js { |
| 254 |
namespace frontend { |
| 255 |
|
| 256 |
// Values of this type are used to index into arrays such as isExprEnding[], |
| 257 |
// so the first value must be zero. |
| 258 |
enum class TokenKind : uint8_t { |
| 259 |
#define EMIT_ENUM(name, desc) name, |
| 260 |
#define EMIT_ENUM_RANGE(name, value) name = value, |
| 261 |
FOR_EACH_TOKEN_KIND_WITH_RANGE(EMIT_ENUM, EMIT_ENUM_RANGE) |
| 262 |
#undef EMIT_ENUM |
| 263 |
#undef EMIT_ENUM_RANGE |
| 264 |
Limit // domain size |
| 265 |
}; |
| 266 |
|
| 267 |
inline bool TokenKindIsBinaryOp(TokenKind tt) { |
| 268 |
return TokenKind::BinOpFirst <= tt && tt <= TokenKind::BinOpLast; |
| 269 |
} |
| 270 |
|
| 271 |
inline bool TokenKindIsEquality(TokenKind tt) { |
| 272 |
return TokenKind::EqualityStart <= tt && tt <= TokenKind::EqualityLast; |
| 273 |
} |
| 274 |
|
| 275 |
inline bool TokenKindIsRelational(TokenKind tt) { |
| 276 |
return TokenKind::RelOpStart <= tt && tt <= TokenKind::RelOpLast; |
| 277 |
} |
| 278 |
|
| 279 |
inline bool TokenKindIsShift(TokenKind tt) { |
| 280 |
return TokenKind::ShiftOpStart <= tt && tt <= TokenKind::ShiftOpLast; |
| 281 |
} |
| 282 |
|
| 283 |
inline bool TokenKindIsAssignment(TokenKind tt) { |
| 284 |
return TokenKind::AssignmentStart <= tt && tt <= TokenKind::AssignmentLast; |
| 285 |
} |
| 286 |
|
| 287 |
[[nodiscard]] inline bool TokenKindIsKeyword(TokenKind tt) { |
| 288 |
return (TokenKind::KeywordFirst <= tt && tt <= TokenKind::KeywordLast) || |
| 289 |
(TokenKind::KeywordBinOpFirst <= tt && |
| 290 |
tt <= TokenKind::KeywordBinOpLast) || |
| 291 |
(TokenKind::KeywordUnOpFirst <= tt && |
| 292 |
tt <= TokenKind::KeywordUnOpLast); |
| 293 |
} |
| 294 |
|
| 295 |
[[nodiscard]] inline bool TokenKindIsContextualKeyword(TokenKind tt) { |
| 296 |
return TokenKind::ContextualKeywordFirst <= tt && |
| 297 |
tt <= TokenKind::ContextualKeywordLast; |
| 298 |
} |
| 299 |
|
| 300 |
[[nodiscard]] inline bool TokenKindIsFutureReservedWord(TokenKind tt) { |
| 301 |
return TokenKind::FutureReservedKeywordFirst <= tt && |
| 302 |
tt <= TokenKind::FutureReservedKeywordLast; |
| 303 |
} |
| 304 |
|
| 305 |
[[nodiscard]] inline bool TokenKindIsStrictReservedWord(TokenKind tt) { |
| 306 |
return TokenKind::StrictReservedKeywordFirst <= tt && |
| 307 |
tt <= TokenKind::StrictReservedKeywordLast; |
| 308 |
} |
| 309 |
|
| 310 |
[[nodiscard]] inline bool TokenKindIsReservedWordLiteral(TokenKind tt) { |
| 311 |
return TokenKind::ReservedWordLiteralFirst <= tt && |
| 312 |
tt <= TokenKind::ReservedWordLiteralLast; |
| 313 |
} |
| 314 |
|
| 315 |
[[nodiscard]] inline bool TokenKindIsReservedWord(TokenKind tt) { |
| 316 |
return TokenKindIsKeyword(tt) || TokenKindIsFutureReservedWord(tt) || |
| 317 |
TokenKindIsReservedWordLiteral(tt); |
| 318 |
} |
| 319 |
|
| 320 |
[[nodiscard]] inline bool TokenKindIsPossibleIdentifier(TokenKind tt) { |
| 321 |
return tt == TokenKind::Name || TokenKindIsContextualKeyword(tt) || |
| 322 |
TokenKindIsStrictReservedWord(tt); |
| 323 |
} |
| 324 |
|
| 325 |
[[nodiscard]] inline bool TokenKindIsPossibleIdentifierName(TokenKind tt) { |
| 326 |
return TokenKindIsPossibleIdentifier(tt) || TokenKindIsReservedWord(tt); |
| 327 |
} |
| 328 |
|
| 329 |
} // namespace frontend |
| 330 |
} // namespace js |
| 331 |
|
| 332 |
#endif /* frontend_TokenKind_h */ |