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.
This commit is contained in:
John MacFarlane
2013-03-28 22:20:05 -07:00
parent 30969974f0
commit 7cb8b60910
2 changed files with 15 additions and 5 deletions
+11 -4
View File
@@ -764,13 +764,20 @@ gridTableFooter = blanklines
---
-- | Parse a string with a given parser and state.
readWith :: Parser [t] ParserState a -- ^ parser
-> ParserState -- ^ initial state
-> [t] -- ^ input
readWith :: Parser [Char] ParserState a -- ^ parser
-> ParserState -- ^ initial state
-> [Char] -- ^ input
-> a
readWith parser state input =
case runParser parser state "source" input of
Left err' -> error $ "\nError:\n" ++ show err'
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) ' ' ++
"^"
Right result -> result
-- | Parse a string with @parser@ (for testing).
+4 -1
View File
@@ -59,7 +59,10 @@ readHtml :: ReaderOptions -- ^ Reader options
-> String -- ^ String to parse (assumes @'\n'@ line endings)
-> Pandoc
readHtml opts inp = Pandoc meta blocks
where blocks = readWith parseBody def{ stateOptions = opts } rest
where blocks = case runParser parseBody def{ stateOptions = opts }
"source" rest of
Left err' -> error $ "\nError at " ++ show err'
Right result -> result
tags = canonicalizeTags $
parseTagsOptions parseOptions{ optTagPosition = True } inp
hasHeader = any (~== TagOpen "head" []) tags