allow "let x : A = e in s" with type annotation

This commit is contained in:
rhiannon morris 2023-12-21 17:54:31 +01:00
parent 54db7e27ef
commit aa4ead592a
2 changed files with 15 additions and 7 deletions

View file

@ -593,15 +593,21 @@ letIntro fname =
<|> withLoc fname (Just . PQ Any <$ res "letω") <|> withLoc fname (Just . PQ Any <$ res "letω")
<|> Nothing <$ resC "let" <|> Nothing <$ resC "let"
export private
letBinder : FileName -> Maybe PQty -> Grammar True (PQty, PatVar, PTerm) letBinder : FileName -> Maybe PQty -> Grammar True (PQty, PatVar, PTerm)
letBinder fname mq = do letBinder fname mq = do
qty <- the (Grammar False PQty) $ case mq of qty <- letQty fname mq
Just q => pure q x <- patVar fname
Nothing => qty fname <* mustWork (resC ".") <|> defLoc fname (PQ One) type <- optional $ resC ":" *> term fname
x <- patVar fname; mustWork (resC "=") rhs <- resC "=" *> term fname
rhs <- term fname pure (qty, x, makeLetRhs rhs type)
pure $ (qty, x, rhs) where
letQty : FileName -> Maybe PQty -> Grammar False PQty
letQty fname Nothing = qty fname <* mustWork (resC ".") <|> defLoc fname (PQ One)
letQty fname (Just q) = pure q
makeLetRhs : PTerm -> Maybe PTerm -> PTerm
makeLetRhs tm ty = maybe tm (\t => Ann tm t (extendL tm.loc t.loc)) ty
export export
letTerm : FileName -> Grammar True PTerm letTerm : FileName -> Grammar True PTerm

View file

@ -422,6 +422,8 @@ tests = "parser" :- [
`(Let (PQ One _, PV "x" {}, V "y" {}) (V "z" {}) _), `(Let (PQ One _, PV "x" {}, V "y" {}) (V "z" {}) _),
parseMatch term "letω x = y in z" parseMatch term "letω x = y in z"
`(Let (PQ Any _, PV "x" {}, V "y" {}) (V "z" {}) _), `(Let (PQ Any _, PV "x" {}, V "y" {}) (V "z" {}) _),
parseMatch term "letω x : X = y in z"
`(Let (PQ Any _, PV "x" {}, Ann (V "y" {}) (V "X" {}) _) (V "z" {}) _),
parseMatch term "let ω.x = y in z" parseMatch term "let ω.x = y in z"
`(Let (PQ Any _, PV "x" {}, V "y" {}) (V "z" {}) _), `(Let (PQ Any _, PV "x" {}, V "y" {}) (V "z" {}) _),
parseMatch term "let x = y1 y2 in z1 z2" parseMatch term "let x = y1 y2 in z1 z2"