This repository has been archived by the owner on Nov 13, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ometa-tests.rkt
646 lines (574 loc) · 18 KB
/
ometa-tests.rkt
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
#lang racket/base
(require rackunit
rackunit/text-ui
racket/match
racket/list
"helpers.rkt"
"ometa.rkt")
(debug-off!)
;; Add your test cases to a suite:
;; ===============================
;; (om-test-case
;; "Name of the case"
;; input ; string or list
;; (ometa ...) ; ometa program with Start rule
;; pattern ; check-match pattern
;; predicate) ; check-match predicate (optional)
;; Make them run:
;; ===========================
;; Add your suits to (run-suites ...)
(define-ometa-namespace ns)
(define-syntax om-test-case
(syntax-rules ()
[(_ case-name input omprog . template)
(test-case case-name
(check-match (omatch omprog Start input ns)
. template))]))
(define-syntax-rule (run-suites suite ...)
(lambda ()
(let ((delim (lambda (l) (list->string (build-list l (lambda (n) #\*))))))
(begin
(printf "~n~a~n~a~n"
(quote suite)
(delim (string-length (symbol->string (quote suite)))))
(run-tests suite)
(newline))
...)))
;; ================================================= ;;
;; Add your suites here ;;
;; ================================================= ;;
(define suites-to-run
(run-suites string-suite
list-suite
left-recursion-suite
scoping-suite
binding-suite
semantic-suite
parameterized-rules-suite
oo-suite
))
;; ================================================= ;;
;; Suite: bindings ;;
;; ================================================= ;;
(define binding-suite
(test-suite
"Binding."
;; ------------------------------------------ ;;
(om-test-case
"Simple binding."
'(1 2)
(ometa
(Start (seq* (list
(seq* (bind x (atom 1))
(bind y (atom 2))))
(-> (list x y)))))
(list (list 1 2)
_
(list-no-order `(x ,_) `(y ,_) _ ...)))
;; ------------------------------------------ ;;
(om-test-case
"Binding call to apply."
'(1 (2 3))
(ometa
(Start (seq*
(list
(seq*
(bind x (apply anything))
(bind y (alt* (atom 3)
(apply Start)))))
(-> (list x y)))))
(list '(1 (2 3))
_
(list-no-order `(x ,_) `(y ,_) _ ...)))
)
)
;; ================================================= ;;
;; Suite: left recursion ;;
;; ================================================= ;;
(define left-recursion-suite
(test-suite
"Left recursion."
;; ------------------------------------------ ;;
;; expr = expr:x - num:y -> (list x y)
;; | expr:x + num:y -> (list x y)
;; | num
(om-test-case
"Nested direct left recursion."
"1+2-3"
(ometa
(Start (alt* (seq* (bind x (apply Start))
(atom #\-)
(bind y (apply N))
(-> (list 'sub x y)))
(seq* (bind x (apply Start))
(atom #\+)
(bind y (apply N))
(-> (list 'add x y)))
(apply N)))
(N (alt* (atom #\1)
(atom #\2)
(atom #\3))))
;; Pattern
(list '(sub (add #\1 #\2) #\3)
stream
_)
;; Predicate
(null? stream))
;; ------------------------------------------ ;;
;; expr = expr:x - num:y -> (list x y)
;; | num
(om-test-case
"Direct left recursion"
"1-2-3"
(ometa
(Start (alt* (seq* (bind x (apply Start))
(atom #\-)
(bind y (apply N))
(-> (list x y)))
(apply N)))
(N (alt* (atom #\1)
(atom #\2)
(atom #\3))))
;; Pattern
(list '((#\1 #\2) #\3)
stream
_)
;; Predicate
(null? stream))))
;; ================================================= ;;
;; Suite: matching lists ;;
;; ================================================= ;;
(define list-suite
(test-suite
"Matching on structured data (lists)."
;; ------------------------------------------ ;;
(om-test-case
"Empty list match."
'()
(ometa
(Start (list (apply End)))
(End (~ (apply anything))))
ans
(success? ans))
(om-test-case
"Empty list don't match."
'()
(ometa
(Start (list (list (apply End))))
(End (~ (apply anything))))
ans
(fail? ans))
(om-test-case
"Nested empty lists match."
'(())
(ometa
(Start (list (list (apply End))))
(End (~ (apply anything))))
ans
(success? ans))
;; ------------------------------------------ ;;
;; A = (10:x B:y) -> (list x y)
;; B = (C 12 anything)
;; | (C 13 anything)
;; C = 12
(om-test-case
"Nested list of numbers."
'(10 (12 13 15))
(ometa
(Start (seq* (list (seq* (bind x (atom 10))
(bind y (apply B))))
(-> (list x y))))
(B (alt* (list (seq* (apply C)
(atom 12)
(apply anything)))
(list (seq* (apply C)
(atom 13)
(apply anything)))))
(C (atom 12)))
;; Pattern
(list `(10 (12 13 15))
(? null?)
_))
;; ------------------------------------------ ;;
;; Not idiomatic OMeta - use example above instead
;; Works because of explicit grouping with (seq ...)
;; A = (10:x B:y) -> (list x y)
;; B = ((seq C 12) | (seq C 13) anything)
;; C = 12
(om-test-case
"Nested list of numbers. Alt inside expression."
'(10 (12 13 15))
(ometa
(Start (seq* (list (seq* (bind x (atom 10))
(bind y (apply B))))
(-> (list x y))))
(B (list (seq* (alt* (seq* (apply C) (atom 12))
(seq* (apply C) (atom 13)))
(apply anything))))
(C (atom 12)))
;; Pattern
(list `(10 (12 13 15))
(? null?)
_))))
;; ================================================= ;;
;; Suite: matching strings ;;
;; ================================================= ;;
(define string-suite
(test-suite
"Matching strings"
;; ------------------------------------------ ;;
(om-test-case
"Empty stream"
""
(ometa
(Start (~ (apply anything))))
ans
(success? ans))
(om-test-case
"Empty stream"
"a"
(ometa
(Start (~ (apply anything))))
ans
(fail? ans))
;; ------------------------------------------ ;;
(om-test-case
"Unlimited seq and alt"
"abc"
(ometa
(Start (seq* (bind a (atom #\a))
(bind b (atom #\b))
(bind c (apply B))
(-> (list a b c))))
(B (alt* (atom #\a)
(atom #\b)
(atom #\c))))
;; Pattern
(list `(#\a #\b #\c)
(? null?)
_))
;; ------------------------------------------ ;;
(om-test-case
"End of stream"
"abc"
(ometa
(Start (seq* (atom #\a)
(~ (apply anything)))))
;; Pattern
(list 'FAIL _ ...))
(om-test-case
"Look-ahead and end of stream 1"
"abc"
(ometa
(Start (seq* (atom #\a) (apply B)))
(B (alt* (seq* (atom #\b) (~ (~ (atom #\c))))
(apply END)))
(END (~ (apply anything))))
ans
(and (success? ans)
(not (null? (value-stream ans)))))
;; ------------------------------------------ ;;
(om-test-case
"Look-ahead and end of stream 2 (fail case)"
"ab"
(ometa
(Start (seq* (atom #\a) (apply B)))
(B (alt* (seq* (atom #\b) (~ (~ (atom #\c))))
(apply END)))
(END (~ (apply anything))))
ans
(fail? ans))
;; ------------------------------------------ ;;
(om-test-case
"Look-ahead and end of stream 3"
"a"
(ometa
(Start (seq* (atom #\a) (apply B)))
(B (alt* (seq* (atom #\b) (~ (~ (atom #\c))))
(apply END)))
(END (~ (apply anything))))
ans
(success? ans))))
;; ================================================= ;;
;; Suite: scope ;;
;; ================================================= ;;
(define scoping-suite
(test-suite
"Lexical scoping and bindings."
;; ------------------------------------------ ;;
(om-test-case
"Lexical scope"
'(1 2)
(ometa
(Start (seq*
(list (bind l (apply inside)))
(-> l)))
(inside (alt*
(seq* (bind x (apply anything))
(bind t (apply inside))
(-> (cons x t)))
(apply end)))
(end (seq*
(~ (apply anything))
(-> null))))
(list '(1 2) _ ...))
;; ------------------------------------------ ;;
(om-test-case
"Flatten a nested list."
'(1 (2 (3 4)) (((5)) 6))
(ometa
(Start (seq*
(list (bind xs (apply inside)))
(-> xs)))
(inside (alt* (seq*
(list (bind xs (apply inside)))
(bind ys (apply inside))
(-> (append xs ys)))
(seq*
(bind x (apply anything))
(bind xs (apply inside))
(-> (cons x xs)))
(seq*
(apply end)
(-> null))))
(end (seq*
(~ (apply anything))
(-> null))))
(list '(1 2 3 4 5 6)
(? empty?)
_))
))
;; ================================================= ;;
;; Semantic actions and predicates ;;
;; ================================================= ;;
(define semantic-suite
(test-suite
"Actions and predicates."
;; ------------------------------------------ ;;
(om-test-case
"Lower-case word."
"abc"
(ometa
(char (seq* (bind c (apply anything))
(->? (char? c))
(-> c)))
(lower (seq* (bind c (apply char))
(->? (char<=? #\a c #\z))
(-> c)))
(end (~ (apply anything)))
(Start (seq* (bind word (many (apply lower)))
(apply end)
(-> (list->string word)))))
(list "abc"
_
_))
;; ------------------------------------------ ;;
(om-test-case
"Lower-case word fail."
"abC"
(ometa
(char (seq* (bind c (apply anything))
(->? (char? c))
(-> c)))
(lower (seq* (bind c (apply char))
(->? (char<=? #\a c #\z))
(-> c)))
(end (~ (apply anything)))
(Start (seq* (bind word (many (apply lower)))
(apply end)
(-> (list->string word)))))
ans
(fail? ans))))
;; ================================================= ;;
;; Semantic actions and predicates ;;
;; ================================================= ;;
(define parameterized-rules-suite
(test-suite
"Parameterized rules."
;; ------------------------------------------ ;;
(om-test-case
"Lower-case letters character class."
"f"
(ometa
(char-range x y
(seq* (bind c (apply anything))
(->? (and (char? c)
(char<=? x c y)))
(-> c)))
(Start (seq* (bind c (apply char-range #\a #\z))
(-> c))))
(list #\f _ _))
;; ------------------------------------------ ;;
(om-test-case
"Lower-case letters character class."
"F"
(ometa
(char-range x y
(seq* (bind c (apply anything))
(->? (and (char? c)
(char<=? x c y)))
(-> c)))
(Start (seq* (bind c (apply char-range #\a #\z))
(-> c))))
ans
(fail? ans))
;; ------------------------------------------ ;;
(om-test-case
"Parsing numbers (many+ digit)."
"234 "
(ometa
(char-range x y
(seq* (bind c (apply anything))
(->? (and (char? c)
(char<=? x c y)))
(-> c)))
(digit (apply char-range #\0 #\9))
(Start (seq* (bind num (many+ (apply digit)))
(-> (list->string num)))))
(list "234" _ _))
;; ------------------------------------------ ;;
(om-test-case
"Invoking the same param-rule with different args in (alt ...)."
"aAbb AA"
(ometa
(char-range x y
(seq* (bind c (apply anything))
(->? (and (char? c)
(char<=? x c y)))
(-> c)))
(letter (alt* (apply char-range #\a #\z)
(apply char-range #\A #\Z)))
(Start (seq* (bind tok (many+ (apply letter)))
(-> (list->string tok)))))
(list "aAbb" _ _))
;; ------------------------------------------ ;;
(om-test-case
"Combine `scannerless' and `scannerful' parsing."
" myVar = 56 "
(ometa
(eq (seq* (atom #\=)
(-> (make-immutable-hash `((kind . =) (value . "="))))))
(num (seq* (bind ds (many+ (apply digit)))
(-> (make-immutable-hash `((kind . num) (value . ,(list->string ds)))))))
(id (seq* (bind ls (many+ (apply letter)))
(-> (make-immutable-hash `((kind . id) (value . ,(list->string ls)))))))
(char-range x y
(seq* (bind c (apply anything))
(->? (and (char? c)
(char<=? x c y)))
(-> c)))
(digit (apply char-range #\0 #\9))
(letter (alt* (apply char-range #\a #\z)
(apply char-range #\A #\Z)))
(spaces (many+ (atom #\space)))
(scanner (seq* (apply spaces)
(alt* (apply eq)
(apply num)
(apply id))))
(token k (seq* (bind t (apply scanner))
(->? (equal? (hash-ref t 'kind #f) k))
(-> (hash-ref t 'value))))
(Start (seq* (bind a (apply token 'id))
(bind b (apply token '=))
(bind c (apply token 'num))
(-> (string-append a b c))))
)
(list "myVar=56" _ _))
))
;; ================================================= ;;
;; Suite: Inheritance and foreign rules ;;
;; ================================================= ;;
;; Must have (define-ometa-namespace ns) at top level.
;; Test-cases will extend rules from these definitions.
(define-ometa std
(char (seq* (bind c (apply anything))
(->? (char? c))
(-> c)))
(char-range x y
(seq* (bind c (apply anything))
(->? (and (char? c)
(char<=? x c y)))
(-> c)))
(letter (alt* (apply char-range #\a #\z)
(apply char-range #\A #\Z)))
(digit (apply char-range #\0 #\9))
(number (many+ (apply digit)))
(spaces (many+ (atom #\space))))
(define-ometa chars
(char-range x y
(seq* (bind c (apply anything))
(->? (and (char? c)
(char<=? x c y)))
(-> c))))
(define-ometa letters
(letter (alt* (apply (^ char-range chars) #\a #\z)
(apply (^ char-range chars) #\A #\Z))))
(define char->number (compose string->number list->string list))
(define oo-suite
(test-suite
"Inheritance and foreign rules."
;; ------------------------------------------ ;;
(om-test-case
"Foreign: extend letters with _."
"hello_Id"
(ometa
(letter (alt* (atom #\_)
(apply (^ letter std))))
(Start (many+ (apply letter))))
(list '(#\h #\e #\l #\l #\o #\_ #\I #\d) _ _))
;; ------------------------------------------ ;;
(om-test-case
"Foreign: invoking rules with arguments."
"F"
(ometa
(Start (alt* (apply (^ char-range chars) #\a #\z)
(apply (^ char-range chars) #\A #\Z))))
(list #\F _ _))
;; ------------------------------------------ ;;
(om-test-case
"Foreign: nested foreign invocation."
"uhFHa"
(ometa
(Start (many+ (apply (^ letter letters)))))
(list '(#\u #\h #\F #\H #\a) _ _))
;; ------------------------------------------ ;;
(om-test-case
"Inheritance: extend letters with _."
"hello_Id"
(ometa (<< std)
(letter (alt* (atom #\_)
(apply (^ letter))))
(Start (many+ (apply letter))))
(list '(#\h #\e #\l #\l #\o #\_ #\I #\d) _ _))
;; ------------------------------------------ ;;
(om-test-case
"Inheritance: invoking rules with arguments."
"F"
(ometa (<< chars)
(Start (alt* (apply (^ char-range) #\a #\z)
(apply (^ char-range) #\A #\Z))))
(list #\F _ _))
;; ------------------------------------------ ;;
(om-test-case
"Inherintance: nested foreign invocation."
"uhFHa"
(ometa (<< letters)
(Start (many+ (apply (^ letter)))))
(list '(#\u #\h #\F #\H #\a) _ _))
;; ------------------------------------------ ;;
(om-test-case
"Inheritance: left-recursive rule."
"567"
(ometa (<< std)
(num (alt* (seq* (bind n (apply num))
(bind d (apply (^ digit)))
(-> (+ (* n 10) (char->number d))))
(seq* (bind d (apply (^ digit)))
(-> (char->number d)))))
(Start (apply num)))
(list 567 _ _))
))
;; ================================================= ;;
;; Run tests ;;
;; ================================================= ;;
(suites-to-run)