Add a reserved words as "stop words" for nixApp

This commit is contained in:
John Wiegley 2014-07-04 16:15:58 -05:00
parent 072b2e8d51
commit 0f0f865e4f
2 changed files with 25 additions and 18 deletions

View file

@ -22,7 +22,7 @@ nixApp = go <$> someTill (whiteSpace *> nixExpr True) (try (lookAhead stop))
go [x] = x go [x] = x
go (f:xs) = Fix (NApp f (go xs)) go (f:xs) = Fix (NApp f (go xs))
stop = () <$ oneOf "=,;])}" <|> eof stop = () <$ oneOf "=,;])}" <|> reservedWords <|> eof
nixExpr :: Bool -> Parser NExpr nixExpr :: Bool -> Parser NExpr
nixExpr = buildExpressionParser table . nixTerm nixExpr = buildExpressionParser table . nixTerm
@ -81,13 +81,13 @@ nixPath :: Parser NExpr
nixPath = try $ fmap mkPath $ mfilter ('/' `elem`) $ some (oneOf "A-Za-z_0-9.:/") nixPath = try $ fmap mkPath $ mfilter ('/' `elem`) $ some (oneOf "A-Za-z_0-9.:/")
nixLet :: Parser NExpr nixLet :: Parser NExpr
nixLet = (Fix .) . NLet nixLet = fmap Fix $ NLet
<$> (reserved "let" *> nixBinders) <$> (reserved "let" *> nixBinders)
<*> (whiteSpace *> reserved "in" *> nixApp) <*> (whiteSpace *> reserved "in" *> nixApp)
nixIf :: Parser NExpr nixIf :: Parser NExpr
nixIf = ((Fix .) .) . NIf nixIf = fmap Fix $ NIf
<$> (reserved "if" *> nixExpr False) <$> (reserved "if" *> nixApp)
<*> (whiteSpace *> reserved "then" *> nixApp) <*> (whiteSpace *> reserved "then" *> nixApp)
<*> (whiteSpace *> reserved "else" *> nixApp) <*> (whiteSpace *> reserved "else" *> nixApp)
@ -159,7 +159,7 @@ setOrArgs = do
trace "parsing arguments" $ return () trace "parsing arguments" $ return ()
args <- argExpr <?> "arguments" args <- argExpr <?> "arguments"
trace ("args: " ++ show args) $ return () trace ("args: " ++ show args) $ return ()
symbolic ':' *> ((Fix .) . NAbs <$> pure args <*> nixApp) symbolic ':' *> fmap Fix (NAbs <$> pure args <*> nixApp)
<|> pure args <|> pure args
lookaheadForSet :: Parser Bool lookaheadForSet :: Parser Bool

View file

@ -3,12 +3,13 @@
module Nix.Parser.Library ( module Nix.Parser.Library, module X ) where module Nix.Parser.Library ( module Nix.Parser.Library, module X ) where
import Control.Applicative import Control.Applicative
import Data.Foldable
#if USE_PARSEC #if USE_PARSEC
import Control.Monad import Control.Monad
import Control.Monad.IO.Class import Control.Monad.IO.Class
import Data.Text as T import Data.Text as T hiding (map)
import Data.Text.IO as T import Data.Text.IO as T
import Text.Parsec as X hiding ((<|>), many, optional) import Text.Parsec as X hiding ((<|>), many, optional)
import Text.Parsec.Expr as X import Text.Parsec.Expr as X
@ -26,17 +27,7 @@ lexer = P.makeTokenParser P.LanguageDef
, P.identLetter = alphaNum <|> oneOf "_" , P.identLetter = alphaNum <|> oneOf "_"
, P.opStart = oneOf ":!#$%&*+./<=>?@\\^|-~" , P.opStart = oneOf ":!#$%&*+./<=>?@\\^|-~"
, P.opLetter = oneOf "@" , P.opLetter = oneOf "@"
, P.reservedNames = , P.reservedNames = reservedNames
[ "let", "in"
, "if", "then", "else"
, "true", "false"
, "null"
, "assert"
, "with"
, "rec"
, "inherit"
, "or"
]
, P.reservedOpNames = [] , P.reservedOpNames = []
, P.caseSensitive = True , P.caseSensitive = True
} }
@ -77,7 +68,7 @@ parseFromFileEx p path =
import Data.Char import Data.Char
import Data.List (nub) import Data.List (nub)
import Data.Text import Data.Text hiding (map)
import Text.Parser.Expression as X import Text.Parser.Expression as X
import Text.Parser.LookAhead as X import Text.Parser.LookAhead as X
import Text.Trifecta as X hiding (whiteSpace, symbol, symbolic) import Text.Trifecta as X hiding (whiteSpace, symbol, symbolic)
@ -143,6 +134,22 @@ inCommentSingle
#endif #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 :: Parser a -> Parser end -> Parser [a]
someTill p end = go someTill p end = go
where where