git.iko.soy/typeable/pandoc
on master help sign in
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

commit8e71c4c388a086efb7ea971fa3ea96d0b4c975fd authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> dateSun 15 Jul 2007, 23:48 (19 years ago) parent54dd46c
1 file changed+17−1
src/Text/Pandoc/ParserCombinators.hs+17−1
src/Text/Pandoc/ParserCombinators.hs+17−1
@@ -39,7 +39,8 @@ module Text.Pandoc.ParserCombinators (
3939 enclosed,
4040 stringAnyCase,
4141 parseFromStr,
42− lineClump
42+ lineClump,
43+ charsInBalanced
4344 ) where
4445import Text.ParserCombinators.Parsec
4546import Data.Char ( toUpper, toLower )
@@ -122,3 +123,18 @@ lineClump = do
122123 blanks <- blanklines <|> (do{eof; return "\n"})
123124 return ((unlines lines) ++ blanks)
124125
126+-- | Parse a string of characters between an open character
127+-- and a close character, including text between balanced
128+-- pairs of open and close. For example,
129+-- @charsInBalanced '(' ')'@ will parse "(hello (there))"
130+-- and return "hello (there)".
131+charsInBalanced :: Char -> Char -> GenParser Char st String
132+charsInBalanced open close = try $ do
133+ char open
134+ raw <- manyTill ( (do res <- charsInBalanced open close
135+ return $ [open] ++ res ++ [close])
136+ <|> (do notFollowedBy' (blankline >> blanklines)
137+ count 1 anyChar))
138+ (char close)
139+ return $ concat raw
140+