From 2a36bef39a3fca473b6a83d169dfba09588fe372 Mon Sep 17 00:00:00 2001 From: HuStmpHrrr Date: Wed, 17 Feb 2016 18:39:05 -0500 Subject: [PATCH] add sepBy' and sepBy1' combinators --- Text/Parsec/Combinator.hs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Text/Parsec/Combinator.hs b/Text/Parsec/Combinator.hs index de5b619e..e1e3d610 100644 --- a/Text/Parsec/Combinator.hs +++ b/Text/Parsec/Combinator.hs @@ -20,6 +20,7 @@ module Text.Parsec.Combinator , skipMany1 , many1 , sepBy, sepBy1 + , sepBy', sepBy1' , endBy, endBy1 , sepEndBy, sepEndBy1 , chainl, chainl1 @@ -119,6 +120,20 @@ sepBy1 p sep = do{ x <- p ; return (x:xs) } +sepBy' :: (Stream s m t) => ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a] +sepBy' p sep = sepBy1' p sep <|> return [] + +sepBy1' :: (Stream s m t) => ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a] +sepBy1' p sep = do{ x <- p + ; xs <- piter [] + ; return $ x:reverse xs + } + where piter acc = do{ m <- optionMaybe sep + ; case m of + Nothing -> return acc + Just _ -> do{ x' <- p; piter $ x':acc } + } + -- | @sepEndBy1 p sep@ parses /one/ or more occurrences of @p@, -- separated and optionally ended by @sep@. Returns a list of values