show correct line number for posting parse errors (#67)

This commit is contained in:
Simon Michael 2011-10-04 23:39:53 +00:00
parent e3e9c6b40a
commit 381ebea35c

View File

@ -463,19 +463,21 @@ ledgermetadataline = do
ledgerpostings :: GenParser Char JournalContext [Posting] ledgerpostings :: GenParser Char JournalContext [Posting]
ledgerpostings = do ledgerpostings = do
ctx <- getState ctx <- getState
-- pass current position to the sub-parses for more useful errors -- we'll set the correct position for sub-parses for more useful errors
pos <- getPosition pos <- getPosition
ls <- many1 $ try linebeginningwithspaces ls <- many1 $ try linebeginningwithspaces
let parses p = isRight . parseWithCtx ctx p let lsnumbered = zip ls [0..]
postinglines = filter (not . (ledgercommentline `parses`)) ls parses p = isRight . parseWithCtx ctx p
postinglines = filter (not . (ledgercommentline `parses`) . fst) lsnumbered
-- group any metadata lines with the posting line above -- group any metadata lines with the posting line above
postinglinegroups :: [String] -> [String] postinglinegroups :: [(String,Line)] -> [(String,Line)]
postinglinegroups [] = [] postinglinegroups [] = []
postinglinegroups (pline:ls) = (unlines $ pline:mdlines):postinglinegroups rest postinglinegroups ((pline,num):ls) = (unlines (pline:(map fst mdlines)), num):postinglinegroups rest
where (mdlines,rest) = span (ledgermetadataline `parses`) ls where (mdlines,rest) = span ((ledgermetadataline `parses`) . fst) ls
pstrs = postinglinegroups postinglines pstrs = postinglinegroups postinglines
parseNumberedPostingLine (str,num) = fromparse $ parseWithCtx ctx (setPosition (incSourceLine pos num) >> ledgerposting) str
when (null pstrs) $ fail "no postings" when (null pstrs) $ fail "no postings"
return $ map (fromparse . parseWithCtx ctx (setPosition pos >> ledgerposting)) pstrs return $ map parseNumberedPostingLine pstrs
<?> "postings" <?> "postings"
linebeginningwithspaces :: GenParser Char JournalContext String linebeginningwithspaces :: GenParser Char JournalContext String