DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (b6d82b1a6b02)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef frontend_TokenKind_h
#define frontend_TokenKind_h

#include <stdint.h>

/*
 * List of token kinds and their ranges.
 *
 * The format for each line is:
 *
 *   MACRO(<TOKEN_KIND_NAME>, <DESCRIPTION>)
 *
 * or
 *
 *   RANGE(<TOKEN_RANGE_NAME>, <TOKEN_KIND_NAME>)
 *
 * where ;
 * <TOKEN_KIND_NAME> is a legal C identifier of the token, that will be used in
 * the JS engine source.
 *
 * <DESCRIPTION> is a string that describe about the token, and will be used in
 * error message.
 *
 * <TOKEN_RANGE_NAME> is a legal C identifier of the range that will be used to
 * JS engine source. It should end with `First` or `Last`. This is used to check
 * TokenKind by range-testing:
 *   BinOpFirst <= tt && tt <= BinOpLast
 *
 * Second argument of `RANGE` is the actual value of the <TOKEN_RANGE_NAME>,
 * should be same as one of <TOKEN_KIND_NAME> in other `MACRO`s.
 *
 * To use this macro, define two macros for `MACRO` and `RANGE`, and pass them
 * as arguments.
 *
 *   #define EMIT_TOKEN(name, desc) ...
 *   #define EMIT_RANGE(name, value) ...
 *   FOR_EACH_TOKEN_KIND_WITH_RANGE(EMIT_TOKEN, EMIT_RANGE)
 *   #undef EMIT_TOKEN
 *   #undef EMIT_RANGE
 *
 * If you don't need range data, use FOR_EACH_TOKEN_KIND instead.
 *
 *   #define EMIT_TOKEN(name, desc) ...
 *   FOR_EACH_TOKEN_KIND(EMIT_TOKEN)
 *   #undef EMIT_TOKEN
 *
 * Note that this list does not contain ERROR and LIMIT.
 */
