@@ -64,6 +64,7 @@ module Text.Pandoc.Parsing ( anyLine,
6464 widthsFromIndices,
6565 gridTableWith,
6666 readWith,
67+ readWithM,
6768 testStringWith,
6869 guardEnabled,
6970 guardDisabled,
@@ -106,7 +107,9 @@ module Text.Pandoc.Parsing ( anyLine,
106107 askF,
107108 asksF,
108109 -- * Re-exports from Text.Pandoc.Parsec
110+ Stream,
109111 runParser,
112+ runParserT,
110113 parse,
111114 anyToken,
112115 getInput,
@@ -825,15 +828,16 @@ gridTableFooter = blanklines
825828
826829---
827830
828−-- | Parse a string with a given parser and state.
829−readWith :: (Stream [Char] Identity Char)
830− => ParserT [Char] st Identity a -- ^ parser
831− -> st -- ^ initial state
832− -> [Char] -- ^ input
833− -> a
834−readWith parser state input =
835− case runParser parser state "source" input of
836− Left err' ->
831+-- | Removes the ParsecT layer from the monad transformer stack
832+readWithM :: (Monad m, Functor m)
833+ => ParserT [Char] st m a -- ^ parser
834+ -> st -- ^ initial state
835+ -> String -- ^ input
836+ -> m a
837+readWithM parser state input =
838+ handleError <$> (runParserT parser state "source" input)
839+ where
840+ handleError (Left err') =
837841 let errPos = errorPos err'
838842 errLine = sourceLine errPos
839843 errColumn = sourceColumn errPos
@@ -841,7 +845,14 @@ readWith parser state input =
841845 in error $ "\nError at " ++ show err' ++ "\n" ++
842846 theline ++ "\n" ++ replicate (errColumn - 1) ' ' ++
843847 "^"
844− Right result -> result
848+ handleError (Right result) = result
849+
850+-- | Parse a string with a given parser and state
851+readWith :: Parser [Char] st a
852+ -> st
853+ -> String
854+ -> a
855+readWith p t inp = runIdentity $ readWithM p t inp
845856
846857-- | Parse a string with @parser@ (for testing).
847858testStringWith :: (Show a, Stream [Char] Identity Char)