buntis
Version:
A 100% compliant, self-hosted typescript parser that emits an ESTree-compatible abstract syntax tree
465 lines (421 loc) • 13.5 kB
text/typescript
/**
* The token types and attributes.
*/
export const enum Token {
Type = 0xff,
/* Precedence for binary operators (always positive) */
PrecStart = 8,
Precedence = 15 << PrecStart, // 8-11
/* Attribute names */
Contextual = 1 << 12,
Keywords = 1 << 13,
FutureReserved = 1 << 14,
IsIdentifier = (1 << 15) | Contextual,
IdentifierOrKeyword = (1 << 16) | IsIdentifier | Keywords,
IsExpressionStart = 1 << 17,
IsStatementStart = (1 << 18) | IsIdentifier | IsExpressionStart,
IsModifier = 1 << 19,
IsLogical = 1 << 20,
IsAutoSemicolon = 1 << 21,
IsPatternStart = 1 << 22, // Start of pattern, '[' or '{'
IsAssignOp = 1 << 23,
IsBinaryOp = (1 << 24) | IsExpressionStart,
IsUnaryOp = (1 << 25) | IsExpressionStart,
IsUpdateOp = (1 << 26) | IsExpressionStart,
IsDeclarationStatement = 1 << 27,
IsStringOrNumber = 1 << 28,
IsCoalesc = 1 << 29,
/* Node types */
EndOfSource = 0, // Pseudo
/* Constants/Bindings */
Identifier = 1 | IsExpressionStart | IsIdentifier,
NumericLiteral = 2 | IsExpressionStart | IsStringOrNumber,
StringLiteral = 3 | IsExpressionStart | IsStringOrNumber,
RegularExpression = 4 | IsExpressionStart,
FalseKeyword = 5 | Keywords | IsExpressionStart,
TrueKeyword = 6 | Keywords | IsExpressionStart,
NullKeyword = 7 | Keywords | IsExpressionStart,
/* Template nodes */
TemplateCont = 8 | IsExpressionStart,
TemplateTail = 9 | IsExpressionStart,
/* Punctuators */
Arrow = 10, // =>
LeftParen = 11 | IsExpressionStart, // (
LeftBrace = 12 | IsExpressionStart | IsPatternStart, // {
Period = 13, // .
Ellipsis = 14, // ...
RightBrace = 15 | IsAutoSemicolon, // }
RightParen = 16, // )
Semicolon = 17 | IsAutoSemicolon, // ;
Comma = 18, // ,
LeftBracket = 19 | IsExpressionStart | IsPatternStart, // [
RightBracket = 20, // ]
Colon = 21, // :
QuestionMark = 22, // ?
QuestionMarkPeriod = 23, // ?.
SingleQuote = 24, // '
DoubleQuote = 25, // "
JSXClose = 26, // </
JSXAutoClose = 27, // />
/* Update operators */
Increment = 28 | IsUpdateOp, // ++
Decrement = 29 | IsUpdateOp, // --
/* Assign operators */
Assign = 30 | IsAssignOp, // =
ShiftLeftAssign = 31 | IsAssignOp, // <<=
ShiftRightAssign = 32 | IsAssignOp, // >>=
LogicalShiftRightAssign = 33 | IsAssignOp, // >>>=
ExponentiateAssign = 34 | IsAssignOp, // **=
AddAssign = 35 | IsAssignOp, // +=
SubtractAssign = 36 | IsAssignOp, // -=
MultiplyAssign = 37 | IsAssignOp, // *=
DivideAssign = 38 | IsAssignOp | IsExpressionStart, // /=
ModuloAssign = 39 | IsAssignOp, // %=
BitwiseXorAssign = 40 | IsAssignOp, // ^=
BitwiseOrAssign = 41 | IsAssignOp, // |=
BitwiseAndAssign = 42 | IsAssignOp, // &=
/* Unary/binary operators */
TypeOfKeyword = 43 | IsUnaryOp | Keywords,
DeleteKeyword = 44 | IsUnaryOp | Keywords,
VoidKeyword = 45 | IsUnaryOp | Keywords,
Negate = 46 | IsUnaryOp, // !
Complement = 47 | IsUnaryOp, // ~
Add = 48 | IsUnaryOp | IsBinaryOp | (10 << PrecStart), // +
Subtract = 49 | IsUnaryOp | IsBinaryOp | (10 << PrecStart), // -
InKeyword = 50 | IsBinaryOp | (8 << PrecStart) | Keywords,
InstanceofKeyword = 51 | IsBinaryOp | (8 << PrecStart) | Keywords,
Multiply = 52 | IsBinaryOp | (11 << PrecStart), // *
Modulo = 53 | IsBinaryOp | (11 << PrecStart), // %
Divide = 54 | IsBinaryOp | IsExpressionStart | (11 << PrecStart), // /
Exponentiate = 55 | IsBinaryOp | (12 << PrecStart), // **
LogicalAnd = 56 | IsBinaryOp | IsLogical | (3 << PrecStart), // &&
LogicalOr = 57 | IsBinaryOp | IsLogical | (2 << PrecStart), // ||
StrictEqual = 58 | IsBinaryOp | (7 << PrecStart), // ===
StrictNotEqual = 59 | IsBinaryOp | (7 << PrecStart), // !==
LooseEqual = 60 | IsBinaryOp | (7 << PrecStart), // ==
LooseNotEqual = 61 | IsBinaryOp | (7 << PrecStart), // !=
LessThanOrEqual = 62 | IsBinaryOp | (7 << PrecStart), // <=
GreaterThanOrEqual = 63 | IsBinaryOp | (7 << PrecStart), // >=
LessThan = 64 | IsBinaryOp | IsExpressionStart | (8 << PrecStart), // <
GreaterThan = 65 | IsBinaryOp | (8 << PrecStart), // >
ShiftLeft = 66 | IsBinaryOp | (9 << PrecStart), // <<
ShiftRight = 67 | IsBinaryOp | (9 << PrecStart), // >>
LogicalShiftRight = 68 | IsBinaryOp | (9 << PrecStart), // >>>
BitwiseAnd = 69 | IsBinaryOp | (6 << PrecStart), // &
BitwiseOr = 70 | IsBinaryOp | (4 << PrecStart), // |
BitwiseXor = 71 | IsBinaryOp | (5 << PrecStart), // ^
Coalesce = 72 | IsBinaryOp | IsCoalesc | (1 << PrecStart), // ?.
/* Variable declaration kinds */
VarKeyword = 73 | IsExpressionStart | Keywords | IsDeclarationStatement,
LetKeyword = 74 | IsExpressionStart | FutureReserved | IsIdentifier,
ConstKeyword = 75 | IsExpressionStart | Keywords | IsDeclarationStatement,
/* Other Keywords words */
BreakKeyword = 76 | Keywords | IsStatementStart,
CaseKeyword = 77 | Keywords | IsStatementStart,
CatchKeyword = 78 | Keywords | IsStatementStart,
ClassKeyword = 79 | Keywords | IsExpressionStart,
ContinueKeyword = 80 | Keywords | IsStatementStart,
DebuggerKeyword = 81 | Keywords | IsStatementStart,
DefaultKeyword = 82 | Keywords | IsStatementStart,
DoKeyword = 83 | Keywords | IsStatementStart,
ElseKeyword = 84 | Keywords | IsStatementStart,
ExportKeyword = 85 | Keywords | IsStatementStart | IsDeclarationStatement,
ExtendsKeyword = 86 | Keywords,
FinallyKeyword = 87 | Keywords | IsStatementStart,
ForKeyword = 88 | Keywords | IsStatementStart,
FunctionKeyword = 89 | Keywords | IsExpressionStart,
IfKeyword = 90 | Keywords | IsStatementStart,
ImportKeyword = 91 | Keywords | IsExpressionStart,
NewKeyword = 92 | Keywords | IsExpressionStart,
ReturnKeyword = 93 | Keywords | IsStatementStart,
SuperKeyword = 94 | Keywords | IsExpressionStart,
SwitchKeyword = 95 | Keywords | IsExpressionStart,
ThisKeyword = 96 | Keywords | IsExpressionStart,
ThrowKeyword = 97 | Keywords | IsStatementStart,
TryKeyword = 98 | Keywords | IsStatementStart,
WhileKeyword = 99 | Keywords | IsStatementStart,
WithKeyword = 100 | Keywords | IsStatementStart,
/* Strict mode Keywords words */
ImplementsKeyword = 101 | FutureReserved,
InterfaceKeyword = 102 | FutureReserved | IsDeclarationStatement | IsStatementStart,
PackageKeyword = 103 | FutureReserved,
PrivateKeyword = 104 | FutureReserved | IsModifier,
ProtectedKeyword = 105 | FutureReserved | IsModifier,
PublicKeyword = 106 | FutureReserved | IsModifier,
StaticKeyword = 107 | FutureReserved | IsModifier,
YieldKeyword = 108 | FutureReserved | IsExpressionStart | IsIdentifier,
/* Contextual keywords */
AsKeyword = 109 | Contextual | IsBinaryOp | (11 << PrecStart),
AsyncKeyword = 110 | Contextual | IsStatementStart,
AwaitKeyword = 111 | Contextual | IsExpressionStart | IsIdentifier,
ConstructorKeyword = 112 | Contextual,
GetKeyword = 113 | Contextual,
SetKeyword = 114 | Contextual,
FromKeyword = 115 | Contextual,
OfKeyword = 116 | Contextual,
/* Others */
WhiteSpace = 117,
CarriageReturn = 118,
LineFeed = 119,
LeadingZero = 120,
Error = 121,
PrivateField = 122,
BigIntLiteral = 123,
EnumKeyword = 124 | IsDeclarationStatement | Token.Keywords,
Backslash = 125,
/* TypeScript */
DeclareKeyword = 126 | IsIdentifier,
TypeKeyword = 127 | IsIdentifier | IsDeclarationStatement,
AbstractKeyword = 128 | IsIdentifier | IsDeclarationStatement | IsModifier,
NamespaceKeyword = 129 | IsIdentifier | IsDeclarationStatement,
ModuleKeyword = 130 | IsIdentifier | IsDeclarationStatement,
GlobalKeyword = 131 | IsIdentifier | IsDeclarationStatement,
KeyOfKeyword = 132 | IsIdentifier,
UniqueKeyword = 133 | IsIdentifier,
IsKeyword = 134 | IsIdentifier,
ReadOnlyKeyword = 135 | IsIdentifier | IsModifier,
InferKeyword = 136 | IsIdentifier,
RequireKeyword = 137 | IsIdentifier,
AssertsKeyword = 138,
/* Escapes */
EscapedStrictReserved = 139,
EscapedKeyword = 140,
/* JSX */
JSXText = 141,
/* Others */
At = 142
}
// Note: this *must* be kept in sync with the enum's order.
//
// It exploits the enum value ordering, and it's necessarily a complete and
// utter hack.
//
// All to lower it to a single monomorphic array access.
export const KeywordDescTable = [
'end of source',
/* Constants/Bindings */
'identifier',
'number',
'string',
'regular expression',
'false',
'true',
'null',
/* Template nodes */
'template continuation',
'template end',
/* Punctuators */
'=>',
'(',
'{',
'.',
'...',
'}',
')',
';',
',',
'[',
']',
':',
'?',
'?.',
"'",
'"',
'</',
'/>',
/* Update operators */
'++',
'--',
/* Assign operators */
'=',
'<<=',
'>>=',
'>>>=',
'**=',
'+=',
'-=',
'*=',
'/=',
'%=',
'^=',
'|=',
'&=',
/* Unary/binary operators */
'typeof',
'delete',
'void',
'!',
'~',
'+',
'-',
'in',
'instanceof',
'*',
'%',
'/',
'**',
'&&',
'||',
'===',
'!==',
'==',
'!=',
'<=',
'>=',
'<',
'>',
'<<',
'>>',
'>>>',
'&',
'|',
'^',
'?.',
/* Variable declaration kinds */
'var',
'let',
'const',
/* Other Keywords words */
'break',
'case',
'catch',
'class',
'continue',
'debugger',
'default',
'do',
'else',
'export',
'extends',
'finally',
'for',
'function',
'if',
'import',
'new',
'return',
'super',
'switch',
'this',
'throw',
'try',
'while',
'with',
/* Strict mode reserved words */
'implements',
'interface',
'package',
'private',
'protected',
'public',
'static',
'yield',
/* Contextual keywords */
'as',
'async',
'await',
'constructor',
'get',
'set',
'from',
'of',
/* Others */
'Whitespace',
'CarriageReturn',
'LineFeed',
'LeadingZero',
'Error',
'#',
'BigIntLiteral',
'enum',
'Backslash',
/* TypeScript */
'declare',
'type',
'abstract',
'namespace',
'module',
'global',
'keyof',
'unique',
'is',
'readonly',
'infer',
'require',
'asserts',
/* Escapes */
'EscapedStrictReserved',
'EscapedKeyword',
/* JSX */
'JSXText',
'At'
];
// Normal object is much faster than Object.create(null), even with typeof check to avoid Object.prototype interference
export const descKeywordTable: { [key: string]: Token } = Object.create(null, {
this: { value: Token.ThisKeyword },
function: { value: Token.FunctionKeyword },
if: { value: Token.IfKeyword },
return: { value: Token.ReturnKeyword },
var: { value: Token.VarKeyword },
else: { value: Token.ElseKeyword },
for: { value: Token.ForKeyword },
new: { value: Token.NewKeyword },
in: { value: Token.InKeyword },
typeof: { value: Token.TypeOfKeyword },
while: { value: Token.WhileKeyword },
case: { value: Token.CaseKeyword },
break: { value: Token.BreakKeyword },
try: { value: Token.TryKeyword },
catch: { value: Token.CatchKeyword },
delete: { value: Token.DeleteKeyword },
throw: { value: Token.ThrowKeyword },
switch: { value: Token.SwitchKeyword },
continue: { value: Token.ContinueKeyword },
default: { value: Token.DefaultKeyword },
instanceof: { value: Token.InstanceofKeyword },
do: { value: Token.DoKeyword },
void: { value: Token.VoidKeyword },
finally: { value: Token.FinallyKeyword },
async: { value: Token.AsyncKeyword },
await: { value: Token.AwaitKeyword },
class: { value: Token.ClassKeyword },
const: { value: Token.ConstKeyword },
constructor: { value: Token.ConstructorKeyword },
debugger: { value: Token.DebuggerKeyword },
export: { value: Token.ExportKeyword },
extends: { value: Token.ExtendsKeyword },
false: { value: Token.FalseKeyword },
from: { value: Token.FromKeyword },
get: { value: Token.GetKeyword },
implements: { value: Token.ImplementsKeyword },
import: { value: Token.ImportKeyword },
interface: { value: Token.InterfaceKeyword },
let: { value: Token.LetKeyword },
null: { value: Token.NullKeyword },
of: { value: Token.OfKeyword },
package: { value: Token.PackageKeyword },
private: { value: Token.PrivateKeyword },
protected: { value: Token.ProtectedKeyword },
public: { value: Token.PublicKeyword },
set: { value: Token.SetKeyword },
static: { value: Token.StaticKeyword },
super: { value: Token.SuperKeyword },
true: { value: Token.TrueKeyword },
with: { value: Token.WithKeyword },
yield: { value: Token.YieldKeyword },
enum: { value: Token.EnumKeyword },
as: { value: Token.AsKeyword },
declare: { value: Token.DeclareKeyword },
type: { value: Token.TypeKeyword },
abstract: { value: Token.AbstractKeyword },
namespace: { value: Token.NamespaceKeyword },
module: { value: Token.ModuleKeyword },
global: { value: Token.GlobalKeyword },
keyof: { value: Token.KeyOfKeyword },
unique: { value: Token.UniqueKeyword },
is: { value: Token.IsKeyword },
readonly: { value: Token.ReadOnlyKeyword },
infer: { value: Token.InferKeyword },
require: { value: Token.RequireKeyword },
asserts: { value: Token.AssertsKeyword }
});