git.iko.soy/typeable/pandoc
on master help sign in
Parsing: Better error reporting in readWith.

- Specialize readWith to String input. - On error have it print the line in which the error occurred, with a caret pointing to the column. - This should help diagnose parsing problems in LaTeX especially.

commit7cb8b60910ede36bba3598f85a06ddde6fc545d0 authorJohn MacFarlane <fiddlosopher@gmail.com> dateThu 28 Mar 2013, 22:20 (14 years ago) parent3096997
2 files changed+15−5
src/Text/Pandoc/Parsing.hs+11−4 src/Text/Pandoc/Readers/HTML.hs+4−1
src/Text/Pandoc/Parsing.hs+11−4
@@ -764,13 +764,20 @@ gridTableFooter = blanklines
764764---
765765
766766-- | Parse a string with a given parser and state.
767−readWith :: Parser [t] ParserState a -- ^ parser
768− -> ParserState -- ^ initial state
769− -> [t] -- ^ input
767+readWith :: Parser [Char] ParserState a -- ^ parser
768+ -> ParserState -- ^ initial state
769+ -> [Char] -- ^ input
770770 -> a
771771readWith parser state input =
772772 case runParser parser state "source" input of
773− Left err' -> error $ "\nError:\n" ++ show err'
773+ Left err' ->
774+ let errPos = errorPos err'
775+ errLine = sourceLine errPos
776+ errColumn = sourceColumn errPos
777+ theline = (lines input ++ [""]) !! (errLine - 1)
778+ in error $ "\nError at " ++ show err' ++ "\n" ++
779+ theline ++ "\n" ++ replicate (errColumn - 1) ' ' ++
780+ "^"
774781 Right result -> result
775782
776783-- | Parse a string with @parser@ (for testing).
src/Text/Pandoc/Readers/HTML.hs+4−1
@@ -59,7 +59,10 @@ readHtml :: ReaderOptions -- ^ Reader options
5959 -> String -- ^ String to parse (assumes @'\n'@ line endings)
6060 -> Pandoc
6161readHtml 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
6366 tags = canonicalizeTags $
6467 parseTagsOptions parseOptions{ optTagPosition = True } inp
6568 hasHeader = any (~== TagOpen "head" []) tags