Repository navigation
Expand file tree
/
Copy pathliterals.ts
More file actions
369 lines (331 loc) · 6.99 KB
/
Copy pathliterals.ts
File metadata and controls
369 lines (331 loc) · 6.99 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
import * as assert from 'assert';
import { describe, expect, test } from 'vitest';
import { } from '../src';
import { NUM, STR, NULL, ARR, OBJ, BOOL, TRUE, FALSE, ERROR ,FN_NATIVE } from '../src/interpreter/value';
import { AiScriptSyntaxError } from '../src/error';
import { exe, eq } from './testutils';
describe('literal', () => {
test.concurrent('string (single quote)', async () => {
const res = await exe(`
<: 'foo'
`);
eq(res, STR('foo'));
});
test.concurrent('string (double quote)', async () => {
const res = await exe(`
<: "foo"
`);
eq(res, STR('foo'));
});
test.concurrent('Escaped double quote', async () => {
const res = await exe('<: "ai saw a note \\"bebeyo\\"."');
eq(res, STR('ai saw a note "bebeyo".'));
});
test.concurrent('Escaped single quote', async () => {
const res = await exe('<: \'ai saw a note \\\'bebeyo\\\'.\'');
eq(res, STR('ai saw a note \'bebeyo\'.'));
});
test.concurrent('bool (true)', async () => {
const res = await exe(`
<: true
`);
eq(res, BOOL(true));
});
test.concurrent('bool (false)', async () => {
const res = await exe(`
<: false
`);
eq(res, BOOL(false));
});
test.concurrent('number (Int)', async () => {
const res = await exe(`
<: 10
`);
eq(res, NUM(10));
});
test.concurrent('number (Float)', async () => {
const res = await exe(`
<: 0.5
`);
eq(res, NUM(0.5));
});
/* 指数表記仕様が必要になった段階で有効化する
test.concurrent('number (positive exponent without plus sign)', async () => {
const res = await exe(`
<: 1.2e3
`);
eq(res, NUM(1200));
});
test.concurrent('number (positive exponent with plus sign)', async () => {
const res = await exe(`
<: 1.2e+3
`);
eq(res, NUM(1200));
});
test.concurrent('number (negative exponent)', async () => {
const res = await exe(`
<: 1.2e-3
`);
eq(res, NUM(0.0012));
});
test.concurrent('number (missing exponent)', async () => {
assert.rejects(() => exe(`
<: 1.2e+
`), 'exponent expected');
});
*/
test.concurrent('arr (separated by comma)', async () => {
const res = await exe(`
<: [1, 2, 3]
`);
eq(res, ARR([NUM(1), NUM(2), NUM(3)]));
});
test.concurrent('arr (separated by comma) (with trailing comma)', async () => {
const res = await exe(`
<: [1, 2, 3,]
`);
eq(res, ARR([NUM(1), NUM(2), NUM(3)]));
});
test.concurrent('arr (separated by line break)', async () => {
const res = await exe(`
<: [
1
2
3
]
`);
eq(res, ARR([NUM(1), NUM(2), NUM(3)]));
});
test.concurrent('arr (separated by line break and comma)', async () => {
const res = await exe(`
<: [
1,
2,
3
]
`);
eq(res, ARR([NUM(1), NUM(2), NUM(3)]));
});
test.concurrent('arr (separated by line break and comma) (with trailing comma)', async () => {
const res = await exe(`
<: [
1,
2,
3,
]
`);
eq(res, ARR([NUM(1), NUM(2), NUM(3)]));
});
test.concurrent('obj (separated by comma)', async () => {
const res = await exe(`
<: { a: 1, b: 2, c: 3 }
`);
eq(res, OBJ(new Map([['a', NUM(1)], ['b', NUM(2)], ['c', NUM(3)]])));
});
test.concurrent('obj (separated by comma) (with trailing comma)', async () => {
const res = await exe(`
<: { a: 1, b: 2, c: 3, }
`);
eq(res, OBJ(new Map([['a', NUM(1)], ['b', NUM(2)], ['c', NUM(3)]])));
});
test.concurrent('obj (separated by line break)', async () => {
const res = await exe(`
<: {
a: 1
b: 2
c: 3
}
`);
eq(res, OBJ(new Map([['a', NUM(1)], ['b', NUM(2)], ['c', NUM(3)]])));
});
test.concurrent('obj (string key)', async () => {
const res = await exe(`
<: {
"藍": 42,
}
`);
eq(res, OBJ(new Map([['藍', NUM(42)]])));
});
describe('obj (reserved word as key)', async () => {
test.each([
['null'],
['true'],
['false'],
['each'],
['for'],
['loop'],
['do'],
['while'],
['break'],
['continue'],
['match'],
['case'],
['default'],
['if'],
['elif'],
['else'],
['return'],
['eval'],
['var'],
['let'],
['exists'],
// unused keywords
['as'],
['async'],
['attr'],
['attribute'],
['await'],
['catch'],
['class'],
['component'],
['constructor'],
['dictionary'],
['enum'],
['export'],
['finally'],
['fn'],
['hash'],
['in'],
['interface'],
['out'],
['private'],
['public'],
['ref'],
['static'],
['struct'],
['table'],
['this'],
['throw'],
['trait'],
['try'],
['undefined'],
['use'],
['using'],
['when'],
['while'],
['yield'],
['import'],
['is'],
['meta'],
['module'],
['namespace'],
['new']
])('key "%s"', async (key) => {
const res = await exe(`
<: {
${key}: 42,
}
`);
eq(res, OBJ(new Map([[key, NUM(42)]])));
});
});
test.concurrent('obj (shorthand)', async () => {
const res = await exe(`
let a = 1
<: { a }
`);
eq(res, OBJ(new Map([['a', NUM(1)]])));
});
test.concurrent('obj (reserved word as shorthand)', async () => {
await expect(() => exe(`
<: { exists }
`)).rejects.toThrow(AiScriptSyntaxError);
});
test.concurrent('obj (string as shorthand)', async () => {
await expect(() => exe(`
<: { "hoge" }
`)).rejects.toThrow(AiScriptSyntaxError);
});
test.concurrent('obj (escaped reserved word as key)', async () => {
await expect(async () => await exe(`
<: {
\\u0064\\u0065\\u0066\\u0061\\u0075\\u006c\\u0074: 42,
}
`)).rejects.toThrow(AiScriptSyntaxError);
})
test.concurrent('obj (duplicate key)', async () => {
await expect(() => exe(`
<: { hoge: 1, hoge: 2 }
`)).rejects.toThrow(AiScriptSyntaxError);
});
test.concurrent('obj (invalid key)', async () => {
assert.rejects(() => exe(`
<: {
42: 42,
}
`));
});
test.concurrent('obj and arr (separated by line break)', async () => {
const res = await exe(`
<: {
a: 1
b: [
1
2
3
]
c: 3
}
`);
eq(res, OBJ(new Map<string, any>([
['a', NUM(1)],
['b', ARR([NUM(1), NUM(2), NUM(3)])],
['c', NUM(3)]
])));
});
});
describe('Template syntax', () => {
test.concurrent('Basic', async () => {
const res = await exe(`
let str = "kawaii"
<: \`Ai is {str}!\`
`);
eq(res, STR('Ai is kawaii!'));
});
test.concurrent('convert to str', async () => {
const res = await exe(`
<: \`1 + 1 = {(1 + 1)}\`
`);
eq(res, STR('1 + 1 = 2'));
});
test.concurrent('invalid', async () => {
try {
await exe(`
<: \`{hoge}\`
`);
} catch (e) {
assert.ok(true);
return;
}
assert.fail();
});
test.concurrent('Escape', async () => {
const res = await exe(`
let message = "Hello"
<: \`\\\`a\\{b\\}c\\\`\`
`);
eq(res, STR('`a{b}c`'));
});
test.concurrent('nested brackets', async () => {
const res = await exe(`
<: \`{if true {1} else {2}}\`
`);
eq(res, STR('1'));
});
test.concurrent('new line before', async () => {
const res = await exe(`
<: \`{"Hello"
// comment
}\`
`);
eq(res, STR('Hello'));
});
test.concurrent('new line after', async () => {
const res = await exe(`
<: \`{
// comment
"Hello"}\`
`);
eq(res, STR('Hello'));
});
});