From 051e8fffc220d2e787554714620f15566e58f689 Mon Sep 17 00:00:00 2001 From: Luca Bruno Date: Fri, 4 Jul 2014 14:39:11 +0200 Subject: [PATCH] Parse if then else --- Nix/Parser.hs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Nix/Parser.hs b/Nix/Parser.hs index 7e637ab..ade2cfc 100644 --- a/Nix/Parser.hs +++ b/Nix/Parser.hs @@ -18,7 +18,7 @@ import Prelude hiding (elem) nixApp :: Parser NExpr nixApp = go <$> someTill (whiteSpace *> nixExpr True) - (try (lookAhead (() <$ oneOf "=,;])}" <|> eof))) + (try (lookAhead (() <$ oneOf "=,;])}" <|> reserved "then" <|> reserved "else" <|> eof))) where go [] = error "some has failed us" go [x] = x @@ -57,6 +57,7 @@ nixTerm allowLambdas = choice , nixList , nixPath , nixLet + , nixIf , setLambdaStringOrSym allowLambdas ] @@ -83,6 +84,12 @@ nixLet = (Fix .) . NLet <$> (reserved "let" *> nixBinders) <*> (whiteSpace *> reserved "in" *> nixApp) +nixIf :: Parser NExpr +nixIf = Fix <$> (NIf + <$> (reserved "if" *> nixApp) + <*> (reserved "then" *> nixApp) + <*> (reserved "else" *> nixApp)) + -- | This is a bit tricky because we don't know whether we're looking at a set -- or a lambda until we've looked ahead a bit. And then it may be neither, -- in which case we fall back to expected a plain string or identifier.