From 4973b82fb4db41541944926a6c5e27b3f0deb086 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Tue, 9 Mar 2010 23:11:12 +0000 Subject: [PATCH] parsing: don't ignore trailing junk in a smart date, eg in web add form --- hledger-lib/Ledger/Dates.hs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/hledger-lib/Ledger/Dates.hs b/hledger-lib/Ledger/Dates.hs index f57da51ee..1135e912d 100644 --- a/hledger-lib/Ledger/Dates.hs +++ b/hledger-lib/Ledger/Dates.hs @@ -84,7 +84,7 @@ parsePeriodExpr refdate expr = (interval,span) spanFromSmartDateString :: Day -> String -> DateSpan spanFromSmartDateString refdate s = spanFromSmartDate refdate sdate where - sdate = fromparse $ parsewith smartdate s + sdate = fromparse $ parsewith smartdateonly s spanFromSmartDate :: Day -> SmartDate -> DateSpan spanFromSmartDate refdate sdate = DateSpan (Just b) (Just e) @@ -127,7 +127,7 @@ fixSmartDateStr t s = either parseerror id $ fixSmartDateStrEither t s -- | A safe version of fixSmartDateStr. fixSmartDateStrEither :: Day -> String -> Either ParseError String -fixSmartDateStrEither t s = case parsewith smartdate (lowercase s) of +fixSmartDateStrEither t s = case parsewith smartdateonly (lowercase s) of Right sd -> Right $ showDay $ fixSmartDate t sd Left e -> Left e @@ -252,6 +252,14 @@ smartdate = do (y,m,d) <- choice $ map try dateparsers return (y,m,d) +-- | Like smartdate, but there must be nothing other than whitespace after the date. +smartdateonly :: GenParser Char st SmartDate +smartdateonly = do + d <- smartdate + many spacenonewline + eof + return d + datesepchar = oneOf "/-." yyyymmdd :: GenParser Char st SmartDate