2018-04-07 21:02:50 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
|
|
|
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
|
|
|
|
|
|
|
|
module Nix.Parser (
|
|
|
|
parseNixFile,
|
|
|
|
parseNixFileLoc,
|
|
|
|
parseNixText,
|
|
|
|
parseNixTextLoc,
|
|
|
|
Result(..)
|
|
|
|
) where
|
|
|
|
|
2018-04-09 19:16:25 +02:00
|
|
|
import Control.Applicative hiding (many, some)
|
2018-04-07 21:02:50 +02:00
|
|
|
import Control.Monad
|
|
|
|
import Control.Monad.IO.Class
|
2018-04-10 06:35:46 +02:00
|
|
|
import Data.Char (isAlpha, isDigit, isSpace)
|
2018-04-07 21:02:50 +02:00
|
|
|
import Data.Functor
|
2018-04-10 01:11:31 +02:00
|
|
|
import qualified Data.List.NonEmpty as NE
|
2018-04-07 21:02:50 +02:00
|
|
|
import qualified Data.HashMap.Strict.InsOrd as M
|
2018-04-10 06:35:46 +02:00
|
|
|
import Data.Text hiding (map)
|
2018-04-07 21:02:50 +02:00
|
|
|
import Nix.Expr hiding (($>))
|
|
|
|
import Nix.Parser.Library
|
|
|
|
import Nix.Parser.Operators
|
|
|
|
import Nix.StringOperations
|
2018-04-10 01:11:31 +02:00
|
|
|
import Text.Megaparsec.Expr
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nixExpr :: Parser NExpr
|
|
|
|
nixExpr = stripAnnotation <$> nixExprLoc
|
|
|
|
|
|
|
|
-- | The lexer for this parser is defined in 'Nix.Parser.Library'.
|
|
|
|
nixExprLoc :: Parser NExprLoc
|
2018-04-10 01:11:31 +02:00
|
|
|
nixExprLoc = whiteSpace *> (nixToplevelForm <|> exprParser)
|
|
|
|
|
|
|
|
exprParser :: Parser NExprLoc
|
2018-04-10 06:35:46 +02:00
|
|
|
exprParser = makeExprParser (nixTerm <* whiteSpace) $
|
|
|
|
map (map snd) (nixOperators (nixTerm <* whiteSpace) nixSelector selDot)
|
2018-04-07 21:02:50 +02:00
|
|
|
|
2018-04-09 19:16:25 +02:00
|
|
|
antiStart :: Parser Text
|
|
|
|
antiStart = try (symbol "${") <?> show ("${" :: String)
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
|
|
nixAntiquoted :: Parser a -> Parser (Antiquoted a NExprLoc)
|
2018-04-10 06:35:46 +02:00
|
|
|
nixAntiquoted p =
|
|
|
|
Antiquoted <$> (antiStart *> nixExprLoc <* symbol "}")
|
|
|
|
<|> Plain <$> p
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
|
|
selDot :: Parser ()
|
2018-04-10 06:35:46 +02:00
|
|
|
selDot = try (char '.' *> notFollowedBy (("path" :: String) <$ nixPath))
|
|
|
|
*> whiteSpace
|
|
|
|
<?> "."
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
|
|
nixSelector :: Parser (Ann SrcSpan (NAttrPath NExprLoc))
|
|
|
|
nixSelector = annotateLocation $ keyName `sepBy1` selDot
|
|
|
|
|
|
|
|
-- | A self-contained unit.
|
|
|
|
nixTerm :: Parser NExprLoc
|
2018-04-10 01:11:31 +02:00
|
|
|
nixTerm = choice
|
2018-04-10 06:35:46 +02:00
|
|
|
[ {-dbg "Path" -} nixPath
|
|
|
|
, {-dbg "SPath" -} nixSPath
|
|
|
|
, {-dbg "Float" -} nixFloat
|
|
|
|
, {-dbg "Int" -} nixInt
|
|
|
|
, {-dbg "Bool" $ -} try (nixBool <* notFollowedBy (char '-'))
|
|
|
|
, {-dbg "Null" $ -} try (nixNull <* notFollowedBy (char '-'))
|
|
|
|
, {-dbg "Parens" -} nixParens
|
|
|
|
, {-dbg "List" -} nixList
|
|
|
|
, {-dbg "Uri" -} nixUri
|
|
|
|
, {-dbg "StringExpr" -} nixStringExpr
|
|
|
|
, {-dbg "Set" -} nixSet
|
|
|
|
, {-dbg "Path" -} nixSym ]
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
|
|
nixToplevelForm :: Parser NExprLoc
|
2018-04-10 06:35:46 +02:00
|
|
|
nixToplevelForm = choice
|
|
|
|
[ {-dbg "Lambda" -} nixLambda
|
|
|
|
, {-dbg "Let" -} nixLet
|
|
|
|
, {-dbg "If" -} nixIf
|
|
|
|
, {-dbg "Assert" -} nixAssert
|
|
|
|
, {-dbg "Lambda" -} nixWith ]
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
|
|
nixSym :: Parser NExprLoc
|
|
|
|
nixSym = annotateLocation1 $ mkSymF <$> identifier
|
|
|
|
|
|
|
|
nixInt :: Parser NExprLoc
|
2018-04-10 01:11:31 +02:00
|
|
|
nixInt = annotateLocation1 (mkIntF <$> integer <?> "integer")
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
|
|
nixFloat :: Parser NExprLoc
|
2018-04-10 01:11:31 +02:00
|
|
|
nixFloat = annotateLocation1 (try (mkFloatF . realToFrac <$> float) <?> "float")
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
|
|
nixBool :: Parser NExprLoc
|
2018-04-10 01:11:31 +02:00
|
|
|
nixBool = annotateLocation1 (try (true <|> false) <?> "bool") where
|
2018-04-07 21:02:50 +02:00
|
|
|
true = mkBoolF True <$ reserved "true"
|
|
|
|
false = mkBoolF False <$ reserved "false"
|
|
|
|
|
|
|
|
nixNull :: Parser NExprLoc
|
2018-04-10 01:11:31 +02:00
|
|
|
nixNull = annotateLocation1 (mkNullF <$ try (reserved "null") <?> "null")
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
|
|
nixParens :: Parser NExprLoc
|
|
|
|
nixParens = parens nixExprLoc <?> "parens"
|
|
|
|
|
|
|
|
nixList :: Parser NExprLoc
|
2018-04-10 01:11:31 +02:00
|
|
|
nixList = annotateLocation1 (brackets (NList <$> many nixTerm) <?> "list")
|
2018-04-07 21:02:50 +02:00
|
|
|
|
2018-04-10 06:35:46 +02:00
|
|
|
pathChar :: Bool -> Parser Char
|
|
|
|
pathChar allowSlash = satisfy $ \x ->
|
|
|
|
isAlpha x || isDigit x || x == '.' || x == '_' || x == '-' || x == '+'
|
|
|
|
|| (allowSlash && x == '/')
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
|
|
slash :: Parser Char
|
2018-04-10 06:35:46 +02:00
|
|
|
slash = try (char '/' <* notFollowedBy (satisfy (\x -> x == '/' || x == '*' || isSpace x)))
|
2018-04-07 21:02:50 +02:00
|
|
|
<?> "slash"
|
|
|
|
|
|
|
|
-- | A path surrounded by angle brackets, indicating that it should be
|
|
|
|
-- looked up in the NIX_PATH environment variable at evaluation.
|
|
|
|
nixSPath :: Parser NExprLoc
|
2018-04-10 06:35:46 +02:00
|
|
|
nixSPath = annotateLocation1
|
|
|
|
(mkPathF True <$> try (char '<' *> some (pathChar True) <* symbol ">")
|
|
|
|
<?> "spath")
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
|
|
nixPath :: Parser NExprLoc
|
2018-04-10 06:35:46 +02:00
|
|
|
nixPath = annotateLocation1 $ try $ do
|
|
|
|
start <- many (pathChar False)
|
|
|
|
middle <- some slash
|
|
|
|
finish <- some (pathChar True)
|
|
|
|
whiteSpace
|
|
|
|
return $ mkPathF False $ start ++ middle ++ finish
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
|
|
nixLet :: Parser NExprLoc
|
2018-04-10 01:11:31 +02:00
|
|
|
nixLet = annotateLocation1 (reserved "let"
|
2018-04-07 21:02:50 +02:00
|
|
|
*> whiteSpace
|
|
|
|
*> (letBody <|> letBinders)
|
2018-04-09 19:16:25 +02:00
|
|
|
<?> "let block")
|
2018-04-07 21:02:50 +02:00
|
|
|
where
|
|
|
|
letBinders = NLet
|
|
|
|
<$> nixBinders
|
|
|
|
<*> (whiteSpace *> reserved "in" *> nixExprLoc)
|
|
|
|
-- Let expressions `let {..., body = ...}' are just desugared
|
|
|
|
-- into `(rec {..., body = ...}).body'.
|
2018-04-10 02:12:16 +02:00
|
|
|
letBody = (\x pos -> NSelect x [StaticKey "body" (Just pos)] Nothing)
|
|
|
|
<$> aset <*> getPosition
|
2018-04-07 21:02:50 +02:00
|
|
|
aset = annotateLocation1 $ NRecSet <$> braces nixBinders
|
|
|
|
|
|
|
|
nixIf :: Parser NExprLoc
|
2018-04-10 01:11:31 +02:00
|
|
|
nixIf = annotateLocation1 (NIf
|
2018-04-07 21:02:50 +02:00
|
|
|
<$> (reserved "if" *> nixExprLoc)
|
|
|
|
<*> (whiteSpace *> reserved "then" *> nixExprLoc)
|
|
|
|
<*> (whiteSpace *> reserved "else" *> nixExprLoc)
|
2018-04-09 19:16:25 +02:00
|
|
|
<?> "if")
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
|
|
nixAssert :: Parser NExprLoc
|
2018-04-10 01:11:31 +02:00
|
|
|
nixAssert = annotateLocation1 (NAssert
|
2018-04-07 21:02:50 +02:00
|
|
|
<$> (reserved "assert" *> nixExprLoc)
|
|
|
|
<*> (semi *> nixExprLoc)
|
2018-04-09 19:16:25 +02:00
|
|
|
<?> "assert")
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
|
|
nixWith :: Parser NExprLoc
|
2018-04-10 01:11:31 +02:00
|
|
|
nixWith = annotateLocation1 (NWith
|
2018-04-07 21:02:50 +02:00
|
|
|
<$> (reserved "with" *> nixExprLoc)
|
|
|
|
<*> (semi *> nixExprLoc)
|
2018-04-09 19:16:25 +02:00
|
|
|
<?> "with")
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
|
|
nixLambda :: Parser NExprLoc
|
2018-04-10 02:12:16 +02:00
|
|
|
nixLambda = (nAbs <$> annotateLocation (try argExpr <?> "lambda arguments")
|
|
|
|
<*> nixExprLoc) <?> "lambda"
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
|
|
nixStringExpr :: Parser NExprLoc
|
|
|
|
nixStringExpr = nStr <$> annotateLocation nixString
|
|
|
|
|
|
|
|
uriAfterColonC :: Parser Char
|
2018-04-10 01:11:31 +02:00
|
|
|
uriAfterColonC = alphaNumChar <|>
|
|
|
|
satisfy (\x -> x `elem` ("%/?:@&=+$,-_.!~*'" :: String))
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
|
|
nixUri :: Parser NExprLoc
|
2018-04-10 01:11:31 +02:00
|
|
|
nixUri = annotateLocation1 $ parseToken $ fmap (mkUriF . pack) $ (++)
|
2018-04-07 21:02:50 +02:00
|
|
|
<$> try ((++) <$> (scheme <* char ':') <*> fmap (\x -> [':',x]) uriAfterColonC)
|
|
|
|
<*> many uriAfterColonC
|
|
|
|
where
|
2018-04-10 01:11:31 +02:00
|
|
|
scheme = (:) <$> letterChar
|
|
|
|
<*> many (alphaNumChar <|> satisfy (\x -> x `elem` ("+-." :: String)))
|
|
|
|
parseToken p = p <* whiteSpace
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
|
|
nixString :: Parser (NString NExprLoc)
|
|
|
|
nixString = doubleQuoted <|> indented <?> "string"
|
|
|
|
where
|
|
|
|
doubleQuoted :: Parser (NString NExprLoc)
|
|
|
|
doubleQuoted = DoubleQuoted . removePlainEmpty . mergePlain
|
|
|
|
<$> (doubleQ *> many (stringChar doubleQ (void $ char '\\') doubleEscape)
|
2018-04-09 19:16:25 +02:00
|
|
|
<* doubleQ)
|
2018-04-07 21:02:50 +02:00
|
|
|
<?> "double quoted string"
|
|
|
|
|
2018-04-10 01:11:31 +02:00
|
|
|
doubleQ = void $ char '"'
|
2018-04-07 21:02:50 +02:00
|
|
|
doubleEscape = Plain . singleton <$> (char '\\' *> escapeCode)
|
|
|
|
|
|
|
|
indented :: Parser (NString NExprLoc)
|
|
|
|
indented = stripIndent
|
|
|
|
<$> (indentedQ *> many (stringChar indentedQ indentedQ indentedEscape)
|
2018-04-09 19:16:25 +02:00
|
|
|
<* indentedQ)
|
2018-04-07 21:02:50 +02:00
|
|
|
<?> "indented string"
|
|
|
|
|
2018-04-10 01:11:31 +02:00
|
|
|
indentedQ = void (try (string "''") <?> "\"''\"")
|
2018-04-07 21:02:50 +02:00
|
|
|
indentedEscape = fmap Plain
|
|
|
|
$ try (indentedQ *> char '\\') *> fmap singleton escapeCode
|
|
|
|
<|> try (indentedQ *> ("''" <$ char '\'' <|> "$" <$ char '$'))
|
|
|
|
|
2018-04-10 01:11:31 +02:00
|
|
|
stringChar end escStart esc = esc
|
|
|
|
<|> Antiquoted <$> (antiStart *> nixExprLoc <* char '}')
|
|
|
|
-- ^ don't skip trailing space
|
|
|
|
<|> Plain . singleton <$> char '$'
|
|
|
|
<|> Plain . pack <$> some plainChar
|
|
|
|
where
|
|
|
|
plainChar =
|
|
|
|
notFollowedBy (end <|> void (char '$') <|> escStart) *> anyChar
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
|
|
escapeCode = choice [ c <$ char e | (c,e) <- escapeCodes ] <|> anyChar
|
|
|
|
|
|
|
|
-- | Gets all of the arguments for a function.
|
|
|
|
argExpr :: Parser (Params NExprLoc)
|
2018-04-09 19:16:25 +02:00
|
|
|
argExpr = choice [atLeft, onlyname, atRight] <* symbol ":" where
|
2018-04-07 21:02:50 +02:00
|
|
|
-- An argument not in curly braces. There's some potential ambiguity
|
|
|
|
-- in the case of, for example `x:y`. Is it a lambda function `x: y`, or
|
|
|
|
-- a URI `x:y`? Nix syntax says it's the latter. So we need to fail if
|
|
|
|
-- there's a valid URI parse here.
|
2018-04-10 01:11:31 +02:00
|
|
|
onlyname = choice [nixUri >> unexpected (Label ('v' NE.:| "alid uri")),
|
2018-04-07 21:02:50 +02:00
|
|
|
Param <$> identifier]
|
|
|
|
|
|
|
|
-- Parameters named by an identifier on the left (`args @ {x, y}`)
|
|
|
|
atLeft = try $ do
|
2018-04-09 19:16:25 +02:00
|
|
|
name <- identifier <* symbol "@"
|
2018-04-07 21:02:50 +02:00
|
|
|
(variadic, params) <- params
|
|
|
|
return $ ParamSet params variadic (Just name)
|
|
|
|
|
|
|
|
-- Parameters named by an identifier on the right, or none (`{x, y} @ args`)
|
|
|
|
atRight = do
|
|
|
|
(variadic, params) <- params
|
2018-04-09 19:16:25 +02:00
|
|
|
name <- optional $ symbol "@" *> identifier
|
2018-04-07 21:02:50 +02:00
|
|
|
return $ ParamSet params variadic name
|
|
|
|
|
|
|
|
-- Return the parameters set.
|
|
|
|
params = do
|
|
|
|
(args, dotdots) <- braces getParams
|
|
|
|
return (dotdots, M.fromList args)
|
|
|
|
|
|
|
|
-- Collects the parameters within curly braces. Returns the parameters and
|
|
|
|
-- a boolean indicating if the parameters are variadic.
|
|
|
|
getParams :: Parser ([(Text, Maybe NExprLoc)], Bool)
|
|
|
|
getParams = go [] where
|
|
|
|
-- Attempt to parse `...`. If this succeeds, stop and return True.
|
|
|
|
-- Otherwise, attempt to parse an argument, optionally with a
|
|
|
|
-- default. If this fails, then return what has been accumulated
|
|
|
|
-- so far.
|
2018-04-09 19:16:25 +02:00
|
|
|
go acc = ((acc, True) <$ symbol "...") <|> getMore acc
|
2018-04-07 21:02:50 +02:00
|
|
|
getMore acc =
|
|
|
|
-- Could be nothing, in which just return what we have so far.
|
|
|
|
option (acc, False) $ do
|
|
|
|
-- Get an argument name and an optional default.
|
2018-04-10 06:35:46 +02:00
|
|
|
pair <- liftA2 (,) (identifier <* whiteSpace)
|
|
|
|
(optional $ question *> nixExprLoc)
|
2018-04-07 21:02:50 +02:00
|
|
|
-- Either return this, or attempt to get a comma and restart.
|
2018-04-09 19:16:25 +02:00
|
|
|
option (acc ++ [pair], False) $ comma >> go (acc ++ [pair])
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
|
|
nixBinders :: Parser [Binding NExprLoc]
|
2018-04-09 19:16:25 +02:00
|
|
|
nixBinders = (inherit <|> namedVar) `endBy` semi where
|
2018-04-07 21:02:50 +02:00
|
|
|
inherit = Inherit <$> (reserved "inherit" *> optional scope)
|
|
|
|
<*> many keyName
|
|
|
|
<?> "inherited binding"
|
|
|
|
namedVar = NamedVar <$> (annotated <$> nixSelector)
|
2018-04-09 19:16:25 +02:00
|
|
|
<*> (equals *> nixExprLoc)
|
2018-04-07 21:02:50 +02:00
|
|
|
<?> "variable binding"
|
|
|
|
scope = parens nixExprLoc <?> "inherit scope"
|
|
|
|
|
|
|
|
keyName :: Parser (NKeyName NExprLoc)
|
2018-04-10 06:35:46 +02:00
|
|
|
keyName = (dynamicKey <|> staticKey) <* whiteSpace where
|
2018-04-07 21:02:50 +02:00
|
|
|
staticKey = do
|
2018-04-10 01:11:31 +02:00
|
|
|
beg <- getPosition
|
2018-04-07 21:02:50 +02:00
|
|
|
StaticKey <$> identifier <*> pure (Just beg)
|
|
|
|
dynamicKey = DynamicKey <$> nixAntiquoted nixString
|
|
|
|
|
|
|
|
nixSet :: Parser NExprLoc
|
2018-04-10 01:11:31 +02:00
|
|
|
nixSet = annotateLocation1 ((isRec <*> braces nixBinders) <?> "set") where
|
2018-04-07 21:02:50 +02:00
|
|
|
isRec = (try (reserved "rec" $> NRecSet) <?> "recursive set")
|
|
|
|
<|> pure NSet
|
|
|
|
|
|
|
|
parseNixFile :: MonadIO m => FilePath -> m (Result NExpr)
|
|
|
|
parseNixFile = parseFromFileEx $ nixExpr <* eof
|
|
|
|
|
|
|
|
parseNixFileLoc :: MonadIO m => FilePath -> m (Result NExprLoc)
|
|
|
|
parseNixFileLoc = parseFromFileEx $ nixExprLoc <* eof
|
|
|
|
|
|
|
|
parseNixText :: Text -> Result NExpr
|
2018-04-10 01:11:31 +02:00
|
|
|
parseNixText = parseFromText $ nixExpr <* eof
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
|
|
parseNixTextLoc :: Text -> Result NExprLoc
|
2018-04-10 01:11:31 +02:00
|
|
|
parseNixTextLoc = parseFromText $ nixExprLoc <* eof
|