-
Notifications
You must be signed in to change notification settings - Fork 25
/
peg.test.scad
379 lines (347 loc) · 15.3 KB
/
peg.test.scad
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
include <peg.scad>
// PEG ENGINE TESTS
echo(_unit_test(
"slice",
[
//TODO: more tests
_slice(["literal", "a"], 1, -1), ["a"]
]
));
echo(_unit_test(
"empty_string",
[
_match_parsed_peg("", undef, 1, ["empty_string"]), undef,
_match_parsed_peg(undef, undef, 0, ["empty_string"]), undef,
_match_parsed_peg("", undef, 0, ["empty_string"])[_PARSED], [],
_match_parsed_peg("a", undef, 0, ["empty_string"])[_PARSED], [],
]
));
echo(_unit_test(
"private",
[
_match_parsed_peg("a", undef, 0, ["private", ["literal", "a"]])[_PARSED], [] ,
_match_parsed_peg("abc", undef, 0,
["private",
["sequence",
["literal", "a"],
["literal", "b"],
["literal", "c"]
]
])[_PARSED], [] ,
_match_parsed_peg("ab", undef, 0, peg_op=["sequence", ["private", ["literal", "a"]], ["literal", "b"]])[_PARSED], ["b"],
_match_parsed_peg("ba", undef, 0, peg_op=["sequence", ["literal", "b"], ["private", ["literal", "a"]] ])[_PARSED], ["b"],
_match_parsed_peg("abc", undef, 0, peg_op=["sequence", ["literal", "a"], ["private", ["literal", "b"]], ["literal", "c"] ])[_PARSED], ["ac"]
]
));
echo(_unit_test(
"wildcard",
[
_match_parsed_peg("a", undef, 0, ["wildcard"])[_PARSED], ["a"],
_match_parsed_peg("ab", undef, 0, ["wildcard"])[_PARSED], ["a"],
_match_parsed_peg("", undef, 0, ["wildcard"]), undef
]
));
echo(_unit_test(
"literal",
[
_match_parsed_peg("a", undef, 0, ["literal", "a"])[_PARSED], ["a"],
_match_parsed_peg("ab", undef, 0, ["literal", "ab"])[_PARSED], ["ab"],
_match_parsed_peg("c", undef, 0, ["literal", "a"]), undef
]
));
echo(_unit_test(
"character_set_shorthand",
[
_match_parsed_peg("a", undef, 0, ["character_set_shorthand", "s"]), undef,
_match_parsed_peg("A", undef, 0, ["character_set_shorthand", "s"]), undef,
_match_parsed_peg("0", undef, 0, ["character_set_shorthand", "s"]), undef,
_match_parsed_peg(" ", undef, 0, ["character_set_shorthand", "s"])[_PARSED], [" "],
_match_parsed_peg("\t", undef, 0, ["character_set_shorthand", "s"])[_PARSED], ["\t"],
_match_parsed_peg("\n", undef, 0, ["character_set_shorthand", "s"])[_PARSED], ["\n"],
_match_parsed_peg("\r", undef, 0, ["character_set_shorthand", "s"])[_PARSED], ["\r"],
_match_parsed_peg("a", undef, 0, ["character_set_shorthand", "S"])[_PARSED], ["a"],
_match_parsed_peg("A", undef, 0, ["character_set_shorthand", "S"])[_PARSED], ["A"],
_match_parsed_peg("0", undef, 0, ["character_set_shorthand", "S"])[_PARSED], ["0"],
_match_parsed_peg(" ", undef, 0, ["character_set_shorthand", "S"])[_PARSED], undef,
_match_parsed_peg("\t", undef, 0, ["character_set_shorthand", "S"])[_PARSED], undef,
_match_parsed_peg("\n", undef, 0, ["character_set_shorthand", "S"])[_PARSED], undef,
_match_parsed_peg("\r", undef, 0, ["character_set_shorthand", "S"])[_PARSED], undef,
_match_parsed_peg("a", undef, 0, ["character_set_shorthand", "w"])[_PARSED], ["a"],
_match_parsed_peg("A", undef, 0, ["character_set_shorthand", "w"])[_PARSED], ["A"],
_match_parsed_peg("0", undef, 0, ["character_set_shorthand", "w"])[_PARSED], ["0"],
_match_parsed_peg("_", undef, 0, ["character_set_shorthand", "w"])[_PARSED], ["_"],
_match_parsed_peg(" ", undef, 0, ["character_set_shorthand", "w"])[_PARSED], undef,
_match_parsed_peg("a", undef, 0, ["character_set_shorthand", "W"])[_PARSED], undef,
_match_parsed_peg("A", undef, 0, ["character_set_shorthand", "W"])[_PARSED], undef,
_match_parsed_peg("0", undef, 0, ["character_set_shorthand", "W"])[_PARSED], undef,
_match_parsed_peg("_", undef, 0, ["character_set_shorthand", "W"])[_PARSED], undef,
_match_parsed_peg(" ", undef, 0, ["character_set_shorthand", "W"])[_PARSED], [" "],
_match_parsed_peg("a", undef, 0, ["character_set_shorthand", "d"])[_PARSED], undef,
_match_parsed_peg("A", undef, 0, ["character_set_shorthand", "d"])[_PARSED], undef,
_match_parsed_peg("0", undef, 0, ["character_set_shorthand", "d"])[_PARSED], ["0"],
_match_parsed_peg(" ", undef, 0, ["character_set_shorthand", "d"])[_PARSED], undef,
_match_parsed_peg("a", undef, 0, ["character_set_shorthand", "D"])[_PARSED], ["a"],
_match_parsed_peg("A", undef, 0, ["character_set_shorthand", "D"])[_PARSED], ["A"],
_match_parsed_peg("0", undef, 0, ["character_set_shorthand", "D"])[_PARSED], undef,
_match_parsed_peg(" ", undef, 0, ["character_set_shorthand", "D"])[_PARSED], [" "],
_match_parsed_peg("\\", undef, 0, ["character_set_shorthand", "\\"])[_PARSED], ["\\"],
_match_parsed_peg("a", undef, 0, ["character_set_shorthand", "\\"])[_PARSED], undef,
]
));
echo(_unit_test("character_range",
[
_match_parsed_peg("a", undef, 0, ["character_range", "az"])[_PARSED], ["a"],
_match_parsed_peg("A", undef, 0, ["character_range", "az"]), undef,
_match_parsed_peg("z", undef, 0, ["character_range", "az"])[_PARSED], ["z"],
_match_parsed_peg("0", undef, 0, ["character_range", "az"]), undef,
_match_parsed_peg(" ", undef, 0, ["character_range", "az"]), undef,
_match_parsed_peg("a", undef, 0, ["character_range", "bz"]), undef,
_match_parsed_peg("z", undef, 0, ["character_range", "ay"]), undef,
_match_parsed_peg("a", undef, 0, ["character_range", "AZ"]), undef,
_match_parsed_peg("A", undef, 0, ["character_range", "AZ"])[_PARSED], ["A"],
_match_parsed_peg("z", undef, 0, ["character_range", "AZ"]), undef,
_match_parsed_peg("Z", undef, 0, ["character_range", "AZ"])[_PARSED], ["Z"],
_match_parsed_peg("0", undef, 0, ["character_range", "AZ"]), undef,
_match_parsed_peg(" ", undef, 0, ["character_range", "AZ"]), undef,
_match_parsed_peg("A", undef, 0, ["character_range", "BZ"]), undef,
_match_parsed_peg("Z", undef, 0, ["character_range", "AY"]), undef,
_match_parsed_peg("a", undef, 0, ["character_range", "09"]), undef,
_match_parsed_peg("A", undef, 0, ["character_range", "09"]), undef,
_match_parsed_peg("0", undef, 0, ["character_range", "09"])[_PARSED], ["0"],
_match_parsed_peg("9", undef, 0, ["character_range", "09"])[_PARSED], ["9"],
_match_parsed_peg(" ", undef, 0, ["character_range", "09"]), undef,
_match_parsed_peg("0", undef, 0, ["character_range", "19"]), undef,
_match_parsed_peg("9", undef, 0, ["character_range", "08"]), undef,
]
));
echo(_unit_test("positive_character_set",
[
_match_parsed_peg("a", undef, 0, ["positive_character_set", "a"])[_PARSED], ["a"],
_match_parsed_peg("b", undef, 0, ["positive_character_set", "a"]), undef,
_match_parsed_peg("a", undef, 0, ["positive_character_set", ["character_set_shorthand", "w"]])[_PARSED], ["a"],
_match_parsed_peg(" ", undef, 0, ["positive_character_set", ["character_set_shorthand", "w"]]), undef,
_match_parsed_peg("0", undef, 0, ["positive_character_set", ["character_set_shorthand", "w"], "0"])[_PARSED], ["0"],
_match_parsed_peg("a", undef, 0, ["positive_character_set", ["character_range", "ac"]])[_PARSED], ["a"],
_match_parsed_peg("z", undef, 0, ["positive_character_set", ["character_range", "ac"]]), undef,
_match_parsed_peg("z", undef, 0, ["positive_character_set", ["character_range", "ac"], "z"])[_PARSED], ["z"],
]
));
echo(_unit_test("negative_character_set",
[
_match_parsed_peg("a", undef, 0, ["negative_character_set", "a"]), undef,
_match_parsed_peg("b", undef, 0, ["negative_character_set", "a"])[_PARSED], ["b"],
_match_parsed_peg("a", undef, 0, ["negative_character_set", ["character_set_shorthand", "w"]]), undef,
_match_parsed_peg(" ", undef, 0, ["negative_character_set", ["character_set_shorthand", "w"]])[_PARSED], [" "],
_match_parsed_peg("0", undef, 0, ["negative_character_set", ["character_set_shorthand", "w"], "0"]), undef,
_match_parsed_peg("a", undef, 0, ["negative_character_set", ["character_range", "ac"]]), undef,
_match_parsed_peg("z", undef, 0, ["negative_character_set", ["character_range", "ac"]])[_PARSED], ["z"],
_match_parsed_peg("z", undef, 0, ["negative_character_set", ["character_range", "ac"], "z"]), undef,
]
));
echo(_unit_test(
"rule",
[
_match_parsed_peg("a", undef, 0, ["rule", "A", ["literal", "a"]])[_PARSED], [["A", "a"]],
_match_parsed_peg("b", undef, 0, ["rule", "A", ["literal", "a"]]), undef,
_match_parsed_peg("a", undef, 0, ["private_rule", "A", ["literal", "a"]])[_PARSED], ["a"],
_match_parsed_peg("b", undef, 0, ["private_rule", "A", ["literal", "a"]]), undef
]
));
echo(_unit_test(
"negative_lookahead",
[
_match_parsed_peg("b", undef, 0, ["negative_lookahead", ["literal", "a"]])[_PARSED], [],
_match_parsed_peg("a", undef, 0, ["negative_lookahead", ["literal", "a"]]), undef,
_match_parsed_peg("ab", undef, 0, ["sequence", ["negative_lookahead", ["literal", "b"]], ["literal", "a"]])[_PARSED], ["a"],
_match_parsed_peg("ab", undef, 0,
["sequence",
["negative_lookahead",
["sequence",
["literal", "a"],
["literal", "b"]
]
],
["literal", "a"],
]), undef,
_match_parsed_peg("ab", undef, 0,
["sequence",
["negative_lookahead",
["sequence",
["literal", "b"],
["literal", "a"]
]
],
["literal", "a"],
])[_PARSED], ["a"],
]
));
echo(_unit_test(
"positive_lookahead",
[
_match_parsed_peg("b", undef, 0, ["positive_lookahead", ["literal", "a"]]), undef,
_match_parsed_peg("a", undef, 0, ["positive_lookahead", ["literal", "a"]])[_PARSED], []
]
));
echo(_unit_test(
"choice",
[
_match_parsed_peg("a", undef, 0, ["choice", ["literal", "a"], ["literal", "b"]])[_PARSED], ["a"],
_match_parsed_peg("b", undef, 0, ["choice", ["literal", "a"], ["literal", "b"]])[_PARSED], ["b"],
_match_parsed_peg("c", undef, 0, ["choice", ["literal", "a"], ["literal", "b"]]), undef,
_match_parsed_peg("a", undef, 0, ["choice", ["literal", "a"], ["literal", "b"], ["literal", "c"]])[_PARSED], ["a"],
_match_parsed_peg("b", undef, 0, ["choice", ["literal", "a"], ["literal", "b"], ["literal", "c"]])[_PARSED], ["b"],
_match_parsed_peg("c", undef, 0, ["choice", ["literal", "a"], ["literal", "b"], ["literal", "c"]])[_PARSED], ["c"],
_match_parsed_peg("d", undef, 0, ["choice", ["literal", "a"], ["literal", "b"], ["literal", "c"]]), undef,
_match_parsed_peg("ab", undef, 0, ["choice", ["literal", "a"], ["literal", "ab"]])[_PARSED], ["a"],
_match_parsed_peg("ab", undef, 0, ["choice", ["literal", "ab"], ["literal", "a"]])[_PARSED], ["ab"]
]
));
echo(_unit_test(
"sequence",
[
_match_parsed_peg("ab", undef, 0, ["sequence", ["literal", "a"]])[_PARSED], ["a"],
_match_parsed_peg("ab", undef, 0, ["sequence", ["literal", "a"], ["literal", "b"]])[_PARSED], ["ab"],
_match_parsed_peg("abc", undef, 0, ["sequence", ["literal", "a"], ["literal", "b"], ["literal", "c"]])[_PARSED], ["abc"],
_match_parsed_peg("abc", undef, 0, ["sequence", ["literal", "ab"], ["literal", "c"]])[_PARSED], ["abc"],
_match_parsed_peg("a", undef, 0, ["sequence", ["literal", "a"], ["literal", "b"]]), undef,
_match_parsed_peg("b", undef, 0, ["sequence", ["literal", "a"], ["literal", "b"]]), undef,
_match_parsed_peg("c", undef, 0, ["sequence", ["literal", "a"], ["literal", "b"]]), undef,
_match_parsed_peg("^abcdcdab$", undef, 0,
["sequence",
["literal", "^"],
["zero_to_many",
["choice",
["sequence",
["literal", "a"],
["literal", "b"]
],
["sequence",
["literal", "c"],
["literal", "d"]
]
],
],
["literal", "$"]
])[_PARSED], ["^abcdcdab$"]
]
));
echo(_unit_test(
"zero_to_many",
[
_match_parsed_peg("", undef, 0, ["zero_to_many", ["literal", "a"]])[_PARSED], [],
_match_parsed_peg("b", undef, 0, ["zero_to_many", ["literal", "a"]])[_PARSED], [],
_match_parsed_peg("a", undef, 0, ["zero_to_many", ["literal", "a"]])[_PARSED], ["a"],
_match_parsed_peg("aaa", undef, 0, ["zero_to_many", ["literal", "a"]])[_PARSED], ["aaa"]
]
));
echo(_unit_test(
"one_to_many",
[
_match_parsed_peg("", undef, 0, ["one_to_many", ["literal", "a"]]), undef,
_match_parsed_peg("b", undef, 0, ["one_to_many", ["literal", "a"]]), undef,
_match_parsed_peg("a", undef, 0, ["one_to_many", ["literal", "a"]])[_PARSED], ["a"],
_match_parsed_peg("aaa", undef, 0, ["one_to_many", ["literal", "a"]])[_PARSED], ["aaa"]
]
));
echo(_unit_test(
"zero_to_one",
[
_match_parsed_peg("a", undef, 0, ["zero_to_one", ["literal", "a"]])[_PARSED], ["a"],
_match_parsed_peg("b", undef, 0, ["zero_to_one", ["literal", "a"]])[_PARSED], []
]
));
echo(_unit_test(
"many_to_many",
[
_match_parsed_peg("", undef, 0, ["many_to_many", ["literal", "a"], "25"])[_PARSED], undef,
_match_parsed_peg("a", undef, 0, ["many_to_many", ["literal", "a"], "25"])[_PARSED], undef,
_match_parsed_peg("aa", undef, 0, ["many_to_many", ["literal", "a"], "25"])[_PARSED], ["aa"],
_match_parsed_peg("aaa", undef, 0, ["many_to_many", ["literal", "a"], "25"])[_PARSED], ["aaa"],
_match_parsed_peg("aaaa", undef, 0, ["many_to_many", ["literal", "a"], "25"])[_PARSED], ["aaaa"],
_match_parsed_peg("aaaaa", undef, 0, ["many_to_many", ["literal", "a"], "25"])[_PARSED], ["aaaaa"],
_match_parsed_peg("aaaaaa", undef, 0, ["many_to_many", ["literal", "a"], "25"])[_PARSED], ["aaaaa"],
_match_parsed_peg("", undef, 0, ["many_to_many", ["literal", "a"], [2, 5]])[_PARSED], undef,
_match_parsed_peg("a", undef, 0, ["many_to_many", ["literal", "a"], [2, 5]])[_PARSED], undef,
_match_parsed_peg("aa", undef, 0, ["many_to_many", ["literal", "a"], [2, 5]])[_PARSED], ["aa"],
_match_parsed_peg("aaa", undef, 0, ["many_to_many", ["literal", "a"], [2, 5]])[_PARSED], ["aaa"],
_match_parsed_peg("aaaa", undef, 0, ["many_to_many", ["literal", "a"], [2, 5]])[_PARSED], ["aaaa"],
_match_parsed_peg("aaaaa", undef, 0, ["many_to_many", ["literal", "a"], [2, 5]])[_PARSED], ["aaaaa"],
_match_parsed_peg("aaaaaa", undef, 0, ["many_to_many", ["literal", "a"], [2, 5]])[_PARSED], ["aaaaa"],
_match_parsed_peg("", undef, 0, ["many_to_many", ["literal", "a"], "2"])[_PARSED], undef,
_match_parsed_peg("a", undef, 0, ["many_to_many", ["literal", "a"], "2"])[_PARSED], undef,
_match_parsed_peg("aa", undef, 0, ["many_to_many", ["literal", "a"], "2"])[_PARSED], ["aa"],
_match_parsed_peg("aaa", undef, 0, ["many_to_many", ["literal", "a"], "2"])[_PARSED], ["aaa"],
_match_parsed_peg("aaaa", undef, 0, ["many_to_many", ["literal", "a"], "2"])[_PARSED], ["aaaa"],
_match_parsed_peg("aaaaa", undef, 0, ["many_to_many", ["literal", "a"], "2"])[_PARSED], ["aaaaa"],
_match_parsed_peg("aaaaaa", undef, 0, ["many_to_many", ["literal", "a"], "2"])[_PARSED], ["aaaaaa"],
]
));
echo(_unit_test(
"ref",
[
_match_parsed_peg("a", [["rule", "A", ["literal", "a"]]], 0, ["ref", 0])[_PARSED], [["A", "a"]],
_match_parsed_peg("ab",
[
["rule", "A",
["sequence",
["literal", "a"],
["ref", 1]
]
],
["rule", "B", ["literal", "b"]]
],
0, ["ref", 0])[_PARSED], [["A", "a", ["B", "b"]]],
_match_parsed_peg("a", [["private_rule", "A", ["literal", "a"]]], 0, ["ref", 0])[_PARSED], ["a"],
_match_parsed_peg("ab",
[
["private_rule", "A",
["sequence",
["literal", "a"],
["ref", 1]
]
],
["rule", "B", ["literal", "b"]]
],
0, ["ref", 0])[_PARSED], ["a", ["B", "b"]],
_match_parsed_peg("ab",
[
["rule", "A",
["sequence",
["literal", "a"],
["ref", 1]
]
],
["private_rule", "B", ["literal", "b"]]
],
0, ["ref", 0])[_PARSED], [["A", "ab"]],
_match_parsed_peg("ab",
[
["sequence",
["ref", 1],
["ref", 2],
],
["rule", "A",
["literal", "a"]
],
["rule", "B",
["literal", "b"]
]
],
0, ["ref", 0])[_PARSED], [["A", "a"], ["B", "b"]]
]
));
echo(_unit_test(
"grammar",
[
_match_parsed_peg("ab", undef, 0,
["grammar",
["rule", "A",
["sequence",
["literal", "a"],
["ref", 2]
]
],
["rule", "B", ["literal", "b"]]
]), ["A", "a", ["B", "b"]]
]
));