git.iko.soy/typeable/pandoc
on master help sign in
Parsing: Better error reporting in readWith.
7cb8b60John MacFarlane
src/Text/Pandoc/Parsing.hs+11−4
@@ -764,13 +764,20 @@ gridTableFooter = blanklines
764---
764---
765
765
766-- | Parse a string with a given parser and state.
766-- | Parse a string with a given parser and state.
767−readWith :: Parser [t] ParserState a -- ^ parser
767+readWith :: Parser [Char] ParserState a -- ^ parser
768− -> ParserState -- ^ initial state
768+ -> ParserState -- ^ initial state
769− -> [t] -- ^ input
769+ -> [Char] -- ^ input
770 -> a
770 -> a
771readWith parser state input =
771readWith parser state input =
772 case runParser parser state "source" input of
772 case runParser parser state "source" input of
773+ Left err' ->
774+ let errPos = errorPos err'
775+ errLine = sourceLine errPos
776+ errColumn = sourceColumn errPos
777+ theline = (lines input ++ [""]) !! (errLine - 1)
773− Left err' -> error $ "\nError:\n" ++ show err'
778+ in error $ "\nError at " ++ show err' ++ "\n" ++
779+ theline ++ "\n" ++ replicate (errColumn - 1) ' ' ++
780+ "^"
774 Right result -> result
781 Right result -> result
775
782
776-- | Parse a string with @parser@ (for testing).
783-- | Parse a string with @parser@ (for testing).
src/Text/Pandoc/Readers/HTML.hs+4−1
@@ -59,7 +59,10 @@ readHtml :: ReaderOptions -- ^ Reader options
59 -> String -- ^ String to parse (assumes @'\n'@ line endings)
59 -> String -- ^ String to parse (assumes @'\n'@ line endings)
60 -> Pandoc
60 -> Pandoc
61readHtml opts inp = Pandoc meta blocks
61readHtml opts inp = Pandoc meta blocks
62− where blocks = readWith parseBody def{ stateOptions = opts } rest
62+ where blocks = case runParser parseBody def{ stateOptions = opts }
63+ "source" rest of
64+ Left err' -> error $ "\nError at " ++ show err'
65+ Right result -> result
63 tags = canonicalizeTags $
66 tags = canonicalizeTags $
64 parseTagsOptions parseOptions{ optTagPosition = True } inp
67 parseTagsOptions parseOptions{ optTagPosition = True } inp
65 hasHeader = any (~== TagOpen "head" []) tags
68 hasHeader = any (~== TagOpen "head" []) tags