@@ -30,6 +30,8 @@ module Streamly.Internal.Data.Parser.ParserD
34 -- * Sequence parsers
36 -- * Sequence parsers
@@ -158,7 +160,8 @@ where
159import Control.Exception (assert)
161import Control.Exception (assert)
160import Control.Monad.Catch (MonadCatch, MonadThrow(..))
162import Control.Monad.Catch (MonadCatch, MonadThrow(..))
161−import Prelude hiding (any, all, take, takeWhile, sequence, concatMap)
164+ hiding (any, all, take, takeWhile, sequence, concatMap, maybe, either)
162import Streamly.Internal.Data.Fold.Types (Fold(..))
165import Streamly.Internal.Data.Fold.Types (Fold(..))
164import Streamly.Internal.Data.Parser.ParserD.Tee
167import Streamly.Internal.Data.Parser.ParserD.Tee
@@ -258,6 +261,44 @@ satisfy predicate = Parser step initial extract
259 extract _ = throwM $ ParseError "satisfy: end of input"
262 extract _ = throwM $ ParseError "satisfy: end of input"
264+-- | See 'Streamly.Internal.Data.Parser.maybe'.
269+maybe :: MonadThrow m => (a -> Maybe b) -> Parser m a b
270+maybe parser = Parser step initial extract
276+ step () a = return $
279+ Nothing -> Error "maybe: predicate failed"
281+ extract _ = throwM $ ParseError "maybe: end of input"
283+-- | See 'Streamly.Internal.Data.Parser.either'.
287+{-# INLINE either #-}
288+either :: MonadThrow m => (a -> Either String b) -> Parser m a b
289+either parser = Parser step initial extract
295+ step () a = return $
298+ Left err -> Error $ "either: " ++ err
300+ extract _ = throwM $ ParseError "either: end of input"
261-------------------------------------------------------------------------------
302-------------------------------------------------------------------------------
262-- Taking elements
303-- Taking elements
263-------------------------------------------------------------------------------
304-------------------------------------------------------------------------------