1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | "use strict";
|
22 |
|
23 | var _classCallCheck = require("babel-runtime/helpers/class-call-check")["default"];
|
24 |
|
25 | exports.__esModule = true;
|
26 |
|
27 | var 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 |
|
45 | exports.TokenType = TokenType;
|
46 |
|
47 | function binop(name, prec) {
|
48 | return new TokenType(name, { beforeExpr: true, binop: prec });
|
49 | }
|
50 | var beforeExpr = { beforeExpr: true },
|
51 | startsExpr = { startsExpr: true };
|
52 |
|
53 | var 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 |
|
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 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
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 |
|
113 | exports.types = types;
|
114 |
|
115 |
|
116 | var keywords = {};
|
117 |
|
118 | exports.keywords = keywords;
|
119 |
|
120 | function 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 |
|
127 | kw("break");
|
128 | kw("case", beforeExpr);
|
129 | kw("catch");
|
130 | kw("continue");
|
131 | kw("debugger");
|
132 | kw("default", beforeExpr);
|
133 | kw("do", { isLoop: true, beforeExpr: true });
|
134 | kw("else", beforeExpr);
|
135 | kw("finally");
|
136 | kw("for", { isLoop: true });
|
137 | kw("function", startsExpr);
|
138 | kw("if");
|
139 | kw("return", beforeExpr);
|
140 | kw("switch");
|
141 | kw("throw", beforeExpr);
|
142 | kw("try");
|
143 | kw("var");
|
144 | kw("let");
|
145 | kw("const");
|
146 | kw("while", { isLoop: true });
|
147 | kw("with");
|
148 | kw("new", { beforeExpr: true, startsExpr: true });
|
149 | kw("this", startsExpr);
|
150 | kw("super", startsExpr);
|
151 | kw("class");
|
152 | kw("extends", beforeExpr);
|
153 | kw("export");
|
154 | kw("import");
|
155 | kw("yield", { beforeExpr: true, startsExpr: true });
|
156 | kw("null", startsExpr);
|
157 | kw("true", startsExpr);
|
158 | kw("false", startsExpr);
|
159 | kw("in", { beforeExpr: true, binop: 7 });
|
160 | kw("instanceof", { beforeExpr: true, binop: 7 });
|
161 | kw("typeof", { beforeExpr: true, prefix: true, startsExpr: true });
|
162 | kw("void", { beforeExpr: true, prefix: true, startsExpr: true });
|
163 | kw("delete", { beforeExpr: true, prefix: true, startsExpr: true }); |
\ | No newline at end of file |