From 0f0f865e4f03cd85e8068a337459cb9aa2c7b4a5 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Fri, 4 Jul 2014 16:15:58 -0500 Subject: [PATCH] Add a reserved words as "stop words" for nixApp --- Nix/Parser.hs | 10 +++++----- Nix/Parser/Library.hs | 33 ++++++++++++++++++++------------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/Nix/Parser.hs b/Nix/Parser.hs index 1024b7a..8e737a3 100644 --- a/Nix/Parser.hs +++ b/Nix/Parser.hs @@ -22,7 +22,7 @@ nixApp = go <$> someTill (whiteSpace *> nixExpr True) (try (lookAhead stop)) go [x] = x go (f:xs) = Fix (NApp f (go xs)) - stop = () <$ oneOf "=,;])}" <|> eof + stop = () <$ oneOf "=,;])}" <|> reservedWords <|> eof nixExpr :: Bool -> Parser NExpr nixExpr = buildExpressionParser table . nixTerm @@ -81,13 +81,13 @@ nixPath :: Parser NExpr nixPath = try $ fmap mkPath $ mfilter ('/' `elem`) $ some (oneOf "A-Za-z_0-9.:/") nixLet :: Parser NExpr -nixLet = (Fix .) . NLet +nixLet = fmap Fix $ NLet <$> (reserved "let" *> nixBinders) <*> (whiteSpace *> reserved "in" *> nixApp) nixIf :: Parser NExpr -nixIf = ((Fix .) .) . NIf - <$> (reserved "if" *> nixExpr False) +nixIf = fmap Fix $ NIf + <$> (reserved "if" *> nixApp) <*> (whiteSpace *> reserved "then" *> nixApp) <*> (whiteSpace *> reserved "else" *> nixApp) @@ -159,7 +159,7 @@ setOrArgs = do trace "parsing arguments" $ return () args <- argExpr "arguments" trace ("args: " ++ show args) $ return () - symbolic ':' *> ((Fix .) . NAbs <$> pure args <*> nixApp) + symbolic ':' *> fmap Fix (NAbs <$> pure args <*> nixApp) <|> pure args lookaheadForSet :: Parser Bool diff --git a/Nix/Parser/Library.hs b/Nix/Parser/Library.hs index 583b5b3..0d36bf9 100644 --- a/Nix/Parser/Library.hs +++ b/Nix/Parser/Library.hs @@ -3,12 +3,13 @@ module Nix.Parser.Library ( module Nix.Parser.Library, module X ) where import Control.Applicative +import Data.Foldable #if USE_PARSEC import Control.Monad import Control.Monad.IO.Class -import Data.Text as T +import Data.Text as T hiding (map) import Data.Text.IO as T import Text.Parsec as X hiding ((<|>), many, optional) import Text.Parsec.Expr as X @@ -26,17 +27,7 @@ lexer = P.makeTokenParser P.LanguageDef , P.identLetter = alphaNum <|> oneOf "_" , P.opStart = oneOf ":!#$%&*+./<=>?@\\^|-~" , P.opLetter = oneOf "@" - , P.reservedNames = - [ "let", "in" - , "if", "then", "else" - , "true", "false" - , "null" - , "assert" - , "with" - , "rec" - , "inherit" - , "or" - ] + , P.reservedNames = reservedNames , P.reservedOpNames = [] , P.caseSensitive = True } @@ -77,7 +68,7 @@ parseFromFileEx p path = import Data.Char import Data.List (nub) -import Data.Text +import Data.Text hiding (map) import Text.Parser.Expression as X import Text.Parser.LookAhead as X import Text.Trifecta as X hiding (whiteSpace, symbol, symbolic) @@ -143,6 +134,22 @@ inCommentSingle #endif +reservedNames :: [String] +reservedNames = + [ "let", "in" + , "if", "then", "else" + , "true", "false" + , "null" + , "assert" + , "with" + , "rec" + , "inherit" + , "or" + ] + +reservedWords :: Parser () +reservedWords = () <$ asum (map string reservedNames) + someTill :: Parser a -> Parser end -> Parser [a] someTill p end = go where