hnix/src/Nix/Parser.hs

349 lines
12 KiB
Haskell
Raw Normal View History

2018-04-10 17:34:21 +02:00
{-# LANGUAGE CPP #-}
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
import Control.Applicative hiding (many, some)
2018-04-07 21:02:50 +02:00
import Control.Monad
import Control.Monad.IO.Class
import Data.Char (isAlpha, isDigit, isSpace)
2018-04-07 21:02:50 +02:00
import Data.Functor
import qualified Data.List.NonEmpty as NE
2018-04-07 21:02:50 +02:00
import qualified Data.HashMap.Strict.InsOrd as M
import Data.Text hiding (map, empty)
2018-04-07 21:02:50 +02:00
import Nix.Expr hiding (($>))
import Nix.Parser.Library
import Nix.Parser.Operators
import Nix.StringOperations
import Text.Megaparsec.Expr
2018-04-07 21:02:50 +02:00
--------------------------------------------------------------------------------
nixExprLoc :: Parser NExprLoc
nixExprLoc = makeExprParser nixTerm $ map (map snd) (nixOperators nixSelector)
2018-04-07 21:02:50 +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)
nixAntiquoted p =
Antiquoted <$> (antiStart *> nixToplevelForm <* symbol "}")
<|> Plain <$> p
<?> "anti-quotation"
2018-04-07 21:02:50 +02:00
selDot :: Parser ()
selDot = try (symbol "." *> notFollowedBy nixPath) <?> "."
nixSelect :: Parser NExprLoc -> Parser NExprLoc
nixSelect term = build
<$> term
<*> optional ((,) <$> (selDot *> nixSelector)
<*> optional (reserved "or" *> nixTerm))
where
build :: NExprLoc
-> Maybe (Ann SrcSpan (NAttrPath NExprLoc), Maybe NExprLoc)
-> NExprLoc
build t Nothing = t
build t (Just (s,o)) = nSelectLoc t s o
2018-04-07 21:02:50 +02:00
nixSelector :: Parser (Ann SrcSpan (NAttrPath NExprLoc))
nixSelector = annotateLocation $ keyName `sepBy1` selDot
2018-04-10 17:34:21 +02:00
-- #define DEBUG_PARSER 1
#if DEBUG_PARSER
2018-04-07 21:02:50 +02:00
nixTerm :: Parser NExprLoc
nixTerm = nixSelect $ do
c <- dbg "lookAhead" $ try $ lookAhead $ satisfy $ \x ->
pathChar x ||
x == '(' ||
x == '{' ||
x == '[' ||
x == '<' ||
x == '"' ||
x == '\''
case c of
'(' -> dbg "Parens" nixParens
'{' -> dbg "Set" nixSet
'[' -> dbg "List" nixList
'<' -> dbg "SPath" nixSPath
'"' -> dbg "StringExpr" nixStringExpr
'\'' -> dbg "StringExpr" nixStringExpr
_ -> choice $
[ dbg "Path" nixPath | pathChar c ] ++
[ dbg "Uri" nixUri | isAlpha c ] ++
(if isDigit c then [ dbg "Float" nixFloat
, dbg "Int" nixInt ] else []) ++
[ dbg "Bool" nixBool | c == 'b' ] ++
[ dbg "Null" nixNull | c == 'n' ] ++
[ dbg "Sym" nixSym ]
nixToplevelForm :: Parser NExprLoc
nixToplevelForm = keywords <|> dbg "Lambda" nixLambda <|> nixExprLoc
where
keywords = do
word <- dbg "keywords" $ try $ lookAhead $ some letterChar <* satisfy reservedEnd
case word of
"let" -> dbg "Let" nixLet
"if" -> dbg "If" nixIf
"assert" -> dbg "Assert" nixAssert
"with" -> dbg "With" nixWith
_ -> empty
2018-04-10 17:34:21 +02:00
#else
nixTerm :: Parser NExprLoc
nixTerm = nixSelect $ do
c <- try $ lookAhead $ satisfy $ \x ->
pathChar x ||
x == '(' ||
x == '{' ||
x == '[' ||
x == '<' ||
x == '/' ||
x == '"' ||
x == '\''
case c of
'(' -> nixParens
'{' -> nixSet
'[' -> nixList
'<' -> nixSPath
'/' -> nixPath
'"' -> nixStringExpr
'\'' -> nixStringExpr
_ -> choice $
[ nixSet | c == 'r' ] ++
[ nixPath | pathChar c ] ++
[ nixUri | isAlpha c ] ++
(if isDigit c then [ nixFloat
, nixInt ] else []) ++
[ nixBool | c == 't' || c == 'f' ] ++
[ nixNull | c == 'n' ] ++
[ nixSym ]
2018-04-07 21:02:50 +02:00
nixToplevelForm :: Parser NExprLoc
nixToplevelForm = keywords <|> nixLambda <|> nixExprLoc
where
keywords = do
word <- try $ lookAhead $ some letterChar <* satisfy reservedEnd
case word of
"let" -> nixLet
"if" -> nixIf
"assert" -> nixAssert
"with" -> nixWith
_ -> empty
2018-04-10 17:34:21 +02:00
#endif
2018-04-07 21:02:50 +02:00
nixSym :: Parser NExprLoc
nixSym = annotateLocation1 $ mkSymF <$> identifier
nixInt :: Parser NExprLoc
nixInt = annotateLocation1 (mkIntF <$> integer <?> "integer")
2018-04-07 21:02:50 +02:00
nixFloat :: Parser NExprLoc
nixFloat = annotateLocation1 (try (mkFloatF . realToFrac <$> float) <?> "float")
2018-04-07 21:02:50 +02:00
nixBool :: Parser NExprLoc
nixBool = annotateLocation1 (try (bool "true" True <|>
bool "false" False) <?> "bool") where
2018-04-10 18:45:04 +02:00
bool str b = mkBoolF b <$ lexeme (string str <* notFollowedBy (satisfy pathChar))
2018-04-07 21:02:50 +02:00
nixNull :: Parser NExprLoc
nixNull = annotateLocation1
2018-04-10 18:45:04 +02:00
(mkNullF <$ try (lexeme (string "null" <* notFollowedBy (satisfy pathChar)))
<?> "null")
2018-04-07 21:02:50 +02:00
nixParens :: Parser NExprLoc
nixParens = parens nixToplevelForm <?> "parens"
2018-04-07 21:02:50 +02:00
nixList :: Parser NExprLoc
nixList = annotateLocation1 (brackets (NList <$> many nixTerm) <?> "list")
2018-04-07 21:02:50 +02:00
2018-04-10 18:45:04 +02:00
pathChar :: Char -> Bool
pathChar x = isAlpha x || isDigit x || x == '.' || x == '_' || x == '-' || x == '+'
2018-04-07 21:02:50 +02:00
slash :: Parser Char
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
nixSPath = annotateLocation1
2018-04-10 18:45:04 +02:00
(mkPathF True <$> try (char '<' *> many (satisfy pathChar <|> slash) <* symbol ">")
<?> "spath")
2018-04-07 21:02:50 +02:00
pathStr :: Parser FilePath
2018-04-10 18:45:04 +02:00
pathStr = lexeme $ liftM2 (++) (many (satisfy pathChar))
(Prelude.concat <$> some (liftM2 (:) slash (some (satisfy pathChar))))
2018-04-07 21:02:50 +02:00
nixPath :: Parser NExprLoc
2018-04-10 09:40:11 +02:00
nixPath = annotateLocation1 (try (mkPathF False <$> pathStr) <?> "path")
2018-04-07 21:02:50 +02:00
nixLet :: Parser NExprLoc
nixLet = annotateLocation1 (reserved "let"
2018-04-07 21:02:50 +02:00
*> (letBody <|> letBinders)
<?> "let block")
2018-04-07 21:02:50 +02:00
where
letBinders = NLet
<$> nixBinders
<*> (reserved "in" *> nixToplevelForm)
2018-04-07 21:02:50 +02:00
-- Let expressions `let {..., body = ...}' are just desugared
-- into `(rec {..., body = ...}).body'.
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
nixIf = annotateLocation1 (NIf
2018-04-07 21:02:50 +02:00
<$> (reserved "if" *> nixExprLoc)
<*> (reserved "then" *> nixToplevelForm)
<*> (reserved "else" *> nixToplevelForm)
<?> "if")
2018-04-07 21:02:50 +02:00
nixAssert :: Parser NExprLoc
nixAssert = annotateLocation1 (NAssert
2018-04-07 21:02:50 +02:00
<$> (reserved "assert" *> nixExprLoc)
<*> (semi *> nixToplevelForm)
<?> "assert")
2018-04-07 21:02:50 +02:00
nixWith :: Parser NExprLoc
nixWith = annotateLocation1 (NWith
<$> (reserved "with" *> nixToplevelForm)
<*> (semi *> nixToplevelForm)
<?> "with")
2018-04-07 21:02:50 +02:00
nixLambda :: Parser NExprLoc
nixLambda = nAbs <$> annotateLocation (try argExpr) <*> nixToplevelForm
2018-04-07 21:02:50 +02:00
nixStringExpr :: Parser NExprLoc
nixStringExpr = nStr <$> annotateLocation nixString
nixUri :: Parser NExprLoc
2018-04-10 17:34:21 +02:00
nixUri = annotateLocation1 $ lexeme $ try $ do
start <- letterChar
protocol <- many $ satisfy $ \x ->
isAlpha x || isDigit x || x `elem` ("+-." :: String)
_ <- string ":"
address <- some $ satisfy $ \x ->
isAlpha x || isDigit x || x `elem` ("%/?:@&=+$,-_.!~*'" :: String)
return $ mkUriF $ pack $ start : protocol ++ ':' : address
2018-04-07 21:02:50 +02:00
nixString :: Parser (NString NExprLoc)
nixString = lexeme (doubleQuoted <|> indented <?> "string")
2018-04-07 21:02:50 +02:00
where
doubleQuoted :: Parser (NString NExprLoc)
doubleQuoted = DoubleQuoted . removePlainEmpty . mergePlain
<$> (doubleQ *> many (stringChar doubleQ (void $ char '\\') doubleEscape)
<* doubleQ)
2018-04-07 21:02:50 +02:00
<?> "double quoted string"
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)
<* indentedQ)
2018-04-07 21:02:50 +02:00
<?> "indented string"
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 '$'))
stringChar end escStart esc = esc
<|> Antiquoted <$> (antiStart *> nixToplevelForm <* char '}')
<|> 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)
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.
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
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
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.
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.
pair <- liftA2 (,) identifier (optional $ question *> nixToplevelForm)
2018-04-07 21:02:50 +02:00
-- Either return this, or attempt to get a comma and restart.
option (acc ++ [pair], False) $ comma >> go (acc ++ [pair])
2018-04-07 21:02:50 +02:00
nixBinders :: Parser [Binding NExprLoc]
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)
<*> (equals *> nixToplevelForm)
2018-04-07 21:02:50 +02:00
<?> "variable binding"
scope = parens nixToplevelForm <?> "inherit scope"
2018-04-07 21:02:50 +02:00
keyName :: Parser (NKeyName NExprLoc)
2018-04-10 18:44:52 +02:00
keyName = dynamicKey <|> staticKey where
2018-04-07 21:02:50 +02:00
staticKey = do
beg <- getPosition
2018-04-07 21:02:50 +02:00
StaticKey <$> identifier <*> pure (Just beg)
dynamicKey = DynamicKey <$> nixAntiquoted nixString
nixSet :: Parser NExprLoc
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 $ stripAnnotation <$> (whiteSpace *> nixToplevelForm <* eof)
2018-04-07 21:02:50 +02:00
parseNixFileLoc :: MonadIO m => FilePath -> m (Result NExprLoc)
parseNixFileLoc = parseFromFileEx (whiteSpace *> nixToplevelForm <* eof)
2018-04-07 21:02:50 +02:00
parseNixText :: Text -> Result NExpr
parseNixText =
parseFromText $ stripAnnotation <$> (whiteSpace *> nixToplevelForm <* eof)
2018-04-07 21:02:50 +02:00
parseNixTextLoc :: Text -> Result NExprLoc
parseNixTextLoc = parseFromText (whiteSpace *> nixToplevelForm <* eof)