#define FOR_EACH_TOKEN_KIND_WITH_RANGE(MACRO, RANGE)                        \
  MACRO(Eof, "end of script")                                               \
                                                                            \
  /* only returned by peekTokenSameLine() */                                \
  MACRO(Eol, "line terminator")                                             \
                                                                            \
  MACRO(Semi, "';'")                                                        \
  MACRO(Comma, "','")                                                       \
  MACRO(Hook, "'?'")        /* conditional */                               \
  MACRO(Colon, "':'")       /* conditional */                               \
  MACRO(Inc, "'++'")        /* increment */                                 \
  MACRO(Dec, "'--'")        /* decrement */                                 \
  MACRO(Dot, "'.'")         /* member operator */                           \
  MACRO(TripleDot, "'...'") /* rest arguments and spread operator */        \
  MACRO(LeftBracket, "'['")                                                 \
  MACRO(RightBracket, "']'")                                                \
  MACRO(LeftCurly, "'{'")                                                   \
  MACRO(RightCurly, "'}'")                                                  \
  MACRO(LeftParen, "'('")                                                   \
  MACRO(RightParen, "')'")                                                  \
  MACRO(Name, "identifier")                                                 \
  MACRO(PrivateName, "private identifier")                                  \
  MACRO(Number, "numeric literal")                                          \
  MACRO(String, "string literal")                                           \
  MACRO(BigInt, "bigint literal")                                           \
                                                                            \
  /* start of template literal with substitutions */                        \
  MACRO(TemplateHead, "'${'")                                               \
  /* template literal without substitutions */                              \
  MACRO(NoSubsTemplate, "template literal")                                 \
                                                                            \
  MACRO(RegExp, "regular expression literal")                               \
  MACRO(True, "boolean literal 'true'")                                     \
  RANGE(ReservedWordLiteralFirst, True)                                     \
  MACRO(False, "boolean literal 'false'")                                   \
  MACRO(Null, "null literal")                                               \
  RANGE(ReservedWordLiteralLast, Null)                                      \
  MACRO(This, "keyword 'this'")                                             \
  RANGE(KeywordFirst, This)                                                 \
  MACRO(Function, "keyword 'function'")                                     \
  MACRO(If, "keyword 'if'")                                                 \
  MACRO(Else, "keyword 'else'")                                             \
  MACRO(Switch, "keyword 'switch'")                                         \
  MACRO(Case, "keyword 'case'")                                             \
  MACRO(Default, "keyword 'default'")                                       \
  MACRO(While, "keyword 'while'")                                           \
  MACRO(Do, "keyword 'do'")                                                 \
  MACRO(For, "keyword 'for'")                                               \
  MACRO(Break, "keyword 'break'")                                           \
  MACRO(Continue, "keyword 'continue'")                                     \
  MACRO(Var, "keyword 'var'")                                               \
  MACRO(Const, "keyword 'const'")                                           \
  MACRO(With, "keyword 'with'")                                             \
  MACRO(Return, "keyword 'return'")                                         \
  MACRO(New, "keyword 'new'")                                               \
  MACRO(Delete, "keyword 'delete'")                                         \
  MACRO(Try, "keyword 'try'")                                               \
  MACRO(Catch, "keyword 'catch'")                                           \
  MACRO(Finally, "keyword 'finally'")                                       \
  MACRO(Throw, "keyword 'throw'")                                           \
  MACRO(Debugger, "keyword 'debugger'")                                     \
  MACRO(Export, "keyword 'export'")                                         \
  MACRO(Import, "keyword 'import'")                                         \
  MACRO(Class, "keyword 'class'")                                           \
  MACRO(Extends, "keyword 'extends'")                                       \
  MACRO(Super, "keyword 'super'")                                           \
  RANGE(KeywordLast, Super)                                                 \
                                                                            \
  /* contextual keywords */                                                 \
  MACRO(As, "'as'")                                                         \
  RANGE(ContextualKeywordFirst, As)                                         \
  MACRO(Async, "'async'")                                                   \
  MACRO(Await, "'await'")                                                   \
  MACRO(Each, "'each'")                                                     \
  MACRO(From, "'from'")                                                     \
  MACRO(Get, "'get'")                                                       \
  MACRO(Let, "'let'")                                                       \
  MACRO(Meta, "'meta'")                                                     \
  MACRO(Of, "'of'")                                                         \
  MACRO(Set, "'set'")                                                       \
  MACRO(Static, "'static'")                                                 \
  MACRO(Target, "'target'")                                                 \
  MACRO(Yield, "'yield'")                                                   \
  RANGE(ContextualKeywordLast, Yield)                                       \
                                                                            \
  /* future reserved words */                                               \
  MACRO(Enum, "reserved word 'enum'")                                       \
  RANGE(FutureReservedKeywordFirst, Enum)                                   \
  RANGE(FutureReservedKeywordLast, Enum)                                    \
                                                                            \
  /* reserved words in strict mode */                                       \
  MACRO(Implements, "reserved word 'implements'")                           \
  RANGE(StrictReservedKeywordFirst, Implements)                             \
  MACRO(Interface, "reserved word 'interface'")                             \
  MACRO(Package, "reserved word 'package'")                                 \
  MACRO(Private, "reserved word 'private'")                                 \
  MACRO(Protected, "reserved word 'protected'")                             \
  MACRO(Public, "reserved word 'public'")                                   \
  RANGE(StrictReservedKeywordLast, Public)                                  \
                                                                            \
  /*                                                                        \
   * The following token types occupy contiguous ranges to enable easy      \
   * range-testing.                                                         \
   */                                                                       \
  /*                                                                        \
   * Binary operators tokens, Or thru Pow. These must be in the same        \
   * order as F(Or) and friends in FOR_EACH_PARSE_NODE_KIND in ParseNode.h. \
   */                                                                       \
  MACRO(Pipeline, "'|>'")                                                   \
  RANGE(BinOpFirst, Pipeline)                                               \
  MACRO(Or, "'||'")    /* logical or */                                     \
  MACRO(And, "'&&'")   /* logical and */                                    \
  MACRO(BitOr, "'|'")  /* bitwise-or */                                     \
  MACRO(BitXor, "'^'") /* bitwise-xor */                                    \
  MACRO(BitAnd, "'&'") /* bitwise-and */                                    \
                                                                            \
  /* Equality operation tokens, per TokenKindIsEquality. */                 \
  MACRO(StrictEq, "'==='")                                                  \
  RANGE(EqualityStart, StrictEq)                                            \
  MACRO(Eq, "'=='")                                                         \
  MACRO(StrictNe, "'!=='")                                                  \
  MACRO(Ne, "'!='")                                                         \
  RANGE(EqualityLast, Ne)                                                   \
                                                                            \
  /* Relational ops, per TokenKindIsRelational. */                          \
  MACRO(Lt, "'<'")                                                          \
  RANGE(RelOpStart, Lt)                                                     \
  MACRO(Le, "'<='")                                                         \
  MACRO(Gt, "'>'")                                                          \
  MACRO(Ge, "'>='")                                                         \
  RANGE(RelOpLast, Ge)                                                      \
                                                                            \
  MACRO(InstanceOf, "keyword 'instanceof'")                                 \
  RANGE(KeywordBinOpFirst, InstanceOf)                                      \
  MACRO(In, "keyword 'in'")                                                 \
  RANGE(KeywordBinOpLast, In)                                               \
                                                                            \
  /* Shift ops, per TokenKindIsShift. */                                    \
  MACRO(Lsh, "'<<'")                                                        \
  RANGE(ShiftOpStart, Lsh)                                                  \
  MACRO(Rsh, "'>>'")                                                        \
  MACRO(Ursh, "'>>>'")                                                      \
  RANGE(ShiftOpLast, Ursh)                                                  \
                                                                            \
  MACRO(Add, "'+'")                                                         \
  MACRO(Sub, "'-'")                                                         \
  MACRO(Mul, "'*'")                                                         \
  MACRO(Div, "'/'")                                                         \
  MACRO(Mod, "'%'")                                                         \
  MACRO(Pow, "'**'")                                                        \
  RANGE(BinOpLast, Pow)                                                     \
                                                                            \
  /* Unary operation tokens. */                                             \
  MACRO(TypeOf, "keyword 'typeof'")                                         \
  RANGE(KeywordUnOpFirst, TypeOf)                                           \
  MACRO(Void, "keyword 'void'")                                             \
  RANGE(KeywordUnOpLast, Void)                                              \
  MACRO(Not, "'!'")                                                         \
  MACRO(BitNot, "'~'")                                                      \
                                                                            \
  MACRO(Arrow, "'=>'") /* function arrow */                                 \
                                                                            \
  /* Assignment ops, per TokenKindIsAssignment */                           \
  MACRO(Assign, "'='")                                                      \
  RANGE(AssignmentStart, Assign)                                            \
  MACRO(AddAssign, "'+='")                                                  \
  MACRO(SubAssign, "'-='")                                                  \
  MACRO(BitOrAssign, "'|='")                                                \
  MACRO(BitXorAssign, "'^='")                                               \
  MACRO(BitAndAssign, "'&='")                                               \
  MACRO(LshAssign, "'<<='")                                                 \
  MACRO(RshAssign, "'>>='")                                                 \
  MACRO(UrshAssign, "'>>>='")                                               \
  MACRO(MulAssign, "'*='")                                                  \
  MACRO(DivAssign, "'/='")                                                  \
  MACRO(ModAssign, "'%='")                                                  \
  MACRO(PowAssign, "'**='")                                                 \
  RANGE(AssignmentLast, PowAssign)

