on master help sign in
Implement release review changes on Streamly.Data.Parser
commit443a2c6c22818d833717c29ebcd8d92269d54a2f authorAdithya Kumar <adithya@composewell.com> dateTue 1 Nov 2022, 22:17 (4 years ago) parentfd4207a
1 file changed+24−18
core/src/Streamly/Internal/Data/Parser.hs+24−18
core/src/Streamly/Internal/Data/Parser.hs+24−18
@@ -252,6 +252,7 @@ import Streamly.Internal.Data.Fold.Type (Fold(..))
252252import Streamly.Internal.Data.Parser.ParserK.Type (Parser)
253253import Streamly.Internal.Data.Stream.Type (Stream)
254254
255+import qualified Data.Foldable as Foldable
255256import qualified Streamly.Internal.Data.Fold.Type as FL
256257import qualified Streamly.Internal.Data.Parser.ParserD as D
257258import qualified Streamly.Internal.Data.Parser.ParserK.Type as K
@@ -268,6 +269,7 @@ import Prelude hiding
268269-- >>> import Control.Applicative ((<|>))
269270-- >>> import Data.Char (isSpace)
270271-- >>> import qualified Data.Maybe as Maybe
272+-- >>> import qualified Data.Foldable as Foldable
271273-- >>> import qualified Streamly.Data.Stream as Stream
272274-- >>> import qualified Streamly.Internal.Data.Stream as Stream (parse, parseMany)
273275-- >>> import qualified Streamly.Internal.Data.Fold as Fold
@@ -378,6 +380,7 @@ lmapM f p = D.toParserK $ D.lmapM f $ D.fromParserK p
378380
379381-- | @rmapM f parser@ maps the monadic function @f@ on the output of the parser.
380382--
383+-- >>> rmap = fmap
381384{-# INLINE rmapM #-}
382385rmapM :: Monad m => (b -> m c) -> Parser m a b -> Parser m a c
383386rmapM f p = D.toParserK $ D.rmapM f $ D.fromParserK p
@@ -457,10 +460,10 @@ oneNotEq x = satisfy (/= x)
457460
458461-- | Match any one of the elements in the supplied list.
459462--
460−-- >>> oneOf xs = Parser.satisfy (`elem` xs)
463+-- >>> oneOf xs = Parser.satisfy (`Foldable.elem` xs)
461464--
462465-- When performance matters a pattern matching predicate could be more
463−-- efficient than a list:
466+-- efficient than a 'Foldable' datatype:
464467--
465468-- @
466469-- let p x =
@@ -476,16 +479,16 @@ oneNotEq x = satisfy (/= x)
476479-- search.
477480--
478481{-# INLINE oneOf #-}
479−oneOf :: (Monad m, Eq a) => [a] -> Parser m a a
480−oneOf xs = satisfy (`elem` xs)
482+oneOf :: (Monad m, Eq a, Foldable f) => f a -> Parser m a a
483+oneOf xs = satisfy (`Foldable.elem` xs)
481484
482485-- | See performance notes in 'oneOf'.
483486--
484−-- >>> noneOf xs = Parser.satisfy (`notElem` xs)
487+-- >>> noneOf xs = Parser.satisfy (`Foldable.notElem` xs)
485488--
486489{-# INLINE noneOf #-}
487−noneOf :: (Monad m, Eq a) => [a] -> Parser m a a
488−noneOf xs = satisfy (`notElem` xs)
490+noneOf :: (Monad m, Eq a, Foldable f) => f a -> Parser m a a
491+noneOf xs = satisfy (`Foldable.notElem` xs)
489492
490493-- | Return the next element of the input. Returns 'Nothing'
491494-- on end of input. Also known as 'head'.
@@ -1008,6 +1011,7 @@ listEqBy cmp xs = D.toParserK (D.listEqBy cmp xs)
10081011-- | Like 'listEqBy' but uses a stream instead of a list and does not return
10091012-- the stream.
10101013--
1014+-- Note: A @Stream m a@ isn't returned as it isn't buffered.
10111015{-# INLINE eqBy #-}
10121016eqBy :: Monad m => (a -> a -> Bool) -> Stream m a -> Parser m a ()
10131017eqBy cmp = D.toParserK . D.eqBy cmp . Stream.toStreamD
@@ -1348,17 +1352,21 @@ manyTillP :: -- Monad m =>
13481352manyTillP _p1 _p2 _f = undefined
13491353 -- D.toParserK $ D.manyTillP (D.fromParserK p1) (D.fromParserK p2) f
13501354
1351−-- | @manyTill collect test f@ tries the parser @test@ on the input, if @test@
1352−-- fails it backtracks and tries @collect@, after @collect@ succeeds @test@ is
1355+-- | @manyTill chunking test f@ tries the parser @test@ on the input, if @test@
1356+-- fails it backtracks and tries @chunking@, after @chunking@ succeeds @test@ is
13531357-- tried again and so on. The parser stops when @test@ succeeds. The output of
1354−-- @test@ is discarded and the output of @collect@ is accumulated by the
1355−-- supplied fold. The parser fails if @collect@ fails.
1358+-- @test@ is discarded and the output of @chunking@ is accumulated by the
1359+-- supplied fold. The parser fails if @chunking@ fails.
13561360--
13571361-- Stops when the fold @f@ stops.
13581362--
13591363{-# INLINE manyTill #-}
13601364manyTill :: Monad m
1361− => Parser m a b -> Parser m a x -> Fold m b c -> Parser m a c
1365+ => Parser m a b -- ^ Chunking parser. Parses chunks of input.
1366+ -> Parser m a x -- ^ Test parser. Parsing stops when this parser succeeds
1367+ -- else backtract and run the chunking parser.
1368+ -> Fold m b c -- ^ Folds the output of the chunking parser.
1369+ -> Parser m a c
13621370manyTill collect test f =
13631371 D.toParserK $ D.manyTill (D.fromParserK collect) (D.fromParserK test) f
13641372
@@ -1402,8 +1410,6 @@ manyThen _parser _recover _f = undefined
14021410-- considered an interleaving of two patterns. The two parsers represent the
14031411-- two patterns.
14041412--
1405−-- This undoes a "gintercalate" of two streams.
1406−--
14071413{-# INLINE deintercalate #-}
14081414deintercalate :: Monad m =>
14091415 Parser m a x
@@ -1423,8 +1429,8 @@ deintercalate contentL contentR sink =
14231429--
14241430{-# INLINE sepBy1 #-}
14251431sepBy1 :: Monad m =>
1426− Fold m b c -> Parser m a b -> Parser m a x -> Parser m a c
1427−sepBy1 sink p sep = do
1432+ Parser m a b -> Parser m a x -> Fold m b c -> Parser m a c
1433+sepBy1 p sep sink = do
14281434 x <- p
14291435 f <- fromEffect $ FL.initialize sink
14301436 f1 <- fromEffect $ FL.snoc f x
@@ -1434,8 +1440,8 @@ sepBy1 sink p sep = do
14341440-- run, when it is done content parser is run again and so on. If none of the
14351441-- parsers consumes an input then parser returns a failure.
14361442--
1437−-- >>> sepBy sink p1 p2 = Parser.deintercalate p1 p2 (Fold.lefts sink)
1438−-- >>> sepBy sink content sep = Parser.sepBy1 sink content sep <|> return mempty
1443+-- >>> sepBy p1 p2 sink = Parser.deintercalate p1 p2 (Fold.lefts sink)
1444+-- >>> sepBy content sep sink = Parser.sepBy1 content sep sink <|> return mempty
14391445--
14401446{-# INLINE sepBy #-}
14411447sepBy :: Monad m =>