aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Parsing.hs
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2014-07-22 09:14:38 -0700
committerJohn MacFarlane <jgm@berkeley.edu>2014-07-22 09:14:38 -0700
commit5089bc8fe29949500443205bb0f91177d0af59dc (patch)
tree6ef170c966b22dc9578194c701a45de8c241718b /src/Text/Pandoc/Parsing.hs
parent5debb492ef64767677d893e2cf77a100d57796e0 (diff)
parent83028c598262ec1d189b0337424ea47720f14308 (diff)
downloadpandoc-5089bc8fe29949500443205bb0f91177d0af59dc.tar.gz
Merge pull request #1444 from mpickering/master
Generalised readWith to readWithM
Diffstat (limited to 'src/Text/Pandoc/Parsing.hs')
-rw-r--r--src/Text/Pandoc/Parsing.hs31
1 files changed, 21 insertions, 10 deletions
diff --git a/src/Text/Pandoc/Parsing.hs b/src/Text/Pandoc/Parsing.hs
index f4f9178d0..eec4a3bc9 100644
--- a/src/Text/Pandoc/Parsing.hs
+++ b/src/Text/Pandoc/Parsing.hs
@@ -64,6 +64,7 @@ module Text.Pandoc.Parsing ( anyLine,
widthsFromIndices,
gridTableWith,
readWith,
+ readWithM,
testStringWith,
guardEnabled,
guardDisabled,
@@ -106,7 +107,9 @@ module Text.Pandoc.Parsing ( anyLine,
askF,
asksF,
-- * Re-exports from Text.Pandoc.Parsec
+ Stream,
runParser,
+ runParserT,
parse,
anyToken,
getInput,
@@ -825,15 +828,16 @@ gridTableFooter = blanklines
---
--- | Parse a string with a given parser and state.
-readWith :: (Stream [Char] Identity Char)
- => ParserT [Char] st Identity a -- ^ parser
- -> st -- ^ initial state
- -> [Char] -- ^ input
- -> a
-readWith parser state input =
- case runParser parser state "source" input of
- Left err' ->
+-- | Removes the ParsecT layer from the monad transformer stack
+readWithM :: (Monad m, Functor m)
+ => ParserT [Char] st m a -- ^ parser
+ -> st -- ^ initial state
+ -> String -- ^ input
+ -> m a
+readWithM parser state input =
+ handleError <$> (runParserT parser state "source" input)
+ where
+ handleError (Left err') =
let errPos = errorPos err'
errLine = sourceLine errPos
errColumn = sourceColumn errPos
@@ -841,7 +845,14 @@ readWith parser state input =
in error $ "\nError at " ++ show err' ++ "\n" ++
theline ++ "\n" ++ replicate (errColumn - 1) ' ' ++
"^"
- Right result -> result
+ handleError (Right result) = result
+
+-- | Parse a string with a given parser and state
+readWith :: Parser [Char] st a
+ -> st
+ -> String
+ -> a
+readWith p t inp = runIdentity $ readWithM p t inp
-- | Parse a string with @parser@ (for testing).
testStringWith :: (Show a, Stream [Char] Identity Char)