From 1f24e025da1627fbcf3c9e816444f5b48c53cc04 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Tue, 31 May 2011 19:49:37 +0000 Subject: [PATCH] parsing: make bad date parse error more reliable --- hledger-lib/Hledger/Read/JournalReader.hs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/hledger-lib/Hledger/Read/JournalReader.hs b/hledger-lib/Hledger/Read/JournalReader.hs index 8c58d7578..b1433c347 100644 --- a/hledger-lib/Hledger/Read/JournalReader.hs +++ b/hledger-lib/Hledger/Read/JournalReader.hs @@ -372,11 +372,12 @@ ledgerdate = do datestr <- many1 $ choice' [digit, datesepchar] let dateparts = wordsBy (`elem` datesepchars) datestr currentyear <- getYear - let [y,m,d] = case (dateparts,currentyear) of - ([m,d],Just y) -> [show y,m,d] - ([_,_],Nothing) -> fail $ "partial date "++datestr++" found, but the current year is unknown" - _ -> dateparts - maybedate = fromGregorianValid (read y) (read m) (read d) + [y,m,d] <- case (dateparts,currentyear) of + ([m,d],Just y) -> return [show y,m,d] + ([_,_],Nothing) -> fail $ "partial date "++datestr++" found, but the current year is unknown" + ([y,m,d],_) -> return [y,m,d] + _ -> fail $ "bad date: " ++ datestr + let maybedate = fromGregorianValid (read y) (read m) (read d) case maybedate of Nothing -> fail $ "bad date: " ++ datestr Just date -> return date @@ -715,6 +716,11 @@ tests_Hledger_Read_JournalReader = TestList [ assertParse (parseWithCtx nullctx ledgercommentline " \t; x\n") assertParse (parseWithCtx nullctx ledgercommentline ";x") + ,"ledgerdate" ~: do + assertParse (parseWithCtx nullctx ledgerdate "2011/1/1") + assertParseFailure (parseWithCtx nullctx ledgerdate "1/1") + assertParse (parseWithCtx nullctx{ctxYear=Just 2011} ledgerdate "1/1") + ,"ledgerDefaultYear" ~: do assertParse (parseWithCtx nullctx ledgerDefaultYear "Y 2010\n") assertParse (parseWithCtx nullctx ledgerDefaultYear "Y 10001\n")