git.iko.soy/typeable/pandoc
on master help sign in
Generalised readWith to readWithM
commite045b1d5f2e5da8ac3be9d01bbe7ae5979535c51 authorMatthew Pickering <matthewtpickering@gmail.com> dateSun 20 Jul 2014, 17:04 (12 years ago) parent5debb49
1 file changed+19−10
src/Text/Pandoc/Parsing.hs+19−10
src/Text/Pandoc/Parsing.hs+19−10
@@ -64,6 +64,7 @@ module Text.Pandoc.Parsing ( anyLine,
6464 widthsFromIndices,
6565 gridTableWith,
6666 readWith,
67+ readWithM,
6768 testStringWith,
6869 guardEnabled,
6970 guardDisabled,
@@ -825,15 +826,16 @@ gridTableFooter = blanklines
825826
826827---
827828
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' ->
829+-- | Removes the ParsecT layer from the monad transformer stack
830+readWithM :: (Monad m, Functor m)
831+ => ParserT [Char] st m a -- ^ parser
832+ -> st -- ^ initial state
833+ -> String -- ^ input
834+ -> m a
835+readWithM parser state input =
836+ handleError <$> (runParserT parser state "source" input)
837+ where
838+ handleError (Left err') =
837839 let errPos = errorPos err'
838840 errLine = sourceLine errPos
839841 errColumn = sourceColumn errPos
@@ -841,7 +843,14 @@ readWith parser state input =
841843 in error $ "\nError at " ++ show err' ++ "\n" ++
842844 theline ++ "\n" ++ replicate (errColumn - 1) ' ' ++
843845 "^"
844− Right result -> result
846+ handleError (Right result) = result
847+
848+-- | Parse a string with a given parser and state
849+readWith :: Parser [Char] st a
850+ -> st
851+ -> String
852+ -> a
853+readWith p t inp = runIdentity $ readWithM p t inp
845854
846855-- | Parse a string with @parser@ (for testing).
847856testStringWith :: (Show a, Stream [Char] Identity Char)