Commits

wren romano committed babdc87

Data.Attoparsec.Combinator: added sepEndBy1 and sepEndBy.

Comments (0)

Files changed (1)

Data/Attoparsec/Combinator.hs

     , manyTill
     , sepBy
     , sepBy1
+    , sepEndBy
+    , sepEndBy1
     , skipMany
     , skipMany1
     , eitherP
 sepBy1 p s = scan
     where scan = liftA2 (:) p ((s *> scan) <|> pure [])
 
+-- | @sepEndBy1 p sep@ parses /one/ or more occurrences of @p@,
+-- separated and optionally ended by @sep@. Returns a list of the
+-- values returned by @p@.
+sepEndBy1 :: Alternative f => f a -> f s -> f [a]
+sepEndBy1 p s = scan
+    where scan = liftA2 (:) p ((s *> (scan <|> pure [])) <|> pure [])
+
+-- | @sepEndBy p sep@ parses /zero/ or more occurrences of @p@,
+-- separated and optionally ended by @sep@, i.e., Haskell style
+-- statements. Returns a list of the values returned by @p@.
+sepEndBy :: Alternative f => f a -> f s -> f [a]
+sepEndBy p s = sepEndBy1 p s <|> return []
+
 -- | @manyTill p end@ applies action @p@ /zero/ or more times until
 -- action @end@ succeeds, and returns the list of values returned by
 -- @p@.  This can be used to scan comments: