f if true then null else null should not parse

This commit is contained in:
Benno Fünfstück 2014-08-16 11:47:45 +02:00
parent 147624425d
commit bf77dde7fc
2 changed files with 19 additions and 13 deletions

View file

@ -14,11 +14,8 @@ import Prelude hiding (elem)
-- | The lexer for this parser is defined in 'Nix.Parser.Library'. -- | The lexer for this parser is defined in 'Nix.Parser.Library'.
nixApp :: Parser NExpr nixApp :: Parser NExpr
nixApp = go <$> some (whiteSpace *> nixExpr) nixApp = foldl' go <$> (whiteSpace *> nixExpr) <*> many nixFunArg
where where go f a = Fix (NApp f a)
go [] = error "some has failed us"
go [x] = x
go (f:x:xs) = go (Fix (NApp f x) : xs)
nixExpr :: Parser NExpr nixExpr :: Parser NExpr
nixExpr = nixExprWith nixOperators nixExpr = nixExprWith nixOperators
@ -83,24 +80,26 @@ nixSym :: Parser NExpr
nixSym = mkSym <$> identifier nixSym = mkSym <$> identifier
nixInt :: Parser NExpr nixInt :: Parser NExpr
nixInt = mkInt <$> decimal <?> "integer" nixInt = mkInt <$> token decimal <?> "integer"
nixBool :: Parser NExpr nixBool :: Parser NExpr
nixBool = try (true <|> false) <?> "bool" where nixBool = try (true <|> false) <?> "bool" where
true = mkBool True <$ string "true" true = mkBool True <$ symbol "true"
false = mkBool False <$ string "false" false = mkBool False <$ symbol "false"
nixNull :: Parser NExpr nixNull :: Parser NExpr
nixNull = try (mkNull <$ string "null") <?> "null" nixNull = mkNull <$ symbol "null" <?> "null"
nixParens :: Parser NExpr nixParens :: Parser NExpr
nixParens = parens nixApp <?> "parens" nixParens = parens nixApp <?> "parens"
nixFunArg :: Parser NExpr
nixFunArg = nixSelect $ choice
[ nixInt, nixParens, nixList, nixSet, nixBool, nixNull, nixPath, nixStringExpr
, nixSym ]
nixList :: Parser NExpr nixList :: Parser NExpr
nixList = brackets (Fix . NList <$> many (listTerm <* whiteSpace)) <?> "list" where nixList = brackets (Fix . NList <$> many nixFunArg) <?> "list"
listTerm = nixSelect $ choice
[ nixInt, nixParens, nixList, nixSet, nixBool, nixNull, nixPath, nixStringExpr
, nixSym ]
nixPath :: Parser NExpr nixPath :: Parser NExpr
nixPath = fmap mkPath $ (++) nixPath = fmap mkPath $ (++)

View file

@ -218,6 +218,13 @@ case_select_path = do
assertParseString "f.b ../a" $ Fix $ NApp select (mkPath "../a") assertParseString "f.b ../a" $ Fix $ NApp select (mkPath "../a")
where select = Fix $ NSelect (mkSym "f") (mkSelector "b") Nothing where select = Fix $ NSelect (mkSym "f") (mkSelector "b") Nothing
case_fun_app :: Assertion
case_fun_app = do
assertParseString "f a b" $ Fix $ NApp (Fix $ NApp (mkSym "f") (mkSym "a")) (mkSym "b")
assertParseString "f a.x or null" $ Fix $ NApp (mkSym "f") $ Fix $
NSelect (mkSym "a") (mkSelector "x") (Just mkNull)
assertParseFail "f if true then null else null"
tests :: TestTree tests :: TestTree
tests = $testGroupGenerator tests = $testGroupGenerator