From 0914047115eefb76ec20ff57fff79aba10ab6476 Mon Sep 17 00:00:00 2001 From: Guillaume Maudoux Date: Fri, 9 Feb 2018 15:29:47 +0100 Subject: [PATCH] Handle `let { body = ... }` expressions --- Nix/Parser.hs | 18 ++++++++++++++---- tests/ParserTests.hs | 7 +++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/Nix/Parser.hs b/Nix/Parser.hs index ccd7151..2480657 100644 --- a/Nix/Parser.hs +++ b/Nix/Parser.hs @@ -140,10 +140,20 @@ nixPath = annotateLocation1 $ token $ fmap (mkPathF False) $ ((++) "path" nixLet :: Parser NExprLoc -nixLet = annotateLocation1 $ NLet - <$> (reserved "let" *> nixBinders) - <*> (whiteSpace *> reserved "in" *> nixExprLoc) - "let" +nixLet = annotateLocation1 $ reserved "let" + *> whiteSpace + *> (letBody <|> letBinders) + "let block" + where + letBinders = NLet + <$> nixBinders + <*> (whiteSpace *> reserved "in" *> nixExprLoc) + -- Let expressions `let {..., body = ...}' are just desugared + -- into `(rec {..., body = ...}).body'. + letBody = (\x -> NSelect x ([StaticKey "body"]) Nothing) <$> aset + aset = annotateLocation1 $ NRecSet + <$> (braces nixBinders) + nixIf :: Parser NExprLoc nixIf = annotateLocation1 $ NIf diff --git a/tests/ParserTests.hs b/tests/ParserTests.hs index aceb38c..9d26a10 100644 --- a/tests/ParserTests.hs +++ b/tests/ParserTests.hs @@ -184,6 +184,13 @@ case_simple_let = do where binds = [NamedVar (mkSelector "a") $ mkInt 4] +case_let_body :: Assertion +case_let_body = do + assertParseString "let { body = 1; }" letBody + where + letBody = Fix $ NSelect aset (mkSelector "body") Nothing + aset = Fix $ NRecSet [NamedVar (mkSelector "body") (mkInt 1)] + case_nested_let :: Assertion case_nested_let = do assertParseString "let a = 4; in let b = 5; in a" $ Fix $ NLet