module Distribution.ModuleName (
ModuleName,
fromString,
components,
toFilePath,
main,
simple,
) where
import Distribution.Text
( Text(..) )
import qualified Distribution.Compat.ReadP as Parse
import qualified Text.PrettyPrint as Disp
import qualified Data.Char as Char
( isAlphaNum, isUpper )
import System.FilePath
( pathSeparator )
import Data.List
( intersperse )
newtype ModuleName = ModuleName [String]
deriving (D:Eq :: (a -> a -> Bool) -> (a -> a -> Bool) -> T:Eq aEq, D:Ord ::
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> T:Ord aOrd, D:Read ::
(Int -> ReadS a)
-> ReadS [a]
-> ReadPrec a
-> ReadPrec [a]
-> T:Read aRead, D:Show ::
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> T:Show aShow)
instance D:Text :: (a -> Doc) -> (forall r. ReadP r a) -> T:Text aText ModuleName where
disp (ModuleName ms) =
hcat :: [Doc] -> DocDisp.hcat (intersperse :: a -> [a] -> [a]intersperse (char :: Char -> DocDisp.char '.') (map :: (a -> b) -> [a] -> [b]map text :: String -> DocDisp.text ms :: [[Char]]ms))
parse = do
ms <- sepBy1 :: ReadP r a -> ReadP r sep -> ReadP r [a]Parse.sepBy1 component :: Parser r Char [Char]component (char :: Char -> ReadP r CharParse.char '.')
return :: Monad m => forall a. a -> m areturn (ModuleName :: [String] -> ModuleNameModuleName ms :: [[Char]]ms)
where
component = do
c <- satisfy :: (Char -> Bool) -> ReadP r CharParse.satisfy isUpper :: Char -> BoolChar.isUpper
cs <- munch :: (Char -> Bool) -> ReadP r StringParse.munch validModuleChar :: Char -> BoolvalidModuleChar
return :: Monad m => forall a. a -> m areturn (c :: Charc(:) :: a -> [a] -> [a]:cs :: Stringcs)
validModuleChar :: Char -> Bool
validModuleChar c = isAlphaNum :: Char -> BoolChar.isAlphaNum c :: Charc (||) :: Bool -> Bool -> Bool|| c :: Charc (==) :: Eq a => a -> a -> Bool== '_' (||) :: Bool -> Bool -> Bool|| c :: Charc (==) :: Eq a => a -> a -> Bool== '\''
validModuleComponent :: String -> Bool
validModuleComponent [] = False :: BoolFalse
validModuleComponent (c:cs) = isUpper :: Char -> BoolChar.isUpper c :: Charc
(&&) :: Bool -> Bool -> Bool&& all :: (a -> Bool) -> [a] -> Boolall validModuleChar :: Char -> BoolvalidModuleChar cs :: Stringcs
simple :: String -> ModuleName
simple str = ModuleName :: [String] -> ModuleNameModuleName [str :: Stringstr]
fromString :: String -> ModuleName
fromString string
| all :: (a -> Bool) -> [a] -> Boolall validModuleComponent :: String -> BoolvalidModuleComponent components' :: [[Char]]components' = ModuleName :: [String] -> ModuleNameModuleName components' :: [[Char]]components'
| otherwise :: Boolotherwise = error :: [Char] -> aerror badName :: [Char]badName
where
components' = split :: [Char] -> [[Char]]split string :: Stringstring
badName = "ModuleName.fromString: invalid module name " (++) :: [a] -> [a] -> [a]++ show :: Show a => a -> Stringshow string :: Stringstring
split cs = case break :: (a -> Bool) -> [a] -> ([a], [a])break ((==) :: Eq a => a -> a -> Bool=='.') cs :: Stringcs of
(chunk,[]) -> chunk :: [Char]chunk (:) :: a -> [a] -> [a]: [] :: [a][]
(chunk,_:rest) -> chunk :: [Char]chunk (:) :: a -> [a] -> [a]: split :: [Char] -> [[Char]]split rest :: [Char]rest
main :: ModuleName
main = ModuleName :: [String] -> ModuleNameModuleName ["Main"]
components :: ModuleName -> [String]
components (ModuleName ms) = ms :: [[Char]]ms
toFilePath :: ModuleName -> FilePath
toFilePath = concat :: [[a]] -> [a]concat (.) :: (b -> c) -> (a -> b) -> a -> c. intersperse :: a -> [a] -> [a]intersperse [pathSeparator :: CharpathSeparator] (.) :: (b -> c) -> (a -> b) -> a -> c. components :: ModuleName -> [String]components