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 (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

View File

@ -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