-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
316 lines (292 loc) · 9.76 KB
/
index.js
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
export function lex(str) { const state = { tokens: [], pos: 0, str };
let char;
while (char !== "") {
char = currentChar(state);
if (isNumeric(char)) { lexNumber(state); }
else if (char === "\"") { lexString(state); }
else if (isIdentifierStart(char)) { lexWord(state); }
else if (isPunctuation(char)) { lexPunctuation(state); }
else if (char === "<" || char === ">") { lexLessOrGreaterThan(state); }
else if (char === "=") { lexEqual(state); }
else if (char === "!") { lexNot(state); }
else if (char === "|" || char === "&") { lexBoolean(state); }
else if (char === " " || char === "\t" || char === "\n") { advanceChar(state); }
else if (char !== "") { throw new Error("unexpected character '" + char + "'"); }
}
return state.tokens;
}
export function parse(str) { return parseTop(lex(str)); }
function finishToken(state, type, value) { state.tokens.push({ type, value }); }
function currentChar(state) { return state.str.charAt(state.pos); }
function nextChar(state) { return state.str.charAt(state.pos + 1) || ""; }
function advanceChar(state, n) { state.pos = state.pos + (n || 1); }
function isAlpha(char) { return (char >= "a" && char <= "z") || (char >= "A" && char <= "Z"); }
function isNumeric(char) { return char >= "0" && char <= "9"; }
function numericCharToNumber(char) { return char.charCodeAt(0) - "0".charCodeAt(0); }
function isIdentifierStart(char) { return char === "$" || char === "_" || isAlpha(char); }
function isIdentifierChar(char) { return isNumeric(char) || isIdentifierStart(char) }
const PUNCTUATION = ["+", "-", "*", ".", ":", "(", ")", ";", ",", "{", "}", "[", "]"];
function isPunctuation(char) { return PUNCTUATION.includes(char); }
const KEYWORDS =
["export", "function", "if", "return", "switch", "throw", "const", "let", "while"];
function isKeyword(char) { return KEYWORDS.includes(char); }
function eat(tokens, type) {
const token = tokens[0];
if (!token) return;
if (!type || token.type === type) {
tokens.shift();
return token;
}
}
function expect(tokens, type) {
const token = eat(tokens);
if (!token) { throw new Error("expected '" + type + "'"); }
if (type && token.type !== type) {
throw new Error("expected '" + type + "' received '" + token.type + "'");
}
return token;
}
function lexNumber(state) {
let char = currentChar(state);
let value = 0;
while (isNumeric(char)) {
value = value * 10 + numericCharToNumber(char);
advanceChar(state);
char = currentChar(state);
}
finishToken(state, "number", value);
}
function lexString(state) {
advanceChar(state);
let inEscape = false;
let value = "";
while (true) {
const char = currentChar(state);
if (inEscape) {
if (char === "n") {
value = value + "\n";
} else if (char === "t") {
value = value + "\t";
} else if (char === "\"") {
value = value + "\"";
} else if (char === "\\") {
value = value + "\\";
} else {
throw new Error("unexpected string escape '\\" + char + "'");
}
inEscape = false;
advanceChar(state);
} else if (char === "\n" || char === "") {
throw new Error("unexpected end of line");
} else if (char === "\\") {
inEscape = true;
advanceChar(state);
} else if (char !== "\"") {
value = value + char;
advanceChar(state);
} else {
break;
}
}
advanceChar(state);
finishToken(state, "string", value);
}
function lexWord(state) {
let value = "";
while (true) {
const char = currentChar(state);
if (isIdentifierChar(char)) {
value += char;
advanceChar(state);
} else {
break;
}
}
if (isKeyword(value)) {
finishToken(state, value, value);
} else {
finishToken(state, "word", value);
}
}
function lexPunctuation(state) {
const char = currentChar(state);
advanceChar(state);
finishToken(state, char);
}
function lexLessOrGreaterThan(state) {
const char = currentChar(state);
const next = nextChar(state);
let type = char;
advanceChar(state);
if (next === "=") {
type = type + "=";
advanceChar(state);
}
finishToken(state, type);
}
function lexEqual(state) {
advanceChar(state);
const second = currentChar(state);
const third = nextChar(state);
if (second === "=" && third === "=") {
advanceChar(state, 2);
finishToken(state, "===");
} else if (second === "=") {
throw new Error("== is not supported");
} else {
finishToken(state, "=");
}
}
function lexBoolean(state) {
const char = currentChar(state);
const next = nextChar(state);
const token = char + next;
if (token === "||") {
finishToken(state, "||");
} else if (token === "&&") {
finishToken(state, "&&");
} else {
throw new Error("unexpected character '" + next + "'");
}
advanceChar(state, 2);
}
function lexNot(state) {
advanceChar(state);
const second = currentChar(state);
const third = nextChar(state);
if (second === "=" && third === "=") {
advanceChar(state, 2);
finishToken(state, "noteqeq");
} else {
finishToken(state, "!");
}
}
function parseTop(toks) {
const tokens = toks.slice();
const node = { type: "TopLevel", body: [] };
while (tokens.length > 0) {
const stmt = parseStatement(tokens, node);
node.body.push(stmt);
}
return node;
}
function parseStatement(tokens) {
const token = tokens[0];
switch (token.type) {
case "export": eat(tokens); return parseStatement(tokens);
case "function": return parseFunction(tokens);
case "if": return parseIf(tokens);
case "return": return parseReturn(tokens);
case "switch": return parseSwitch(tokens);
case "throw": return parseThrow(tokens);
case "const": return parseBinding(tokens, true);
case "let": return parseBinding(tokens, false);
case "while": return parseWhile(tokens);
case ";": eat(tokens); return parseStatement(tokens);
default:
const expr = parseExpression(tokens);
expect(tokens, ";");
return { type: "ExpressionStatement", expr };
}
}
function parseExpression(tokens) {
const token = tokens[0];
if (token.type === "!" || token.type === "-" || token.type === "+") {
eat(tokens);
const arg = parseExpression(tokens);
return { operator: token.type, arg, type: "UnaryExpression" };
} else {
return parseExpressionSubscripts(tokens);
}
}
function parseExpressionSubscripts(tokens) {
return parseSubscripts(tokens, parseExpressionAtom(tokens));
}
function parseSubscripts(tokens, base) {
while (true) {
const element = parseSubscript(tokens, base);
if (element === base) return base;
base = element;
}
}
function parseSubscript(tokens, object) {
const computed = eat(tokens, "[");
if (computed || eat(tokens, ".")) {
let property;
if (computed) {
property = parseExpression(tokens);
} else {
property = parseIdentifier(tokens);
}
if (computed) { expect(tokens, "]") }
object = { type: "MemberExpression", object, property }
} else if (eat(tokens, "(")) {
const args = parseExpressionList(")", tokens);
object = { callee: object, args, type: "CallExpression" };
}
return object;
}
function parseExpressionAtom(tokens) {
const token = tokens[0];
switch (token.type) {
case "word": return parseIdentifier(tokens);
case "number": case "string": return parseLiteral(tokens);
case "(": return parseParenExpression(tokens);
case "[":
const list = parseExpressionList("]", tokens);
return { type: "ArrayExpression", elements: list.exprs };
case "{": return parseObject(tokens);
default: throw new Error("unexpected expression type '" + token.type + "'");
}
}
function parseLiteral(tokens) {
const token = eat(tokens, "number") || eat(tokens, "string");
return { type: "Literal", value: token.value };
}
function parseIdentifier(tokens) {
const token = expect(tokens, "word");
return { type: "Identifier", name: token.value };
}
function parseParenExpression(tokens) {
expect(tokens, "(");
return parseExpressionList(")", tokens);
}
function parseExpressionList(end, tokens) {
eat(tokens);
const exprs = [];
let first = true;
while (true) {
const comma = eat(tokens, ",");
const token = tokens[0];
if (token.type === end) {
eat(tokens);
return { type: "SequenceExpression", exprs };
} else if (!first && !comma) {
throw new Error("expected ,");
} else {
exprs.push(parseExpression(tokens));
first = false;
}
}
}
function parseObject(tokens) {
throw new Error("unsupported");
}
function parseFunction(tokens) {
expect(tokens, "function");
const name = parseIdentifier(expect(tokens, "word"));
expect(tokens, "(");
const args = parseExpressionList(")", tokens);
if (args.exprs.some(e => e.type !== "Identifier"))
throw new Error("unexpected function argument");
const body = parseBlock(tokens);
return { name, args, body };
}
function parseBlock(tokens) {
const body = [];
while (!eat(tokens, "}")) {
const stmt = parseStatement(tokens);
body.push(stmt);
}
return { type: "BlockStatement", body };
}