git.iko.soy/typeable/pandoc
on master help sign in
Added charsInBalanced parser combinator to Text.Pandoc.ParserCombinators.
8e71c4cfiddlosopher
src/Text/Pandoc/ParserCombinators.hs+17−1
@@ -39,7 +39,8 @@ module Text.Pandoc.ParserCombinators (
39 enclosed,
39 enclosed,
40 stringAnyCase,
40 stringAnyCase,
41 parseFromStr,
41 parseFromStr,
42− lineClump
42+ lineClump,
43+ charsInBalanced
43 ) where
44 ) where
44import Text.ParserCombinators.Parsec
45import Text.ParserCombinators.Parsec
45import Data.Char ( toUpper, toLower )
46import Data.Char ( toUpper, toLower )
@@ -122,3 +123,18 @@ lineClump = do
122 blanks <- blanklines <|> (do{eof; return "\n"})
123 blanks <- blanklines <|> (do{eof; return "\n"})
123 return ((unlines lines) ++ blanks)
124 return ((unlines lines) ++ blanks)
124
125
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+