@@ -18,6 +18,9 @@ module Streamly.Internal.Data.Parser.ParserD
18 , Initial (..)
18 , Initial (..)
21+ -- * Downgrade to Fold
21 -- First order parsers
24 -- First order parsers
22 -- * Accumulators
25 -- * Accumulators
23 , fromFold
26 , fromFold
@@ -162,7 +165,7 @@ module Streamly.Internal.Data.Parser.ParserD
165−import Control.Exception (assert)
168+import Control.Exception (assert, Exception)
166import Control.Monad.Catch (MonadCatch, MonadThrow(..))
169import Control.Monad.Catch (MonadCatch, MonadThrow(..))
167import Fusion.Plugin.Types (Fuse(..))
170import Fusion.Plugin.Types (Fuse(..))
168import Streamly.Internal.Data.Fold.Type (Fold(..))
171import Streamly.Internal.Data.Fold.Type (Fold(..))
@@ -184,6 +187,48 @@ import Streamly.Internal.Data.Parser.ParserD.Type
184-- >>> import qualified Streamly.Data.Fold as Fold
187-- >>> import qualified Streamly.Data.Fold as Fold
185-- >>> import qualified Streamly.Internal.Data.Parser as Parser
188-- >>> import qualified Streamly.Internal.Data.Parser as Parser
190+-------------------------------------------------------------------------------
191+-- Downgrade a parser to a Fold
192+-------------------------------------------------------------------------------
194+data ParserToFoldError =
202+instance Exception ParserToFoldError
204+-- | See 'Streamly.Internal.Data.Parser.toFold'.
208+{-# INLINE toFold #-}
209+toFold :: MonadThrow m => Parser m a b -> Fold m a b
210+toFold (Parser pstep pinitial pextract) = Fold step initial pextract
217+ IPartial s -> return $ FL.Partial s
218+ IDone b -> return $ FL.Done b
219+ IError err -> throwM $ InitialError err
224+ Partial 0 s -> return $ FL.Partial s
225+ Continue 0 s -> return $ FL.Partial s
226+ Done 0 b -> return $ FL.Done b
227+ Partial n _ -> throwM $ PartialError n
228+ Continue n _ -> throwM $ ContinueError n
229+ Done n _ -> throwM $ DoneError n
230+ Error err -> throwM $ ErrorError err
187-------------------------------------------------------------------------------
232-------------------------------------------------------------------------------
188-- Upgrade folds to parses
233-- Upgrade folds to parses
189-------------------------------------------------------------------------------
234-------------------------------------------------------------------------------