Add Text.Pandoc.Error module with PandocError type
This commit is contained in:
parent
ef981492fd
commit
8381ac3b02
3 changed files with 46 additions and 13 deletions
|
@ -289,6 +289,7 @@ Library
|
|||
Text.Pandoc.Pretty,
|
||||
Text.Pandoc.Shared,
|
||||
Text.Pandoc.MediaBag,
|
||||
Text.Pandoc.Error,
|
||||
Text.Pandoc.Readers.HTML,
|
||||
Text.Pandoc.Readers.LaTeX,
|
||||
Text.Pandoc.Readers.Markdown,
|
||||
|
|
39
src/Text/Pandoc/Error.hs
Normal file
39
src/Text/Pandoc/Error.hs
Normal file
|
@ -0,0 +1,39 @@
|
|||
module Text.Pandoc.Error (PandocError(..), handleError,hush, mapLeft) where
|
||||
|
||||
import Text.Parsec.Error
|
||||
import Text.Parsec.Pos hiding (Line)
|
||||
import Text.Pandoc.Compat.Except
|
||||
|
||||
type Input = String
|
||||
|
||||
data PandocError = ParseFailure String
|
||||
| ParsecError Input ParseError
|
||||
deriving (Show)
|
||||
|
||||
|
||||
instance Error PandocError where
|
||||
strMsg = ParseFailure
|
||||
|
||||
|
||||
mapLeft :: (a -> b) -> Either a c -> Either b c
|
||||
mapLeft f (Left x) = Left (f x)
|
||||
mapLeft _ (Right x) = Right x
|
||||
|
||||
hush :: Either a b -> Maybe b
|
||||
hush (Left _) = Nothing
|
||||
hush (Right x) = Just x
|
||||
|
||||
handleError :: Either PandocError a -> a
|
||||
handleError (Right r) = r
|
||||
handleError (Left err) =
|
||||
case err of
|
||||
ParseFailure string -> error string
|
||||
ParsecError input err' ->
|
||||
let errPos = errorPos err'
|
||||
errLine = sourceLine errPos
|
||||
errColumn = sourceColumn errPos
|
||||
theline = (lines input ++ [""]) !! (errLine - 1)
|
||||
in error $ "\nError at " ++ show err' ++ "\n" ++
|
||||
theline ++ "\n" ++ replicate (errColumn - 1) ' ' ++
|
||||
"^"
|
||||
|
|
@ -190,6 +190,8 @@ import Control.Applicative ((<$>), (<*>), (*>), (<*), (<$))
|
|||
import Data.Monoid
|
||||
import Data.Maybe (catMaybes)
|
||||
|
||||
import Text.Pandoc.Error
|
||||
|
||||
type Parser t s = Parsec t s
|
||||
|
||||
type ParserT = ParsecT
|
||||
|
@ -845,25 +847,16 @@ readWithM :: (Monad m, Functor m)
|
|||
=> ParserT [Char] st m a -- ^ parser
|
||||
-> st -- ^ initial state
|
||||
-> String -- ^ input
|
||||
-> m a
|
||||
-> m (Either PandocError 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
|
||||
theline = (lines input ++ [""]) !! (errLine - 1)
|
||||
in error $ "\nError at " ++ show err' ++ "\n" ++
|
||||
theline ++ "\n" ++ replicate (errColumn - 1) ' ' ++
|
||||
"^"
|
||||
handleError (Right result) = result
|
||||
mapLeft (ParsecError input) <$> runParserT parser state "source" input
|
||||
|
||||
|
||||
-- | Parse a string with a given parser and state
|
||||
readWith :: Parser [Char] st a
|
||||
-> st
|
||||
-> String
|
||||
-> a
|
||||
-> Either PandocError a
|
||||
readWith p t inp = runIdentity $ readWithM p t inp
|
||||
|
||||
returnWarnings :: (Stream s m c)
|
||||
|
|
Loading…
Add table
Reference in a new issue