add string context
This commit is contained in:
parent
34d641a2fe
commit
8e7d9d32fa
|
@ -41,6 +41,7 @@ import Data.Coerce
|
|||
import Data.Fix
|
||||
import Data.Foldable (foldrM)
|
||||
import qualified Data.HashMap.Lazy as M
|
||||
import qualified Data.HashSet as HS
|
||||
import Data.List
|
||||
import Data.Maybe
|
||||
import Data.Semigroup
|
||||
|
@ -250,7 +251,7 @@ nixPath = fmap nvList $ flip foldNixPath [] $ \p mn rest ->
|
|||
|
||||
toString :: MonadNix e m => m (NValue m) -> m (NValue m)
|
||||
toString str =
|
||||
str >>= normalForm >>= valueText False >>= toNix @(Text, DList Text)
|
||||
str >>= normalForm >>= valueText False >>= toNix @NixString
|
||||
|
||||
hasAttr :: forall e m. MonadNix e m => m (NValue m) -> m (NValue m) -> m (NValue m)
|
||||
hasAttr x y =
|
||||
|
@ -266,7 +267,7 @@ attrsetGet k s = case M.lookup k s of
|
|||
|
||||
hasContext :: MonadNix e m => m (NValue m) -> m (NValue m)
|
||||
hasContext =
|
||||
toNix . not . null . (appEndo ?? []) . snd <=< fromValue @(Text, DList Text)
|
||||
toNix . not . HS.null . nsContext <=< fromValue
|
||||
|
||||
getAttr :: MonadNix e m => m (NValue m) -> m (NValue m) -> m (NValue m)
|
||||
getAttr x y = x >>= \x' -> y >>= \y' -> case (x', y') of
|
||||
|
|
|
@ -148,7 +148,7 @@ instance (Convertible e m, MonadEffects m)
|
|||
=> FromValue Text m (NValueNF m) where
|
||||
fromValueMay = \case
|
||||
Fix (NVConstantF (NUri u)) -> pure $ Just u
|
||||
Fix (NVStrF t _) -> pure $ Just t
|
||||
Fix (NVStrF (NixString t _)) -> pure $ Just t
|
||||
Fix (NVPathF p) -> Just . Text.pack . unStorePath <$> addPath p
|
||||
Fix (NVSetF s _) -> case M.lookup "outPath" s of
|
||||
Nothing -> pure Nothing
|
||||
|
@ -173,28 +173,28 @@ instance (Convertible e m, MonadThunk (NValue m) (NThunk m) m, MonadEffects m)
|
|||
_ -> throwError $ Expectation TString v
|
||||
|
||||
instance (Convertible e m, MonadEffects m)
|
||||
=> FromValue (Text, DList Text) m (NValueNF m) where
|
||||
=> FromValue NixString m (NValueNF m) where
|
||||
fromValueMay = \case
|
||||
Fix (NVConstantF (NUri u)) -> pure $ Just (u, mempty)
|
||||
Fix (NVStrF t d) -> pure $ Just (t, d)
|
||||
Fix (NVPathF p) -> Just . (,mempty) . Text.pack . unStorePath <$> addPath p
|
||||
Fix (NVConstantF (NUri u)) -> pure $ Just (NixString u mempty)
|
||||
Fix (NVStrF (NixString t d)) -> pure $ Just (NixString t d)
|
||||
Fix (NVPathF p) -> Just . (flip NixString mempty) . Text.pack . unStorePath <$> addPath p
|
||||
Fix (NVSetF s _) -> case M.lookup "outPath" s of
|
||||
Nothing -> pure Nothing
|
||||
Just p -> fmap (,mempty) <$> fromValueMay @Text p
|
||||
Just p -> fmap (flip NixString mempty) <$> fromValueMay @Text p
|
||||
_ -> pure Nothing
|
||||
fromValue v = fromValueMay v >>= \case
|
||||
Just b -> pure b
|
||||
_ -> throwError $ ExpectationNF TString v
|
||||
|
||||
instance (Convertible e m, MonadThunk (NValue m) (NThunk m) m, MonadEffects m)
|
||||
=> FromValue (Text, DList Text) m (NValue m) where
|
||||
=> FromValue NixString m (NValue m) where
|
||||
fromValueMay = \case
|
||||
NVConstant (NUri u) -> pure $ Just (u, mempty)
|
||||
NVStr t d -> pure $ Just (t, d)
|
||||
NVPath p -> Just . (,mempty) . Text.pack . unStorePath <$> addPath p
|
||||
NVConstant (NUri u) -> pure $ Just (NixString u mempty)
|
||||
NVStr t d -> pure $ Just (NixString t d)
|
||||
NVPath p -> Just . (flip NixString mempty) . Text.pack . unStorePath <$> addPath p
|
||||
NVSet s _ -> case M.lookup "outPath" s of
|
||||
Nothing -> pure Nothing
|
||||
Just p -> fmap (,mempty) <$> fromValueMay @Text p
|
||||
Just p -> fmap (flip NixString mempty) <$> fromValueMay @Text p
|
||||
_ -> pure Nothing
|
||||
fromValue v = fromValueMay v >>= \case
|
||||
Just b -> pure b
|
||||
|
@ -203,7 +203,7 @@ instance (Convertible e m, MonadThunk (NValue m) (NThunk m) m, MonadEffects m)
|
|||
instance Convertible e m
|
||||
=> FromValue ByteString m (NValueNF m) where
|
||||
fromValueMay = \case
|
||||
Fix (NVStrF t _) -> pure $ Just (encodeUtf8 t)
|
||||
Fix (NVStrF (NixString t _)) -> pure $ Just (encodeUtf8 t)
|
||||
_ -> pure Nothing
|
||||
fromValue v = fromValueMay v >>= \case
|
||||
Just b -> pure b
|
||||
|
@ -225,7 +225,7 @@ instance Convertible e m => FromValue Path m (NValueNF m) where
|
|||
fromValueMay = \case
|
||||
Fix (NVConstantF (NUri u)) -> pure $ Just (Path (Text.unpack u))
|
||||
Fix (NVPathF p) -> pure $ Just (Path p)
|
||||
Fix (NVStrF s _) -> pure $ Just (Path (Text.unpack s))
|
||||
Fix (NVStrF (NixString s _)) -> pure $ Just (Path (Text.unpack s))
|
||||
Fix (NVSetF s _) -> case M.lookup "outPath" s of
|
||||
Nothing -> pure Nothing
|
||||
Just p -> fromValueMay @Path p
|
||||
|
@ -328,7 +328,7 @@ instance (Convertible e m, MonadEffects m)
|
|||
NBool b -> toJSON b
|
||||
NNull -> A.Null
|
||||
NUri u -> toJSON u
|
||||
Fix (NVStrF s _) -> pure $ Just $ toJSON s
|
||||
Fix (NVStrF (NixString s _)) -> pure $ Just $ toJSON s
|
||||
Fix (NVListF l) -> fmap (A.Array . V.fromList) . sequence
|
||||
<$> traverse fromValueMay l
|
||||
Fix (NVSetF m _) -> fmap A.Object . sequence <$> traverse fromValueMay m
|
||||
|
@ -373,19 +373,19 @@ instance Applicative m => ToValue Float m (NValue m) where
|
|||
toValue = pure . nvConstant . NFloat
|
||||
|
||||
instance Applicative m => ToValue Text m (NValueNF m) where
|
||||
toValue = pure . Fix . flip NVStrF mempty
|
||||
toValue = pure . Fix . NVStrF . flip NixString mempty
|
||||
|
||||
instance Applicative m => ToValue Text m (NValue m) where
|
||||
toValue = pure . flip nvStr mempty
|
||||
|
||||
instance Applicative m => ToValue (Text, DList Text) m (NValueNF m) where
|
||||
toValue = pure . Fix . uncurry NVStrF
|
||||
instance Applicative m => ToValue NixString m (NValueNF m) where
|
||||
toValue = pure . Fix . NVStrF
|
||||
|
||||
instance Applicative m => ToValue (Text, DList Text) m (NValue m) where
|
||||
toValue = pure . uncurry nvStr
|
||||
instance Applicative m => ToValue NixString m (NValue m) where
|
||||
toValue = pure . nvStrNS
|
||||
|
||||
instance Applicative m => ToValue ByteString m (NValueNF m) where
|
||||
toValue = pure . Fix . flip NVStrF mempty . decodeUtf8
|
||||
toValue = pure . Fix . NVStrF . flip NixString mempty . decodeUtf8
|
||||
|
||||
instance Applicative m => ToValue ByteString m (NValue m) where
|
||||
toValue = pure . flip nvStr mempty . decodeUtf8
|
||||
|
@ -501,8 +501,8 @@ instance Convertible e m => FromNix Float m (NValueNF m) where
|
|||
instance Convertible e m => FromNix Float m (NValue m) where
|
||||
instance (Convertible e m, MonadEffects m) => FromNix Text m (NValueNF m) where
|
||||
instance (Convertible e m, MonadEffects m, MonadThunk (NValue m) (NThunk m) m) => FromNix Text m (NValue m) where
|
||||
instance (Convertible e m, MonadEffects m) => FromNix (Text, DList Text) m (NValueNF m) where
|
||||
instance (Convertible e m, MonadEffects m, MonadThunk (NValue m) (NThunk m) m) => FromNix (Text, DList Text) m (NValue m) where
|
||||
instance (Convertible e m, MonadEffects m) => FromNix NixString m (NValueNF m) where
|
||||
instance (Convertible e m, MonadEffects m, MonadThunk (NValue m) (NThunk m) m) => FromNix NixString m (NValue m) where
|
||||
instance Convertible e m => FromNix ByteString m (NValueNF m) where
|
||||
instance Convertible e m => FromNix ByteString m (NValue m) where
|
||||
instance Convertible e m => FromNix Path m (NValueNF m) where
|
||||
|
@ -563,8 +563,8 @@ instance Applicative m => ToNix Float m (NValueNF m) where
|
|||
instance Applicative m => ToNix Float m (NValue m) where
|
||||
instance Applicative m => ToNix Text m (NValueNF m) where
|
||||
instance Applicative m => ToNix Text m (NValue m) where
|
||||
instance Applicative m => ToNix (Text, DList Text) m (NValueNF m) where
|
||||
instance Applicative m => ToNix (Text, DList Text) m (NValue m) where
|
||||
instance Applicative m => ToNix NixString m (NValueNF m) where
|
||||
instance Applicative m => ToNix NixString m (NValue m) where
|
||||
instance Applicative m => ToNix ByteString m (NValueNF m) where
|
||||
instance Applicative m => ToNix ByteString m (NValue m) where
|
||||
instance Applicative m => ToNix Path m (NValueNF m) where
|
||||
|
|
|
@ -44,6 +44,7 @@ import Nix.Scope
|
|||
import Nix.Strings (runAntiquoted)
|
||||
import Nix.Thunk
|
||||
import Nix.Utils
|
||||
import Nix.Value (NixString(..))
|
||||
|
||||
class (Show v, Monad m) => MonadEval v m | v -> m where
|
||||
freeVariable :: Text -> m v
|
||||
|
@ -52,7 +53,7 @@ class (Show v, Monad m) => MonadEval v m | v -> m where
|
|||
|
||||
evalCurPos :: m v
|
||||
evalConstant :: NAtom -> m v
|
||||
evalString :: NString (m v) -> m v
|
||||
evalString :: NixString -> m v
|
||||
evalLiteralPath :: FilePath -> m v
|
||||
evalEnvPath :: FilePath -> m v
|
||||
evalUnary :: NUnaryOp -> v -> m v
|
||||
|
@ -92,7 +93,7 @@ type MonadNixEval e v t m =
|
|||
MonadFix m,
|
||||
ToValue Bool m v,
|
||||
ToValue [t] m v,
|
||||
FromValue (Text, DList Text) m v,
|
||||
FromValue NixString m v,
|
||||
ToValue (AttrSet t, AttrSet SourcePos) m v,
|
||||
FromValue (AttrSet t, AttrSet SourcePos) m v)
|
||||
|
||||
|
@ -111,7 +112,9 @@ eval (NSym var) =
|
|||
lookupVar var >>= maybe (freeVariable var) (force ?? evaledSym var)
|
||||
|
||||
eval (NConstant x) = evalConstant x
|
||||
eval (NStr str) = evalString str
|
||||
eval (NStr str) = assembleString str >>= \case
|
||||
Nothing -> evalError @v $ ErrorCall ("failed to evaluate string" :: String)
|
||||
Just e -> evalString e
|
||||
eval (NLiteralPath p) = evalLiteralPath p
|
||||
eval (NEnvPath p) = evalEnvPath p
|
||||
eval (NUnary op arg) = evalUnary op =<< arg
|
||||
|
@ -331,14 +334,14 @@ evalSelect aset attr = do
|
|||
Nothing ->
|
||||
return $ Left (x, path)
|
||||
|
||||
evalSelector :: (MonadEval v m, FromValue (Text, DList Text) m v)
|
||||
evalSelector :: (MonadEval v m, FromValue NixString m v)
|
||||
=> Bool -> NAttrPath (m v) -> m (NonEmpty Text)
|
||||
evalSelector allowDynamic binds =
|
||||
NE.map fst <$> traverse (evalGetterKeyName allowDynamic) binds
|
||||
|
||||
-- | Evaluate a component of an attribute path in a context where we are
|
||||
-- *retrieving* a value
|
||||
evalGetterKeyName :: (MonadEval v m, FromValue (Text, DList Text) m v)
|
||||
evalGetterKeyName :: (MonadEval v m, FromValue NixString m v)
|
||||
=> Bool -> NKeyName (m v) -> m (Text, Maybe SourcePos)
|
||||
evalGetterKeyName canBeDynamic
|
||||
| canBeDynamic = evalKeyNameDynamicNotNull
|
||||
|
@ -352,7 +355,7 @@ evalKeyNameStatic = \case
|
|||
evalError @v $ ErrorCall "dynamic attribute not allowed in this context"
|
||||
|
||||
evalKeyNameDynamicNotNull
|
||||
:: forall v m. (MonadEval v m, FromValue (Text, DList Text) m v)
|
||||
:: forall v m. (MonadEval v m, FromValue NixString m v)
|
||||
=> NKeyName (m v) -> m (Text, Maybe SourcePos)
|
||||
evalKeyNameDynamicNotNull = evalKeyNameDynamicNullable >=> \case
|
||||
(Nothing, _) ->
|
||||
|
@ -361,7 +364,7 @@ evalKeyNameDynamicNotNull = evalKeyNameDynamicNullable >=> \case
|
|||
|
||||
-- | Evaluate a component of an attribute path in a context where we are
|
||||
-- *binding* a value
|
||||
evalSetterKeyName :: (MonadEval v m, FromValue (Text, DList Text) m v)
|
||||
evalSetterKeyName :: (MonadEval v m, FromValue NixString m v)
|
||||
=> Bool -> NKeyName (m v) -> m (Maybe Text, Maybe SourcePos)
|
||||
evalSetterKeyName canBeDynamic
|
||||
| canBeDynamic = evalKeyNameDynamicNullable
|
||||
|
@ -369,23 +372,23 @@ evalSetterKeyName canBeDynamic
|
|||
|
||||
-- | Returns Nothing iff the key value is null
|
||||
evalKeyNameDynamicNullable
|
||||
:: forall v m. (MonadEval v m, FromValue (Text, DList Text) m v)
|
||||
:: forall v m. (MonadEval v m, FromValue NixString m v)
|
||||
=> NKeyName (m v)
|
||||
-> m (Maybe Text, Maybe SourcePos)
|
||||
evalKeyNameDynamicNullable = \case
|
||||
StaticKey k p -> pure (Just k, p)
|
||||
DynamicKey k ->
|
||||
runAntiquoted "\n" assembleString (>>= fromValueMay) k
|
||||
<&> \case Just (t, _) -> (Just t, Nothing)
|
||||
<&> \case Just (NixString t _) -> (Just t, Nothing)
|
||||
_ -> (Nothing, Nothing)
|
||||
|
||||
assembleString :: forall v m. (MonadEval v m, FromValue (Text, DList Text) m v)
|
||||
=> NString (m v) -> m (Maybe (Text, DList Text))
|
||||
assembleString :: forall v m. (MonadEval v m, FromValue NixString m v)
|
||||
=> NString (m v) -> m (Maybe NixString)
|
||||
assembleString = \case
|
||||
Indented _ parts -> fromParts parts
|
||||
DoubleQuoted parts -> fromParts parts
|
||||
where
|
||||
go = runAntiquoted "\n" (pure . Just . (, mempty)) (>>= fromValueMay)
|
||||
go = runAntiquoted "\n" (pure . Just . (flip NixString mempty)) (>>= fromValueMay)
|
||||
|
||||
fromParts parts = fmap mconcat . sequence <$> mapM go parts
|
||||
|
||||
|
|
|
@ -178,13 +178,11 @@ instance MonadNix e m => MonadEval (NValue m) m where
|
|||
span <- currentPos
|
||||
pure $ nvConstantP (Provenance scope (NConstant_ span c)) c
|
||||
|
||||
evalString = assembleString >=> \case
|
||||
Just (s, c) -> do
|
||||
scope <- currentScopes
|
||||
span <- currentPos
|
||||
pure $ nvStrP (Provenance scope
|
||||
(NStr_ span (DoubleQuoted [Plain s]))) s c
|
||||
Nothing -> nverr $ ErrorCall $ "Failed to assemble string"
|
||||
evalString ns@(NixString s _) = do
|
||||
scope <- currentScopes
|
||||
span <- currentPos
|
||||
pure $ nvStrPNS (Provenance scope
|
||||
(NStr_ span (DoubleQuoted [Plain s]))) ns
|
||||
|
||||
evalLiteralPath p = do
|
||||
scope <- currentScopes
|
||||
|
|
|
@ -49,6 +49,7 @@ import Nix.Options
|
|||
import Nix.Scope
|
||||
import Nix.Thunk
|
||||
import Nix.Utils
|
||||
import Nix.Value (NixString(..))
|
||||
|
||||
data TAtom
|
||||
= TInt
|
||||
|
@ -239,7 +240,7 @@ instance ToValue Bool m (Symbolic m) where
|
|||
|
||||
instance ToValue [SThunk m] m (Symbolic m) where
|
||||
|
||||
instance FromValue (Text, DList Text) m (Symbolic m) where
|
||||
instance FromValue NixString m (Symbolic m) where
|
||||
|
||||
instance FromValue (AttrSet (SThunk m), AttrSet SourcePos) m (Symbolic m) where
|
||||
|
||||
|
|
|
@ -39,7 +39,7 @@ normalFormBy k n v = do
|
|||
when (n > 2000) $ throwError $ NormalLoop v
|
||||
case v of
|
||||
NVConstant a -> return $ Fix $ NVConstantF a
|
||||
NVStr t s -> return $ Fix $ NVStrF t s
|
||||
NVStr t s -> return $ Fix $ NVStrF $ NixString t s
|
||||
NVList l ->
|
||||
fmap (Fix . NVListF) $ forM (zip [0..] l) $ \(i :: Int, t) -> do
|
||||
traceM $ replicate n ' ' ++ "normalFormBy: List[" ++ show i ++ "]"
|
||||
|
@ -62,7 +62,7 @@ embed :: forall m. (MonadThunk (NValue m) (NThunk m) m)
|
|||
=> NValueNF m -> m (NValue m)
|
||||
embed (Fix x) = case x of
|
||||
NVConstantF a -> return $ nvConstant a
|
||||
NVStrF t s -> return $ nvStr t s
|
||||
NVStrF (NixString t s) -> return $ nvStr t s
|
||||
NVListF l -> nvList . fmap (value @_ @_ @m)
|
||||
<$> traverse embed l
|
||||
NVSetF s p -> flip nvSet p . fmap (value @_ @_ @m)
|
||||
|
@ -72,12 +72,12 @@ embed (Fix x) = case x of
|
|||
NVBuiltinF name f -> return $ nvBuiltin name f
|
||||
|
||||
valueText :: forall e m. (Framed e m, MonadEffects m, Typeable m)
|
||||
=> Bool -> NValueNF m -> m (Text, DList Text)
|
||||
=> Bool -> NValueNF m -> m NixString
|
||||
valueText addPathsToStore = cata phi
|
||||
where
|
||||
phi :: NValueF m (m (Text, DList Text)) -> m (Text, DList Text)
|
||||
phi (NVConstantF a) = pure (atomText a, mempty)
|
||||
phi (NVStrF t c) = pure (t, c)
|
||||
--phi :: () -- NValueF m (m ns) -> m ns
|
||||
phi (NVConstantF a) = pure (NixString (atomText a) mempty)
|
||||
phi (NVStrF ns) = pure ns
|
||||
phi v@(NVListF _) = coercionFailed v
|
||||
phi v@(NVSetF s _)
|
||||
| Just asString <- M.lookup "__asString" s = asString
|
||||
|
@ -86,8 +86,8 @@ valueText addPathsToStore = cata phi
|
|||
phi (NVPathF originalPath)
|
||||
| addPathsToStore = do
|
||||
storePath <- addPath originalPath
|
||||
pure (Text.pack $ unStorePath storePath, mempty)
|
||||
| otherwise = pure (Text.pack originalPath, mempty)
|
||||
pure (NixString (Text.pack $ unStorePath storePath) mempty)
|
||||
| otherwise = pure (NixString (Text.pack originalPath) mempty)
|
||||
phi v@(NVBuiltinF _ _) = coercionFailed v
|
||||
|
||||
coercionFailed v =
|
||||
|
@ -95,4 +95,4 @@ valueText addPathsToStore = cata phi
|
|||
|
||||
valueTextNoContext :: (Framed e m, MonadEffects m, Typeable m)
|
||||
=> Bool -> NValueNF m -> m Text
|
||||
valueTextNoContext addPathsToStore = fmap fst . valueText addPathsToStore
|
||||
valueTextNoContext addPathsToStore = fmap nsContents . valueText addPathsToStore
|
||||
|
|
|
@ -224,7 +224,7 @@ prettyNValueNF = prettyNix . valueToExpr
|
|||
valueToExpr = transport go
|
||||
|
||||
go (NVConstantF a) = NConstant a
|
||||
go (NVStrF t _) = NStr (DoubleQuoted [Plain t])
|
||||
go (NVStrF (NixString t _)) = NStr (DoubleQuoted [Plain t])
|
||||
go (NVListF l) = NList l
|
||||
go (NVSetF s p) = NSet
|
||||
[ NamedVar (StaticKey k (M.lookup k p) :| []) v
|
||||
|
@ -237,7 +237,7 @@ printNix :: Functor m => NValueNF m -> String
|
|||
printNix = cata phi
|
||||
where phi :: NValueF m String -> String
|
||||
phi (NVConstantF a) = unpack $ atomText a
|
||||
phi (NVStrF t _) = show t
|
||||
phi (NVStrF (NixString t _)) = show t
|
||||
phi (NVListF l) = "[ " ++ unwords l ++ " ]"
|
||||
phi (NVSetF s _) =
|
||||
"{ " ++ concat [ unpack k ++ " = " ++ v ++ "; "
|
||||
|
@ -250,7 +250,7 @@ removeEffects :: Functor m => NValueF m (NThunk m) -> NValueNF m
|
|||
removeEffects = Fix . fmap dethunk
|
||||
where
|
||||
dethunk (NThunk _ (Value v)) = removeEffects (baseValue v)
|
||||
dethunk (NThunk _ _) = Fix $ NVStrF "<thunk>" mempty
|
||||
dethunk (NThunk _ _) = Fix $ NVStrF (NixString "<thunk>" mempty)
|
||||
|
||||
removeEffectsM :: MonadVar m => NValueF m (NThunk m) -> m (NValueNF m)
|
||||
removeEffectsM = fmap Fix . traverse dethunk
|
||||
|
@ -282,9 +282,9 @@ dethunk = \case
|
|||
NThunk _ (Thunk _ active ref) -> do
|
||||
nowActive <- atomicModifyVar active (True,)
|
||||
if nowActive
|
||||
then pure $ Fix $ NVStrF "<thunk>" mempty
|
||||
then pure $ Fix $ NVStrF (NixString "<thunk>" mempty)
|
||||
else do
|
||||
eres <- readVar ref
|
||||
case eres of
|
||||
Computed v -> removeEffectsM (baseValue v)
|
||||
_ -> pure $ Fix $ NVStrF "<thunk>" mempty
|
||||
_ -> pure $ Fix $ NVStrF (NixString "<thunk>" mempty)
|
||||
|
|
|
@ -22,6 +22,7 @@ module Nix.Type.Infer (
|
|||
inferTop
|
||||
) where
|
||||
|
||||
import Nix.Value (NixString)
|
||||
import Control.Applicative
|
||||
import Control.Arrow
|
||||
import Control.Monad.Catch
|
||||
|
@ -444,7 +445,7 @@ data Judgment s = Judgment
|
|||
}
|
||||
deriving Show
|
||||
|
||||
instance FromValue (Text, DList Text) (Infer s) (Judgment s) where
|
||||
instance FromValue NixString (Infer s) (Judgment s) where
|
||||
fromValueMay _ = return Nothing
|
||||
fromValue _ = error "Unused"
|
||||
|
||||
|
|
|
@ -31,7 +31,8 @@ import qualified Data.Aeson as A
|
|||
import Data.Align
|
||||
import Data.Fix
|
||||
import qualified Data.HashMap.Lazy as M
|
||||
import Data.Monoid (appEndo)
|
||||
import qualified Data.HashSet as S
|
||||
import Data.Hashable
|
||||
import Data.Text (Text)
|
||||
import Data.These
|
||||
import Data.Typeable (Typeable)
|
||||
|
@ -43,6 +44,38 @@ import Nix.Frames
|
|||
import Nix.Scope
|
||||
import Nix.Thunk
|
||||
import Nix.Utils
|
||||
import Data.Semigroup
|
||||
|
||||
-- | A 'ContextFlavor' describes the sum of possible derivations for string contexts
|
||||
data ContextFlavor =
|
||||
DirectPath
|
||||
| DerivationOutput
|
||||
| AllDerivationOutput
|
||||
deriving (Bounded, Show, Eq, Generic)
|
||||
|
||||
instance Hashable ContextFlavor
|
||||
|
||||
-- | A 'StringContext' ...
|
||||
data StringContext =
|
||||
StringContext { scPath :: !Text
|
||||
, scFlavor :: !ContextFlavor
|
||||
} deriving (Eq, Show, Generic)
|
||||
|
||||
instance Hashable StringContext
|
||||
|
||||
data NixString = NixString
|
||||
{ nsContents :: !Text
|
||||
, nsContext :: !(S.HashSet StringContext)
|
||||
} deriving (Eq, Show, Generic)
|
||||
|
||||
instance Hashable NixString
|
||||
|
||||
instance Semigroup NixString where
|
||||
NixString s1 t1 <> NixString s2 t2 = NixString (s1 <> s2) (t1 <> t2)
|
||||
|
||||
instance Monoid NixString where
|
||||
mempty = NixString mempty mempty
|
||||
mappend = (<>)
|
||||
|
||||
-- | An 'NValue' is the most reduced form of an 'NExpr' after evaluation is
|
||||
-- completed. 's' is related to the type of errors that might occur during
|
||||
|
@ -51,7 +84,7 @@ data NValueF m r
|
|||
= NVConstantF NAtom
|
||||
-- | A string has a value and a context, which can be used to record what a
|
||||
-- string has been build from
|
||||
| NVStrF Text (DList Text)
|
||||
| NVStrF NixString
|
||||
| NVPathF FilePath
|
||||
| NVListF [r]
|
||||
| NVSetF (AttrSet r) (AttrSet SourcePos)
|
||||
|
@ -110,10 +143,13 @@ pattern NVConstant x <- NValue _ (NVConstantF x)
|
|||
nvConstant x = NValue [] (NVConstantF x)
|
||||
nvConstantP p x = NValue [p] (NVConstantF x)
|
||||
|
||||
pattern NVStr s d <- NValue _ (NVStrF s d)
|
||||
pattern NVStr s d <- NValue _ (NVStrF (NixString s d))
|
||||
|
||||
nvStr s d = NValue [] (NVStrF s d)
|
||||
nvStrP p s d = NValue [p] (NVStrF s d)
|
||||
nvStr = (nvStrNS .) . NixString
|
||||
nvStrNS ns = NValue [] (NVStrF ns)
|
||||
|
||||
nvStrP p = (nvStrPNS p .) . NixString
|
||||
nvStrPNS p ns = NValue [p] (NVStrF ns)
|
||||
|
||||
pattern NVPath x <- NValue _ (NVPathF x)
|
||||
|
||||
|
@ -143,7 +179,7 @@ nvBuiltinP p name f = NValue [p] (NVBuiltinF name f)
|
|||
instance Show (NValueF m (Fix (NValueF m))) where
|
||||
showsPrec = flip go where
|
||||
go (NVConstantF atom) = showsCon1 "NVConstant" atom
|
||||
go (NVStrF txt ctxt) = showsCon2 "NVStr" txt (appEndo ctxt [])
|
||||
go (NVStrF (NixString txt ctxt)) = showsCon2 "NVStr" txt ctxt
|
||||
go (NVListF lst) = showsCon1 "NVList" lst
|
||||
go (NVSetF attrs _) = showsCon1 "NVSet" attrs
|
||||
go (NVClosureF p _) = showsCon1 "NVClosure" p
|
||||
|
@ -239,12 +275,12 @@ isDerivation m = case M.lookup "type" m of
|
|||
valueEq :: MonadThunk (NValue m) (NThunk m) m
|
||||
=> NValue m -> NValue m -> m Bool
|
||||
valueEq l r = case (l, r) of
|
||||
(NVStr ls _, NVConstant (NUri ru)) -> pure $ ls == ru
|
||||
(NVConstant (NUri lu), NVStr rs _) -> pure $ lu == rs
|
||||
(NVStr ls ct, NVConstant (NUri ru)) -> pure (ls == ru && S.null ct)
|
||||
(NVConstant (NUri lu), NVStr rs ct) -> pure (lu == rs && S.null ct)
|
||||
(NVConstant lc, NVConstant rc) -> pure $ lc == rc
|
||||
(NVStr ls _, NVStr rs _) -> pure $ ls == rs
|
||||
(NVStr ls _, NVConstant NNull) -> pure $ ls == ""
|
||||
(NVConstant NNull, NVStr rs _) -> pure $ "" == rs
|
||||
(NVStr ls lc, NVStr rs rc) -> pure (ls == rs && lc == rc)
|
||||
(NVStr ls ct, NVConstant NNull) -> pure (ls == "" && S.null ct)
|
||||
(NVConstant NNull, NVStr rs ct) -> pure ("" == rs && S.null ct)
|
||||
(NVList ls, NVList rs) -> alignEqM thunkEq ls rs
|
||||
(NVSet lm _, NVSet rm _) -> do
|
||||
let compareAttrs = alignEqM thunkEq lm rm
|
||||
|
|
|
@ -26,7 +26,7 @@ toXML = (.) ((++ "\n") .
|
|||
NNull -> Element (unqual "null") [] [] Nothing
|
||||
NUri u -> mkElem "uri" "value" (Text.unpack u)
|
||||
|
||||
NVStrF t _ -> mkElem "string" "value" (Text.unpack t)
|
||||
NVStrF (NixString t _) -> mkElem "string" "value" (Text.unpack t)
|
||||
NVListF l -> Element (unqual "list") [] (Elem <$> l) Nothing
|
||||
|
||||
NVSetF s _ -> Element (unqual "attrs") []
|
||||
|
|
|
@ -165,7 +165,7 @@ genEvalCompareTests = do
|
|||
|
||||
instance (Show r, Show (NValueF m r), Eq r) => Eq (NValueF m r) where
|
||||
NVConstantF x == NVConstantF y = x == y
|
||||
NVStrF x _ == NVStrF y _ = x == y
|
||||
NVStrF (NixString x1 y1) == NVStrF (NixString x2 y2) = x1 == x2 && y1 == y2
|
||||
NVListF x == NVListF y = and (zipWith (==) x y)
|
||||
NVSetF x _ == NVSetF y _ =
|
||||
M.keys x == M.keys y &&
|
||||
|
|
Loading…
Reference in a new issue