on master help sign in
Add smart constructors to create parsers
commite689788ea5b78dddcffeb7774aabb74f9864f8eb authorAdithya Kumar <adithya@composewell.com> dateFri 4 Feb 2022, 06:50 (5 years ago) parentaaf24b3
3 files changed+37−4
src/Streamly/Internal/Data/Fold/Type.hs+3 src/Streamly/Internal/Data/Parser/ParserD.hs+4−4 src/Streamly/Internal/Data/Parser/ParserD/Type.hs+30
src/Streamly/Internal/Data/Fold/Type.hs+3
@@ -454,6 +454,9 @@ foldrM g z =
454454-- need that?
455455--
456456-- Until we investigate this we are not releasing these.
457+--
458+-- XXX The above text would apply to
459+-- Streamly.Internal.Data.Parser.ParserD.Type.parser
457460
458461-- | Make a terminating fold using a pure step function, a pure initial state
459462-- and a pure state extraction function.
src/Streamly/Internal/Data/Parser/ParserD.hs+4−4
@@ -331,14 +331,14 @@ next = Parser step initial extract
331331--
332332{-# INLINE maybe #-}
333333maybe :: MonadThrow m => (a -> Maybe b) -> Parser m a b
334−maybe parser = Parser step initial extract
334+maybe parserF = Parser step initial extract
335335
336336 where
337337
338338 initial = return $ IPartial ()
339339
340340 step () a = return $
341− case parser a of
341+ case parserF a of
342342 Just b -> Done 0 b
343343 Nothing -> Error "maybe: predicate failed"
344344
@@ -350,14 +350,14 @@ maybe parser = Parser step initial extract
350350--
351351{-# INLINE either #-}
352352either :: MonadThrow m => (a -> Either String b) -> Parser m a b
353−either parser = Parser step initial extract
353+either parserF = Parser step initial extract
354354
355355 where
356356
357357 initial = return $ IPartial ()
358358
359359 step () a = return $
360− case parser a of
360+ case parserF a of
361361 Right b -> Done 0 b
362362 Left err -> Error $ "either: " ++ err
363363
src/Streamly/Internal/Data/Parser/ParserD/Type.hs+30
@@ -112,12 +112,17 @@
112112
113113module Streamly.Internal.Data.Parser.ParserD.Type
114114 (
115+ -- * Types
115116 Initial (..)
116117 , Step (..)
117118 , Parser (..)
118119 , ParseError (..)
119120 , rmapM
120121
122+ -- * Constructors
123+ , parser
124+ , parserM
125+
121126 , fromPure
122127 , fromEffect
123128 , serialWith
@@ -160,6 +165,7 @@ import Prelude hiding (concatMap)
160165-- >>> import qualified Streamly.Prelude as Stream
161166-- >>> import qualified Streamly.Internal.Data.Stream.IsStream as Stream (parse)
162167-- >>> import qualified Streamly.Internal.Data.Parser as Parser
168+-- >>> import qualified Streamly.Internal.Data.Parser.ParserD as ParserD
163169
164170-- XXX The only differences between Initial and Step types are:
165171--
@@ -348,6 +354,30 @@ instance Functor m => Functor (Parser m a) where
348354 step s b = fmap2 f (step1 s b)
349355 fmap2 g = fmap (fmap g)
350356
357+------------------------------------------------------------------------------
358+-- General parser constructors
359+------------------------------------------------------------------------------
360+
361+-- | Make a parser using a parsing step and a starting value.
362+--
363+-- /Pre-release/
364+--
365+{-# INLINE parser #-}
366+parser :: Monad m => (b -> a -> Step b b) -> Initial b b -> Parser m a b
367+parser step initial =
368+ Parser (\s a -> return $ step s a) (return initial) return
369+
370+-- | Like 'parser' but with a monadic step function.
371+--
372+-- >>> parserM step initial = ParserD.Parser step initial return
373+--
374+-- /Pre-release/
375+--
376+{-# INLINE parserM #-}
377+parserM ::
378+ Monad m => (b -> a -> m (Step b b)) -> m (Initial b b) -> Parser m a b
379+parserM step initial = Parser step initial return
380+
351381------------------------------------------------------------------------------
352382-- Mapping on the output
353383------------------------------------------------------------------------------