@@ -219,7 +219,6 @@ module Streamly.Internal.Data.Parser
222−import Control.Applicative ((<|>))
223import Control.Monad.Catch (MonadCatch, MonadThrow)
222import Control.Monad.Catch (MonadCatch, MonadThrow)
224import Data.Functor (($>))
223import Data.Functor (($>))
225import Prelude hiding
224import Prelude hiding
@@ -238,6 +237,7 @@ import qualified Streamly.Internal.Data.Parser.ParserK.Type as K
239-- >>> :set -package streamly
238-- >>> :set -package streamly
240-- >>> import Prelude hiding (any, all, dropWhile, take, takeWhile, sequence, concatMap, maybe, either, filter)
239-- >>> import Prelude hiding (any, all, dropWhile, take, takeWhile, sequence, concatMap, maybe, either, filter)
240+-- >>> import Control.Applicative ((<|>))
241-- >>> import qualified Streamly.Prelude as Stream
241-- >>> import qualified Streamly.Prelude as Stream
242-- >>> import qualified Streamly.Internal.Data.Stream.IsStream as Stream (parse, parseMany)
242-- >>> import qualified Streamly.Internal.Data.Stream.IsStream as Stream (parse, parseMany)
243-- >>> import qualified Streamly.Data.Fold as Fold
243-- >>> import qualified Streamly.Data.Fold as Fold
@@ -1276,6 +1276,10 @@ deintercalate = undefined
1277-- | Parse items separated by a separator parsed by the supplied parser. At
1277-- | Parse items separated by a separator parsed by the supplied parser. At
1278-- least one item must be present for the parser to succeed.
1278-- least one item must be present for the parser to succeed.
1280+-- Note that this can go in infinite loop if both the parsers fail on some
1281+-- input. Detection of that would make the implementation more complex.
1279{-# INLINE sepBy1 #-}
1283{-# INLINE sepBy1 #-}
1280sepBy1 :: MonadCatch m =>
1284sepBy1 :: MonadCatch m =>
1281 Fold m b c -> Parser m a b -> Parser m a x -> Parser m a c
1285 Fold m b c -> Parser m a b -> Parser m a x -> Parser m a c
@@ -1285,11 +1289,17 @@ sepBy1 sink p sep = do
1285 f1 <- fromEffect $ FL.snoc f x
1289 f1 <- fromEffect $ FL.snoc f x
1286 many (sep >> p) f1
1290 many (sep >> p) f1
1288−-- | sepBy1 or empty, does not fail.
1292+-- | Run the content parser first, when it is done, the separator parser is
1293+-- run, when it is done content parser is run again and so on. If none of the
1294+-- parsers consumes an input then parser returns a failure.
1296+-- >>> sepBy sink content sep = Parser.sepBy1 sink content sep <|> return mempty
1289{-# INLINE sepBy #-}
1298{-# INLINE sepBy #-}
1290−sepBy :: (MonadCatch m, Monoid c) =>
1299+sepBy :: MonadCatch m =>
1291 Fold m b c -> Parser m a b -> Parser m a x -> Parser m a c
1300 Fold m b c -> Parser m a b -> Parser m a x -> Parser m a c
1292−sepBy sink p sep = sepBy1 sink p sep <|> return mempty
1301+sepBy sink content sep =
1302+ D.toParserK $ D.sepBy sink (D.fromParserK content) (D.fromParserK sep)
1294-------------------------------------------------------------------------------
1304-------------------------------------------------------------------------------
1295-- Interleaving a collection of parsers
1305-- Interleaving a collection of parsers