@@ -745,6 +745,306 @@ manyEqParseMany =
746 split i = P.fromFold (FL.take i FL.toList)
746 split i = P.fromFold (FL.take i FL.toList)
749+takeEndBy1 :: Property
751+ forAll (listOf (chooseInt (0, 1))) $ \ls ->
752+ case S.parse (P.takeEndBy predicate prsr) (S.fromList ls) of
753+ Right parsed_list ->
756+ (takeWhileAndFirstFail (not . predicate) ls)
757+ Left _ -> property False
759+ prsr = P.many (P.satisfy (const True)) FL.toList
763+ takeWhileAndFirstFail prd (x : xs) =
765+ then x : takeWhileAndFirstFail prd xs
767+ takeWhileAndFirstFail _ [] = []
769+takeEndBy2 :: Property
771+ forAll (listOf (chooseInt (0, 1))) $ \ls ->
773+ strm = S.fromList ls
779+ S.parseMany (P.takeEndBy predicate prsr) strm
783+ prsr = P.many (P.satisfy (const True)) FL.toList
790+ then S.toList $ S.append strm1 (S.fromList [])
795+ strm1 = S.splitWithSuffix predicate FL.toList strm
797+ case eitherParsedList of
798+ Left _ -> property False
800+ case eitherSplitList of
801+ Left _ -> property False
802+ Right splitList -> checkListEqual parsedList splitList
804+takeEndByEsc :: Property
806+ forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
808+ msg = "takeEndByEsc: trailing escape"
812+ isEsc x = x `mod` 6 == 0
814+ prsr = P.takeEndByEsc isEsc isSep prsr0
818+ prsr0 = P.many (P.satisfy (const True)) FL.toList
820+ escapeSep maybePrevEsc [] =
821+ case maybePrevEsc of
823+ Just prevEsc -> [prevEsc]
824+ escapeSep maybePrevEsc (x : xs) =
825+ case maybePrevEsc of
828+ then escapeSep (Just x) xs
832+ else x : escapeSep Nothing xs
834+ x : escapeSep Nothing xs
836+ case S.parse prsr (S.fromList ls) of
837+ Right parsed_list -> checkListEqual parsed_list $ escapeSep Nothing ls
838+ Left err -> property (displayException err == msg)
840+takeFramedByEsc_ :: Property
842+ forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
850+ prsr = P.takeFramedByEsc_ isEsc isBegin isEnd FL.toList
852+ checkPass (x : xs) maybePrevEsc openMinusClose =
853+ case maybePrevEsc of
856+ then checkPass xs (Just x) openMinusClose
859+ then checkPass xs Nothing (openMinusClose + 1)
863+ case openMinusClose of
870+ (openMinusClose - 1)
872+ checkPass xs Nothing openMinusClose
873+ Just _ -> checkPass xs Nothing openMinusClose
874+ checkPass [] _ _ = False
876+ escapeFrame begin end escape l =
878+ helper (x : xs) maybePrevEsc openMinusClose =
879+ case maybePrevEsc of
882+ then helper xs (Just x) openMinusClose
885+ then helper xs Nothing (openMinusClose + 1)
889+ if openMinusClose - 1 == 0
895+ (openMinusClose - 1)
897+ x : helper xs Nothing openMinusClose
899+ if escape x || begin x || end x
900+ then x : helper xs Nothing openMinusClose
902+ prevEsc : x : helper xs Nothing openMinusClose
903+ helper [] _ _ = error "Cannot Reach Here"
905+ helper l Nothing (0 :: Int)
907+ case S.parse prsr (S.fromList ls) of
908+ Right parsed_list ->
909+ if checkPass ls Nothing (0 :: Int)
910+ then checkListEqual parsed_list $
911+ escapeFrame isBegin isEnd isEsc ls
914+ if checkPass ls Nothing (0 :: Int)
918+takeFramedByEsc_Pass :: Property
919+takeFramedByEsc_Pass =
920+ forAll (listOf (chooseInt (min_value, max_value))) $ \list ->
922+ ls = (0 : list) ++ (Prelude.replicate (Prelude.length list + 1) 1)
930+ prsr = P.takeFramedByEsc_ isEsc isBegin isEnd FL.toList
932+ escapeFrame begin end escape l =
934+ helper (x : xs) maybePrevEsc openMinusClose =
935+ case maybePrevEsc of
938+ then helper xs (Just x) openMinusClose
942+ if openMinusClose == 0
943+ then helper xs Nothing (openMinusClose + 1)
944+ else x : helper xs Nothing (openMinusClose + 1)
948+ if openMinusClose - 1 == 0
955+ (openMinusClose - 1)
957+ x : helper xs Nothing openMinusClose
959+ if escape x || begin x || end x
960+ then helper xs Nothing openMinusClose
962+ x : helper xs Nothing openMinusClose
963+ helper [] _ _ = error "Cannot Reach Here"
965+ helper l Nothing (0 :: Int)
967+ case S.parse prsr (S.fromList ls) of
968+ Right parsed_list -> checkListEqual parsed_list $ escapeFrame isBegin isEnd isEsc ls
971+takeFramedByEsc_Fail1 :: Property
972+takeFramedByEsc_Fail1 =
974+ msg = "takeFramedByEsc_: missing frame end"
982+ prsr = P.takeFramedByEsc_ isEsc isBegin isEnd FL.toList
986+ case S.parse prsr (S.fromList ls) of
987+ Right _ -> property False
988+ Left err -> property (displayException err == msg)
990+takeFramedByEsc_Fail2 :: Property
991+takeFramedByEsc_Fail2 =
993+ msg = "takeFramedByEsc_: missing frame start"
1001+ prsr = P.takeFramedByEsc_ isEsc isBegin isEnd FL.toList
1005+ case S.parse prsr (S.fromList ls) of
1006+ Right _ -> property False
1007+ Left err -> property (displayException err == msg)
1009+takeFramedByEsc_Fail3 :: Property
1010+takeFramedByEsc_Fail3 =
1012+ msg = "takeFramedByEsc_: missing frame end"
1020+ prsr = P.takeFramedByEsc_ isEsc isBegin isEnd FL.toList
1024+ case S.parse prsr (S.fromList ls) of
1025+ Right _ -> property False
1026+ Left err -> property $ (displayException err == msg)
1028+takeStartBy_ :: Property
1030+ forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
1032+ msg = "takeFramedByGeneric: empty token"
1034+ case S.parse parser (S.fromList ls1) of
1035+ Right parsed_list ->
1036+ if not $ Prelude.null ls1
1038+ let tls = Prelude.takeWhile (not . predicate) (tail ls1)
1039+ in checkListEqual parsed_list $
1040+ if predicate (head ls1)
1042+ else Prelude.takeWhile (not . predicate) ls1
1043+ else property $ Prelude.null parsed_list
1044+ Left err -> property (displayException err == msg)
1047+ parser = P.takeStartBy_ predicate FL.toList
748-------------------------------------------------------------------------------
1048-------------------------------------------------------------------------------
750-------------------------------------------------------------------------------
1050-------------------------------------------------------------------------------
@@ -763,7 +1063,6 @@ main =
763 prop "applicative" applicative
1063 prop "applicative" applicative
764 prop "monad" monad
1064 prop "monad" monad
765 prop "sequence" sequence
1065 prop "sequence" sequence
767 describe "Stream parsing" $ do
1066 describe "Stream parsing" $ do
768 prop "parseMany" parseMany
1067 prop "parseMany" parseMany
769 prop "parseMany2Events" parseMany2Events
1068 prop "parseMany2Events" parseMany2Events
@@ -831,4 +1130,16 @@ main =
831 ++ "Prelude.filter (== 0)") some
1130 ++ "Prelude.filter (== 0)") some
832 -- prop "fail due to parser being die" someFail
1131 -- prop "fail due to parser being die" someFail
833 prop "P.many == S.parseMany" manyEqParseMany
1132 prop "P.many == S.parseMany" manyEqParseMany
1134+ prop "takeEndBy_" takeEndBy_
1135+ prop "takeEndBy1" takeEndBy1
1136+ prop "takeEndBy2" takeEndBy2
1137+ prop "takeEndByEsc" takeEndByEsc
1138+ prop "takeFramedByEsc_" takeFramedByEsc_
1139+ prop "takeFramedByEsc_Pass" takeFramedByEsc_Pass
1140+ prop "takeFramedByEsc_Fail1" takeFramedByEsc_Fail1
1141+ prop "takeFramedByEsc_Fail2" takeFramedByEsc_Fail2
1142+ prop "takeFramedByEsc_Fail3" takeFramedByEsc_Fail3
1143+ prop "takeStartBy_" takeStartBy_
834 takeProperties
1145 takeProperties