module GHC.SourceGen.Name.Internal where
import Data.Char (isUpper)
import Data.String (IsString(..))
import FastString (FastString, fsLit)
import Module (mkModuleNameFS, ModuleName)
import RdrName
import OccName
import SrcLoc (Located)
import GHC.SourceGen.Syntax.Internal (builtLoc)
data OccNameStr = OccNameStr !RawNameSpace !FastString
data RawNameSpace = Constructor | Value
rawNameSpace :: String -> RawNameSpace
rawNameSpace (c:_)
| isUpper c = Constructor
rawNameSpace _ = Value
instance IsString OccNameStr where
fromString s = OccNameStr (rawNameSpace s) (fsLit s)
valueOccName, typeOccName :: OccNameStr -> OccName
valueOccName (OccNameStr Constructor s) = mkDataOccFS s
valueOccName (OccNameStr Value s) = mkVarOccFS s
typeOccName (OccNameStr Constructor s) = mkTcOccFS s
typeOccName (OccNameStr Value s) = mkTyVarOccFS s
newtype ModuleNameStr = ModuleNameStr { unModuleNameStr :: ModuleName }
instance IsString ModuleNameStr where
fromString = ModuleNameStr . mkModuleNameFS . fsLit
data RdrNameStr = RawUnqual OccNameStr | RawQual ModuleNameStr OccNameStr
valueRdrName, typeRdrName :: RdrNameStr -> Located RdrName
valueRdrName (RawUnqual r) = builtLoc $ Unqual $ valueOccName r
valueRdrName (RawQual (ModuleNameStr m) r) = builtLoc $ Qual m $ valueOccName r
typeRdrName (RawUnqual r) = builtLoc $ Unqual $ typeOccName r
typeRdrName (RawQual (ModuleNameStr m) r) = builtLoc $ Qual m $ typeOccName r
instance IsString RdrNameStr where
fromString f = case span (/= '.') (reverse f) of
(f', '.':f'') ->
RawQual (fromString $ reverse f'') (fromString $ reverse f')
_ -> RawUnqual (fromString f)