@@ -21,8 +21,8 @@ module Streamly.Internal.Data.Parser
2121 , Step (..)
2222 , Initial (..)
2323
24− -- * Downgrade to Fold
25− , toFold
24+ -- -- * Downgrade to Fold
25+ -- , toFold
2626
2727 -- First order parsers
2828 -- * Accumulators
@@ -270,6 +270,10 @@ import Prelude hiding
270270-- Downgrade a parser to a Fold
271271-------------------------------------------------------------------------------
272272
273+-- XXX Parsers cannot be converted to folds, because they do not have a
274+-- scanning function. Can we move the applicative folds to parsers instead?
275+-- need to measure the performance.
276+{-
273277-- | Make a 'Fold' from a 'Parser'. The fold just throws an exception if the
274278-- parser fails or tries to backtrack.
275279--
@@ -281,7 +285,7 @@ import Prelude hiding
281285--
282286{-# INLINE toFold #-}
283287toFold :: Monad m => Parser a m b -> Fold m a b
284−toFold (Parser pstep pinitial pextract) = Fold step initial extract
288+toFold (Parser pstep pinitial pextract) = Fold step initial extract final
285289
286290 where
287291
@@ -317,6 +321,7 @@ toFold (Parser pstep pinitial pextract) = Fold step initial extract
317321 Continue n _ -> cerror n
318322 Done n _ -> derror n
319323 Error err -> eerror err
324+-}
320325
321326-------------------------------------------------------------------------------
322327-- Upgrade folds to parses
@@ -327,7 +332,7 @@ toFold (Parser pstep pinitial pextract) = Fold step initial extract
327332--
328333{-# INLINE fromFold #-}
329334fromFold :: Monad m => Fold m a b -> Parser a m b
330−fromFold (Fold fstep finitial fextract) = Parser step initial extract
335+fromFold (Fold fstep finitial _ ffinal) = Parser step initial extract
331336
332337 where
333338
@@ -345,7 +350,7 @@ fromFold (Fold fstep finitial fextract) = Parser step initial extract
345350 FL.Partial s1 -> Partial 0 s1
346351 FL.Done b -> Done 0 b
347352
348− extract = fmap (Done 0) . fextract
353+ extract = fmap (Done 0) . ffinal
349354
350355-- | Convert a Maybe returning fold to an error returning parser. The first
351356-- argument is the error message that the parser would return when the fold
@@ -355,7 +360,7 @@ fromFold (Fold fstep finitial fextract) = Parser step initial extract
355360--
356361{-# INLINE fromFoldMaybe #-}
357362fromFoldMaybe :: Monad m => String -> Fold m a (Maybe b) -> Parser a m b
358−fromFoldMaybe errMsg (Fold fstep finitial fextract) =
363+fromFoldMaybe errMsg (Fold fstep finitial _ ffinal) =
359364 Parser step initial extract
360365
361366 where
@@ -381,7 +386,7 @@ fromFoldMaybe errMsg (Fold fstep finitial fextract) =
381386 Nothing -> Error errMsg
382387
383388 extract s = do
384− res <- fextract s
389+ res <- ffinal s
385390 case res of
386391 Just x -> return $ Done 0 x
387392 Nothing -> return $ Error errMsg
@@ -630,7 +635,7 @@ data Tuple'Fused a b = Tuple'Fused !a !b deriving Show
630635--
631636{-# INLINE takeBetween #-}
632637takeBetween :: Monad m => Int -> Int -> Fold m a b -> Parser a m b
633−takeBetween low high (Fold fstep finitial fextract) =
638+takeBetween low high (Fold fstep finitial _ ffinal) =
634639
635640 Parser step initial (extract streamErr)
636641
@@ -685,7 +690,7 @@ takeBetween low high (Fold fstep finitial fextract) =
685690 then return $ Continue 0 s1
686691 else if i1 < high
687692 then return $ Partial 0 s1
688− else fmap (Done 0) (fextract s)
693+ else fmap (Done 0) (ffinal s)
689694 FL.Done b ->
690695 return
691696 $ if i1 >= low
@@ -695,12 +700,12 @@ takeBetween low high (Fold fstep finitial fextract) =
695700 step (Tuple'Fused i s) a = fstep s a >>= snext i
696701
697702 extract f (Tuple'Fused i s)
698− | i >= low && i <= high = fmap (Done 0) (fextract s)
703+ | i >= low && i <= high = fmap (Done 0) (ffinal s)
699704 | otherwise = return $ Error (f i)
700705
701706 -- XXX Need to make Initial return type Step to deduplicate this
702707 iextract f (Tuple'Fused i s)
703− | i >= low && i <= high = fmap IDone (fextract s)
708+ | i >= low && i <= high = fmap IDone (ffinal s)
704709 | otherwise = return $ IError (f i)
705710
706711-- | Stops after taking exactly @n@ input elements.
@@ -717,7 +722,7 @@ takeBetween low high (Fold fstep finitial fextract) =
717722--
718723{-# INLINE takeEQ #-}
719724takeEQ :: Monad m => Int -> Fold m a b -> Parser a m b
720−takeEQ n (Fold fstep finitial fextract) = Parser step initial extract
725+takeEQ n (Fold fstep finitial _ ffinal) = Parser step initial extract
721726
722727 where
723728
@@ -727,7 +732,7 @@ takeEQ n (Fold fstep finitial fextract) = Parser step initial extract
727732 FL.Partial s ->
728733 if n > 0
729734 then return $ IPartial $ Tuple'Fused 1 s
730− else fmap IDone (fextract s)
735+ else fmap IDone (ffinal s)
731736 FL.Done b -> return $
732737 if n > 0
733738 then IError
@@ -751,7 +756,7 @@ takeEQ n (Fold fstep finitial fextract) = Parser step initial extract
751756 -- assert (n == i1)
752757 Done 0
753758 <$> case res of
754− FL.Partial s -> fextract s
759+ FL.Partial s -> ffinal s
755760 FL.Done b -> return b
756761
757762 extract (Tuple'Fused i _) =
@@ -783,7 +788,7 @@ data TakeGEState s =
783788--
784789{-# INLINE takeGE #-}
785790takeGE :: Monad m => Int -> Fold m a b -> Parser a m b
786−takeGE n (Fold fstep finitial fextract) = Parser step initial extract
791+takeGE n (Fold fstep finitial _ ffinal) = Parser step initial extract
787792
788793 where
789794
@@ -831,7 +836,7 @@ takeGE n (Fold fstep finitial fextract) = Parser step initial extract
831836 $ Error
832837 $ "takeGE: Expecting at least " ++ show n
833838 ++ " elements, input terminated on " ++ show (i - 1)
834− extract (TakeGEGE r) = fmap (Done 0) $ fextract r
839+ extract (TakeGEGE r) = fmap (Done 0) $ ffinal r
835840
836841-------------------------------------------------------------------------------
837842-- Conditional splitting
@@ -892,7 +897,7 @@ takeWhileP predicate (Parser pstep pinitial pextract) =
892897{-# INLINE takeWhile #-}
893898takeWhile :: Monad m => (a -> Bool) -> Fold m a b -> Parser a m b
894899-- takeWhile cond f = takeWhileP cond (fromFold f)
895−takeWhile predicate (Fold fstep finitial fextract) =
900+takeWhile predicate (Fold fstep finitial _ ffinal) =
896901 Parser step initial extract
897902
898903 where
@@ -911,9 +916,9 @@ takeWhile predicate (Fold fstep finitial fextract) =
911916 $ case fres of
912917 FL.Partial s1 -> Partial 0 s1
913918 FL.Done b -> Done 0 b
914− else Done 1 <$> fextract s
919+ else Done 1 <$> ffinal s
915920
916− extract s = fmap (Done 0) (fextract s)
921+ extract s = fmap (Done 0) (ffinal s)
917922
918923{-
919924-- XXX This may not be composable because of the b argument. We can instead
@@ -933,7 +938,7 @@ takeWhile1 acc cond f = undefined
933938{-# INLINE takeWhile1 #-}
934939takeWhile1 :: Monad m => (a -> Bool) -> Fold m a b -> Parser a m b
935940-- takeWhile1 cond f = takeWhileP cond (takeBetween 1 maxBound f)
936−takeWhile1 predicate (Fold fstep finitial fextract) =
941+takeWhile1 predicate (Fold fstep finitial _ ffinal) =
937942 Parser step initial extract
938943
939944 where
@@ -963,11 +968,11 @@ takeWhile1 predicate (Fold fstep finitial fextract) =
963968 if predicate a
964969 then process s a
965970 else do
966− b <- fextract s
971+ b <- ffinal s
967972 return $ Done 1 b
968973
969974 extract (Left' _) = return $ Error "takeWhile1: end of input"
970− extract (Right' s) = fmap (Done 0) (fextract s)
975+ extract (Right' s) = fmap (Done 0) (ffinal s)
971976
972977-- | Drain the input as long as the predicate succeeds, running the effects and
973978-- discarding the results.
@@ -996,7 +1001,7 @@ takeFramedByGeneric :: Monad m =>
9961001 -> Maybe (a -> Bool) -- is frame end?
9971002 -> Fold m a b
9981003 -> Parser a m b
999−takeFramedByGeneric esc begin end (Fold fstep finitial fextract) =
1004+takeFramedByGeneric esc begin end (Fold fstep finitial _ ffinal) =
10001005
10011006 Parser step initial extract
10021007
@@ -1028,20 +1033,20 @@ takeFramedByGeneric esc begin end (Fold fstep finitial fextract) =
10281033 if isEnd a
10291034 then
10301035 if n == 0
1031− then Done 0 <$> fextract s
1036+ then Done 0 <$> ffinal s
10321037 else process s a (n - 1)
10331038 else
10341039 let n1 = if isBegin a then n + 1 else n
10351040 in process s a n1
10361041 Nothing -> -- takeEndBy case
10371042 if isEnd a
1038− then Done 0 <$> fextract s
1043+ then Done 0 <$> ffinal s
10391044 else process s a n
10401045 Nothing -> -- takeStartBy case
10411046 case begin of
10421047 Just isBegin ->
10431048 if isBegin a
1044− then Done 0 <$> fextract s
1049+ then Done 0 <$> ffinal s
10451050 else process s a n
10461051 Nothing ->
10471052 error $ "takeFramedByGeneric: "
@@ -1066,7 +1071,7 @@ takeFramedByGeneric esc begin end (Fold fstep finitial fextract) =
10661071 case end of
10671072 Just isEnd ->
10681073 if isEnd a
1069− then Done 0 <$> fextract s
1074+ then Done 0 <$> ffinal s
10701075 else processCheckEsc s a 0
10711076 Nothing ->
10721077 error "Both begin and end frame predicate missing"
@@ -1081,7 +1086,7 @@ takeFramedByGeneric esc begin end (Fold fstep finitial fextract) =
10811086 case begin of
10821087 Just _ ->
10831088 case end of
1084− Nothing -> fmap (Done 0) $ fextract s
1089+ Nothing -> fmap (Done 0) $ ffinal s
10851090 Just _ -> err "takeFramedByGeneric: missing frame end"
10861091 Nothing -> err "takeFramedByGeneric: missing closing frame"
10871092 extract (FrameEscEsc _ _) = err "takeFramedByGeneric: trailing escape"
@@ -1120,7 +1125,7 @@ blockWithQuotes :: (Monad m, Eq a) =>
11201125 -> Fold m a b
11211126 -> Parser a m b
11221127blockWithQuotes isEsc isQuote bopen bclose
1123− (Fold fstep finitial fextract) =
1128+ (Fold fstep finitial _ ffinal) =
11241129 Parser step initial extract
11251130
11261131 where
@@ -1150,7 +1155,7 @@ blockWithQuotes isEsc isQuote bopen bclose
11501155 | a == bopen = process s a (BlockUnquoted (level + 1))
11511156 | a == bclose =
11521157 if level == 1
1153− then fmap (Done 0) (fextract s)
1158+ then fmap (Done 0) (ffinal s)
11541159 else process s a (BlockUnquoted (level - 1))
11551160 | isQuote a = process s a (BlockQuoted level)
11561161 | otherwise = process s a (BlockUnquoted level)
@@ -1164,7 +1169,7 @@ blockWithQuotes isEsc isQuote bopen bclose
11641169
11651170 err = return . Error
11661171
1167− extract (BlockInit s) = fmap (Done 0) $ fextract s
1172+ extract (BlockInit s) = fmap (Done 0) $ ffinal s
11681173 extract (BlockUnquoted level _) =
11691174 err $ "blockWithQuotes: finished at block nest level " ++ show level
11701175 extract (BlockQuoted level _) =
@@ -1299,7 +1304,7 @@ takeEitherSepBy _cond = undefined -- D.toParserK . D.takeEitherSepBy cond
12991304--
13001305{-# INLINE takeStartBy #-}
13011306takeStartBy :: Monad m => (a -> Bool) -> Fold m a b -> Parser a m b
1302−takeStartBy cond (Fold fstep finitial fextract) =
1307+takeStartBy cond (Fold fstep finitial _ ffinal) =
13031308
13041309 Parser step initial extract
13051310
@@ -1327,10 +1332,10 @@ takeStartBy cond (Fold fstep finitial fextract) =
13271332 step (Right' s) a =
13281333 if not (cond a)
13291334 then process s a
1330− else Done 1 <$> fextract s
1335+ else Done 1 <$> ffinal s
13311336
1332− extract (Left' s) = fmap (Done 0) $ fextract s
1333− extract (Right' s) = fmap (Done 0) $ fextract s
1337+ extract (Left' s) = fmap (Done 0) $ ffinal s
1338+ extract (Right' s) = fmap (Done 0) $ ffinal s
13341339
13351340-- | Like 'takeStartBy' but drops the separator.
13361341--
@@ -1366,7 +1371,7 @@ takeFramedByEsc_ :: Monad m =>
13661371 (a -> Bool) -> (a -> Bool) -> (a -> Bool) -> Fold m a b -> Parser a m b
13671372-- takeFramedByEsc_ isEsc isEnd p =
13681373-- takeFramedByGeneric (Just isEsc) Nothing (Just isEnd) (toFold p)
1369−takeFramedByEsc_ isEsc isBegin isEnd (Fold fstep finitial fextract) =
1374+takeFramedByEsc_ isEsc isBegin isEnd (Fold fstep finitial _ ffinal ) =
13701375
13711376 Parser step initial extract
13721377
@@ -1402,7 +1407,7 @@ takeFramedByEsc_ isEsc isBegin isEnd (Fold fstep finitial fextract) =
14021407 in process s a n1
14031408 else
14041409 if n == 0
1405− then Done 0 <$> fextract s
1410+ then Done 0 <$> ffinal s
14061411 else process s a (n - 1)
14071412 step (FrameEscEsc s n) a = process s a n
14081413
@@ -1424,7 +1429,7 @@ takeFramedBy_ :: Monad m =>
14241429 (a -> Bool) -> (a -> Bool) -> Fold m a b -> Parser a m b
14251430-- takeFramedBy_ isBegin isEnd =
14261431-- takeFramedByGeneric (Just (const False)) (Just isBegin) (Just isEnd)
1427−takeFramedBy_ isBegin isEnd (Fold fstep finitial fextract) =
1432+takeFramedBy_ isBegin isEnd (Fold fstep finitial _ ffinal) =
14281433
14291434 Parser step initial extract
14301435
@@ -1454,7 +1459,7 @@ takeFramedBy_ isBegin isEnd (Fold fstep finitial fextract) =
14541459 | not (isEnd a) =
14551460 let n1 = if isBegin a then n + 1 else n
14561461 in process s a n1
1457− | n == 0 = Done 0 <$> fextract s
1462+ | n == 0 = Done 0 <$> ffinal s
14581463 | otherwise = process s a (n - 1)
14591464
14601465 err = return . Error
@@ -1489,7 +1494,7 @@ data WordByState s b = WBLeft !s | WBWord !s | WBRight !b
14891494--
14901495{-# INLINE wordBy #-}
14911496wordBy :: Monad m => (a -> Bool) -> Fold m a b -> Parser a m b
1492−wordBy predicate (Fold fstep finitial fextract) = Parser step initial extract
1497+wordBy predicate (Fold fstep finitial _ ffinal) = Parser step initial extract
14931498
14941499 where
14951500
@@ -1516,7 +1521,7 @@ wordBy predicate (Fold fstep finitial fextract) = Parser step initial extract
15161521 if not (predicate a)
15171522 then worder s a
15181523 else do
1519− b <- fextract s
1524+ b <- ffinal s
15201525 return $ Partial 0 $ WBRight b
15211526 step (WBRight b) a =
15221527 return
@@ -1524,8 +1529,8 @@ wordBy predicate (Fold fstep finitial fextract) = Parser step initial extract
15241529 then Done 1 b
15251530 else Partial 0 $ WBRight b
15261531
1527− extract (WBLeft s) = fmap (Done 0) $ fextract s
1528− extract (WBWord s) = fmap (Done 0) $ fextract s
1532+ extract (WBLeft s) = fmap (Done 0) $ ffinal s
1533+ extract (WBWord s) = fmap (Done 0) $ ffinal s
15291534 extract (WBRight b) = return (Done 0 b)
15301535
15311536data WordFramedState s b =
@@ -1562,7 +1567,7 @@ wordFramedBy :: Monad m =>
15621567 -> Fold m a b
15631568 -> Parser a m b
15641569wordFramedBy isEsc isBegin isEnd isSep
1565− (Fold fstep finitial fextract) =
1570+ (Fold fstep finitial _ ffinal) =
15661571 Parser step initial extract
15671572
15681573 where
@@ -1593,7 +1598,7 @@ wordFramedBy isEsc isBegin isEnd isSep
15931598 step (WordFramedWord s n) a
15941599 | isEsc a = return $ Continue 0 $ WordFramedEsc s n
15951600 | n == 0 && isSep a = do
1596− b <- fextract s
1601+ b <- ffinal s
15971602 return $ Partial 0 $ WordFramedSkipPost b
15981603 | otherwise = do
15991604 -- We need to use different order for checking begin and end for
@@ -1626,10 +1631,10 @@ wordFramedBy isEsc isBegin isEnd isSep
16261631
16271632 err = return . Error
16281633
1629− extract (WordFramedSkipPre s) = fmap (Done 0) $ fextract s
1634+ extract (WordFramedSkipPre s) = fmap (Done 0) $ ffinal s
16301635 extract (WordFramedWord s n) =
16311636 if n == 0
1632− then fmap (Done 0) $ fextract s
1637+ then fmap (Done 0) $ ffinal s
16331638 else err "wordFramedBy: missing frame end"
16341639 extract (WordFramedEsc _ _) =
16351640 err "wordFramedBy: trailing escape"
@@ -1716,7 +1721,7 @@ wordWithQuotes :: (Monad m, Eq a) =>
17161721 -> Fold m a b
17171722 -> Parser a m b
17181723wordWithQuotes keepQuotes tr escChar toRight isSep
1719− (Fold fstep finitial fextract) =
1724+ (Fold fstep finitial _ ffinal) =
17201725 Parser step initial extract
17211726
17221727 where
@@ -1767,7 +1772,7 @@ wordWithQuotes keepQuotes tr escChar toRight isSep
17671772 step (WordUnquotedWord s) a
17681773 | isEsc a = return $ Continue 0 $ WordUnquotedEsc s
17691774 | isSep a = do
1770− b <- fextract s
1775+ b <- ffinal s
17711776 return $ Partial 0 $ WordQuotedSkipPost b
17721777 | otherwise = do
17731778 case toRight a of
@@ -1815,11 +1820,11 @@ wordWithQuotes keepQuotes tr escChar toRight isSep
18151820
18161821 err = return . Error
18171822
1818− extract (WordQuotedSkipPre s) = fmap (Done 0) $ fextract s
1819− extract (WordUnquotedWord s) = fmap (Done 0) $ fextract s
1823+ extract (WordQuotedSkipPre s) = fmap (Done 0) $ ffinal s
1824+ extract (WordUnquotedWord s) = fmap (Done 0) $ ffinal s
18201825 extract (WordQuotedWord s n _ _) =
18211826 if n == 0
1822− then fmap (Done 0) $ fextract s
1827+ then fmap (Done 0) $ ffinal s
18231828 else err "wordWithQuotes: missing frame end"
18241829 extract WordQuotedEsc {} =
18251830 err "wordWithQuotes: trailing escape"
@@ -1897,7 +1902,7 @@ data GroupByState a s
18971902--
18981903{-# INLINE groupBy #-}
18991904groupBy :: Monad m => (a -> a -> Bool) -> Fold m a b -> Parser a m b
1900−groupBy eq (Fold fstep finitial fextract) = Parser step initial extract
1905+groupBy eq (Fold fstep finitial _ ffinal) = Parser step initial extract
19011906
19021907 where
19031908
@@ -1920,10 +1925,10 @@ groupBy eq (Fold fstep finitial fextract) = Parser step initial extract
19201925 step (GroupByGrouping a0 s) a =
19211926 if eq a0 a
19221927 then grouper s a0 a
1923− else Done 1 <$> fextract s
1928+ else Done 1 <$> ffinal s
19241929
1925− extract (GroupByInit s) = fmap (Done 0) $ fextract s
1926− extract (GroupByGrouping _ s) = fmap (Done 0) $ fextract s
1930+ extract (GroupByInit s) = fmap (Done 0) $ ffinal s
1931+ extract (GroupByGrouping _ s) = fmap (Done 0) $ ffinal s
19271932
19281933-- | Unlike 'groupBy' this combinator performs a rolling comparison of two
19291934-- successive elements in the input stream. Assuming the input stream
@@ -1957,7 +1962,7 @@ groupBy eq (Fold fstep finitial fextract) = Parser step initial extract
19571962--
19581963{-# INLINE groupByRolling #-}
19591964groupByRolling :: Monad m => (a -> a -> Bool) -> Fold m a b -> Parser a m b
1960−groupByRolling eq (Fold fstep finitial fextract) = Parser step initial extract
1965+groupByRolling eq (Fold fstep finitial _ ffinal) = Parser step initial extract
19611966
19621967 where
19631968
@@ -1980,10 +1985,10 @@ groupByRolling eq (Fold fstep finitial fextract) = Parser step initial extract
19801985 step (GroupByGrouping a0 s) a =
19811986 if eq a0 a
19821987 then grouper s a
1983− else Done 1 <$> fextract s
1988+ else Done 1 <$> ffinal s
19841989
1985− extract (GroupByInit s) = fmap (Done 0) $ fextract s
1986− extract (GroupByGrouping _ s) = fmap (Done 0) $ fextract s
1990+ extract (GroupByInit s) = fmap (Done 0) $ ffinal s
1991+ extract (GroupByGrouping _ s) = fmap (Done 0) $ ffinal s
19871992
19881993{-# ANN type GroupByStatePair Fuse #-}
19891994data GroupByStatePair a s1 s2
@@ -2007,8 +2012,8 @@ groupByRollingEither :: Monad m =>
20072012 (a -> a -> Bool) -> Fold m a b -> Fold m a c -> Parser a m (Either b c)
20082013groupByRollingEither
20092014 eq
2010− (Fold fstep1 finitial1 fextract1)
2011− (Fold fstep2 finitial2 fextract2) = Parser step initial extract
2015+ (Fold fstep1 finitial1 _ ffinal1)
2016+ (Fold fstep2 finitial2 _ ffinal2) = Parser step initial extract
20122017
20132018 where
20142019
@@ -2067,21 +2072,21 @@ groupByRollingEither
20672072 step (GroupByGroupingPairL a0 s1 s2) a =
20682073 if not (eq a0 a)
20692074 then grouperL2 s1 s2 a
2070− else Done 1 . Left <$> fextract1 s1
2075+ else Done 1 . Left <$> ffinal1 s1
20712076
20722077 step (GroupByGroupingPairR a0 s1 s2) a =
20732078 if eq a0 a
20742079 then grouperR2 s1 s2 a
2075− else Done 1 . Right <$> fextract2 s2
2080+ else Done 1 . Right <$> ffinal2 s2
20762081
2077− extract (GroupByInitPair s1 _) = Done 0 . Left <$> fextract1 s1
2078− extract (GroupByGroupingPairL _ s1 _) = Done 0 . Left <$> fextract1 s1
2079− extract (GroupByGroupingPairR _ _ s2) = Done 0 . Right <$> fextract2 s2
2082+ extract (GroupByInitPair s1 _) = Done 0 . Left <$> ffinal1 s1
2083+ extract (GroupByGroupingPairL _ s1 _) = Done 0 . Left <$> ffinal1 s1
2084+ extract (GroupByGroupingPairR _ _ s2) = Done 0 . Right <$> ffinal2 s2
20802085 extract (GroupByGroupingPair a s1 _) = do
20812086 res <- fstep1 s1 a
20822087 case res of
20832088 FL.Done b -> return $ Done 0 (Left b)
2084− FL.Partial s11 -> Done 0 . Left <$> fextract1 s11
2089+ FL.Partial s11 -> Done 0 . Left <$> ffinal1 s11
20852090
20862091-- XXX use an Unfold instead of a list?
20872092-- XXX custom combinators for matching list, array and stream?
@@ -2234,7 +2239,7 @@ postscan = undefined
22342239{-# INLINE zipWithM #-}
22352240zipWithM :: Monad m =>
22362241 (a -> b -> m c) -> D.Stream m a -> Fold m c x -> Parser b m x
2237−zipWithM zf (D.Stream sstep state) (Fold fstep finitial fextract) =
2242+zipWithM zf (D.Stream sstep state) (Fold fstep finitial _ ffinal) =
22382243 Parser step initial extract
22392244
22402245 where
@@ -2247,7 +2252,7 @@ zipWithM zf (D.Stream sstep state) (Fold fstep finitial fextract) =
22472252 case r of
22482253 D.Yield x s -> return $ IPartial (Just' x, s, fs)
22492254 D.Stop -> do
2250− x <- fextract fs
2255+ x <- ffinal fs
22512256 return $ IDone x
22522257 -- Need Skip/Continue in initial to loop right here
22532258 D.Skip s -> return $ IPartial (Nothing', s, fs)
@@ -2262,7 +2267,7 @@ zipWithM zf (D.Stream sstep state) (Fold fstep finitial fextract) =
22622267 case r of
22632268 D.Yield x1 s -> return $ Continue 0 (Just' x1, s, fs1)
22642269 D.Stop -> do
2265− x <- fextract fs1
2270+ x <- ffinal fs1
22662271 return $ Done 0 x
22672272 D.Skip s -> return $ Continue 1 (Nothing', s, fs1)
22682273 FL.Done x -> return $ Done 0 x
@@ -2277,7 +2282,7 @@ zipWithM zf (D.Stream sstep state) (Fold fstep finitial fextract) =
22772282 return $ Continue 0 (Nothing', s, fs1)
22782283 FL.Done x -> return $ Done 0 x
22792284 D.Stop -> do
2280− x <- fextract fs
2285+ x <- ffinal fs
22812286 return $ Done 1 x
22822287 D.Skip s -> return $ Continue 1 (Nothing', s, fs)
22832288
@@ -2561,7 +2566,7 @@ deintercalateAll :: Monad m =>
25612566deintercalateAll
25622567 (Parser stepL initialL extractL)
25632568 (Parser stepR initialR _)
2564− (Fold fstep finitial fextract) = Parser step initial extract
2569+ (Fold fstep finitial _ ffinal) = Parser step initial extract
25652570
25662571 where
25672572
@@ -2632,9 +2637,9 @@ deintercalateAll
26322637 extractResult n fs r = do
26332638 res <- fstep fs r
26342639 case res of
2635− FL.Partial fs1 -> fmap (Done n) $ fextract fs1
2640+ FL.Partial fs1 -> fmap (Done n) $ ffinal fs1
26362641 FL.Done c -> return (Done n c)
2637− extract (DeintercalateAllInitL fs) = fmap (Done 0) $ fextract fs
2642+ extract (DeintercalateAllInitL fs) = fmap (Done 0) $ ffinal fs
26382643 extract (DeintercalateAllL fs sL) = do
26392644 r <- extractL sL
26402645 case r of
@@ -2642,7 +2647,7 @@ deintercalateAll
26422647 Error err -> return $ Error err
26432648 Continue n s -> return $ Continue n (DeintercalateAllL fs s)
26442649 Partial _ _ -> error "Partial in extract"
2645− extract (DeintercalateAllInitR fs) = fmap (Done 0) $ fextract fs
2650+ extract (DeintercalateAllInitR fs) = fmap (Done 0) $ ffinal fs
26462651 extract (DeintercalateAllR _ _) =
26472652 return $ Error "deintercalateAll: input ended at 'Right' value"
26482653
@@ -2685,7 +2690,7 @@ deintercalate :: Monad m =>
26852690deintercalate
26862691 (Parser stepL initialL extractL)
26872692 (Parser stepR initialR _)
2688− (Fold fstep finitial fextract) = Parser step initial extract
2693+ (Fold fstep finitial _ ffinal) = Parser step initial extract
26892694
26902695 where
26912696
@@ -2716,7 +2721,7 @@ deintercalate
27162721 Done n b ->
27172722 processL (fstep fs (Left b)) n DeintercalateInitR
27182723 Error _ -> do
2719− xs <- fextract fs
2724+ xs <- ffinal fs
27202725 return $ Done cnt1 xs
27212726
27222727 {-# INLINE processR #-}
@@ -2736,7 +2741,7 @@ deintercalate
27362741 Continue n s -> return $ Continue n (DeintercalateR (cnt1 - n) fs s)
27372742 Done n b -> processR (cnt1 - n) b fs n
27382743 Error _ -> do
2739− xs <- fextract fs
2744+ xs <- ffinal fs
27402745 return $ Done cnt1 xs
27412746
27422747 step (DeintercalateInitL fs) a = do
@@ -2771,17 +2776,17 @@ deintercalate
27712776 -- XXX We could have the fold accept pairs of (bR, bL)
27722777 FL.Done _ -> error "Fold terminated consuming partial input"
27732778 Error _ -> do
2774− xs <- fextract fs
2779+ xs <- ffinal fs
27752780 return $ Done cnt1 xs
27762781
27772782 {-# INLINE extractResult #-}
27782783 extractResult n fs r = do
27792784 res <- fstep fs r
27802785 case res of
2781− FL.Partial fs1 -> fmap (Done n) $ fextract fs1
2786+ FL.Partial fs1 -> fmap (Done n) $ ffinal fs1
27822787 FL.Done c -> return (Done n c)
27832788
2784− extract (DeintercalateInitL fs) = fmap (Done 0) $ fextract fs
2789+ extract (DeintercalateInitL fs) = fmap (Done 0) $ ffinal fs
27852790 extract (DeintercalateL cnt fs sL) = do
27862791 r <- extractL sL
27872792 case r of
@@ -2789,10 +2794,10 @@ deintercalate
27892794 Continue n s -> return $ Continue n (DeintercalateL (cnt - n) fs s)
27902795 Partial _ _ -> error "Partial in extract"
27912796 Error _ -> do
2792− xs <- fextract fs
2797+ xs <- ffinal fs
27932798 return $ Done cnt xs
2794− extract (DeintercalateInitR fs) = fmap (Done 0) $ fextract fs
2795− extract (DeintercalateR cnt fs _) = fmap (Done cnt) $ fextract fs
2799+ extract (DeintercalateInitR fs) = fmap (Done 0) $ ffinal fs
2800+ extract (DeintercalateR cnt fs _) = fmap (Done cnt) $ ffinal fs
27962801 extract (DeintercalateRL cnt bR fs sL) = do
27972802 r <- extractL sL
27982803 case r of
@@ -2804,7 +2809,7 @@ deintercalate
28042809 Continue n s -> return $ Continue n (DeintercalateRL (cnt - n) bR fs s)
28052810 Partial _ _ -> error "Partial in extract"
28062811 Error _ -> do
2807− xs <- fextract fs
2812+ xs <- ffinal fs
28082813 return $ Done cnt xs
28092814
28102815{-# ANN type Deintercalate1State Fuse #-}
@@ -2841,7 +2846,7 @@ deintercalate1 :: Monad m =>
28412846deintercalate1
28422847 (Parser stepL initialL extractL)
28432848 (Parser stepR initialR _)
2844− (Fold fstep finitial fextract) = Parser step initial extract
2849+ (Fold fstep finitial _ ffinal) = Parser step initial extract
28452850
28462851 where
28472852
@@ -2895,7 +2900,7 @@ deintercalate1
28952900 Continue n s -> return $ Continue n (Deintercalate1R (cnt1 - n) fs s)
28962901 Done n b -> processR (cnt1 - n) b fs n
28972902 Error _ -> do
2898− xs <- fextract fs
2903+ xs <- ffinal fs
28992904 return $ Done cnt1 xs
29002905
29012906 step (Deintercalate1InitL cnt fs sL) a = runStepInitL cnt fs sL a
@@ -2924,14 +2929,14 @@ deintercalate1
29242929 -- XXX We could have the fold accept pairs of (bR, bL)
29252930 FL.Done _ -> error "Fold terminated consuming partial input"
29262931 Error _ -> do
2927− xs <- fextract fs
2932+ xs <- ffinal fs
29282933 return $ Done cnt1 xs
29292934
29302935 {-# INLINE extractResult #-}
29312936 extractResult n fs r = do
29322937 res <- fstep fs r
29332938 case res of
2934− FL.Partial fs1 -> fmap (Done n) $ fextract fs1
2939+ FL.Partial fs1 -> fmap (Done n) $ ffinal fs1
29352940 FL.Done c -> return (Done n c)
29362941
29372942 extract (Deintercalate1InitL cnt fs sL) = do
@@ -2941,8 +2946,8 @@ deintercalate1
29412946 Continue n s -> return $ Continue n (Deintercalate1InitL (cnt - n) fs s)
29422947 Partial _ _ -> error "Partial in extract"
29432948 Error err -> return $ Error err
2944− extract (Deintercalate1InitR fs) = fmap (Done 0) $ fextract fs
2945− extract (Deintercalate1R cnt fs _) = fmap (Done cnt) $ fextract fs
2949+ extract (Deintercalate1InitR fs) = fmap (Done 0) $ ffinal fs
2950+ extract (Deintercalate1R cnt fs _) = fmap (Done cnt) $ ffinal fs
29462951 extract (Deintercalate1RL cnt bR fs sL) = do
29472952 r <- extractL sL
29482953 case r of
@@ -2954,7 +2959,7 @@ deintercalate1
29542959 Continue n s -> return $ Continue n (Deintercalate1RL (cnt - n) bR fs s)
29552960 Partial _ _ -> error "Partial in extract"
29562961 Error _ -> do
2957− xs <- fextract fs
2962+ xs <- ffinal fs
29582963 return $ Done cnt xs
29592964
29602965{-# ANN type SepByState Fuse #-}
@@ -2997,7 +3002,7 @@ sepBy :: Monad m =>
29973002sepBy
29983003 (Parser stepL initialL extractL)
29993004 (Parser stepR initialR _)
3000− (Fold fstep finitial fextract) = Parser step initial extract
3005+ (Fold fstep finitial _ ffinal) = Parser step initial extract
30013006
30023007 where
30033008
@@ -3028,7 +3033,7 @@ sepBy
30283033 Done n b ->
30293034 processL (fstep fs b) n SepByInitR
30303035 Error _ -> do
3031− xs <- fextract fs
3036+ xs <- ffinal fs
30323037 return $ Done cnt1 xs
30333038
30343039 {-# INLINE processR #-}
@@ -3048,7 +3053,7 @@ sepBy
30483053 Continue n s -> return $ Continue n (SepByR (cnt1 - n) fs s)
30493054 Done n _ -> processR (cnt1 - n) fs n
30503055 Error _ -> do
3051− xs <- fextract fs
3056+ xs <- ffinal fs
30523057 return $ Done cnt1 xs
30533058
30543059 step (SepByInitL fs) a = do
@@ -3070,10 +3075,10 @@ sepBy
30703075 extractResult n fs r = do
30713076 res <- fstep fs r
30723077 case res of
3073− FL.Partial fs1 -> fmap (Done n) $ fextract fs1
3078+ FL.Partial fs1 -> fmap (Done n) $ ffinal fs1
30743079 FL.Done c -> return (Done n c)
30753080
3076− extract (SepByInitL fs) = fmap (Done 0) $ fextract fs
3081+ extract (SepByInitL fs) = fmap (Done 0) $ ffinal fs
30773082 extract (SepByL cnt fs sL) = do
30783083 r <- extractL sL
30793084 case r of
@@ -3081,10 +3086,10 @@ sepBy
30813086 Continue n s -> return $ Continue n (SepByL (cnt - n) fs s)
30823087 Partial _ _ -> error "Partial in extract"
30833088 Error _ -> do
3084− xs <- fextract fs
3089+ xs <- ffinal fs
30853090 return $ Done cnt xs
3086− extract (SepByInitR fs) = fmap (Done 0) $ fextract fs
3087− extract (SepByR cnt fs _) = fmap (Done cnt) $ fextract fs
3091+ extract (SepByInitR fs) = fmap (Done 0) $ ffinal fs
3092+ extract (SepByR cnt fs _) = fmap (Done cnt) $ ffinal fs
30883093
30893094-- | Non-backtracking version of sepBy. Several times faster.
30903095{-# INLINE sepByAll #-}
@@ -3139,7 +3144,7 @@ sepBy1 :: Monad m =>
31393144sepBy1
31403145 (Parser stepL initialL extractL)
31413146 (Parser stepR initialR _)
3142− (Fold fstep finitial fextract) = Parser step initial extract
3147+ (Fold fstep finitial _ ffinal) = Parser step initial extract
31433148
31443149 where
31453150
@@ -3186,7 +3191,7 @@ sepBy1
31863191 Done n b ->
31873192 processL (fstep fs b) n SepBy1InitR
31883193 Error _ -> do
3189− xs <- fextract fs
3194+ xs <- ffinal fs
31903195 return $ Done cnt1 xs
31913196
31923197 {-# INLINE processR #-}
@@ -3206,7 +3211,7 @@ sepBy1
32063211 Continue n s -> return $ Continue n (SepBy1R (cnt1 - n) fs s)
32073212 Done n _ -> processR (cnt1 - n) fs n
32083213 Error _ -> do
3209− xs <- fextract fs
3214+ xs <- ffinal fs
32103215 return $ Done cnt1 xs
32113216
32123217 step (SepBy1InitL cnt fs sL) a = runStepInitL cnt fs sL a
@@ -3223,7 +3228,7 @@ sepBy1
32233228 extractResult n fs r = do
32243229 res <- fstep fs r
32253230 case res of
3226− FL.Partial fs1 -> fmap (Done n) $ fextract fs1
3231+ FL.Partial fs1 -> fmap (Done n) $ ffinal fs1
32273232 FL.Done c -> return (Done n c)
32283233
32293234 extract (SepBy1InitL cnt fs sL) = do
@@ -3240,10 +3245,10 @@ sepBy1
32403245 Continue n s -> return $ Continue n (SepBy1L (cnt - n) fs s)
32413246 Partial _ _ -> error "Partial in extract"
32423247 Error _ -> do
3243− xs <- fextract fs
3248+ xs <- ffinal fs
32443249 return $ Done cnt xs
3245− extract (SepBy1InitR fs) = fmap (Done 0) $ fextract fs
3246− extract (SepBy1R cnt fs _) = fmap (Done cnt) $ fextract fs
3250+ extract (SepBy1InitR fs) = fmap (Done 0) $ ffinal fs
3251+ extract (SepBy1R cnt fs _) = fmap (Done cnt) $ ffinal fs
32473252
32483253-------------------------------------------------------------------------------
32493254-- Interleaving a collection of parsers
@@ -3273,7 +3278,7 @@ roundRobin _ps _f = undefined
32733278{-# INLINE sequence #-}
32743279sequence :: Monad m =>
32753280 D.Stream m (Parser a m b) -> Fold m b c -> Parser a m c
3276−sequence (D.Stream sstep sstate) (Fold fstep finitial fextract) =
3281+sequence (D.Stream sstep sstate) (Fold fstep finitial _ ffinal) =
32773282 Parser step initial extract
32783283
32793284 where
@@ -3291,7 +3296,7 @@ sequence (D.Stream sstep sstate) (Fold fstep finitial fextract) =
32913296 case sres of
32923297 D.Yield p ss1 -> return $ Continue 1 (Just' p, ss1, fs)
32933298 D.Stop -> do
3294− c <- fextract fs
3299+ c <- ffinal fs
32953300 return $ Done 1 c
32963301 D.Skip ss1 -> return $ Continue 1 (Nothing', ss1, fs)
32973302
@@ -3327,7 +3332,7 @@ sequence (D.Stream sstep sstate) (Fold fstep finitial fextract) =
33273332 FL.Done c -> return $ Done 1 c
33283333 IError err -> return $ Error err
33293334
3330− extract (Nothing', _, fs) = fmap (Done 0) $ fextract fs
3335+ extract (Nothing', _, fs) = fmap (Done 0) $ ffinal fs
33313336 extract (Just' (Parser pstep pinit pextr), ss, fs) = do
33323337 ps <- pinit
33333338 case ps of
@@ -3337,7 +3342,7 @@ sequence (D.Stream sstep sstate) (Fold fstep finitial fextract) =
33373342 Done n b -> do
33383343 res <- fstep fs b
33393344 case res of
3340− FL.Partial fs1 -> fmap (Done n) $ fextract fs1
3345+ FL.Partial fs1 -> fmap (Done n) $ ffinal fs1
33413346 FL.Done c -> return (Done n c)
33423347 Error err -> return $ Error err
33433348 Continue n s -> return $ Continue n (Just' (Parser pstep (return (IPartial s)) pextr), ss, fs)
@@ -3345,7 +3350,7 @@ sequence (D.Stream sstep sstate) (Fold fstep finitial fextract) =
33453350 IDone b -> do
33463351 fres <- fstep fs b
33473352 case fres of
3348− FL.Partial fs1 -> fmap (Done 0) $ fextract fs1
3353+ FL.Partial fs1 -> fmap (Done 0) $ ffinal fs1
33493354 FL.Done c -> return (Done 0 c)
33503355 IError err -> return $ Error err
33513356
@@ -3480,7 +3485,7 @@ manyTill :: Monad m
34803485 => Parser a m b -> Parser a m x -> Fold m b c -> Parser a m c
34813486manyTill (Parser stepL initialL extractL)
34823487 (Parser stepR initialR _)
3483− (Fold fstep finitial fextract) =
3488+ (Fold fstep finitial _ ffinal) =
34843489 Parser step initial extract
34853490
34863491 where
@@ -3502,7 +3507,7 @@ manyTill (Parser stepL initialL extractL)
35023507 resR <- initialR
35033508 case resR of
35043509 IPartial sr -> return $ p (ManyTillR 0 fs sr)
3505− IDone _ -> d <$> fextract fs
3510+ IDone _ -> d <$> ffinal fs
35063511 IError _ -> scrutL fs p c d e
35073512
35083513 initial = do
@@ -3519,7 +3524,7 @@ manyTill (Parser stepL initialL extractL)
35193524 assertM(cnt + 1 - n >= 0)
35203525 return $ Continue n (ManyTillR (cnt + 1 - n) fs s)
35213526 Done n _ -> do
3522− b <- fextract fs
3527+ b <- ffinal fs
35233528 return $ Done n b
35243529 Error _ -> do
35253530 resL <- initialL
@@ -3558,12 +3563,12 @@ manyTill (Parser stepL initialL extractL)
35583563 Done n b -> do
35593564 r <- fstep fs b
35603565 case r of
3561− FL.Partial fs1 -> fmap (Done n) $ fextract fs1
3566+ FL.Partial fs1 -> fmap (Done n) $ ffinal fs1
35623567 FL.Done c -> return (Done n c)
35633568 Error err -> return $ Error err
35643569 Continue n s -> return $ Continue n (ManyTillL fs s)
35653570 Partial _ _ -> error "Partial in extract"
3566− extract (ManyTillR _ fs _) = fmap (Done 0) $ fextract fs
3571+ extract (ManyTillR _ fs _) = fmap (Done 0) $ ffinal fs
35673572
35683573-- | @manyThen f collect recover@ repeats the parser @collect@ on the input and
35693574-- collects the output in the supplied fold. If the the parser @collect@ fails,