on master help sign in
Add smart constructors to create parsers
e689788Adithya Kumar
src/Streamly/Internal/Data/Fold/Type.hs+3
@@ -454,6 +454,9 @@ foldrM g z =
454-- need that?
454-- need that?
455--
455--
456-- Until we investigate this we are not releasing these.
456-- 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
457
460
458-- | Make a terminating fold using a pure step function, a pure initial state
461-- | Make a terminating fold using a pure step function, a pure initial state
459-- and a pure state extraction function.
462-- and a pure state extraction function.
src/Streamly/Internal/Data/Parser/ParserD.hs+4−4
@@ -331,14 +331,14 @@ next = Parser step initial extract
331--
331--
332{-# INLINE maybe #-}
332{-# INLINE maybe #-}
333maybe :: MonadThrow m => (a -> Maybe b) -> Parser m a b
333maybe :: MonadThrow m => (a -> Maybe b) -> Parser m a b
334−maybe parser = Parser step initial extract
334+maybe parserF = Parser step initial extract
335
335
336 where
336 where
337
337
338 initial = return $ IPartial ()
338 initial = return $ IPartial ()
339
339
340 step () a = return $
340 step () a = return $
341− case parser a of
341+ case parserF a of
342 Just b -> Done 0 b
342 Just b -> Done 0 b
343 Nothing -> Error "maybe: predicate failed"
343 Nothing -> Error "maybe: predicate failed"
344
344
@@ -350,14 +350,14 @@ maybe parser = Parser step initial extract
350--
350--
351{-# INLINE either #-}
351{-# INLINE either #-}
352either :: MonadThrow m => (a -> Either String b) -> Parser m a b
352either :: MonadThrow m => (a -> Either String b) -> Parser m a b
353−either parser = Parser step initial extract
353+either parserF = Parser step initial extract
354
354
355 where
355 where
356
356
357 initial = return $ IPartial ()
357 initial = return $ IPartial ()
358
358
359 step () a = return $
359 step () a = return $
360− case parser a of
360+ case parserF a of
361 Right b -> Done 0 b
361 Right b -> Done 0 b
362 Left err -> Error $ "either: " ++ err
362 Left err -> Error $ "either: " ++ err
363
363
src/Streamly/Internal/Data/Parser/ParserD/Type.hs+30
@@ -112,12 +112,17 @@
112
112
113module Streamly.Internal.Data.Parser.ParserD.Type
113module Streamly.Internal.Data.Parser.ParserD.Type
114 (
114 (
115+ -- * Types
115 Initial (..)
116 Initial (..)
116 , Step (..)
117 , Step (..)
117 , Parser (..)
118 , Parser (..)
118 , ParseError (..)
119 , ParseError (..)
119 , rmapM
120 , rmapM
120
121
122+ -- * Constructors
123+ , parser
124+ , parserM
125+
121 , fromPure
126 , fromPure
122 , fromEffect
127 , fromEffect
123 , serialWith
128 , serialWith
@@ -160,6 +165,7 @@ import Prelude hiding (concatMap)
160-- >>> import qualified Streamly.Prelude as Stream
165-- >>> import qualified Streamly.Prelude as Stream
161-- >>> import qualified Streamly.Internal.Data.Stream.IsStream as Stream (parse)
166-- >>> import qualified Streamly.Internal.Data.Stream.IsStream as Stream (parse)
162-- >>> import qualified Streamly.Internal.Data.Parser as Parser
167-- >>> import qualified Streamly.Internal.Data.Parser as Parser
168+-- >>> import qualified Streamly.Internal.Data.Parser.ParserD as ParserD
163
169
164-- XXX The only differences between Initial and Step types are:
170-- XXX The only differences between Initial and Step types are:
165--
171--
@@ -348,6 +354,30 @@ instance Functor m => Functor (Parser m a) where
348 step s b = fmap2 f (step1 s b)
354 step s b = fmap2 f (step1 s b)
349 fmap2 g = fmap (fmap g)
355 fmap2 g = fmap (fmap g)
350
356
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+
351------------------------------------------------------------------------------
381------------------------------------------------------------------------------
352-- Mapping on the output
382-- Mapping on the output
353------------------------------------------------------------------------------
383------------------------------------------------------------------------------