Added charsInBalanced parser combinator to Text.Pandoc.ParserCombinators.

This is not currently used, but should be useful in parsing strings
containing balanced pairs of brackets or parentheses.


git-svn-id: https://pandoc.googlecode.com/svn/trunk@728 788f1e2b-df1e-0410-8736-df70ead52e1b
This commit is contained in:
fiddlosopher 2007-07-15 23:48:17 +00:00
parent 54dd46c07c
commit 8e71c4c388

View file

@ -39,7 +39,8 @@ module Text.Pandoc.ParserCombinators (
enclosed,
stringAnyCase,
parseFromStr,
lineClump
lineClump,
charsInBalanced
) where
import Text.ParserCombinators.Parsec
import Data.Char ( toUpper, toLower )
@ -122,3 +123,18 @@ lineClump = do
blanks <- blanklines <|> (do{eof; return "\n"})
return ((unlines lines) ++ blanks)
-- | Parse a string of characters between an open character
-- and a close character, including text between balanced
-- pairs of open and close. For example,
-- @charsInBalanced '(' ')'@ will parse "(hello (there))"
-- and return "hello (there)".
charsInBalanced :: Char -> Char -> GenParser Char st String
charsInBalanced open close = try $ do
char open
raw <- manyTill ( (do res <- charsInBalanced open close
return $ [open] ++ res ++ [close])
<|> (do notFollowedBy' (blankline >> blanklines)
count 1 anyChar))
(char close)
return $ concat raw