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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
|
require "./spec_helper"
include Parcom
describe "parser_chain" do
it "works with zero intermediate steps" do
p = parser_chain "test", Char, Char, finally: Parser(Char, Char).pure('x')
tokens = Tokens.from_string("abcd")
result = p.parse(tokens)
result.value.should eq('x')
result.tokens.should eq(tokens)
end
it "works with one intermediate step" do
p = parser_chain "test", Char, Char,
{c, Parser(Char, Char).any_token},
finally: Parser(Char, Char).pure(c)
tokens = Tokens.from_string("abcd")
result = p.parse(tokens)
result.value.should eq('a')
result.tokens.should eq(tokens[1..])
end
it "works with many intermediate steps" do
digit = Parser(Char, Char).satisfy(&.number?)
p = parser_chain "float", Char, Float64,
{sign, Parser.token('-').map_const(-1).recover(1)},
{front, digit.many},
{point, Parser.token('.').optional},
{back, digit.many},
finally: case {front.empty?, point.nil?, back.empty?}
when {false, _, true}
Parser(Char, Float64).pure(front.join.to_f64 * sign)
when {true, false, false}
Parser(Char, Float64).pure("0.#{back.join}".to_f64 * sign)
when {false, false, false}
Parser(Char, Float64).pure("#{front.join}.#{back.join}".to_f64 * sign)
else
Parser(Char, Float64).flunk
end
{
{"1", 1_f64},
{"-1", -1_f64},
{"2.", 2_f64},
{"-2.", -2_f64},
{".3", 0.3_f64},
{"-.3", -0.3_f64},
{"0.4", 0.4_f64},
{"-0.4", -0.4_f64},
}.each do |s, v|
tokens = Tokens.from_string(s)
result = p.parse(tokens)
result.value.should eq(v)
result.tokens.empty?.should be_true
end
end
it "allows ignoring results with underscores" do
any_word = Parser(Char, String).satisfy(&.letter?).some.map(&.join)
ws = Parser(Char, Array(Char)).satisfy(&.whitespace?).many
two_words = parser_chain "two words", Char, {String, String},
{word, any_word},
{_, ws},
finally: Parser.token_sequence(word.chars).map_const({word, word})
tokens = Tokens.from_string("foo \n foo")
result = two_words.parse(tokens)
result.value.should eq({"foo", "foo"})
tokens = Tokens.from_string("foo bar")
expect_raises(ParserFail) { two_words.parse(tokens) }
tokens = Tokens.from_string("foofoo")
expect_raises(ParserFail) { two_words.parse(tokens) }
end
end
describe Tokens do
describe ".from_string" do
it "constructs a Tokens(Char) from a String" do
tokens = Tokens.from_string("abcd")
tokens.tokens.should eq("abcd".chars)
end
end
describe "#initialize" do
it "wraps an array with the contents of the given iterable" do
set = Set{'a', 'b', 'c', 'd'}
tokens = Tokens.new(set)
tokens.tokens.should eq(set.to_a)
arr = "abcd".chars
tokens = Tokens.new(arr)
tokens.tokens.should eq(arr)
end
end
context do
tokens_empty = Tokens.new([] of Char)
tokens = Tokens.from_string("abcd")
describe "#[]" do
it "returns the token at the given index" do
tokens[2].should eq('c')
expect_raises(IndexError) { tokens_empty[2] }
end
it "returns a new Tokens similar to Array#[](Int, Int)" do
tokens[1, 5].should eq(Tokens.new(['b', 'c', 'd']))
expect_raises(IndexError) { tokens_empty[1, 5] }
end
it "returns a new Tokens similar to Array#[](Range)" do
tokens[1..3].should eq(Tokens.new(['b', 'c', 'd']))
expect_raises(IndexError) { tokens_empty[1..3] }
end
end
describe "#[]?" do
it "analogous to `Array#[]?`" do
# we should only need to check the nil-returning cases
tokens_empty[2]?.should be_nil
tokens_empty[1, 5]?.should be_nil
tokens_empty[1..3]?.should be_nil
end
end
describe "#empty?" do
it "exposes the `#empty?` method of the wrapped array" do
tokens.empty?.should be_false
tokens_empty.empty?.should be_true
end
end
end
end
describe Result do
describe "#map" do
r = Result.new(Tokens.from_string("abcd"), 'x')
r_expected = Result.new(Tokens.from_string("abcd"), 'x'.ord)
it "accepts a proc" do
f = ->(c : Char) { c.ord }
r.map(f).should eq(r_expected)
end
it "accepts a block" do
r.map { |c| c.ord }.should eq(r_expected)
end
end
end
describe Parser do
describe "self.pure" do
v = 'a'
p = Parser(Char, Char).pure(v)
tokens = Tokens.from_string("____")
result = p.parse(tokens)
it "returns a value of whatever it was initialized with" do
result.value.should eq(v)
end
it "does not modify the input" do
result.tokens.should eq(tokens)
end
end
describe "self.flunk" do
p = Parser(Char, Char).flunk
it "always fails" do
expect_raises(ParserFail) { p.parse(Tokens.from_string("arbitrary")) }
end
end
describe "self.any_token" do
p = Parser(Char, Nil).any_token
it "parses the first token in the input stream" do
tokens = Tokens.from_string("abcd")
result = p.parse(tokens)
result.value.should eq(tokens[0])
result.tokens.should eq(tokens[1..])
end
it "fails if the input stream is empty" do
tokens = Tokens.from_string("")
expect_raises(ParserFail) { p.parse(tokens) }
end
end
describe "self.eof" do
p = Parser(Char, Nil).eof
it "succeeds with nil if the input stream is empty" do
tokens = Tokens.from_string("")
result = p.parse(tokens)
result.value.should be_nil
result.tokens.empty?.should be_true
end
it "fails if the input stream is not empty" do
tokens = Tokens.from_string("____")
expect_raises(ParserFail) { p.parse(tokens) }
end
end
describe "self.satisfy" do
p = Parser(Char, Char).satisfy { |c| c == 'a' }
it "succeeds if the token passes the predicate" do
tokens = Tokens.from_string("abcd")
result = p.parse(tokens)
result.value.should eq(tokens[0])
result.tokens.should eq(tokens[1..])
end
it "fails if the token fails the predicate" do
tokens = Tokens.from_string("bbcd")
expect_raises(ParserFail) { p.parse(tokens) }
end
end
describe "self.token" do
a = Parser(Char, Char).token('a')
it "succeeds with the given token if it is at the start" do
tokens = Tokens.from_string("abc")
result = a.parse(tokens)
result.value.should eq('a')
result.tokens.should eq(tokens[1..])
end
it "fails if the given token is not present" do
tokens = Tokens.from_string("bbc")
expect_raises(ParserFail) { a.parse(tokens) }
end
end
describe "self.sequence" do
it "always succeeds with 0 parsers" do
p = Parser(Char, Char).sequence([] of Parser(Char, Char))
tokens = Tokens.from_string("")
result = p.parse(tokens)
result.value.empty?.should be_true
result.tokens.should eq(tokens)
end
p = Parser(Char, Char).sequence([
Parser(Char, Char).token('a'),
Parser(Char, Char).token('b'),
Parser(Char, Char).token('c'),
])
it "runs each parser in sequence" do
tokens = Tokens.from_string("abcd")
result = p.parse(tokens)
result.value.should eq("abc".chars)
result.tokens.should eq(tokens[3..])
end
it "fails if any of the parsers fail" do
"xbc axc abx".split.each do |s|
tokens = Tokens.from_string(s)
expect_raises(ParserFail) { p.parse(tokens) }
end
end
end
# most testing should be able to be skipped, since it is already
# done for `Parser.sequence`
describe "self.token_sequence" do
p = Parser(Char, Char).token_sequence("abc".chars)
it "parses the specified tokens in sequence" do
tokens = Tokens.from_string("abcd")
result = p.parse(tokens)
result.value.should eq("abc".chars)
result.tokens.should eq(tokens[3..])
end
end
describe "self.first_of" do
ps = [
Parser.token('a'),
Parser.token('b'),
Parser.token('c'),
]
abc = Parser(Char, Char).first_of(ps)
it "fails to instantiate if array is empty" do
expect_raises(ArgumentError) { Parser.first_of([] of Parser(Char, Char)) }
end
it "returns the result of the first parser that succeeds" do
{"a", "b", "c"}.each do |s|
tokens = Tokens.from_string(s)
result = abc.parse(tokens)
result.value.should eq(s[0])
result.tokens.empty?.should be_true
end
end
it "fails if none of the parsers succeed" do
{"", "d"}.each do |s|
tokens = Tokens.from_string(s)
expect_raises(ParserFail) { abc.parse(tokens) }
end
end
end
describe "#assert" do
p = Parser(Char, Char).any_token.assert { |c| c == 'a' }
it "succeeds if the parser succeeds and if the predicate passes" do
tokens = Tokens.from_string("abcd")
result = p.parse(tokens)
result.value.should eq(tokens[0])
result.tokens.should eq(tokens[1..])
end
it "fails if the predicate fails" do
tokens = Tokens.from_string("bbcd")
expect_raises(ParserFail) { p.parse(tokens) }
end
end
describe "#map" do
p = Parser(Char, Char).any_token
p_mapped = p.map { |c| c.ord }
tokens = Tokens.from_string("abcd")
normal_result = p.parse(tokens)
mapped_result = p_mapped.parse(tokens)
it "parses the same amount from the input" do
mapped_result.tokens.should eq(normal_result.tokens)
end
it "changes the value of the result" do
mapped_result.value.should eq('a'.ord)
end
end
describe "#map_const" do
p = Parser(Char, Char).any_token
p_mapped = p.map_const(42)
tokens = Tokens.from_string("abcd")
result = p_mapped.parse(tokens)
it "changes the value of the result" do
result.value.should eq(42)
end
end
describe "#|" do
a = Parser(Char, Char).token('a')
b = Parser(Char, Char).token('b')
p = a | b
it "succeeds with the first value if both parsers succeed" do
p2 = a | Parser(Char, Char).pure('z')
tokens = Tokens.from_string("abcd")
result = p2.parse(tokens)
result.value.should eq('a')
result.tokens.should eq(tokens[1..])
end
it "succeeds with the first value if only the first parser succeeds" do
tokens = Tokens.from_string("abcd")
result = p.parse(tokens)
result.value.should eq('a')
result.tokens.should eq(tokens[1..])
end
it "succeeds with the second value if only the second parser succeeds" do
tokens = Tokens.from_string("bbcd")
result = p.parse(tokens)
result.value.should eq('b')
result.tokens.should eq(tokens[1..])
end
it "fails if both parsers fail" do
tokens = Tokens.from_string("cbcd")
expect_raises(ParserFail) { p.parse(tokens) }
end
end
describe "#recover" do
p = Parser(Char, Char).token('a').recover('z')
it "behaves as normal on success" do
tokens = Tokens.from_string("abcd")
result = p.parse(tokens)
result.value.should eq('a')
result.tokens.should eq(tokens[1..])
end
tokens2 = Tokens.from_string("bbcd")
result2 = p.parse(tokens2)
it "returns the default value instead of failing" do
result2.value.should eq('z')
end
it "does not modify the input when recovering" do
result2.tokens.should eq(tokens2)
end
end
describe "#optional" do
p = Parser(Char, Char).token('a').optional
it "behaves as normal on success" do
tokens = Tokens.from_string("abcd")
result = p.parse(tokens)
result.value.should eq('a')
result.tokens.should eq(tokens[1..])
end
tokens2 = Tokens.from_string("bbcd")
result2 = p.parse(tokens2)
it "returns nil instead of failing" do
result2.value.should be_nil
end
it "does not modify the input when recovering" do
result2.tokens.should eq(tokens2)
end
end
describe "#+" do
a = Parser(Char, Char).token('a')
b = Parser(Char, Char).token('b')
p = a + b
it "combines both success results if both parsers succeed" do
tokens = Tokens.from_string("abcd")
result = p.parse(tokens)
result.value.should eq({'a', 'b'})
result.tokens.should eq(tokens[2..])
end
it "fails if either parser fails" do
"aacd bbcd cccd".split.each do |s|
tokens = Tokens.from_string(s)
expect_raises(ParserFail) { p.parse(tokens) }
end
end
end
# Should be able to skip some tests because they are already
# written for #+, which this is based on.
describe "#<<" do
a = Parser(Char, Char).token('a')
b = Parser(Char, Char).token('b')
p = a << b
tokens = Tokens.from_string("abcd")
result = p.parse(tokens)
it "discards the second parser's value" do
result.value.should eq('a')
end
end
# Should be able to skip some tests because they are already
# written for #+, which this is based on.
describe "#>>" do
a = Parser(Char, Char).token('a')
b = Parser(Char, Char).token('b')
p = a >> b
tokens = Tokens.from_string("abcd")
result = p.parse(tokens)
it "discards the second parser's value" do
result.value.should eq('b')
end
end
describe "#and_then" do
p_string = ->(s : String) do
Parser.token_sequence(s.chars).map(&.join)
end
p_any_word = Parser(Char, Array(Char)).satisfy(&.letter?).some.map(&.join)
space = Parser(Char, Array(Char)).satisfy(&.whitespace?).some
# Parses two of the same word, with whitespace between:
two_words = (p_any_word << space).and_then(p_string)
it "succeeds if the original parser and generated parser succeed" do
tokens = Tokens.from_string("foo foo")
result = two_words.parse(tokens)
result.value.should eq("foo")
result.tokens.empty?.should be_true
end
it "fails if the original parser fails" do
tokens = Tokens.from_string("__ foo")
expect_raises(ParserFail) { two_words.parse(tokens) }
end
it "fails if the generated parser fails" do
tokens = Tokens.from_string("bar foo")
expect_raises(ParserFail) { two_words.parse(tokens) }
end
end
describe "#many" do
p = Parser(Char, Char).token('a').many
it "returns no results if it never succeeds" do
["", "bb"].each do |s|
tokens = Tokens.from_string(s)
result = p.parse(tokens)
result.value.empty?.should be_true
result.tokens.should eq(tokens)
end
end
it "parses as many times as possible" do
["a", "aaa", "aaaaa"].each do |s|
tokens = Tokens.from_string(s)
result = p.parse(tokens)
result.value.should eq(s.chars)
result.tokens.empty?.should be_true
end
end
end
describe "#some" do
p = Parser(Char, Char).token('a').some
it "fails if it cannot parse at least once" do
["", "bb"].each do |s|
tokens = Tokens.from_string(s)
expect_raises(ParserFail) { p.parse(tokens) }
end
end
end
describe "#exactly" do
a = Parser.token('a')
tokens = Tokens.from_string("aaabbb")
it "fails to instantiate if `n` is negative" do
expect_raises(ArgumentError) { a * -3 }
end
it "accepts parsing 0 times" do
p = a * 0
result = p.parse(tokens)
result.value.empty?.should be_true
result.tokens.should eq(tokens)
end
it "tries to parse exactly n times" do
p = a * 3
result = p.parse(tokens)
result.value.should eq(['a', 'a', 'a'])
result.tokens.should eq(tokens[3..])
end
it "does not parse more than n times, even if it could" do
p = a * 1
result = p.parse(tokens)
result.value.should eq(['a'])
result.tokens.should eq(tokens[1..])
end
it "fails if unable to parse enough times" do
p = a * 5
expect_raises(ParserFail) { p.parse(tokens) }
end
end
describe "#at_least" do
a = Parser.token('a')
tokens = Tokens.from_string("aaabbb")
it "fails to instantiate if `n` is negative" do
expect_raises(ArgumentError) { a.at_least(-3) }
end
it "accepts parsing 0 times" do
p = a.at_least(0)
result = p.parse(tokens)
result.value.should eq(['a', 'a', 'a'])
result.tokens.should eq(tokens[3..])
end
it "tries to parse at least n times" do
p = a.at_least(3)
result = p.parse(tokens)
result.value.should eq(['a', 'a', 'a'])
result.tokens.should eq(tokens[3..])
end
it "parses as many times as possible (>=n)" do
p = a.at_least(1)
result = p.parse(tokens)
result.value.should eq(['a', 'a', 'a'])
result.tokens.should eq(tokens[3..])
end
it "fails if unable to parse enough times" do
p = a.at_least(5)
expect_raises(ParserFail) { p.parse(tokens) }
end
end
describe "#at_most" do
a = Parser.token('a')
tokens = Tokens.from_string("aaabbb")
it "fails to instantiate if `n` is negative" do
expect_raises(ArgumentError) { a.at_most(-3) }
end
it "accepts parsing 0 times" do
p = a.at_most(0)
result = p.parse(tokens)
result.value.should eq([] of Char)
result.tokens.should eq(tokens)
end
it "tries to parse at most n times" do
p = a.at_most(3)
result = p.parse(tokens)
result.value.should eq(['a', 'a', 'a'])
result.tokens.should eq(tokens[3..])
end
it "stops if it succeeds `n` times" do
p = a.at_most(1)
result = p.parse(tokens)
result.value.should eq(['a'])
result.tokens.should eq(tokens[1..])
end
it "stops if unable to parse `n` times" do
p = a.at_most(5)
result = p.parse(tokens)
result.value.should eq(['a', 'a', 'a'])
result.tokens.should eq(tokens[3..])
end
end
describe "#between" do
a = Parser.token('a')
tokens = Tokens.from_string("aaabbb")
it "fails to instantiate if `n` or `m` are negative" do
expect_raises(ArgumentError) { a.between(-1, 1) }
expect_raises(ArgumentError) { a.between(1, -1) }
expect_raises(ArgumentError) { a.between(-1, -1) }
end
it "fails to instantiate if any range values are negative" do
{(-1..3), (-3..-1), (..-3), (-3..)}.each do |r|
expect_raises(ArgumentError) { a.between(r) }
end
end
it "accepts either order for `n` and `m`" do
p1 = a.between(1,2)
r1 = p1.parse(tokens)
p2 = a.between(2,1)
r2 = p1.parse(tokens)
r1.should eq(r2)
end
it "accepts `0` for `n` and `m`" do
p = a.between(0, 2)
result = p.parse(tokens)
result.value.should eq(['a', 'a'])
result.tokens.should eq(tokens[2..])
end
it "accepts Range objects" do
p = a.between(1..2)
result = p.parse(tokens)
result.value.should eq(['a', 'a'])
result.tokens.should eq(tokens[2..])
end
it "accepts beginless ranges" do
p = a.between(..2)
result = p.parse(tokens)
result.value.should eq(['a', 'a'])
result.tokens.should eq(tokens[2..])
end
it "accepts endless ranges" do
p = a.between(2..)
result = p.parse(tokens)
result.value.should eq(['a', 'a', 'a'])
result.tokens.should eq(tokens[3..])
end
it "tries to parse between `n` and `m` times" do
p = a.between(1, 2)
result = p.parse(tokens)
result.value.should eq(['a', 'a'])
result.tokens.should eq(tokens[2..])
end
it "fails if it does not succeed the minimum number of times" do
p = a.between(5, 6)
expect_raises(ParserFail) { p.parse(tokens) }
end
it "stops parsing if it fails after succeeding the minimum number of times" do
p = a.between(2, 4)
result = p.parse(tokens)
result.value.should eq(['a', 'a', 'a'])
result.tokens.should eq(tokens[3..])
end
it "stops parsing if it succeeds the maximum number of times" do
p = a.between(1, 2)
result = p.parse(tokens)
result.value.should eq(['a', 'a'])
result.tokens.should eq(tokens[2..])
end
end
describe "#sep_by" do
a = Parser.token('a')
b = Parser.token('b')
p = a.sep_by(b)
it "fails if it cannot parse the first element" do
tokens = Tokens.from_string("cba")
expect_raises(ParserFail) { p.parse(tokens) }
end
it "will parse a single element without separators" do
tokens = Tokens.from_string("aca")
result = p.parse(tokens)
result.value.should eq(['a'])
result.tokens.should eq(tokens[1..])
end
it "will parse one element, and as many others separated by sep" do
tokens = Tokens.from_string("abababaa")
result = p.parse(tokens)
result.value.should eq(['a', 'a', 'a', 'a'])
result.tokens.should eq(tokens[7..])
end
end
# TODO: phrase
# TODO: peek
end
|