Fix a ParserK to ParserD conversion bug

This commit is contained in:
Harendra Kumar 2024-07-31 08:27:17 +05:30
parent 7fbc066e73
commit b86d3be5a1
2 changed files with 25 additions and 7 deletions

View File

@ -725,10 +725,12 @@ adaptCG (ParserD.Parser step initial extract) =
-- | A continuation to extract the result when a CPS parser is done.
{-# INLINE parserDone #-}
parserDone :: Monad m => ParseResult b -> Int -> Input a -> m (Step a m b)
parserDone (Success n b) _ None = return $ Done (negate n) b
parserDone (Success n b) _ (Chunk _) = return $ Done (1 - n) b
parserDone (Failure n e) _ None = return $ Error (negate n) e
parserDone (Failure n e) _ (Chunk _) = return $ Error (1 - n) e
parserDone (Success n b) _ _ = do
assertM(n <= 1)
return $ Done n b
parserDone (Failure n e) _ _ = do
assertM(n <= 1)
return $ Error n e
-- XXX Note that this works only for single element parsers and not for Array
-- input parsers. The asserts will fail for array parsers.

View File

@ -797,19 +797,35 @@ toParser = do
-- NOTE: Without fusionBreaker this test would pass even if toParser has
-- incorrect implementation because of fusion rules.
let p2 =
let p2 = Parser.takeWhile (<= 3) FL.toList
runP2 xs = Stream.parseBreak p2 (Stream.fromList xs)
p3 = ParserK.adapt (Parser.takeWhile (<= 3) FL.toList)
runP3 xs = StreamK.parseBreak p3 (StreamK.fromList xs)
p4 =
ParserK.toParser
$ fusionBreaker
$ ParserK.adapt (Parser.takeWhile (<= 3) FL.toList)
runP2 xs = Stream.parseBreak p2 (Stream.fromList xs)
runP4 xs = Stream.parseBreak p4 (Stream.fromList xs)
describe "toParser . adapt" $ do
it "(<= 3) for [1, 2, 3, 4, 5]" $ do
(a, b) <- runP2 ([1, 2, 3, 4, 5] :: [Int])
fromRight undefined a `shouldBe` [1, 2, 3]
rest <- Stream.toList b
rest `shouldBe` [4, 5]
it "(<= 3) for [1, 2, 3, 4, 5]" $ do
(a, b) <- runP3 ([1, 2, 3, 4, 5] :: [Int])
fromRight undefined a `shouldBe` [1, 2, 3]
rest <- StreamK.toList b
rest `shouldBe` [4, 5]
it "(<= 3) for [1, 2, 3, 4, 5]" $ do
(a, b) <- runP4 ([1, 2, 3, 4, 5] :: [Int])
fromRight undefined a `shouldBe` [1,2,3]
rest <- Stream.toList b
rest `shouldBe` [4, 5]
it "(<= 3) for [1, 2, 3]" $ do
(a, b) <- runP2 ([1, 2, 3] :: [Int])
(a, b) <- runP4 ([1, 2, 3] :: [Int])
fromRight undefined a `shouldBe` [1, 2, 3]
rest <- Stream.toList b
rest `shouldBe` []