-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathMain.purs
More file actions
1200 lines (1020 loc) Β· 39.8 KB
/
Main.purs
File metadata and controls
1200 lines (1020 loc) Β· 39.8 KB
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
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Run tests:
--
-- spago -x spago-dev.dhall test
--
module Test.Main where
import Prelude hiding (between, when)
import Control.Alt ((<|>))
import Control.Lazy (fix, defer)
import Control.Monad.State (State, lift, modify, runState)
import Data.Array (some, toUnfoldable)
import Data.Array as Array
import Data.Bifunctor (lmap, rmap)
import Data.CodePoint.Unicode (isSpace)
import Data.CodePoint.Unicode as CodePoint.Unicode
import Data.Either (Either(..), either, fromLeft, hush)
import Data.Foldable (oneOf)
import Data.List (List(..), fromFoldable, (:))
import Data.List as List
import Data.List.NonEmpty (NonEmptyList(..), catMaybes, cons, cons')
import Data.List.NonEmpty as NE
import Data.Maybe (Maybe(..), fromJust, maybe)
import Data.NonEmpty ((:|))
import Data.Number (infinity, nan)
import Data.Number as Data.Number
import Data.String (toUpper)
import Data.String.CodePoints as SCP
import Data.String.CodeUnits (fromCharArray, singleton)
import Data.String.CodeUnits as SCU
import Data.String.Regex.Flags (RegexFlags, ignoreCase, noFlags)
import Data.Tuple (Tuple(..), fst, snd)
import Data.Tuple.Nested (get2, (/\))
import Effect (Effect)
import Effect.Console (log, logShow)
import Effect.Unsafe (unsafePerformEffect)
import Node.Process (lookupEnv)
import Parsing (ParseError(..), ParseState(..), Parser, ParserT, Position(..), consume, fail, getParserT, initialPos, parseErrorMessage, parseErrorPosition, position, region, runParser)
import Parsing.Combinators (advance, between, chainl, chainl1, chainr, chainr1, choice, empty, endBy, endBy1, lookAhead, many, many1, many1Till, many1Till_, manyIndex, manyTill, manyTill_, notFollowedBy, optionMaybe, replicateA, sepBy, sepBy1, sepEndBy, sepEndBy1, skipMany, skipMany1, try, tryRethrow, withRecovery, (<?>), (<??>), (<~?>))
import Parsing.Combinators.Array as Combinators.Array
import Parsing.Expr (Assoc(..), Operator(..), buildExprParser)
import Parsing.Language (haskellDef, haskellStyle, javaStyle)
import Parsing.String (anyChar, anyCodePoint, anyTill, char, eof, match, parseErrorHuman, regex, rest, satisfy, string, takeN)
import Parsing.String.Basic (intDecimal, letter, noneOfCodePoints, number, oneOfCodePoints, skipSpaces, takeWhile, takeWhile1, whiteSpace)
import Parsing.String.Basic as String.Basic
import Parsing.String.Replace (breakCap, replace, replaceT, splitCap, splitCapT)
import Parsing.Token (TokenParser, makeTokenParser, token, when)
import Parsing.Token as Token
import Partial.Unsafe (unsafePartial)
import Test.Assert (assert', assertEqual')
parens :: forall m a. ParserT String m a -> ParserT String m a
parens = between (string "(") (string ")")
nested :: forall m. ParserT String m Int
nested = fix \p ->
( do
_ <- string "a"
pure 0
) <|> ((+) 1) <$> parens p
parseTest :: forall s a. Show a => Eq a => s -> a -> Parser s a -> Effect Unit
parseTest input expected p = case runParser input p of
Right actual -> do
assert' ("expected: " <> show expected <> ", actual: " <> show actual) (expected == actual)
logShow actual
Left err -> assert' ("error: " <> show err) false
parseErrorTestPosition :: forall s a. Show a => Parser s a -> s -> Position -> Effect Unit
parseErrorTestPosition p input expected = case runParser input p of
Right x -> assert' ("ParseError expected at " <> show expected <> " but parsed " <> show x) false
Left err -> do
let pos = parseErrorPosition err
assert' ("expected: " <> show expected <> ", pos: " <> show pos) (expected == pos)
logShow expected
parseErrorTestMessage :: forall s a. Show a => Parser s a -> s -> String -> Effect Unit
parseErrorTestMessage p input expected = case runParser input p of
Right x -> assert' ("ParseError expected '" <> expected <> "' but parsed " <> show x) false
Left err -> do
let msg = parseErrorMessage err
assert' ("expected: " <> expected <> ", message: " <> msg) (expected == msg)
logShow expected
opTest :: Parser String String
opTest = chainl (singleton <$> anyChar) (char '+' $> append) ""
digit :: Parser String Int
digit = (string "0" >>= \_ -> pure 0)
<|> (string "1" >>= \_ -> pure 1)
<|> (string "2" >>= \_ -> pure 2)
<|> (string "3" >>= \_ -> pure 3)
<|> (string "4" >>= \_ -> pure 4)
<|> (string "5" >>= \_ -> pure 5)
<|> (string "6" >>= \_ -> pure 6)
<|> (string "7" >>= \_ -> pure 7)
<|> (string "8" >>= \_ -> pure 8)
<|> (string "9" >>= \_ -> pure 9)
exprTest :: Parser String Int
exprTest = buildExprParser
[ [ Infix (string "/" >>= \_ -> pure (/)) AssocRight ]
, [ Infix (string "*" >>= \_ -> pure (*)) AssocRight ]
, [ Infix (string "-" >>= \_ -> pure (-)) AssocRight ]
, [ Infix (string "+" >>= \_ -> pure (+)) AssocRight ]
]
digit
manySatisfyTest :: Parser String String
manySatisfyTest = do
r <- some $ satisfy (\s -> s /= '?')
_ <- char '?'
pure (fromCharArray r)
mkRegexTest :: String -> String -> String -> RegexFlags -> (Parser String String -> Parser String String) -> Effect Unit
mkRegexTest input expected pattern flags pars =
case regex pattern flags of
Left err -> assert' ("error: " <> show err) false
Right p -> parseTest input expected $ pars p
-- This test doesn't test the actual stack safety of these combinators, mainly
-- because I don't know how to come up with an example guaranteed to be large
-- enough to overflow the stack. But thankfully, their stack safety is more or
-- less guaranteed by construction.
--
-- Instead, this test checks functional correctness of the combinators, since
-- that's the more tricky part to get right (or to break later) in the absence
-- of clear explicit recursion.
stackSafeLoopsTest :: TestM
stackSafeLoopsTest = do
parseTest "aaabaa" (toUnfoldable [ "a", "a", "a" ]) $
manyTill (string "a") (string "b")
parseTest "baa" Nil $
manyTill (string "a") (string "b")
parseTest "aaabaa" (NE.cons' "a" $ toUnfoldable [ "a", "a" ]) $
many1Till (string "a") (string "b")
parseErrorTestPosition
(many1Till (string "a") (string "b"))
"baa"
(Position { index: 0, line: 1, column: 1 })
parseTest "a,a,a,b,a,a" (toUnfoldable [ "a", "a", "a" ]) $
sepEndBy (string "a") (string ",")
parseTest "a,a,abaa" (toUnfoldable [ "a", "a", "a" ]) $
sepEndBy (string "a") (string ",")
parseTest "b,a,a" Nil $
sepEndBy (string "a") (string ",")
parseTest "a,a,a,b,a,a" (NE.cons' "a" $ toUnfoldable [ "a", "a" ]) $
sepEndBy1 (string "a") (string ",")
parseTest "a,a,abaa" (NE.cons' "a" $ toUnfoldable [ "a", "a" ]) $
sepEndBy1 (string "a") (string ",")
parseErrorTestPosition
(sepEndBy1 (string "a") (string ","))
"b,a,a"
(Position { index: 0, line: 1, column: 1 })
-- 8 `div` (8 `div` 2) == 2
parseTest "8x8x2" 2 $
chainr digit (string "x" $> div) 42
parseTest "" 42 $
chainr digit (string "x" $> div) 42
parseTest "8x8x2" 2 $
chainr1 digit (string "x" $> div)
parseErrorTestPosition
(chainr1 digit (string "x" $> div))
""
(Position { index: 0, line: 1, column: 1 })
-- (8 `div` 2) `div` 2 == 2
parseTest "8x2x2" 2 $
chainl digit (string "x" $> div) 42
parseTest "" 42 $
chainl digit (string "x" $> div) 42
parseTest "8x2x2" 2 $
chainl1 digit (string "x" $> div)
parseErrorTestPosition
(chainl1 digit (string "x" $> div))
""
(Position { index: 0, line: 1, column: 1 })
parseTest "aaaabcd" "b"
$ skipMany1 (string "a")
*> string "b"
parseErrorTestPosition
(skipMany1 (string "a"))
"bcd"
(Position { index: 0, line: 1, column: 1 })
parseTest "aaaabcd" "b"
$ skipMany (string "a")
*> string "b"
parseTest "bcd" "b"
$ skipMany (string "a")
*> string "b"
parseTest "aaa" (NE.cons' "a" $ toUnfoldable [ "a", "a" ]) $
many1 (string "a")
parseErrorTestPosition
(many1 (string "a"))
""
(Position { index: 0, line: 1, column: 1 })
parseTest "a,a,ab" (toUnfoldable [ "a", "a", "a" ])
$ sepBy (string "a") (string ",")
<* string "b"
parseTest "b" Nil
$ sepBy (string "a") (string ",")
<* string "b"
parseTest "a,a,ab" (NE.cons' "a" $ toUnfoldable [ "a", "a" ])
$ sepBy1 (string "a") (string ",")
<* string "b"
parseErrorTestPosition
(sepBy1 (string "a") (string ","))
""
(Position { index: 0, line: 1, column: 1 })
parseErrorTestPosition
(sepBy1 (string "a") (string ","))
"a,"
(Position { index: 2, line: 1, column: 3 })
parseTest "a,a,a,b" (toUnfoldable [ "a", "a", "a" ])
$ endBy (string "a") (string ",")
<* string "b"
parseTest "b" Nil
$ endBy (string "a") (string ",")
<* string "b"
parseTest "a,a,a,b" (NE.cons' "a" $ toUnfoldable [ "a", "a" ])
$ endBy1 (string "a") (string ",")
<* string "b"
parseErrorTestPosition
(endBy1 (string "a") (string ","))
""
(Position { index: 0, line: 1, column: 1 })
parseErrorTestPosition
(endBy1 (string "a") (string ","))
"a,a"
(Position { index: 3, line: 1, column: 4 })
data TestToken = A | B
instance showTestTokens :: Show TestToken where
show A = "A"
show B = "B"
instance testTokensEq :: Eq TestToken where
eq A A = true
eq B B = true
eq _ _ = false
isA :: TestToken -> Boolean
isA A = true
isA _ = false
testTokenParser :: TokenParser
testTokenParser = makeTokenParser haskellDef
mkPos :: Int -> Position
mkPos n = Position { index: n - 1, line: 1, column: n }
type TestM = Effect Unit
tokenParserIdentifierTest :: TestM
tokenParserIdentifierTest = do
-- parse normal identifier
parseTest "hello" "hello" testTokenParser.identifier
-- error on reserved words
parseErrorTestPosition testTokenParser.identifier "let" $ mkPos 4
-- parse whitespace after identifier
parseTest "hello twice " "twice" (testTokenParser.identifier *> testTokenParser.identifier)
-- can't start identifiers with numbers
parseErrorTestPosition testTokenParser.identifier "3hello" $ mkPos 1
-- but numbers can be in the identifier
parseTest "h3ll0 " "h3ll0" testTokenParser.identifier
-- comments count as whitespace
parseTest "h3ll0 -- this is a comment\nbye {- this is another comment -}" (Tuple "h3ll0" "bye")
(Tuple <$> testTokenParser.identifier <*> testTokenParser.identifier)
-- multiline comments work well
parseTest "hello {- this \nis a comment -} bye" (Tuple "hello" "bye")
(Tuple <$> testTokenParser.identifier <*> testTokenParser.identifier)
-- nested comments are okay
parseTest "hello {- this {- \nis a comment -} foo -} bye" (Tuple "hello" "bye")
(Tuple <$> testTokenParser.identifier <*> testTokenParser.identifier)
-- fail on non-matching comments
parseErrorTestPosition testTokenParser.identifier "hello {-" $ mkPos 9
tokenParserReservedTest :: TestM
tokenParserReservedTest = do
-- parse reserved identifier
parseTest "forall" unit $ testTokenParser.reserved "forall"
-- fail on nonmatching reserved identifier
parseErrorTestPosition (testTokenParser.reserved "forall") "forall3" $ mkPos 7
-- fail on nonmatching reserved identifier
parseErrorTestPosition (testTokenParser.reserved "forall") "forall3" $ mkPos 7
tokenParserOperatorTest :: TestM
tokenParserOperatorTest = do
-- parse operator
parseTest "<>" "<>" testTokenParser.operator
-- fail on nonoperator
parseErrorTestPosition testTokenParser.operator "foo" $ mkPos 1
-- fail on reserved operator
parseErrorTestPosition testTokenParser.operator "=" $ mkPos 2
-- TODO
tokenParserReservedOpTest :: TestM
tokenParserReservedOpTest = pure unit
tokenParserCharLiteralTest :: TestM
tokenParserCharLiteralTest = do
-- parse char literal
parseTest "'c'" 'c' testTokenParser.charLiteral
-- fail on slash
parseErrorTestPosition testTokenParser.charLiteral "'\'" $ mkPos 2
-- parse escape code
parseTest "'\\n'" '\n' testTokenParser.charLiteral
-- parse oct number
parseTest "'\\o101'" 'A' testTokenParser.charLiteral
-- parse hex number
parseTest "'\\x41'" 'A' testTokenParser.charLiteral
-- fail on bad oct
parseErrorTestPosition testTokenParser.charLiteral "'\\o389'" $ mkPos 5
-- parse ascii
parseTest "'\\^I'" '\t' testTokenParser.charLiteral
tokenParserStringLiteralTest :: TestM
tokenParserStringLiteralTest = do
-- parse string char
parseTest "\"hello\"" "hello" testTokenParser.stringLiteral
-- fail on non-operator
parseErrorTestPosition testTokenParser.stringLiteral "he\"llo" $ mkPos 1
-- parse string gap
parseTest "\"he\\ \\llo\"" "hello" testTokenParser.stringLiteral
-- parse string empty
parseTest "\"he\\&llo\"" "hello" testTokenParser.stringLiteral
-- parse string escape
parseTest "\"he\\nllo\"" "he\nllo" testTokenParser.stringLiteral
tokenParserNaturalTest :: TestM
tokenParserNaturalTest = do
-- parse natural
parseTest "1" 1 testTokenParser.natural
-- parse hex natural
parseTest "0xFF" 255 testTokenParser.natural
-- parse oct natural
parseTest "0o10 " 8 testTokenParser.natural
-- fail on nonoct
parseErrorTestPosition testTokenParser.natural "0o8" $ mkPos 3
-- fail on no digits
parseErrorTestPosition testTokenParser.natural "0o" $ mkPos 3
tokenParserIntegerTest :: TestM
tokenParserIntegerTest = do
-- parse integer
parseTest "100" 100 testTokenParser.integer
-- parse plus
parseTest "+200" 200 testTokenParser.integer
-- parse minus
parseTest "- 100" (-100) testTokenParser.integer
tokenParserFloatTest :: TestM
tokenParserFloatTest = do
-- parse float
parseTest "100.5" 100.5 testTokenParser.float
-- parse float with exponent
parseTest "100e1" 1000.0 testTokenParser.float
-- parse float with exponent
parseTest "100.5e1" 1005.0 testTokenParser.float
-- fail on nonfloat
parseErrorTestPosition testTokenParser.float "100.e1" $ mkPos 5
-- TODO
tokenParserNaturalOrFloatTest :: TestM
tokenParserNaturalOrFloatTest = do
pure unit
tokenParserDecimalTest :: TestM
tokenParserDecimalTest = do
-- parse decimal
parseTest "0202" 202 testTokenParser.decimal
-- fail on nondecimal
parseErrorTestPosition testTokenParser.decimal "foo" $ mkPos 1
-- TODO
tokenParserHexadecimalTest :: TestM
tokenParserHexadecimalTest = do
pure unit
-- TODO
tokenParserOctalTest :: TestM
tokenParserOctalTest = do
pure unit
tokenParserSymbolTest :: TestM
tokenParserSymbolTest = do
-- parse symbol
parseTest "hello " "hello" $ testTokenParser.symbol "hello"
-- TODO
tokenParserLexemeTest :: TestM
tokenParserLexemeTest = do
pure unit
-- TODO
tokenParserWhiteSpaceTest :: TestM
tokenParserWhiteSpaceTest = do
pure unit
tokenParserParensTest :: TestM
tokenParserParensTest = do
-- parse parens
parseTest "(hello)" "hello" $ testTokenParser.parens $ string "hello"
-- fail on non-closed parens
parseErrorTestPosition (testTokenParser.parens $ string "hello") "(hello" $ mkPos 7
tokenParserBracesTest :: TestM
tokenParserBracesTest = do
-- parse braces
parseTest "{hello}" "hello" $ testTokenParser.braces $ string "hello"
-- fail on non-closed braces
parseErrorTestPosition (testTokenParser.braces $ string "hello") "{hello" $ mkPos 7
tokenParserAnglesTest :: TestM
tokenParserAnglesTest = do
-- parse angles
parseTest "<hello>" "hello" $ testTokenParser.angles $ string "hello"
-- fail on non-closed angles
parseErrorTestPosition (testTokenParser.angles $ string "hello") "<hello" $ mkPos 7
tokenParserBracketsTest :: TestM
tokenParserBracketsTest = do
-- parse brackets
parseTest "[hello]" "hello" $ testTokenParser.brackets $ string "hello"
-- fail on non-closed brackets
parseErrorTestPosition (testTokenParser.brackets $ string "hello") "[hello" $ mkPos 7
tokenParserSemiTest :: TestM
tokenParserSemiTest = do
-- parse semicolon
parseTest ";" ";" testTokenParser.semi
-- fail on non-semicolon
parseErrorTestPosition testTokenParser.semi "a" $ mkPos 1
tokenParserCommaTest :: TestM
tokenParserCommaTest = do
-- parse comma
parseTest "," "," testTokenParser.comma
-- fail on non-comma
parseErrorTestPosition testTokenParser.comma "a" $ mkPos 1
tokenParserColonTest :: TestM
tokenParserColonTest = do
-- parse colon
parseTest ":" ":" testTokenParser.colon
-- fail on non-colon
parseErrorTestPosition testTokenParser.colon "a" $ mkPos 1
tokenParserDotTest :: TestM
tokenParserDotTest = do
-- parse dot
parseTest "." "." testTokenParser.dot
-- fail on non-dot
parseErrorTestPosition testTokenParser.dot "a" $ mkPos 1
tokenParserSemiSepTest :: TestM
tokenParserSemiSepTest = do
-- parse semi sep
parseTest "foo; foo" (fromFoldable [ "foo", "foo" ]) $ testTokenParser.semiSep $ string "foo"
-- parse semi sep with newline
parseTest "foo; \nfoo" (fromFoldable [ "foo", "foo" ]) $ testTokenParser.semiSep $ string "foo"
-- parse no semi sep
parseTest "" (fromFoldable []) $ testTokenParser.semiSep $ string "foo"
-- parseErrorTestPosition testTokenParser.operator "foo" $ mkPos 1
tokenParserSemiSep1Test :: TestM
tokenParserSemiSep1Test = do
-- parse semi sep1
parseTest "foo; foo" (cons "foo" (cons' "foo" Nil)) $ testTokenParser.semiSep1 $ string "foo"
-- parse semi sep1 with newline
parseTest "foo; \nfoo" (cons "foo" (cons' "foo" Nil)) $ testTokenParser.semiSep1 $ string "foo"
-- no parse on empty string
parseErrorTestPosition (testTokenParser.semiSep1 $ string "foo") "" $ mkPos 1
tokenParserCommaSepTest :: TestM
tokenParserCommaSepTest = do
-- parse comma sep
parseTest "foo, foo" (fromFoldable [ "foo", "foo" ]) $ testTokenParser.commaSep $ string "foo"
-- parse comma sep with newline
parseTest "foo, \nfoo" (fromFoldable [ "foo", "foo" ]) $ testTokenParser.commaSep $ string "foo"
-- parse no comma sep
parseTest "" (fromFoldable []) $ testTokenParser.commaSep $ string "foo"
tokenParserCommaSep1Test :: TestM
tokenParserCommaSep1Test = do
-- parse comma sep1
parseTest "foo, foo" (cons "foo" (cons' "foo" Nil)) $ testTokenParser.commaSep1 $ string "foo"
-- parse comma sep1 with newline
parseTest "foo, \nfoo" (cons "foo" (cons' "foo" Nil)) $ testTokenParser.commaSep1 $ string "foo"
-- no parse on empty string
parseErrorTestPosition (testTokenParser.commaSep1 $ string "foo") "" $ mkPos 1
haskellStyleTest :: TestM
haskellStyleTest = do
let haskellTokParser = makeTokenParser haskellStyle
-- make sure haskell-style comments work
parseTest "hello {- comment\n -} fo_" "fo_" $ haskellTokParser.identifier *> haskellTokParser.identifier
-- make sure java-style comments do not work
parseErrorTestPosition
(haskellTokParser.identifier *> haskellTokParser.identifier)
"hello /* comment\n */ foo"
(mkPos 7)
javaStyleTest :: TestM
javaStyleTest = do
let javaTokParser = makeTokenParser javaStyle
-- make sure java-style comments work
parseTest "hello /* comment\n */ fo_" "fo_" $ javaTokParser.identifier *> javaTokParser.identifier
-- make sure java-style identifier work
parseTest "$hello /* comment\n */ _f$o_" "_f$o_" $ javaTokParser.identifier *> javaTokParser.identifier
-- make sure haskell-style comments do not work
parseErrorTestPosition
(javaTokParser.identifier *> javaTokParser.identifier)
"hello {- comment\n -} foo"
(mkPos 7)
main :: Effect Unit
main = do
log "\nTESTS String\n"
parseErrorTestPosition
(many $ char 'f' *> char '?')
"foo"
(Position { index: 1, column: 2, line: 1 })
parseErrorTestPosition
(satisfy (_ == '?'))
"foo"
(Position { index: 0, column: 1, line: 1 })
parseTest
"foo"
Nil
(many $ try $ char 'f' *> char '?')
parseTest "(((a)))" 3 nested
parseTest "aaa" (Cons "a" (Cons "a" (Cons "a" Nil))) $ many (string "a")
parseTest "abc-" [ 'a', 'b', 'c' ] $ Combinators.Array.many letter
parseTest "(ab)" (Just "b") $ parens do
_ <- string "a"
optionMaybe $ string "b"
parseTest "a,a,a" (cons "a" (cons "a" (cons' "a" Nil))) $ string "a" `sepBy1` string ","
parseTest "a,a,a," (cons "a" (cons "a" (cons' "a" Nil))) $ do
as <- string "a" `endBy1` string ","
eof
pure as
parseTest "a+b+c" "abc" opTest
parseTest "1*2+3/4-5" (-3) exprTest
parseTest "ab?" "ab" manySatisfyTest
parseTest "ab" unit (char 'a' *> notFollowedBy (char 'a'))
parseTest "rest" "rest" rest
parseTest "rest" unit (rest *> eof)
parseTest "rest\nrest" (Position { index: 9, line: 2, column: 5 }) (rest *> position)
parseErrorTestPosition
(rest *> notFollowedBy eof)
"aa\naa"
(Position { index: 5, column: 3, line: 2 })
parseErrorTestPosition
(string "π
π
" *> string "π
π
")
"π
π
xπ
‘"
(Position { index: 2, column: 3, line: 1 })
parseTest "π
π
xπ
‘" [ "π
", "π
", "x", "π
‘" ] do
quarter <- anyCodePoint
eighth <- (singleton <$> char 'x') <|> string "π
"
letterx <- string "π
‘" <|> string "x"
sixteenth <- string "π
‘" <|> (singleton <$> char 'x')
pure $ [ SCP.singleton quarter, eighth, letterx, sixteenth ]
parseTest "π€π―β
π€π―" [ "π€π―", "β
π€π―" ] do
none <- Array.many $ noneOfCodePoints $ SCP.toCodePointArray "ββ
"
one <- Array.many $ oneOfCodePoints $ SCP.toCodePointArray "π€π―β
"
pure $ SCP.fromCodePointArray <$> [ none, one ]
parseTest "abcd" "ab" $ takeN 2
parseTest "abcd" "" $ takeN 0
parseErrorTestPosition (takeN 10) "abcd" (Position { index: 0, column: 1, line: 1 })
parseErrorTestPosition (takeN (-1)) "abcd" (Position { index: 0, column: 1, line: 1 })
parseErrorTestMessage
(noneOfCodePoints $ SCP.toCodePointArray "ββ
")
"β"
"Expected none of [\"β\",\"β
\"]"
parseErrorTestMessage
(oneOfCodePoints $ SCP.toCodePointArray "ββ
")
"abc"
"Expected one of [\"β\",\"β
\"]"
parseTest "aa bb" [ "aa", " ", "bb" ] do
aa <- SCU.fromCharArray <$> some letter
w <- whiteSpace
bb <- SCU.fromCharArray <$> some letter
pure [ aa, w, bb ]
let tokpos = const initialPos
parseTest (fromFoldable [ A, B ]) A (token tokpos)
parseTest (fromFoldable [ B, A ]) B (token tokpos)
parseTest (fromFoldable [ A, B ]) A (when tokpos isA)
parseTest (fromFoldable [ A ]) A (Token.match tokpos A)
parseTest (fromFoldable [ B ]) B (Token.match tokpos B)
parseTest (fromFoldable [ A, B ]) A (Token.match tokpos A)
parseTest (fromFoldable []) unit Token.eof
parseTest "aabb" (Tuple (fromFoldable [ 'a', 'a' ]) 'b') (manyTill_ (char 'a') (char 'b'))
parseTest "aabb" (Tuple (unsafePartial $ fromJust (NE.fromFoldable [ 'a', 'a' ])) 'b') (many1Till_ (char 'a') (char 'b'))
parseTest "aab" (Tuple (fromFoldable [ 'a', 'a' ]) 'b') do
Tuple a b <- manyTill_ letter do
char 'b'
pure (Tuple a b)
parseTest "ababab" [ 'b', 'b', 'b' ] $ Array.many (char 'a' *> char 'b')
parseTest "abaXab" [ 'b' ] $ Array.many (try (char 'a' *> char 'b'))
parseErrorTestPosition (string "abc") "bcd" (Position { index: 0, column: 1, line: 1 })
parseErrorTestPosition (string "abc" *> eof) "abcdefg" (Position { index: 3, column: 4, line: 1 })
parseErrorTestPosition (string "a\nb\nc\n" *> eof) "a\nb\nc\nd\n" (Position { index: 6, column: 1, line: 4 })
parseErrorTestPosition (string "\ta" *> eof) "\tab" (Position { index: 2, column: 10, line: 1 })
assertEqual' "withRecovery1"
{ actual: runParser " not-an-int here" do
_ <- takeWhile isSpace
withRecovery
( \err -> do
nonint <- takeWhile (not <<< isSpace)
pure $ Left
{ error: err
, input: nonint
}
)
(Right <$> intDecimal)
, expected:
Right $ Left
{ error: ParseError "Expected Int" (Position { index: 2, column: 3, line: 1 })
, input: "not-an-int"
} :: Either ParseError (Either { error :: ParseError, input :: String } Int)
}
assertEqual' "skipSpaces consumes if position advancement issue #200"
{ actual: runParser " " do
skipSpaces
ParseState _ _ c <- getParserT
pure c
, expected: Right true
}
assertEqual' "skipSpaces doesn't consume if no position advancement issue #200"
{ actual: runParser "x" do
skipSpaces
ParseState _ _ c <- getParserT
pure c
, expected: Right false
}
do
let
inContext :: forall s m a. (String -> String) -> ParserT s m a -> ParserT s m a
inContext context = region \(ParseError message pos) -> ParseError (context message) pos
input = "Tokyo thirty-nine million"
assertEqual' "region 1"
{ actual: runParser input do
inContext ("Megacity list: " <> _) do
cityname <- inContext ("city name: " <> _) (takeWhile CodePoint.Unicode.isLetter)
skipSpaces
population <- inContext ("population: " <> _) intDecimal
pure $ Tuple cityname population
, expected: (Left (ParseError "Megacity list: population: Expected Int" (Position { column: 7, index: 6, line: 1 })))
}
assertEqual' "tryRethrow 1"
{ actual: runParser "abx" $ char 'a' *> tryRethrow (char 'b' *> char 'c')
, expected: Left $ ParseError "Expected 'c'" (Position { index: 1, column: 2, line: 1 })
}
assertEqual' "takeWhile 1"
{ actual: runParser "Tackling the Awkward" do
takeWhile CodePoint.Unicode.isLetter <* string " the Awkward"
, expected: Right "Tackling"
}
assertEqual' "takeWhile1 1"
{ actual: runParser "3ackling the Awkward" do
takeWhile1 CodePoint.Unicode.isLetter <* string " the Awkward" <?> "letter"
, expected: Left $ ParseError "Expected letter" (Position { index: 0, line: 1, column: 1 })
}
log "\nTESTS number\n"
-- assert' "Number.fromString" $ Just infinity == Data.Number.fromString "Infinity"
assertEqual' "number Infinity"
{ actual: runParser "Infinity" number
, expected: Right infinity
}
assertEqual' "number +Infinity"
{ actual: runParser "+Infinity" number
, expected: Right infinity
}
assertEqual' "number -Infinity"
{ actual: runParser "-Infinity" number
, expected: Right (negate infinity)
}
assertEqual' "number +xxx"
{ actual: lmap parseErrorPosition $ runParser "+xxx" number
, expected: Left $ Position { index: 0, line: 1, column: 1 }
}
assertEqual' "number 1"
{ actual: runParser "-3.0E-1.0" number
, expected: Right (-0.3)
}
assertEqual' "number xEy"
{ actual: runParser "2e1" number
, expected: Right 20.0
}
-- test from issue #73
assertEqual' "number 2"
{ actual: runParser "0.7531531167929774" number
, expected: Right 0.7531531167929774
}
-- test from issue #115
assertEqual' "number 3"
{ actual: runParser "-6.0" number
, expected: Right (-6.0)
}
assertEqual' "number 4"
{ actual: runParser "+6.0" number
, expected: Right (6.0)
}
assert' "number NaN 1" $ either (\_ -> false) Data.Number.isNaN (runParser (show nan) number)
assert' "number NaN 2" $ either (\_ -> false) Data.Number.isNaN (runParser "NaN" number)
assertEqual' "number 5"
{ actual: runParser "1..3" number
, expected: Right (1.0)
}
log "\nTESTS Operator\n"
-- test from issue #161
-- all the below operators should play well together
parseErrorTestMessage
( oneOf
[ fail "test <?>"
, string " " <?> "1"
, string " " <|> string " " <?> "2"
, string " " <?> "3" <|> string " " <?> "4"
, "" <$ string " " <?> "5"
<|> string " " $> "" <?> "6"
<|> const "" <$> string " " <?> "7"
<* string " "
$> ""
<?> "8"
*> string " "
$> ""
<?> "9"
, fail "test <~?>"
, string " " <~?> \_ -> "21"
, string " " <|> string " " <~?> \_ -> "22"
, string " " <~?> (\_ -> "23") <|> string " " <~?> \_ -> "24"
, "" <$ string " " <~?> (\_ -> "25")
<|> string " " $> "" <~?> (\_ -> "26")
<|> const "" <$> string " " <~?> (\_ -> "27")
<* string " "
$> ""
<~?> (\_ -> "28")
*> string " "
$> ""
<~?> \_ -> "29"
, fail "test <??>"
, "41" <??> string " "
, "42" <??> string " " <|> string " "
, "43" <??> string " " <|> "44" <??> string " "
, "45" <??> "" <$ string " "
<|>
"46"
<??> string " " $> ""
<|>
"47"
<??> const "" <$> string " "
<* ("48" <??> string " ")
*> ("49" <??> string " ")
]
)
"no"
"No alternative"
-- Choice shouldn't always yield "No alternative", that's what `oneOf` is for.
parseErrorTestMessage
( choice
[ string "a"
, string "b"
]
)
"c"
"Expected \"b\""
log "\nTESTS intDecimal\n"
parseTest "-300" (-300) intDecimal
log "\nTESTS Regex\n"
mkRegexTest "regex-" "regex" "regex" noFlags (\regex -> regex <* char '-' <* eof)
mkRegexTest "-regex" "regex" "regex" noFlags (\regex -> char '-' *> regex <* eof)
mkRegexTest "regexregex" "regexregex" "(regex)*" noFlags identity
mkRegexTest "regexregex" "regex" "(^regex)*" noFlags identity
mkRegexTest "ReGeX" "ReGeX" "regex" ignoreCase identity
mkRegexTest "regexcapregexcap" "regexcap" "(?<CaptureGroupName>regexcap)" noFlags identity
mkRegexTest "regexcapregexcap" "regexcap" "(((?<CaptureGroupName>(r)e(g)excap)))" noFlags identity
log "\nTESTS Stack Safe Loops\n"
stackSafeLoopsTest
log "\nTESTS Token Parser\n"
tokenParserIdentifierTest
tokenParserReservedTest
tokenParserOperatorTest
tokenParserReservedOpTest
tokenParserCharLiteralTest
tokenParserStringLiteralTest
tokenParserNaturalTest
tokenParserIntegerTest
tokenParserFloatTest
tokenParserNaturalOrFloatTest
tokenParserDecimalTest
tokenParserHexadecimalTest
tokenParserOctalTest
tokenParserSymbolTest
tokenParserLexemeTest
tokenParserWhiteSpaceTest
tokenParserParensTest
tokenParserBracesTest
tokenParserAnglesTest
tokenParserBracketsTest
tokenParserSemiTest
tokenParserCommaTest
tokenParserColonTest
tokenParserDotTest
tokenParserSemiSepTest
tokenParserSemiSep1Test
tokenParserCommaSepTest
tokenParserCommaSep1Test
log "\nTESTS Haskell Style\n"
haskellStyleTest
log "\nTESTS Java Style\n"
javaStyleTest
log "\nTESTS region\n"
let
prependContext m' (ParseError m pos) = ParseError (m' <> m) pos
p = region (prependContext "context1 ") $ do
_ <- string "a"
region (prependContext "context2 ") $ do
string "b"
case runParser "aa" p of
Right _ -> assert' "error: ParseError expected!" false
Left (ParseError message _) -> do
let messageExpected = "context1 context2 Expected \"b\""
assert' ("expected message: " <> messageExpected <> ", message: " <> message) (message == messageExpected)
logShow messageExpected
log "\nTESTS anyTill\n"
parseTest "π
π
π
π
π
" (Tuple "" "π
") $ anyTill (string "π
")
parseTest "π
π
π
π
π
" (Tuple "π
π
" "π
") $ anyTill (string "π
")
parseTest "π
π
π
π
π
" (Tuple "π
π
π
π
" "π
") $ anyTill (string "π
") <* eof
parseErrorTestPosition (anyTill (string "π
")) "π
π
π
π
" (Position { index: 4, line: 1, column: 5 })
log "\nTESTS Replace\n"
assertEqual' "anyTill1"
{ actual: runParser "Baaaa" $ anyTill $ string "B"
, expected: Right $ Tuple "" "B"
}
assertEqual' "anyTill2"
{ actual: runParser "aaBaa" $ anyTill $ string "B"
, expected: Right $ Tuple "aa" "B"
}
assertEqual' "anyTill3"
{ actual: runParser "aaaaB" $ anyTill $ string "B"
, expected: Right $ Tuple "aaaa" "B"
}
assertEqual' "breakCap1"
{ actual: breakCap "Baaaa" (string "B")
, expected: Just $ "" /\ "B" /\ "aaaa"
}
assertEqual' "breakCap2"
{ actual: breakCap "aaBaa" (string "B")
, expected: Just $ "aa" /\ "B" /\ "aa"
}
assertEqual' "breakCap3"
{ actual: breakCap "aaaaB" (string "B")
, expected: Just $ "aaaa" /\ "B" /\ ""
}
assertEqual' "breakCap3"
{ actual: breakCap "" (string "B")
, expected: Nothing
}
assertEqual' "breakCap4"
{ actual: breakCap "aaBaa" (lookAhead $ string "B")
, expected: Just $ "aa" /\ "B" /\ "Baa"
}
assertEqual' "breakCap5"
{ actual: breakCap "aaBaa" (match $ string "B")
, expected: Just $ "aa" /\ ("B" /\ "B") /\ "aa"
}
assertEqual' "breakCap6"
{ actual: breakCap "aaBaa" (match $ lookAhead $ string "B")
, expected: Just $ "aa" /\ ("" /\ "B") /\ "Baa"
}
assertEqual' "splitCap1"
{ actual: splitCap "BaB" (string "B")
, expected: NonEmptyList $ Right "B" :| Left "a" : Right "B" : Nil
}
assertEqual' "splitCap2"
{ actual: splitCap "aBaB" (string "B")
, expected: NonEmptyList $ Left "a" :| Right "B" : Left "a" : Right "B" : Nil
}
assertEqual' "splitCap3"
{ actual: splitCap "aBaBa" (string "B")
, expected: NonEmptyList $ Left "a" :| Right "B" : Left "a" : Right "B" : Left "a" : Nil
}
assertEqual' "splitCap4"