X.P.OrgMode: Check num validity while parsing

There are a lot of times when we could already check for "valid" input
during parsing instead of relaying possibly incorrect dates and times to
`org-mode`.  This seems like a sensible thing to do—I don't think anyone
would ever want to schedule anything for 35:90 :)
This commit is contained in:
slotThe
2021-12-30 09:21:06 +01:00
parent 0f8b3616b2
commit 4f048fb563

View File

@@ -380,10 +380,13 @@ pInput inp = (`runParser` inp) . choice $
-- | Try to parse a 'Time'.
pTimeOfDay :: Parser (Maybe TimeOfDay)
pTimeOfDay = choice
[ Just <$> (TimeOfDay <$> num <* string ":" <*> num ) -- HH:MM
, Just <$> (TimeOfDay <$> num <*> pure 0) -- HH
[ Just <$> (TimeOfDay <$> pHour <* string ":" <*> pMinute) -- HH:MM
, Just <$> (TimeOfDay <$> pHour <*> pure 0 ) -- HH
, pure Nothing
]
where
pMinute :: Parser Int = pNumBetween 1 60
pHour :: Parser Int = pNumBetween 1 24
-- | Parse a 'Date'.
pDate :: Parser Date
@@ -403,7 +406,7 @@ pDate = skipSpaces *> choice
numWithoutColon :: Parser Int
numWithoutColon = do
str <- num
str <- pNumBetween 1 12 -- month
c <- get
if c == ':'
then pfail
@@ -411,7 +414,7 @@ pDate = skipSpaces *> choice
pDate' :: Parser (Int, Maybe Int, Maybe Integer)
pDate' =
(,,) <$> num
(,,) <$> pNumBetween 1 31 -- day
<*> optional (skipSpaces *> choice
[ pPrefix "ja" "nuary" 1 , pPrefix "f" "ebruary" 2
, pPrefix "mar" "ch" 3 , pPrefix "ap" "ril" 4
@@ -431,3 +434,9 @@ pDate = skipSpaces *> choice
l <- munch (/= ' ')
guard (l `isPrefixOf` leftover)
pure ret
-- | Parse a number between @lo@ (inclusive) and @hi@ (inclusive).
pNumBetween :: Int -> Int -> Parser Int
pNumBetween lo hi = do
n <- num
n <$ guard (n >= lo && n <= hi)