From 8e71c4c388a086efb7ea971fa3ea96d0b4c975fd Mon Sep 17 00:00:00 2001 From: fiddlosopher Date: Sun, 15 Jul 2007 23:48:17 +0000 Subject: 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 --- src/Text/Pandoc/ParserCombinators.hs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/Text/Pandoc/ParserCombinators.hs b/src/Text/Pandoc/ParserCombinators.hs index 7a129b6be..2e4c1413c 100644 --- a/src/Text/Pandoc/ParserCombinators.hs +++ b/src/Text/Pandoc/ParserCombinators.hs @@ -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 + -- cgit v1.2.3