#define TOKEN_KIND_RANGE_EMIT_NONE(name, value)
#define FOR_EACH_TOKEN_KIND(MACRO) \
  FOR_EACH_TOKEN_KIND_WITH_RANGE(MACRO, TOKEN_KIND_RANGE_EMIT_NONE)

namespace js {
namespace frontend {

// Values of this type are used to index into arrays such as isExprEnding[],
// so the first value must be zero.
enum class TokenKind : uint8_t {
#define EMIT_ENUM(name, desc) name,
#define EMIT_ENUM_RANGE(name, value) name = value,
  FOR_EACH_TOKEN_KIND_WITH_RANGE(EMIT_ENUM, EMIT_ENUM_RANGE)
#undef EMIT_ENUM
#undef EMIT_ENUM_RANGE
      Limit  // domain size
};

inline bool TokenKindIsBinaryOp(TokenKind tt) {
  return TokenKind::BinOpFirst <= tt && tt <= TokenKind::BinOpLast;
}

inline bool TokenKindIsEquality(TokenKind tt) {
  return TokenKind::EqualityStart <= tt && tt <= TokenKind::EqualityLast;
}

inline bool TokenKindIsRelational(TokenKind tt) {
  return TokenKind::RelOpStart <= tt && tt <= TokenKind::RelOpLast;
}

inline bool TokenKindIsShift(TokenKind tt) {
  return TokenKind::ShiftOpStart <= tt && tt <= TokenKind::ShiftOpLast;
}

inline bool TokenKindIsAssignment(TokenKind tt) {
  return TokenKind::AssignmentStart <= tt && tt <= TokenKind::AssignmentLast;
}

inline MOZ_MUST_USE bool TokenKindIsKeyword(TokenKind tt) {
  return (TokenKind::KeywordFirst <= tt && tt <= TokenKind::KeywordLast) ||
         (TokenKind::KeywordBinOpFirst <= tt &&
          tt <= TokenKind::KeywordBinOpLast) ||
         (TokenKind::KeywordUnOpFirst <= tt &&
          tt <= TokenKind::KeywordUnOpLast);
}

inline MOZ_MUST_USE bool TokenKindIsContextualKeyword(TokenKind tt) {
  return TokenKind::ContextualKeywordFirst <= tt &&
         tt <= TokenKind::ContextualKeywordLast;
}

inline MOZ_MUST_USE bool TokenKindIsFutureReservedWord(TokenKind tt) {
  return TokenKind::FutureReservedKeywordFirst <= tt &&
         tt <= TokenKind::FutureReservedKeywordLast;
}

inline MOZ_MUST_USE bool TokenKindIsStrictReservedWord(TokenKind tt) {
  return TokenKind::StrictReservedKeywordFirst <= tt &&
         tt <= TokenKind::StrictReservedKeywordLast;
}

inline MOZ_MUST_USE bool TokenKindIsReservedWordLiteral(TokenKind tt) {
  return TokenKind::ReservedWordLiteralFirst <= tt &&
         tt <= TokenKind::ReservedWordLiteralLast;
}

inline MOZ_MUST_USE bool TokenKindIsReservedWord(TokenKind tt) {
  return TokenKindIsKeyword(tt) || TokenKindIsFutureReservedWord(tt) ||
         TokenKindIsReservedWordLiteral(tt);
}

inline MOZ_MUST_USE bool TokenKindIsPossibleIdentifier(TokenKind tt) {
  return tt == TokenKind::Name || tt == TokenKind::PrivateName ||
         TokenKindIsContextualKeyword(tt) || TokenKindIsStrictReservedWord(tt);
}

inline MOZ_MUST_USE bool TokenKindIsPossibleIdentifierName(TokenKind tt) {
  return TokenKindIsPossibleIdentifier(tt) || TokenKindIsReservedWord(tt);
}

}  // namespace frontend
}  // namespace js

#endif /* frontend_TokenKind_h */