UNPKG

5.42 kBJavaScriptView Raw
1/* @flow */
2
3// ## Token types
4
5// The assignment of fine-grained, information-carrying type objects
6// allows the tokenizer to store the information it has about a
7// token in a way that is very cheap for the parser to look up.
8
9// All token type variables start with an underscore, to make them
10// easy to recognize.
11
12// The `beforeExpr` property is used to disambiguate between regular
13// expressions and divisions. It is set on all token types that can
14// be followed by an expression (thus, a slash after them would be a
15// regular expression).
16//
17// `isLoop` marks a keyword as starting a loop, which is important
18// to know when parsing a label, in order to allow or disallow
19// continue jumps to that label.
20
21"use strict";
22
23var _classCallCheck = require("babel-runtime/helpers/class-call-check")["default"];
24
25exports.__esModule = true;
26
27var TokenType = function TokenType(label) {
28 var conf = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
29
30 _classCallCheck(this, TokenType);
31
32 this.label = label;
33 this.keyword = conf.keyword;
34 this.beforeExpr = !!conf.beforeExpr;
35 this.startsExpr = !!conf.startsExpr;
36 this.rightAssociative = !!conf.rightAssociative;
37 this.isLoop = !!conf.isLoop;
38 this.isAssign = !!conf.isAssign;
39 this.prefix = !!conf.prefix;
40 this.postfix = !!conf.postfix;
41 this.binop = conf.binop || null;
42 this.updateContext = null;
43};
44
45exports.TokenType = TokenType;
46
47function binop(name, prec) {
48 return new TokenType(name, { beforeExpr: true, binop: prec });
49}
50var beforeExpr = { beforeExpr: true },
51 startsExpr = { startsExpr: true };
52
53var types = {
54 num: new TokenType("num", startsExpr),
55 regexp: new TokenType("regexp", startsExpr),
56 string: new TokenType("string", startsExpr),
57 name: new TokenType("name", startsExpr),
58 eof: new TokenType("eof"),
59
60 // Punctuation token types.
61 bracketL: new TokenType("[", { beforeExpr: true, startsExpr: true }),
62 bracketR: new TokenType("]"),
63 braceL: new TokenType("{", { beforeExpr: true, startsExpr: true }),
64 braceR: new TokenType("}"),
65 parenL: new TokenType("(", { beforeExpr: true, startsExpr: true }),
66 parenR: new TokenType(")"),
67 comma: new TokenType(",", beforeExpr),
68 semi: new TokenType(";", beforeExpr),
69 colon: new TokenType(":", beforeExpr),
70 doubleColon: new TokenType("::", beforeExpr),
71 dot: new TokenType("."),
72 question: new TokenType("?", beforeExpr),
73 arrow: new TokenType("=>", beforeExpr),
74 template: new TokenType("template"),
75 ellipsis: new TokenType("...", beforeExpr),
76 backQuote: new TokenType("`", startsExpr),
77 dollarBraceL: new TokenType("${", { beforeExpr: true, startsExpr: true }),
78 at: new TokenType("@"),
79
80 // Operators. These carry several kinds of properties to help the
81 // parser use them properly (the presence of these properties is
82 // what categorizes them as operators).
83 //
84 // `binop`, when present, specifies that this operator is a binary
85 // operator, and will refer to its precedence.
86 //
87 // `prefix` and `postfix` mark the operator as a prefix or postfix
88 // unary operator.
89 //
90 // `isAssign` marks all of `=`, `+=`, `-=` etcetera, which act as
91 // binary operators with a very low precedence, that should result
92 // in AssignmentExpression nodes.
93
94 eq: new TokenType("=", { beforeExpr: true, isAssign: true }),
95 assign: new TokenType("_=", { beforeExpr: true, isAssign: true }),
96 incDec: new TokenType("++/--", { prefix: true, postfix: true, startsExpr: true }),
97 prefix: new TokenType("prefix", { beforeExpr: true, prefix: true, startsExpr: true }),
98 logicalOR: binop("||", 1),
99 logicalAND: binop("&&", 2),
100 bitwiseOR: binop("|", 3),
101 bitwiseXOR: binop("^", 4),
102 bitwiseAND: binop("&", 5),
103 equality: binop("==/!=", 6),
104 relational: binop("</>", 7),
105 bitShift: binop("<</>>", 8),
106 plusMin: new TokenType("+/-", { beforeExpr: true, binop: 9, prefix: true, startsExpr: true }),
107 modulo: binop("%", 10),
108 star: binop("*", 10),
109 slash: binop("/", 10),
110 exponent: new TokenType("**", { beforeExpr: true, binop: 11, rightAssociative: true })
111};
112
113exports.types = types;
114// Map keyword names to token types.
115
116var keywords = {};
117
118exports.keywords = keywords;
119// Succinct definitions of keyword token types
120function kw(name) {
121 var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
122
123 options.keyword = name;
124 keywords[name] = types["_" + name] = new TokenType(name, options);
125}
126
127kw("break");
128kw("case", beforeExpr);
129kw("catch");
130kw("continue");
131kw("debugger");
132kw("default", beforeExpr);
133kw("do", { isLoop: true, beforeExpr: true });
134kw("else", beforeExpr);
135kw("finally");
136kw("for", { isLoop: true });
137kw("function", startsExpr);
138kw("if");
139kw("return", beforeExpr);
140kw("switch");
141kw("throw", beforeExpr);
142kw("try");
143kw("var");
144kw("let");
145kw("const");
146kw("while", { isLoop: true });
147kw("with");
148kw("new", { beforeExpr: true, startsExpr: true });
149kw("this", startsExpr);
150kw("super", startsExpr);
151kw("class");
152kw("extends", beforeExpr);
153kw("export");
154kw("import");
155kw("yield", { beforeExpr: true, startsExpr: true });
156kw("null", startsExpr);
157kw("true", startsExpr);
158kw("false", startsExpr);
159kw("in", { beforeExpr: true, binop: 7 });
160kw("instanceof", { beforeExpr: true, binop: 7 });
161kw("typeof", { beforeExpr: true, prefix: true, startsExpr: true });
162kw("void", { beforeExpr: true, prefix: true, startsExpr: true });
163kw("delete", { beforeExpr: true, prefix: true, startsExpr: true });
\No newline at end of file