git.iko.soy/typeable/pandoc
on master help sign in
Merge pull request #1444 from mpickering/master

Generalised readWith to readWithM

commit5089bc8fe29949500443205bb0f91177d0af59dc authorJohn MacFarlane <jgm@berkeley.edu> dateTue 22 Jul 2014, 09:14 (12 years ago) parent5debb4983028c5
2 files changed+22−12
src/Text/Pandoc/Parsing.hs+21−10 src/Text/Pandoc/Readers/LaTeX.hs+1−2
src/Text/Pandoc/Parsing.hs+21−10
@@ -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)
src/Text/Pandoc/Readers/LaTeX.hs+1−2
@@ -41,7 +41,6 @@ import Text.Pandoc.Shared
4141import Text.Pandoc.Options
4242import Text.Pandoc.Parsing hiding ((<|>), many, optional, space,
4343 mathDisplay, mathInline)
44−import Text.Parsec.Prim (ParsecT, runParserT)
4544import qualified Text.Pandoc.UTF8 as UTF8
4645import Data.Char ( chr, ord )
4746import Control.Monad.Trans (lift)
@@ -808,7 +807,7 @@ rawEnv name = do
808807
809808----
810809
811−type IncludeParser = ParsecT [Char] [String] IO String
810+type IncludeParser = ParserT [Char] [String] IO String
812811
813812-- | Replace "include" commands with file contents.
814813handleIncludes :: String -> IO String