module Distribution.Compiler (
CompilerFlavor(..),
buildCompilerFlavor,
defaultCompilerFlavor,
parseCompilerFlavorCompat,
CompilerId(..),
) where
import Distribution.Version (Version(..))
import qualified System.Info (compilerName)
import Distribution.Text (Text(..), display)
import qualified Distribution.Compat.ReadP as Parse
import qualified Text.PrettyPrint as Disp
import Text.PrettyPrint ((<>))
import qualified Data.Char as Char (toLower, isDigit, isAlphaNum)
import Control.Monad (when)
data CompilerFlavor = GHC | NHC | YHC | Hugs | HBC | Helium | JHC | LHC | UHC
| OtherCompiler String
deriving (D:Show ::
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> T:Show aShow, D:Read ::
(Int -> ReadS a)
-> ReadS [a]
-> ReadPrec a
-> ReadPrec [a]
-> T:Read aRead, 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)
knownCompilerFlavors :: [CompilerFlavor]
knownCompilerFlavors = [GHC :: CompilerFlavorGHC, NHC :: CompilerFlavorNHC, YHC :: CompilerFlavorYHC, Hugs :: CompilerFlavorHugs, HBC :: CompilerFlavorHBC, Helium :: CompilerFlavorHelium, JHC :: CompilerFlavorJHC, LHC :: CompilerFlavorLHC, UHC :: CompilerFlavorUHC]
instance D:Text :: (a -> Doc) -> (forall r. ReadP r a) -> T:Text aText CompilerFlavor where
disp (OtherCompiler name) = text :: String -> DocDisp.text name :: Stringname
disp NHC = text :: String -> DocDisp.text "nhc98"
disp other = text :: String -> DocDisp.text (lowercase :: String -> Stringlowercase (show :: Show a => a -> Stringshow other :: CompilerFlavorother))
parse = do
comp <- munch1 :: (Char -> Bool) -> ReadP r StringParse.munch1 isAlphaNum :: Char -> BoolChar.isAlphaNum
when :: Monad m => Bool -> m () -> m ()when (all :: (a -> Bool) -> [a] -> Boolall isDigit :: Char -> BoolChar.isDigit comp :: Stringcomp) pfail :: ReadP r aParse.pfail
return :: Monad m => forall a. a -> m areturn (classifyCompilerFlavor :: String -> CompilerFlavorclassifyCompilerFlavor comp :: Stringcomp)
classifyCompilerFlavor :: String -> CompilerFlavor
classifyCompilerFlavor s =
case lookup :: Eq a => a -> [(a, b)] -> Maybe blookup (lowercase :: String -> Stringlowercase s :: Strings) compilerMap :: [(String, CompilerFlavor)]compilerMap of
Just compiler -> compiler :: CompilerFlavorcompiler
Nothing -> OtherCompiler :: String -> CompilerFlavorOtherCompiler s :: Strings
where
compilerMap = [ (display :: Text a => a -> Stringdisplay compiler :: CompilerFlavorcompiler, compiler :: CompilerFlavorcompiler)
| compiler <- knownCompilerFlavors :: [CompilerFlavor]knownCompilerFlavors ]
parseCompilerFlavorCompat :: Parse.ReadP r CompilerFlavor
parseCompilerFlavorCompat = do
comp <- munch1 :: (Char -> Bool) -> ReadP r StringParse.munch1 isAlphaNum :: Char -> BoolChar.isAlphaNum
when :: Monad m => Bool -> m () -> m ()when (all :: (a -> Bool) -> [a] -> Boolall isDigit :: Char -> BoolChar.isDigit comp :: Stringcomp) pfail :: ReadP r aParse.pfail
case lookup :: Eq a => a -> [(a, b)] -> Maybe blookup comp :: Stringcomp compilerMap :: [(String, CompilerFlavor)]compilerMap of
Just compiler -> return :: Monad m => forall a. a -> m areturn compiler :: CompilerFlavorcompiler
Nothing -> return :: Monad m => forall a. a -> m areturn (OtherCompiler :: String -> CompilerFlavorOtherCompiler comp :: Stringcomp)
where
compilerMap = [ (show :: Show a => a -> Stringshow compiler :: CompilerFlavorcompiler, compiler :: CompilerFlavorcompiler)
| compiler <- knownCompilerFlavors :: [CompilerFlavor]knownCompilerFlavors
, compiler :: CompilerFlavorcompiler (/=) :: Eq a => a -> a -> Bool/= YHC :: CompilerFlavorYHC ]
buildCompilerFlavor :: CompilerFlavor
buildCompilerFlavor = classifyCompilerFlavor :: String -> CompilerFlavorclassifyCompilerFlavor compilerName :: StringSystem.Info.compilerName
defaultCompilerFlavor :: Maybe CompilerFlavor
defaultCompilerFlavor = case buildCompilerFlavor :: CompilerFlavorbuildCompilerFlavor of
OtherCompiler _ -> Nothing :: Maybe aNothing
_ -> Just :: a -> Maybe aJust buildCompilerFlavor :: CompilerFlavorbuildCompilerFlavor
data CompilerId = CompilerId CompilerFlavor Version
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 CompilerId where
disp (CompilerId f (Version [] _)) = disp :: Text a => a -> Docdisp f :: CompilerFlavorf
disp (CompilerId f v) = disp :: Text a => a -> Docdisp f :: CompilerFlavorf (<>) :: Doc -> Doc -> Doc<> char :: Char -> DocDisp.char '-' (<>) :: Doc -> Doc -> Doc<> disp :: Text a => a -> Docdisp v :: Versionv
parse = do
flavour <- parse :: Text a => forall r. ReadP r aparse
version <- (char :: Char -> ReadP r CharParse.char '-' (>>) :: Monad m => forall a b. m a -> m b -> m b>> parse :: Text a => forall r. ReadP r aparse) (<++) :: ReadP a a -> ReadP r a -> ReadP r aParse.<++ return :: Monad m => forall a. a -> m areturn (Version :: [Int] -> [String] -> VersionVersion [] :: [a][] [] :: [a][])
return :: Monad m => forall a. a -> m areturn (CompilerId :: CompilerFlavor -> Version -> CompilerIdCompilerId flavour :: CompilerFlavorflavour version :: Versionversion)
lowercase :: String -> String
lowercase = map :: (a -> b) -> [a] -> [b]map toLower :: Char -> CharChar.toLower