-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest-lang.js
287 lines (233 loc) · 6.41 KB
/
test-lang.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
import test from 'ava';
import lang from '../src/lang.mjs';
import { empty, succeed } from '../src/parser.mjs';
test('lang with a single token literal rule', t => {
let { a } = lang`
a = 'a';
`;
t.deepEqual(a.tryParse('a'), 'a');
t.throws(() => a.tryParse('b'));
t.throws(() => a.tryParse(''));
});
test('lang with a multi char single token literal rule', t => {
let { a } = lang`
a = 'foo';
`;
t.deepEqual(a.tryParse('foo'), 'foo');
t.throws(() => a.tryParse('f'));
t.throws(() => a.tryParse(''));
});
test('lang with a string with escape sequences', t => {
let { a, b, c, d, e } = lang`
a = '\n';
b = '\t';
c = '\u{ffff}';
d = '\\';
e = '\z';
`;
t.deepEqual(a.tryParse('\n'), '\n');
t.deepEqual(b.tryParse('\t'), '\t');
t.deepEqual(c.tryParse('\u{ffff}'), '\u{ffff}');
t.deepEqual(d.tryParse('\\'), '\\');
t.deepEqual(e.tryParse('z'), 'z');
t.throws(() => a.tryParse('f'));
t.throws(() => a.tryParse(''));
});
test('lang with regex rule', t => {
let { a } = lang`
a = /foo/i;
`;
t.deepEqual(a.tryParse('Foo'), 'Foo');
t.deepEqual(a.tryParse('foo'), 'foo');
t.throws(() => a.tryParse('f'));
t.throws(() => a.tryParse(''));
});
test('lang with regex rule with escaped slash', t => {
let { a } = lang`
a = /\\//i;
`;
t.deepEqual(a.tryParse('/'), '/');
t.throws(() => a.tryParse('f'));
t.throws(() => a.tryParse(''));
});
test('lang with regex rule with escaped paren', t => {
let { a } = lang`
a = /\(/i;
`;
t.deepEqual(a.tryParse('('), '(');
t.throws(() => a.tryParse('f'));
t.throws(() => a.tryParse(''));
});
test('lang with a single token literal rule with star', t => {
let { a } = lang`
a = 'a'*;
`;
t.deepEqual(a.tryParse(''), []);
t.deepEqual(a.tryParse('a'), ['a']);
t.deepEqual(a.tryParse('aaa'), ['a', 'a', 'a']);
});
test('lang with a single token literal rule with plus', t => {
let { a } = lang`
a = 'a'+;
`;
t.throws(() => a.tryParse(''));
t.deepEqual(a.tryParse('a'), ['a']);
t.deepEqual(a.tryParse('aaa'), ['a', 'a', 'a']);
});
test('lang with a sequence of token literals', t => {
let { a } = lang`
a = 'a' 'b';
`;
t.deepEqual(a.tryParse('ab'), ['a', 'b']);
t.throws(() => a.tryParse(''));
t.throws(() => a.tryParse('a'));
t.throws(() => a.tryParse('ba'));
});
test('lang with alternation of token literals', t => {
let { a } = lang`
a = 'a' | 'b';
`;
t.deepEqual(a.tryParse('a'), 'a');
t.deepEqual(a.tryParse('b'), 'b');
t.deepEqual(a.tryParse('bc'), 'b');
t.deepEqual(a.tryParse('ba'), 'b');
t.deepEqual(a.tryParse('bb'), 'b');
t.throws(() => a.tryParse(''));
t.throws(() => a.tryParse('c'));
});
test('lang with sequence of an alternative', t => {
let { a } = lang`
a = 'a' | 'b' 'c';
`;
t.deepEqual(a.tryParse('a'), 'a');
t.deepEqual(a.tryParse('abc'), 'a');
t.deepEqual(a.tryParse('bc'), ['b', 'c']);
t.throws(() => a.tryParse(''));
t.throws(() => a.tryParse('b'));
t.throws(() => a.tryParse('c'));
});
test('lang with either "a" or many "b"', t => {
let { a } = lang`
a = 'a' | 'b'*;
`;
t.deepEqual(a.tryParse(''), []);
t.deepEqual(a.tryParse('a'), 'a');
t.deepEqual(a.tryParse('b'), ['b']);
t.deepEqual(a.tryParse('bb'), ['b', 'b']);
});
test('lang with sequence of either "a" or "b"', t => {
let { a } = lang`
a = ('a' | 'b')*;
`;
t.deepEqual(a.tryParse(''), []);
t.deepEqual(a.tryParse('a'), ['a']);
t.deepEqual(a.tryParse('b'), ['b']);
t.deepEqual(a.tryParse('abba'), ['a', 'b', 'b', 'a']);
});
test('lang with two rules', t => {
let { a, b } = lang`
a = 'a';
b = 'b';
`;
t.deepEqual(a.tryParse('a'), 'a');
t.throws(() => a.tryParse('b'));
t.deepEqual(b.tryParse('b'), 'b');
t.throws(() => b.tryParse('a'));
});
test('lang with two rules where b depends on a', t => {
let { a, b } = lang`
a = 'a';
b = a;
`;
t.throws(() => a.tryParse(''));
t.throws(() => a.tryParse('b'));
t.deepEqual(a.tryParse('a'), 'a');
t.throws(() => b.tryParse(''));
t.throws(() => b.tryParse('b'));
t.deepEqual(b.tryParse('a'), 'a');
});
test('lang with a sequence with two rules', t => {
let { c } = lang`
a = 'a';
b = 'b';
c = a b;
`;
t.throws(() => c.tryParse(''));
t.throws(() => c.tryParse('a'));
t.throws(() => c.tryParse('b'));
t.deepEqual(c.tryParse('ab'), ['a', 'b']);
});
test('lang with a satisfies hole', t => {
let { a } = lang`
a = !${ch => ch === 'a'};
`;
t.deepEqual(a.tryParse('a'), 'a');
t.throws(() => a.tryParse('b'));
t.throws(() => a.tryParse(''));
});
test('lang with a parser hole', t => {
let { a } = lang`
a = @${empty()};
`;
t.deepEqual(a.tryParse(''), void 0);
t.throws(() => a.tryParse('b'));
});
test('lang with a satisfies hole and a sequence', t => {
let { a } = lang`
a = !${ch => ch === 'a'} 'b';
`;
t.throws(() => a.tryParse('b'));
t.throws(() => a.tryParse(''));
t.deepEqual(a.tryParse('ab'), ['a', 'b']);
});
test('lang with a map', t => {
let { a } = lang`
a = 'a' > ${value => ({ value })} ;
`;
t.deepEqual(a.tryParse('a'), { value: 'a' });
});
test('map is under sequence', t => {
let { a } = lang`
a = 'a' 'b' > ${value => ({ value })} ;
`;
t.deepEqual(a.tryParse('ab'), { value: ['a', 'b'] });
});
test('map to the left of a sequence', t => {
let { a } = lang`
a = 'a' > ${value => ({ value })} 'b';
`;
t.deepEqual(a.tryParse('ab'), [{ value: 'a' }, 'b']);
});
test('map is under alternation', t => {
let { a } = lang`
a = 'a' | 'b' > ${value => ({ value })} ;
`;
t.deepEqual(a.tryParse('a'), 'a');
t.deepEqual(a.tryParse('b'), { value: 'b' });
});
test('lang with multiple maps that associate to the left', t => {
let { a } = lang`
a = 'a' > ${value => ({ value })} > ${o => o.value + 'b'} ;
`;
t.deepEqual(a.tryParse('a'), 'ab');
});
test('lang with a chain', t => {
let { a } = lang`
a = 'a' >> ${value => succeed({ value })} ;
`;
t.deepEqual(a.tryParse('a'), { value: 'a' });
});
test('calc language', t => {
const { multExpr } = lang`
num = /[0-9]+/ > ${ch => parseInt(ch, 10)};
addExpr = num '+' multExpr > ${([left, op, right]) => left + right}
| num ;
multExpr = addExpr '*' multExpr > ${([left, op, right]) => left * right}
| addExpr ;
`;
t.deepEqual(multExpr.tryParse('1'), 1);
t.deepEqual(multExpr.tryParse('12'), 12);
t.deepEqual(multExpr.tryParse('1+1'), 2);
t.deepEqual(multExpr.tryParse('1+1*2'), 3);
t.deepEqual(multExpr.tryParse('1*1+2'), 3);
});