-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.test.ts
More file actions
407 lines (341 loc) · 10.5 KB
/
parser.test.ts
File metadata and controls
407 lines (341 loc) · 10.5 KB
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// noinspection SpellCheckingInspection
import {
afterEach,
Custom,
describe,
expect,
TaskContext,
Test,
test,
} from "vitest";
import { Expression, lex, nameof, parse, Token } from "./parser.js";
async function snap(ctx: TaskContext<Test | Custom>, $it: unknown) {
const file = `./__snapshots__/${ctx.task.suite.name.replaceAll(" ", "_")}/${ctx.task.name.replaceAll(" ", "_")}.snap`;
return await ctx.task.context.expect($it).toMatchFileSnapshot(file);
}
let $tokens: readonly Readonly<Token>[];
function testLex(expr: string) {
$tokens = lex(expr);
}
describe("lexer", () => {
afterEach(async (ctx) => {
await snap(
ctx,
$tokens.map((op) => ({ ...op, type: nameof(op.type) })),
);
});
test("arithmetic operators", () => {
testLex("+ - * / ** %");
});
test("assignment operators", () => {
testLex("= += -= *= /= %= **=");
});
test("comparison operators", () => {
testLex("== === != !== < > <= >=");
});
test("ternary operator", () => {
testLex("? :");
});
test("logical operators", () => {
testLex("&& ||");
});
test("bitwise operators", () => {
testLex("~ & | ^ << >> >>>");
});
test("unary operators", () => {
testLex("++ -- !");
});
test("nullish coalescing operator", () => {
testLex("??");
});
test("other punctuation", () => {
testLex(", ; ( ) [ ] { } . ?. => ...");
});
test("keywords", () => {
testLex(
"this true false null undefined typeof void in instanceof new super",
);
});
test("identifiers", () => {
testLex("foo $bar _baz $123 _456 $ _ abc123");
});
test("whitespace", () => {
testLex("a \t\n\rb");
});
test("strings", () => {
testLex(
`"" '' "foo" 'bar' "foo\\"bar" 'bar\\'foo' "foo\\nbar"
'bar\\tfoo' "\x00" '\\x00' "😀" '\\u{1F600}' "\\u1234"
"\u{0}" "\\u{0}"`,
);
});
test("templates", () => {
testLex(
"${ `` `$` `\\${` `}` `foo` `${bar}` `abc${bar}def` `${ `a${b}c` }` " +
"`\\u{1F600}` sql`select` `${{ $id }}` `${ { id: { } } }` sql``.f " +
"{ a: `${a.title}!` }",
);
});
test("long strings", () => {
testLex(`"abs \\
def"`);
});
test("numbers", () => {
testLex(
"123 -123.456 .456 0.456 123e45 123e+45 123e-45 1. +42.4" +
" -69e12 .34e-5 123456789123456789 123456789123456789n -123n",
);
});
test("binary numbers", () => {
testLex("0b0 0b1 0B1111 -0b01111 +0b10101010 0b0101n");
});
test("octal numbers", () => {
testLex("0o0 0o1 0O7777 -0o7777 +0o7777 0o123n");
});
test("hexadecimal numbers", () => {
testLex("0x0 0x1 0XFfFF -0xFFFF +0xAaAA 0x123n");
});
test("separators", () => {
testLex("0b1_0_0 1_000_000 1.0e1_1 1_2.3_4 .55_5 0o7_7 0xf_f");
});
test("line comments", () => {
testLex("// foo\n42 // bar\r// baz qux");
});
test("block comments", () => {
testLex(
"1 /* foo */ 2 /* bar\n\nabc */ 3 /*** baz qux **/ " +
"4 /*\\*/ 5 /*/*/ 6 /**/ 7 /* /* \\*\\/ */ 8",
);
});
test("regexes", () => {
testLex("/foo/,/fo\\/o/,/fo\\[[/a\\]]a\\/]o\\\\/,/abc/gimsuy");
});
});
test("lexer errors", () => {
// @ts-ignore
expect(() => testLex(null)).toThrow("Empty expression.");
// @ts-ignore
expect(() => testLex(undefined)).toThrow("Empty expression.");
expect(() => testLex("")).toThrow("Empty expression.");
expect(() => testLex(" ")).toThrow("Empty expression.");
expect(() => testLex('"')).toThrow("Unterminated string literal.");
expect(() => testLex("'")).toThrow("Unterminated string literal.");
expect(() => testLex("'\n'")).toThrow("Unterminated string literal.");
expect(() => testLex("'\r'")).toThrow("Unterminated string literal.");
expect(() => testLex("'\\' 12")).toThrow("Unterminated string literal.");
expect(() => testLex("`${` 12")).toThrow("Unterminated string literal.");
expect(() => testLex('"\\x"')).toThrow("Hexadecimal digit expected.");
expect(() => testLex('"\\x0"')).toThrow("Hexadecimal digit expected.");
expect(() => testLex('"\\u0"')).toThrow("Hexadecimal digit expected.");
expect(() => testLex('"\\u01"')).toThrow("Hexadecimal digit expected.");
expect(() => testLex('"\\u012"')).toThrow("Hexadecimal digit expected.");
expect(() => testLex('"\\u012z"')).toThrow("Hexadecimal digit expected.");
expect(() => testLex('"\\u{1F600A}"')).toThrow(
"An extended Unicode escape allows only values between 0x0 and 0x10FFFF.",
);
expect(() => testLex('"\\u{1F600A"')).toThrow(
"Unterminated Unicode escape sequence.",
);
expect(() => testLex('"\\u{1F600AAAAA"')).toThrow(
"Unterminated Unicode escape sequence.",
);
expect(() => testLex('"\\u{1FW}"')).toThrow(
"Unterminated Unicode escape sequence.",
);
expect(() => testLex('"\\u{1FA')).toThrow(
"Unterminated Unicode escape sequence.",
);
expect(() => testLex("/abc")).toThrow(
"Unterminated regular expression literal.",
);
});
let $ast: Expression[];
function testParse(...expr: string[]) {
$ast = [...expr.map((e) => parse(e))];
}
function precedence(expr: string, expected: string) {
const ast1 = parse(expr);
const ast2 = parse(expected);
expect(ast1).toEqual(ast2);
}
describe("parser", () => {
afterEach(async (ctx) => {
await snap(ctx, $ast);
});
test("primaries", () => {
testParse(
"false",
"true",
"null",
"undefined",
"this",
"super",
"123",
`"abc"`,
"/foo/",
"(123)",
"[123]",
"[123,]",
"[123, 456]",
"[]",
"[_, al]",
"{}",
"{foo}",
"{foo, bar}",
"{foo: 123}",
'{1: "foo"}',
'{"foo:bar": 42}',
"{...foo}",
"{foo: {bar} }",
"{ album: a.title, artist: ar.name, track: t.name }",
"``",
"`abc`",
"`${foo}`",
"`$`",
"`\\${`",
"`${ `a${b}c` }`",
"tag`foo`",
"sql`select * from table`",
"`${{ $id }}`",
"`${ { id: { } } }`",
"sql``.f",
"{ a: `${a.title}!` }",
);
});
test("computed key", () => {
testParse("{[abc]: 123}");
});
test("comma", () => {
testParse("a, 12, b", "(a, 12)", "foo[a, b]");
});
test("spread", () => {
testParse("...foo", "...foo.bar", "foo(...bar)", "foo(...bar, ...baz)");
});
test("arrow", () => {
testParse(
"(foo,baz) => bar",
"foo => bar",
"() => a+b",
"(a, [b, _]) => a+b",
"() => (a, b) => a+b",
"([_, al]) => al.title",
"(al,) => al.title",
);
});
test("ternary", () => {
testParse("foo ? bar : baz", "foo ? bar : baz ? qux : quux");
precedence(
"foo ? bar : baz ? qux : quux",
"foo ? bar : (baz ? qux : quux)",
);
});
test("nullish", () => {
testParse("foo ?? bar", "foo ?? bar ?? baz");
precedence("foo ?? bar ?? baz", "(foo ?? bar) ?? baz");
});
test("logicalOr", () => {
testParse("123 || 456");
precedence("123 || 456 || 789", "(123 || 456) || 789");
precedence("123 || 456 && 789", "123 || (456 && 789)");
});
test("logicalAnd", () => {
testParse("123 && 456", "foo || bar && baz");
precedence("foo && bar && baz", "(foo && bar) && baz");
precedence("foo && bar | baz", "foo && (bar | baz)");
});
test("bitwiseOr", () => {
testParse("123 | 456");
precedence("123 | 456 | 789", "(123 | 456) | 789");
precedence("123 | 456 ^ 789", "123 | (456 ^ 789)");
});
test("bitwiseXor", () => {
testParse("123 ^ 456");
precedence("123 ^ 456 ^ 789", "(123 ^ 456) ^ 789");
precedence("123 ^ 456 & 789", "123 ^ (456 & 789)");
});
test("bitwiseAnd", () => {
testParse("123 & 456");
precedence("123 & 456 & 789", "(123 & 456) & 789");
precedence("123 & 456 === 789", "123 & (456 === 789)");
});
test("equality", () => {
testParse("123 == bar[12]", "123 === 456", "123 != 456", "123 !== 456");
precedence("123 == 456 === 789", "(123 == 456) === 789");
precedence("123 == 456 < 789", "123 == (456 < 789)");
});
test("relational", () => {
testParse("123 < foo.bar", "123 > 456", '123 <= "abc"', "bar >= 456");
precedence("123 < 456 > 789", "(123 < 456) > 789");
precedence("123 < 456 << foo", "123 < (456 << foo)");
});
test("bitwiseShift", () => {
testParse("123 << 456", "123 >> 456", "123 >>> 456");
});
test("additive", () => {
testParse("123 + 456", "123 - 456 * 789");
precedence("123 + 456 + 789", "(123 + 456) + 789");
precedence("123 + 456 * 789", "123 + (456 * 789)");
});
test("multiplicative", () => {
testParse("123 / 456 * 789", "123 % (456 / abc.def)");
precedence("123 * 456 / 789", "(123 * 456) / 789");
precedence("123 * 456 ** 789", "123 * (456 ** 789)");
});
test("exponentiation", () => {
testParse("123 ** 456 ** 789");
precedence("123 ** 456 ** 789", "123 ** (456 ** 789)");
precedence("123 ** -456", "123 ** (-456)");
});
test("prefix", () => {
testParse("+123", "+abc", "-abc", "!abc", "-(foo)", "~foo");
});
test("prefix increment", () => {
testParse(
"++foo",
"--foo",
"void 0",
"typeof foo",
"await obj[key]",
"delete obj[key]",
);
});
test("postfix", () => {
testParse("foo++", "abc--");
});
test("members", () => {
testParse(
"a[10]",
"this.foo",
"foo.bar.baz",
"foo?.bar?.baz",
"foo[bar]",
"foo[bar]?.baz",
"foo[(a, b)]",
);
});
test("new", () => {
testParse("new Foo()", "new Foo(123)", "new x(y)");
});
test("call", () => {
testParse("foo()", "foo.bar()", "foo.bar()[baz?.qux()]", "foo(12, abc)");
});
test("trailing commas", () => {
testParse("foo(12,)", "{foo,}", "(foo,)=>123");
});
});
test("parser errors", () => {
expect(() => testParse("(")).toThrow("Unexpected token: 'eof'.");
expect(() => testParse("new Foo")).toThrow("Expected '(' before arguments.");
expect(() => testParse("new Foo(12")).toThrow("Unexpected token: 'eof'.");
expect(() => testParse("foo.")).toThrow("Unexpected token: 'eof'.");
expect(() => testParse("foo[12")).toThrow(
"Expected token ']' but was 'eof'.",
);
expect(() => testParse("(12")).toThrow("Expected token ')' but was 'eof'.");
expect(() => testParse("12,")).toThrow("Unexpected token: 'eof'.");
expect(() => testParse("(12,)")).toThrow("Unexpected token: ')'.");
expect(() => testParse("foo(12")).toThrow("Unexpected token: 'eof'.");
expect(() => testParse("[,]")).toThrow("Unexpected token: ','.");
expect(() => testParse("foo(,)")).toThrow("Unexpected token: ','.");
expect(() => testParse("{12}")).toThrow("':' expected.");
});