hnix/src/Nix/Scope.hs

94 lines
3 KiB
Haskell
Raw Normal View History

2018-04-07 21:02:50 +02:00
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DeriveFoldable #-}
2018-11-17 22:21:03 +01:00
{-# LANGUAGE DeriveFunctor #-}
2018-04-07 21:02:50 +02:00
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE FlexibleContexts #-}
2018-11-17 22:21:03 +01:00
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2018-11-17 22:21:03 +01:00
{-# LANGUAGE MultiParamTypeClasses #-}
2018-04-07 21:02:50 +02:00
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module Nix.Scope where
import Control.Applicative
import Control.Monad.Reader
import qualified Data.HashMap.Lazy as M
import Data.Text (Text)
import Lens.Family2
2018-04-07 21:02:50 +02:00
import Nix.Utils
newtype Scope a = Scope { getScope :: AttrSet a }
deriving (Functor, Foldable, Traversable, Eq)
2018-04-07 21:02:50 +02:00
instance Show (Scope a) where
2018-04-07 21:02:50 +02:00
show (Scope m) = show (M.keys m)
newScope :: AttrSet a -> Scope a
2018-04-07 21:02:50 +02:00
newScope = Scope
scopeLookup :: Text -> [Scope v] -> Maybe v
scopeLookup key = foldr go Nothing
where
go (Scope m) rest = M.lookup key m <|> rest
2018-04-07 21:02:50 +02:00
data Scopes m v = Scopes
{ lexicalScopes :: [Scope v]
, dynamicScopes :: [m (Scope v)]
}
2018-04-07 21:02:50 +02:00
instance Show (Scopes m v) where
show (Scopes m v) =
"Scopes: " ++ show m ++ ", and "
++ show (length v) ++ " with-scopes"
instance Semigroup (Scopes m v) where
Scopes ls lw <> Scopes rs rw = Scopes (ls <> rs) (lw <> rw)
instance Monoid (Scopes m v) where
mempty = emptyScopes
mappend = (<>)
2018-04-07 21:02:50 +02:00
2018-11-17 22:21:03 +01:00
emptyScopes :: forall m v. Scopes m v
emptyScopes = Scopes [] []
2018-04-07 21:02:50 +02:00
2018-11-17 22:21:03 +01:00
class Scoped t m | m -> t where
currentScopes :: m (Scopes m t)
clearScopes :: m a -> m a
pushScopes :: Scopes m t -> m a -> m a
lookupVar :: Text -> m (Maybe t)
currentScopesReader :: forall m t e. (MonadReader e m, Has e (Scopes m t)) => m (Scopes m t)
currentScopesReader = asks (view hasLens)
2018-04-07 21:02:50 +02:00
2018-11-17 22:21:03 +01:00
clearScopesReader :: forall m t e a. (MonadReader e m, Has e (Scopes m t)) => m a -> m a
clearScopesReader = local (set hasLens (emptyScopes @m @t))
2018-04-07 21:02:50 +02:00
2018-11-17 22:21:03 +01:00
pushScope :: Scoped t m => AttrSet t -> m a -> m a
pushScope s = pushScopes (Scopes [Scope s] [])
2018-04-07 21:02:50 +02:00
2018-11-17 22:21:03 +01:00
pushWeakScope :: (Functor m, Scoped t m) => m (AttrSet t) -> m a -> m a
pushWeakScope s = pushScopes (Scopes [] [Scope <$> s])
2018-04-07 21:02:50 +02:00
2018-11-17 22:21:03 +01:00
pushScopesReader :: (MonadReader e m, Has e (Scopes m t)) => Scopes m t -> m a -> m a
pushScopesReader s = local (over hasLens (s <>))
2018-04-07 21:02:50 +02:00
2018-11-17 22:21:03 +01:00
lookupVarReader :: forall m t e. (MonadReader e m, Has e (Scopes m t)) => Text -> m (Maybe t)
lookupVarReader k = do
mres <- asks (scopeLookup k . lexicalScopes @m . view hasLens)
case mres of
Just sym -> return $ Just sym
Nothing -> do
ws <- asks (dynamicScopes . view hasLens)
foldr (\x rest -> do
mres' <- M.lookup k . getScope <$> x
case mres' of
Just sym -> return $ Just sym
Nothing -> rest)
(return Nothing) ws
2018-04-07 21:02:50 +02:00
2018-11-17 22:21:03 +01:00
withScopes :: Scoped t m => Scopes m t -> m a -> m a
withScopes scope = clearScopes . pushScopes scope