{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE RankNTypes #-}
module Distribution.Types.ModuleRenaming (
ModuleRenaming(..),
interpModuleRenaming,
defaultRenaming,
isDefaultRenaming,
) where
import Distribution.CabalSpecVersion
import Distribution.Compat.Prelude hiding (empty)
import Prelude ()
import Distribution.ModuleName
import Distribution.Parsec
import Distribution.Pretty
import qualified Data.Map as Map
import qualified Data.Set as Set
import qualified Distribution.Compat.CharParsing as P
import Text.PrettyPrint (hsep, parens, punctuate, text, (<+>), comma)
data ModuleRenaming
= ModuleRenaming [(ModuleName, ModuleName)]
| DefaultRenaming
| HidingRenaming [ModuleName]
deriving (Show, Read, Eq, Ord, Typeable, Data, Generic)
interpModuleRenaming :: ModuleRenaming -> ModuleName -> Maybe ModuleName
interpModuleRenaming DefaultRenaming = Just
interpModuleRenaming (ModuleRenaming rns) =
let m = Map.fromList rns
in \k -> Map.lookup k m
interpModuleRenaming (HidingRenaming hs) =
let s = Set.fromList hs
in \k -> if k `Set.member` s then Nothing else Just k
defaultRenaming :: ModuleRenaming
defaultRenaming = DefaultRenaming
isDefaultRenaming :: ModuleRenaming -> Bool
isDefaultRenaming DefaultRenaming = True
isDefaultRenaming _ = False
instance Binary ModuleRenaming where
instance Structured ModuleRenaming where
instance NFData ModuleRenaming where rnf = genericRnf
instance Pretty ModuleRenaming where
pretty DefaultRenaming = mempty
pretty (HidingRenaming hides)
= text "hiding" <+> parens (hsep (punctuate comma (map pretty hides)))
pretty (ModuleRenaming rns)
= parens . hsep $ punctuate comma (map dispEntry rns)
where dispEntry (orig, new)
| orig == new = pretty orig
| otherwise = pretty orig <+> text "as" <+> pretty new
instance Parsec ModuleRenaming where
parsec = do
csv <- askCabalSpecVersion
if csv >= CabalSpecV3_0
then moduleRenamingParsec parensLax lexemeParsec
else moduleRenamingParsec parensStrict parsec
where
parensLax p = P.between (P.char '(' >> P.spaces) (P.char ')' >> P.spaces) p
parensStrict p = P.between (P.char '(' >> warnSpaces) (P.char ')') p
warnSpaces = P.optional $
P.space *> fail "space after parenthesis, use cabal-version: 3.0 or higher"
moduleRenamingParsec
:: CabalParsing m
=> (forall a. m a -> m a)
-> m ModuleName
-> m ModuleRenaming
moduleRenamingParsec bp mn =
P.choice [ parseRename, parseHiding, return DefaultRenaming ]
where
cma = P.char ',' >> P.spaces
parseRename = do
rns <- bp parseList
P.spaces
return (ModuleRenaming rns)
parseHiding = do
_ <- P.string "hiding"
P.spaces
hides <- bp (P.sepBy mn cma)
return (HidingRenaming hides)
parseList =
P.sepBy parseEntry cma
parseEntry = do
orig <- parsec
P.spaces
P.option (orig, orig) $ do
_ <- P.string "as"
P.skipSpaces1
new <- parsec
P.spaces
return (orig, new)