{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
module Distribution.Types.ModuleRenaming (
ModuleRenaming(..),
defaultRenaming,
isDefaultRenaming,
) where
import Prelude ()
import Distribution.Compat.Prelude hiding (empty)
import qualified Distribution.Compat.ReadP as Parse
import Distribution.Compat.ReadP ((<++))
import Distribution.ModuleName
import Distribution.Text
import Text.PrettyPrint
data ModuleRenaming
= ModuleRenaming [(ModuleName, ModuleName)]
| DefaultRenaming
| HidingRenaming [ModuleName]
deriving (Show, Read, Eq, Ord, Typeable, Data, Generic)
defaultRenaming :: ModuleRenaming
defaultRenaming = DefaultRenaming
isDefaultRenaming :: ModuleRenaming -> Bool
isDefaultRenaming DefaultRenaming = True
isDefaultRenaming _ = False
instance Binary ModuleRenaming where
instance Text ModuleRenaming where
disp DefaultRenaming = empty
disp (HidingRenaming hides)
= text "hiding" <+> parens (hsep (punctuate comma (map disp hides)))
disp (ModuleRenaming rns)
= parens . hsep $ punctuate comma (map dispEntry rns)
where dispEntry (orig, new)
| orig == new = disp orig
| otherwise = disp orig <+> text "as" <+> disp new
parse = do fmap ModuleRenaming parseRns
<++ parseHidingRenaming
<++ return DefaultRenaming
where parseRns = do
rns <- Parse.between (Parse.char '(') (Parse.char ')') parseList
Parse.skipSpaces
return rns
parseHidingRenaming = do
_ <- Parse.string "hiding"
Parse.skipSpaces
hides <- Parse.between (Parse.char '(') (Parse.char ')')
(Parse.sepBy parse (Parse.char ',' >> Parse.skipSpaces))
return (HidingRenaming hides)
parseList =
Parse.sepBy parseEntry (Parse.char ',' >> Parse.skipSpaces)
parseEntry :: Parse.ReadP r (ModuleName, ModuleName)
parseEntry = do
orig <- parse
Parse.skipSpaces
(do _ <- Parse.string "as"
Parse.skipSpaces
new <- parse
Parse.skipSpaces
return (orig, new)
<++
return (orig, orig))