{-# LANGUAGE LambdaCase #-}-- | Attribute-path selection, the machinery behind @build -A@.---- An attribute path is a dotted sequence of names selected left to right-- from an evaluated value: @stdenv.mkDerivation@ takes @mkDerivation@ out-- of @stdenv@. A component may be double-quoted to carry a literal dot,-- as in @foo.\"bar.baz\"@.---- Upstream tokenizes the same way (@parseAttrPath@,-- src\/libexpr\/attr-path.cc), including three behaviours that read as-- accidents but are load-bearing for compatibility: quotes concatenate-- rather than delimit, so @foo\"bar\"@ is the single name @foobar@; the-- final component is pushed only when non-empty, so a trailing dot and a-- lone @\"\"@ both vanish instead of becoming a component; and an interior-- empty component (a leading or doubled dot) survives tokenizing to be-- rejected during selection, after the type of the value it would index.---- Two upstream behaviours are deliberately absent. A numeric component-- indexes a list upstream (@-A foo.3.bar@); here it is an ordinary name,-- because nothing this selects over is a list and upstream's-- integer-or-name test defers to a C++ numeric parse whose accepted-- spellings (a leading @+@, leading zeros) have not been checked against a-- running Nix. And a near-miss attribute name gets no @Did you mean@-- line, which upstream renders from a Levenshtein search this evaluator-- has no suggestion channel to carry.---- The four messages otherwise match upstream byte for byte, diffed against-- @nix-instantiate@ 2.33.2.module Nix.Eval.AttrPath ( parseAttrPath, selectAttrPath, )whereimport Data.Text (Text)import qualified Data.Text as Timport Nix.Eval (MonadEval, NixValue (..), attrSetLookup, force, typeName)-- ----------------------------------------------------------------------------- Tokenizing-- ----------------------------------------------------------------------------- | Component separator.attrSeparator :: CharattrSeparator = '.'-- | Opens and closes a component that may contain 'attrSeparator'.quoteChar :: CharquoteChar = '"'quoteMark :: TextquoteMark = T.singleton quoteCharisDelimiter :: Char -> BoolisDelimiter c = c == attrSeparator || c == quoteChar-- | Split an attribute path into its components.---- @Left@ carries a message ready for the user; the only way to get one is-- a quote that is never closed. An empty component is returned rather-- than rejected, because upstream reports it against the value being-- indexed and so cannot decide it here.parseAttrPath :: Text -> Either Text [Text]parseAttrPath path = go path T.empty [] where go rest !current acc = let (plain, delimited) = T.break isDelimiter rest taken = current <> plain in case T.uncons delimited of Nothing | T.null taken -> Right (reverse acc) | otherwise -> Right (reverse (taken : acc)) Just (c, more) | c == attrSeparator -> go more T.empty (taken : acc) | otherwise -> let (quotedRun, closing) = T.breakOn quoteMark more in case T.stripPrefix quoteMark closing of Nothing -> Left (missingQuoteMessage path) Just after -> go after (taken <> quotedRun) acc-- ----------------------------------------------------------------------------- Selecting-- ----------------------------------------------------------------------------- | Follow an attribute path into an evaluated value, forcing each step.-- Before each component, the value it indexes is passed through @call@,-- as upstream's @findAlongAttrPath@ calls it with the command line's-- arguments (attr-path.cc, "Nix.Eval.AutoCall"); a caller with nothing to-- call passes 'pure'.---- Only the components named are forced: selecting one attribute leaves its-- siblings as thunks, so naming one package does not evaluate the rest of-- a package set.selectAttrPath :: (MonadEval m) => (NixValue -> m NixValue) -> Text -> NixValue -> m (Either Text NixValue)selectAttrPath call path root = case parseAttrPath path of Left err -> pure (Left err) Right names -> walk names root where walk [] val = pure (Right val) walk (name : rest) val = call val >>= select name rest select name rest val = case val of VAttrs attrs | T.null name -> pure (Left (emptyNameMessage path)) | otherwise -> attrSetLookup name attrs >>= \case Nothing -> pure (Left (notFoundMessage name path)) Just thunk -> force thunk >>= walk rest -- Ordered as upstream orders it: the type of what is being indexed -- is reported before an empty component is complained about. _ -> pure (Left (notASetMessage path (typeName val)))-- ----------------------------------------------------------------------------- Messages-- ---------------------------------------------------------------------------missingQuoteMessage :: Text -> TextmissingQuoteMessage path = "missing closing quote in selection path '" <> path <> "'"emptyNameMessage :: Text -> TextemptyNameMessage path = "empty attribute name in selection path '" <> path <> "'"notFoundMessage :: Text -> Text -> TextnotFoundMessage name path = "attribute '" <> name <> "' in selection path '" <> path <> "' not found"notASetMessage :: Text -> Text -> TextnotASetMessage path actual = "the expression selected by the selection path '" <> path <> "' should be a set but is " <> actual