This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.nim
769 lines (620 loc) · 25.1 KB
/
test.nim
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
import unittest
import honeycomb
import strutils
import sequtils
import math
from sugar import `=>`
suite "core parsers":
test "literal string":
let
parser = s("Hello")
result1 = parser.parse("Hello, world!")
result2 = parser.parse("Greetings, peasants!")
check result1.kind == success
check result1.value == "Hello"
check result1.tail == ", world!"
check result1.fromInput == "Hello, world!"
check result2.kind == failure
check result2.error == "[1:1] Expected 'Hello'"
check result2.tail == "Greetings, peasants!"
check result2.fromInput == "Greetings, peasants!"
test "single character":
let
parser = c('H')
result1 = parser.parse("Hello, world!")
result2 = parser.parse("Greetings, peasants!")
check result1.kind == success
check result1.value == 'H'
check result1.tail == "ello, world!"
check result1.fromInput == "Hello, world!"
check result2.kind == failure
check result2.error == "[1:1] Expected 'H'"
check result2.tail == "Greetings, peasants!"
check result2.fromInput == "Greetings, peasants!"
test "character of string":
let
parser = c("HG")
result1 = parser.parse("Hello, world!")
result2 = parser.parse("Greetings, peasants!")
result3 = parser.parse("Ahoy there!")
check result1.kind == success
check result1.value == 'H'
check result1.tail == "ello, world!"
check result1.fromInput == "Hello, world!"
check result2.kind == success
check result2.value == 'G'
check result2.tail == "reetings, peasants!"
check result2.fromInput == "Greetings, peasants!"
check result3.kind == failure
check result3.error == "[1:1] Expected character from 'HG'"
check result3.tail == "Ahoy there!"
check result3.fromInput == "Ahoy there!"
test "character of range":
let
parser = c('G'..'H')
result1 = parser.parse("Hello, world!")
result2 = parser.parse("Greetings, peasants!")
result3 = parser.parse("Ahoy there!")
check result1.kind == success
check result1.value == 'H'
check result1.tail == "ello, world!"
check result1.fromInput == "Hello, world!"
check result2.kind == success
check result2.value == 'G'
check result2.tail == "reetings, peasants!"
check result2.fromInput == "Greetings, peasants!"
check result3.kind == failure
check result3.error == "[1:1] Expected character from G..H"
check result3.tail == "Ahoy there!"
check result3.fromInput == "Ahoy there!"
test "regex":
let
parser = regex(r"H\w+")
result1 = parser.parse("Hello, world!")
result2 = parser.parse("Greetings, peasants!")
check result1.kind == success
check result1.value == "Hello"
check result1.tail == ", world!"
check result1.fromInput == "Hello, world!"
check result2.kind == failure
check result2.error == r"[1:1] Expected match for regex 'H\w+'"
check result2.tail == "Greetings, peasants!"
check result2.fromInput == "Greetings, peasants!"
test "no-op":
let
parser = nop[string]()
result1 = parser.parse("Hello, world!")
check result1.kind == success
check result1.value == ""
check result1.tail == "Hello, world!"
check result1.fromInput == "Hello, world!"
test "raiseIfFailed":
let
parser = s("Hello")
result1 = parser.parse("Hello, world!")
result2 = parser.parse("Greetings, peasants!")
result1.raiseIfFailed
expect ParseError: result2.raiseIfFailed
test "forward declarations":
var parser1 = fwdcl[string]()
let
parser2 = (s("Hello, ") & parser1 & c('!'))
result1 = parser2.parse("Hello, world!")
parser1.become(s("world"))
let
result2 = parser2.parse("Hello, world!")
result3 = parser2.parse("Hello, peasants!")
check result1.kind == failure
check result1.error == "[1:8] Expected forward-declared parser to be initialized with .become(Parser)"
check result1.tail == "world!"
check result1.fromInput == "Hello, world!"
check result2.kind == success
check result2.value == @["Hello, ", "world", "!"]
check result2.tail == ""
check result2.fromInput == "Hello, world!"
check result3.kind == failure
check result3.error == "[1:8] Expected 'world'"
check result3.tail == "peasants!"
check result3.fromInput == "Hello, peasants!"
suite "predefined parsers":
test "eof":
let
result1 = eof.parse("")
result2 = eof.parse("Hello, world!")
check result1.kind == success
check result1.value == ""
check result1.tail == ""
check result1.fromInput == ""
check result2.kind == failure
check result2.error == "[1:1] Expected EOF"
check result2.tail == "Hello, world!"
check result2.fromInput == "Hello, world!"
test "anyChar":
let
result1 = anyChar.parse("Hello, world!")
result2 = anyChar.parse("")
check result1.kind == success
check result1.value == 'H'
check result1.tail == "ello, world!"
check result1.fromInput == "Hello, world!"
check result2.kind == failure
check result2.error == "[1:1] Expected any character"
check result2.tail == ""
check result2.fromInput == ""
test "whitespace":
let
result1 = whitespace.parse(" \t \n \r Hello, world!")
result2 = whitespace.parse("Hello, world!")
check result1.kind == success
check result1.value == " \t \n \r "
check result1.tail == "Hello, world!"
check result1.fromInput == " \t \n \r Hello, world!"
check result2.kind == failure
check result2.error == r"[1:1] Expected match for regex '\s+'"
check result2.tail == "Hello, world!"
check result2.fromInput == "Hello, world!"
test "letter":
let
result1 = letter.parse("Hello, world!")
result2 = letter.parse("127")
check result1.kind == success
check result1.value == 'H'
check result1.tail == "ello, world!"
check result1.fromInput == "Hello, world!"
check result2.kind == failure
check result2.error == "[1:1] Expected one of character from a..z, character from A..Z"
check result2.tail == "127"
check result2.fromInput == "127"
test "digit":
let
result1 = digit.parse("127")
result2 = digit.parse("Hello, world!")
check result1.kind == success
check result1.value == '1'
check result1.tail == "27"
check result1.fromInput == "127"
check result2.kind == failure
check result2.error == "[1:1] Expected character from 0..9"
check result2.tail == "Hello, world!"
check result2.fromInput == "Hello, world!"
test "alphanumeric":
let
result1 = alphanumeric.parse("Hello, world!")
result2 = alphanumeric.parse("127")
result3 = alphanumeric.parse("!@%$!^#")
check result1.kind == success
check result1.value == 'H'
check result1.tail == "ello, world!"
check result1.fromInput == "Hello, world!"
check result2.kind == success
check result2.value == '1'
check result2.tail == "27"
check result2.fromInput == "127"
check result3.kind == failure
check result3.error == "[1:1] Expected one of character from a..z, character from A..Z, character from 0..9"
check result3.tail == "!@%$!^#"
check result3.fromInput == "!@%$!^#"
suite "general combinators":
test "map":
let
parser = digit.atLeast(1).join.map(parseInt)
result1 = parser.parse("127")
result2 = parser.parse("Hello, world!")
check result1.kind == success
check result1.value == 127
check result1.tail == ""
check result1.fromInput == "127"
check result2.kind == failure
check result2.error == "[1:1] Expected character from 0..9"
check result2.tail == "Hello, world!"
check result2.fromInput == "Hello, world!"
test "mapEach":
let
parser = digit.asString.atLeast(1).mapEach(parseInt)
result1 = parser.parse("127")
result2 = parser.parse("Hello, world!")
check result1.kind == success
check result1.value == @[1,2,7]
check result1.tail == ""
check result1.fromInput == "127"
check result2.kind == failure
check result2.error == "[1:1] Expected character from 0..9"
check result2.tail == "Hello, world!"
check result2.fromInput == "Hello, world!"
test "result":
let
parser = digit.atLeast(1).result("Successfully parsed number")
result1 = parser.parse("127")
result2 = parser.parse("Hello, world!")
check result1.kind == success
check result1.value == "Successfully parsed number"
check result1.tail == ""
check result1.fromInput == "127"
check result2.kind == failure
check result2.error == "[1:1] Expected character from 0..9"
check result2.tail == "Hello, world!"
check result2.fromInput == "Hello, world!"
test "filter":
let
parser = digit.asString.atLeast(1).mapEach(parseInt).filter(x => x >= 5)
result1 = parser.parse("0918273654")
result2 = parser.parse("Hello, world!")
check result1.kind == success
check result1.value == @[9,8,7,6,5]
check result1.tail == ""
check result1.fromInput == "0918273654"
check result2.kind == failure
check result2.error == "[1:1] Expected character from 0..9"
check result2.tail == "Hello, world!"
check result2.fromInput == "Hello, world!"
test "validate":
let
num = digit.atLeast(1).map(a => a.join().parseInt)
numSmallerThan500 = num.validate(a => a < 500, "number smaller than 500")
result1 = numSmallerThan500.parse("323")
result2 = numSmallerThan500.parse("874")
check result1.kind == success
check result1.value == 323
check result1.tail == ""
check result1.fromInput == "323"
check result2.kind == failure
check result2.error == "[1:1] Expected number smaller than 500"
check result2.tail == "874"
check result2.fromInput == "874"
test "oneOf":
let parsers = [
s("Hello") | s("Greetings"),
oneOf(s("Hello"), s("Greetings"))
]
for parser in parsers:
let
result1 = parser.parse("Hello, world!")
result2 = parser.parse("Greetings, peasants!")
result3 = parser.parse("Ahoy there!")
check result1.kind == success
check result1.value == "Hello"
check result1.tail == ", world!"
check result1.fromInput == "Hello, world!"
check result2.kind == success
check result2.value == "Greetings"
check result2.tail == ", peasants!"
check result2.fromInput == "Greetings, peasants!"
check result3.kind == failure
check result3.error == "[1:1] Expected one of 'Hello', 'Greetings'"
check result3.tail == "Ahoy there!"
check result3.fromInput == "Ahoy there!"
test "chain":
let parsers = [
s("Hello") & c(',') & whitespace & letter.atLeast(1).join & c('!'),
chain(s("Hello"), c(','), whitespace, letter.atLeast(1).join, c('!'))
]
for parser in parsers:
let
result1 = parser.parse("Hello, world!")
result2 = parser.parse("Greetings, peasants!")
result3 = parser.parse("Hello,world!")
check result1.kind == success
check result1.value == @["Hello", ",", " ", "world", "!"]
check result1.tail == ""
check result1.fromInput == "Hello, world!"
check result2.kind == failure
check result2.error == "[1:1] Expected 'Hello'"
check result2.tail == "Greetings, peasants!"
check result2.fromInput == "Greetings, peasants!"
check result3.kind == failure
check result3.error == r"[1:7] Expected match for regex '\s+'"
check result3.tail == "world!"
check result3.fromInput == "Hello,world!"
test "then":
let parsers = [
s("Hello") >> c(',').asString >> whitespace >> letter.atLeast(1).join,
s("Hello").then(c(',')).then(whitespace).then(letter.atLeast(1).join)
]
for parser in parsers:
let
result1 = parser.parse("Hello, world!")
result2 = parser.parse("Greetings, peasants!")
check result1.kind == success
check result1.value == "world"
check result1.tail == "!"
check result1.fromInput == "Hello, world!"
check result2.kind == failure
check result2.error == "[1:1] Expected 'Hello'"
check result2.tail == "Greetings, peasants!"
check result2.fromInput == "Greetings, peasants!"
test "skip":
let parsers = [
s("Hello") << c(',').asString << whitespace << letter.atLeast(1).join,
s("Hello").skip(c(',')).skip(whitespace).skip(letter.atLeast(1).join)
]
for parser in parsers:
let
result1 = parser.parse("Hello, world!")
result2 = parser.parse("Greetings, peasants!")
check result1.kind == success
check result1.value == "Hello"
check result1.tail == "!"
check result1.fromInput == "Hello, world!"
check result2.kind == failure
check result2.error == "[1:1] Expected 'Hello'"
check result2.tail == "Greetings, peasants!"
check result2.fromInput == "Greetings, peasants!"
test "times":
let parsers = [
s("Hello ") * 3,
s("Hello ").times(3)
]
for parser in parsers:
let
result1 = parser.parse("Hello Hello Hello ")
result2 = parser.parse("Hello Hello ")
check result1.kind == success
check result1.value == @["Hello ","Hello ","Hello "]
check result1.tail == ""
check result1.fromInput == "Hello Hello Hello "
check result2.kind == failure
check result2.error == "[1:13] Expected 'Hello '"
check result2.tail == ""
check result2.fromInput == "Hello Hello "
test "times range":
let parsers = [
s("Hello ") * (3..4) << eof,
s("Hello ").times(3..4) << eof
]
for parser in parsers:
let
result1 = parser.parse("Hello Hello Hello ")
result2 = parser.parse("Hello Hello Hello Hello ")
result3 = parser.parse("Hello Hello ")
result4 = parser.parse("Hello Hello Hello Hello Hello ")
check result1.kind == success
check result1.value == @["Hello ","Hello ","Hello "]
check result1.tail == ""
check result1.fromInput == "Hello Hello Hello "
check result2.kind == success
check result2.value == @["Hello ","Hello ","Hello ","Hello "]
check result2.tail == ""
check result2.fromInput == "Hello Hello Hello Hello "
check result3.kind == failure
check result3.error == "[1:13] Expected 'Hello '"
check result3.tail == ""
check result3.fromInput == "Hello Hello "
check result4.kind == failure
check result4.error == "[1:25] Expected EOF"
check result4.tail == "Hello "
check result4.fromInput == "Hello Hello Hello Hello Hello "
test "negla":
let parsers = [
!eof,
eof.negla
]
for parser in parsers:
let
result1 = parser.parse("Hello, world!")
result2 = parser.parse("")
check result1.kind == success
check result1.value == ""
check result1.tail == "Hello, world!"
check result1.fromInput == "Hello, world!"
check result2.kind == failure
check result2.error == "[1:1] Expected successful negative lookahead"
check result2.tail == ""
check result2.fromInput == ""
test "atLeast":
let
parser = s("Hello ").atLeast(3)
result1 = parser.parse("Hello Hello Hello ")
result2 = parser.parse("Hello Hello Hello Hello ")
result3 = parser.parse("Hello Hello ")
check result1.kind == success
check result1.value == @["Hello ","Hello ","Hello "]
check result1.tail == ""
check result1.fromInput == "Hello Hello Hello "
check result2.kind == success
check result2.value == @["Hello ","Hello ","Hello ","Hello "]
check result2.tail == ""
check result2.fromInput == "Hello Hello Hello Hello "
check result3.kind == failure
check result3.error == "[1:13] Expected 'Hello '"
check result3.tail == ""
check result3.fromInput == "Hello Hello "
test "atMost":
let
parser = s("Hello ").atMost(3) << eof
result1 = parser.parse("Hello Hello Hello ")
result2 = parser.parse("Hello Hello ")
result3 = parser.parse("Hello Hello Hello Hello ")
check result1.kind == success
check result1.value == @["Hello ","Hello ","Hello "]
check result1.tail == ""
check result1.fromInput == "Hello Hello Hello "
check result2.kind == success
check result2.value == @["Hello ","Hello "]
check result2.tail == ""
check result2.fromInput == "Hello Hello "
check result3.kind == failure
check result3.error == "[1:19] Expected EOF"
check result3.tail == "Hello "
check result3.fromInput == "Hello Hello Hello Hello "
test "many":
let
parser = s("Hello ").many()
result1 = parser.parse("Hello Hello Hello ")
result2 = parser.parse("Hello Hello ")
result3 = parser.parse("")
check result1.kind == success
check result1.value == @["Hello ","Hello ","Hello "]
check result1.tail == ""
check result1.fromInput == "Hello Hello Hello "
check result2.kind == success
check result2.value == @["Hello ","Hello "]
check result2.tail == ""
check result2.fromInput == "Hello Hello "
check result3.kind == success
check result3.value == newSeq[string]()
check result3.tail == ""
check result3.fromInput == ""
test "optional":
let
parser = s("Hello").optional()
result1 = parser.parse("Hello")
result2 = parser.parse("")
check result1.kind == success
check result1.value == "Hello"
check result1.tail == ""
check result1.fromInput == "Hello"
check result2.kind == success
check result2.value == ""
check result2.tail == ""
check result2.fromInput == ""
test "orEmpty":
let
parser = s("Hello").orEmpty()
result1 = parser.parse("Hello")
result2 = parser.parse("")
check result1.kind == success
check result1.value == @["Hello"]
check result1.tail == ""
check result1.fromInput == "Hello"
check result2.kind == success
check result2.value == newSeq[string]()
check result2.tail == ""
check result2.fromInput == ""
test "flatten":
let
parser = (digit & digit & digit).atLeast(1).flatten().join
result1 = parser.parse("127")
result2 = parser.parse("Hello, world!")
check result1.kind == success
check result1.value == "127"
check result1.tail == ""
check result1.fromInput == "127"
check result2.kind == failure
check result2.error == "[1:1] Expected character from 0..9"
check result2.tail == "Hello, world!"
check result2.fromInput == "Hello, world!"
test "join":
let
parser = s("Hello ").times(3).join
result1 = parser.parse("Hello Hello Hello ")
result2 = parser.parse("Hello Hello ")
check result1.kind == success
check result1.value == "Hello Hello Hello "
check result1.tail == ""
check result1.fromInput == "Hello Hello Hello "
check result2.kind == failure
check result2.error == "[1:13] Expected 'Hello '"
check result2.tail == ""
check result2.fromInput == "Hello Hello "
test "join delimited":
let
parser1 = (s("Hello") << whitespace).times(3).join(", ")
parser2 = (s("Hello") << whitespace).times(3).join(',')
result1 = parser1.parse("Hello Hello Hello ")
result2 = parser2.parse("Hello Hello Hello ")
result3 = parser1.parse("Hello Hello ")
check result1.kind == success
check result1.value == "Hello, Hello, Hello"
check result1.tail == ""
check result1.fromInput == "Hello Hello Hello "
check result2.kind == success
check result2.value == "Hello,Hello,Hello"
check result2.tail == ""
check result2.fromInput == "Hello Hello Hello "
check result3.kind == failure
check result3.error == "[1:13] Expected 'Hello'"
check result3.tail == ""
check result3.fromInput == "Hello Hello "
suite "custom parsers":
test "custom parser":
let parser = createParser(string):
if input.len < 10: return fail(input, @["at least 10 characters"], input)
return succeed(input, input[0..9], input[10..^1])
let
result1 = parser.parse("Hello, world!")
result2 = parser.parse("Alfalfa")
check result1.kind == success
check result1.value == "Hello, wor"
check result1.tail == "ld!"
check result1.fromInput == "Hello, world!"
check result2.kind == failure
check result2.error == "[1:1] Expected at least 10 characters"
check result2.tail == "Alfalfa"
check result2.fromInput == "Alfalfa"
test "custom combinator":
proc both[T](a,b: Parser[T]): Parser[(T,T)] = createParser((T,T)):
{.push warnings: off.} # Hide the warnings from applyParser, since I know what I'm doing
let
result1 = applyParser(a, input, (T,T))
result2 = applyParser(b, input, (T,T))
resLen = max(result1.fromInput.len-result1.tail.len, result2.fromInput.len-result2.tail.len)
{.pop.}
return succeed(input, (result1.value, result2.value), input[resLen..^1])
let
parser = both(c('H').asString, s("Hello"))
result1 = parser.parse("Hello, world!")
result2 = parser.parse("Greetings, peasants!")
check result1.kind == success
check result1.value == ("H", "Hello")
check result1.tail == ", world!"
check result1.fromInput == "Hello, world!"
check result2.kind == failure
check result2.error == "[1:1] Expected 'H'"
check result2.tail == "Greetings, peasants!"
check result2.fromInput == "Greetings, peasants!"
suite "integration examples":
test "receipt parser":
let
receiptEntry = (regex(r"[\w\s]+") << s(": ")) & (digit.atLeast(1) << c('.')).join & digit.times(2).join << c('\n').optional()
receiptParser = receiptEntry.map(x => (x[0], parseFloat("$1.$2" % x[1..^1]))).atLeast(1)
testReceipt = "Milk: 4.00\nEggs: 15.99\nCool robot: 69.99"
result1 = receiptParser.parse(testReceipt)
check result1.kind == success
check result1.value == @[("Milk", 4.00), ("Eggs", 15.99), ("Cool robot", 69.99)]
check result1.tail == ""
check result1.fromInput == testReceipt
test "arithmetic expression evaluator":
var expression = fwdcl[float]()
let
padding = regex(r"\s*")
number = (digit.atLeast(1) & (c('.') & digit.atLeast(1)).optional).join.map(parseFloat)
parens = c('(') >> padding >> expression << padding << c(')')
operand = number | parens
func processOp(input: seq[string]): float =
if input.len < 3: return input[0].parseFloat
case input[1]:
of "+": return input[0].parseFloat + processOp(input[2..^1])
of "-": return input[0].parseFloat - processOp(input[2..^1])
of "*": return input[0].parseFloat * processOp(input[2..^1])
of "/": return input[0].parseFloat / processOp(input[2..^1])
of "%": return input[0].parseFloat.mod(processOp(input[2..^1]))
of "^": return input[0].parseFloat.pow(processOp(input[2..^1]))
var prevt: Parser[float] = operand
template defineBinOp(parseOp: Parser[string]) =
let right = ((padding >> parseOp << padding) & prevt.asString).many().flatten
prevt = (prevt.asString & right).map(processOp)
defineBinOp c("^")
defineBinOp c("*/%")
defineBinOp c("+-")
expression.become(padding >> prevt << padding)
let
parser = expression << eof.desc("valid expression")
result1 = parser.parse(" 1 + (2) * (3 - 4) / 5 ^ 6 ")
result2 = parser.parse("1 + ")
check result1.kind == success
check result1.value == 0.999872
check result1.tail == ""
check result1.fromInput == " 1 + (2) * (3 - 4) / 5 ^ 6 "
check result2.kind == failure
check result2.error == "[1:3] Expected valid expression"
check result2.tail == "+ "
check result2.fromInput == "1 + "
suite "reported issues":
test "#3: atLeast and atMost together makes the parser hang":
let
parser = digit.atMost(3).atLeast(4).removeEmpty
result = parser.parse("255314")
check result.kind == success
check result.value == @[@['2','5','5'], @['3','1','4']]
check result.tail == ""
check result.fromInput == "255314"