module DDC.Core.Salt.Convert.Name
( sanitizeName
, sanitizeGlobal
, seaNameOfSuper
, seaNameOfLocal)
where
import DDC.Core.Salt.Name
import DDC.Core.Module
import DDC.Base.Pretty
import Data.Maybe
sanitizeGlobal :: String -> String
sanitizeGlobal = sanitizeName
seaNameOfSuper
:: Maybe (ImportValue Name)
-> Maybe (ExportSource Name)
-> Name
-> Maybe Doc
seaNameOfSuper mImport mExport nm
| Nothing <- mImport
, Nothing <- mExport
, Just str <- takeNameVar nm
= Just $ text $ sanitizeName str
| Nothing <- mImport
, Just _ <- mExport
, Just str <- takeNameVar nm
= Just $ text $ sanitizeName str
| Just ImportValueModule{} <- mImport
, Nothing <- mExport
, Just str <- takeNameVar nm
= Just $ text $ sanitizeName str
| Just (ImportValueSea strSea _) <- mImport
, Nothing <- mExport
= Just $ text strSea
| otherwise
= Nothing
seaNameOfLocal :: Name -> Maybe Doc
seaNameOfLocal nn
= case takeNameVar nn of
Just str -> Just $ text $ "_" ++ sanitizeGlobal str
_ -> Nothing
sanitizeName :: String -> String
sanitizeName str
= let hasSymbols = any isJust $ map convertSymbol str
in if hasSymbols
then "_sym_" ++ concatMap rewriteChar str
else str
rewriteChar :: Char -> String
rewriteChar c
| Just str <- convertSymbol c = "Z" ++ str
| 'Z' <- c = "ZZ"
| otherwise = [c]
convertSymbol :: Char -> Maybe String
convertSymbol c
= case c of
'!' -> Just "Bg"
'@' -> Just "At"
'#' -> Just "Hs"
'$' -> Just "Dl"
'%' -> Just "Pc"
'^' -> Just "Ht"
'&' -> Just "An"
'*' -> Just "St"
'~' -> Just "Tl"
'-' -> Just "Ms"
'+' -> Just "Ps"
'=' -> Just "Eq"
'|' -> Just "Pp"
'\\' -> Just "Bs"
'/' -> Just "Fs"
':' -> Just "Cl"
'.' -> Just "Dt"
'?' -> Just "Qm"
'<' -> Just "Lt"
'>' -> Just "Gt"
'[' -> Just "Br"
']' -> Just "Kt"
'\'' -> Just "Pm"
'`' -> Just "Bt"
_ -> Nothing