on master help sign in
Remove MonadThrow from toFold, use error instead
cd85ea7Harendra Kumar
core/src/Streamly/Internal/Data/Parser.hs+1−2
@@ -244,7 +244,6 @@ module Streamly.Internal.Data.Parser
244 )
244 )
245where
245where
246
246
247−import Control.Monad.Catch (MonadThrow)
248import Data.Functor (($>))
247import Data.Functor (($>))
249import Prelude hiding
248import Prelude hiding
250 ( any, all, dropWhile, take, takeWhile, sequence, concatMap, maybe, either
249 ( any, all, dropWhile, take, takeWhile, sequence, concatMap, maybe, either
@@ -286,7 +285,7 @@ import qualified Streamly.Internal.Data.Stream.Type as Stream
286-- /Pre-release/
285-- /Pre-release/
287--
286--
288{-# INLINE toFold #-}
287{-# INLINE toFold #-}
289−toFold :: MonadThrow m => Parser m a b -> Fold m a b
288+toFold :: Monad m => Parser m a b -> Fold m a b
290toFold p = D.toFold $ D.fromParserK p
289toFold p = D.toFold $ D.fromParserK p
291
290
292-------------------------------------------------------------------------------
291-------------------------------------------------------------------------------
core/src/Streamly/Internal/Data/Parser/ParserD.hs+16−22
@@ -201,9 +201,7 @@ where
201
201
202#include "assert.hs"
202#include "assert.hs"
203
203
204−import Control.Exception (Exception)
205import Control.Monad (when)
204import Control.Monad (when)
206−import Control.Monad.Catch (MonadThrow(..))
207import Data.Bifunctor (first)
205import Data.Bifunctor (first)
208import Fusion.Plugin.Types (Fuse(..))
206import Fusion.Plugin.Types (Fuse(..))
209import Streamly.Internal.Data.Fold.Type (Fold(..))
207import Streamly.Internal.Data.Fold.Type (Fold(..))
@@ -237,22 +235,12 @@ import Streamly.Internal.Data.Parser.ParserD.Type
237-- Downgrade a parser to a Fold
235-- Downgrade a parser to a Fold
238-------------------------------------------------------------------------------
236-------------------------------------------------------------------------------
239
237
240−data ParserToFoldError =
241− InitialError String
242− | PartialError Int
243− | ContinueError Int
244− | DoneError Int
245− | ErrorError String
246− deriving Show
247−
248−instance Exception ParserToFoldError
249−
250-- | See 'Streamly.Internal.Data.Parser.toFold'.
238-- | See 'Streamly.Internal.Data.Parser.toFold'.
251--
239--
252-- /Internal/
240-- /Internal/
253--
241--
254{-# INLINE toFold #-}
242{-# INLINE toFold #-}
255−toFold :: MonadThrow m => Parser m a b -> Fold m a b
243+toFold :: Monad m => Parser m a b -> Fold m a b
256toFold (Parser pstep pinitial pextract) = Fold step initial extract
244toFold (Parser pstep pinitial pextract) = Fold step initial extract
257
245
258 where
246 where
@@ -262,7 +250,13 @@ toFold (Parser pstep pinitial pextract) = Fold step initial extract
262 case r of
250 case r of
263 IPartial s -> return $ FL.Partial s
251 IPartial s -> return $ FL.Partial s
264 IDone b -> return $ FL.Done b
252 IDone b -> return $ FL.Done b
265− IError err -> throwM $ InitialError err
253+ IError err ->
254+ error $ "toFold: parser throws error in initial" ++ err
255+
256+ perror n = error $ "toFold: parser backtracks in Partial: " ++ show n
257+ cerror n = error $ "toFold: parser backtracks in Continue: " ++ show n
258+ derror n = error $ "toFold: parser backtracks in Done: " ++ show n
259+ eerror err = error $ "toFold: parser throws error: " ++ err
266
260
267 step st a = do
261 step st a = do
268 r <- pstep st a
262 r <- pstep st a
@@ -270,19 +264,19 @@ toFold (Parser pstep pinitial pextract) = Fold step initial extract
270 Partial 0 s -> return $ FL.Partial s
264 Partial 0 s -> return $ FL.Partial s
271 Continue 0 s -> return $ FL.Partial s
265 Continue 0 s -> return $ FL.Partial s
272 Done 0 b -> return $ FL.Done b
266 Done 0 b -> return $ FL.Done b
273− Partial n _ -> throwM $ PartialError n
267+ Partial n _ -> perror n
274− Continue n _ -> throwM $ ContinueError n
268+ Continue n _ -> cerror n
275− Done n _ -> throwM $ DoneError n
269+ Done n _ -> derror n
276− Error err -> throwM $ ErrorError err
270+ Error err -> eerror err
277
271
278 extract st = do
272 extract st = do
279 r <- pextract st
273 r <- pextract st
280 case r of
274 case r of
281 Done 0 b -> return b
275 Done 0 b -> return b
282− Partial n _ -> throwM $ PartialError n
276+ Partial n _ -> perror n
283− Continue n _ -> throwM $ ContinueError n
277+ Continue n _ -> cerror n
284− Done n _ -> throwM $ DoneError n
278+ Done n _ -> derror n
285− Error err -> throwM $ ErrorError err
279+ Error err -> eerror err
286
280
287-------------------------------------------------------------------------------
281-------------------------------------------------------------------------------
288-- Upgrade folds to parses
282-- Upgrade folds to parses