@@ -200,6 +200,7 @@ module Streamly.Internal.Data.Parser.ParserD
200200 -- @
201201 , sepBy1
202202 , sepBy
203+ , sepByAll
203204
204205 -- ** Sequential Alternative
205206 , alt
@@ -2535,9 +2536,13 @@ data DeintercalateAllState fs sp ss =
25352536-- useful. For example, in case of JSON parsing we get an entire block of
25362537-- key-value pairs which we need to verify. This version may be simpler, more
25372538-- efficient. We could implement this as a stream operation like parseMany.
2539+--
2540+-- XXX Also, it may be a good idea to provide a parse driver for a fold. For
2541+-- example, in case of csv parsing as we are feeding a line to a fold we can
2542+-- parse it.
25382543
25392544-- | Like 'deintercalate' but the entire input must satisfy the pattern
2540−-- otherwise the parser fails.
2545+-- otherwise the parser fails. This is many times faster than deintercalate.
25412546--
25422547-- >>> p1 = Parser.takeWhile1 (not . (== '+')) Fold.toList
25432548-- >>> p2 = Parser.satisfy (== '+')
@@ -2972,7 +2977,7 @@ data SepByState fs sp ss =
29722977-- Definitions:
29732978--
29742979-- >>> sepBy p1 p2 f = Parser.deintercalate p1 p2 (Fold.catLefts f)
2975−-- >>> sepBy p1 p2 f = Parser.sepBy1 p1 p2 f <|> return mempty
2980+-- >>> sepBy p1 p2 f = Parser.sepBy1 p1 p2 f <|> Parser.fromEffect (Fold.extractM f)
29762981--
29772982-- Examples:
29782983--
@@ -2991,6 +2996,8 @@ data SepByState fs sp ss =
29912996{-# INLINE sepBy #-}
29922997sepBy :: Monad m =>
29932998 Parser a m b -> Parser a m x -> Fold m b c -> Parser a m c
2999+-- This has similar performance as the custom impl below.
3000+-- sepBy p1 p2 f = deintercalate p1 p2 (FL.catLefts f)
29943001sepBy
29953002 (Parser stepL initialL extractL)
29963003 (Parser stepR initialR _)
@@ -3083,6 +3090,12 @@ sepBy
30833090 extract (SepByInitR fs) = fmap (Done 0) $ fextract fs
30843091 extract (SepByR cnt fs _) = fmap (Done cnt) $ fextract fs
30853092
3093+-- | Non-backtracking version of sepBy. Several times faster.
3094+{-# INLINE sepByAll #-}
3095+sepByAll :: Monad m =>
3096+ Parser a m b -> Parser a m x -> Fold m b c -> Parser a m c
3097+sepByAll p1 p2 f = deintercalateAll p1 p2 (FL.catLefts f)
3098+
30863099-- XXX This can be implemented using refold, parse one and then continue
30873100-- collecting the rest in that.
30883101
@@ -3106,6 +3119,10 @@ sepBy1 p sep sink = do
31063119
31073120-- | Like 'sepBy' but requires at least one successful parse.
31083121--
3122+-- Definition:
3123+--
3124+-- >>> sepBy1 p1 p2 f = Parser.deintercalate1 p1 p2 (Fold.catLefts f)
3125+--
31093126-- Examples:
31103127--
31113128-- >>> p1 = Parser.takeWhile1 (not . (== '+')) Fold.toList