module TextShow.Language.Haskell.TH (showbName, showbName') where
import Data.Char (isAlpha)
import Data.Maybe (fromJust)
import Data.Monoid.Compat
import qualified Data.Text.Lazy as TL (Text, dropWhile, null, tail)
import Data.Text.Lazy (uncons)
#if !(MIN_VERSION_template_haskell(2,10,0))
import GHC.Exts (Int(I#))
#endif
import Language.Haskell.TH.PprLib (Doc, to_HPJ_Doc)
import Language.Haskell.TH.Syntax
import TextShow (TextShow(..), Builder,
fromString, singleton, toLazyText)
import TextShow.Text.PrettyPrint (renderB)
import TextShow.TH (deriveTextShow)
showbName :: Name -> Builder
showbName = showbName' Alone
showbName' :: NameIs -> Name -> Builder
showbName' ni nm = case ni of
Alone -> nms
Applied
| pnam -> nms
| otherwise -> singleton '(' <> nms <> singleton ')'
Infix
| pnam -> singleton '`' <> nms <> singleton '`'
| otherwise -> nms
where
nms :: Builder
nms = case nm of
Name occ NameS -> occB occ
Name occ (NameQ m) -> modB m <> singleton '.' <> occB occ
Name occ (NameG _ _ m) -> modB m <> singleton '.' <> occB occ
Name occ (NameU u) -> occB occ <> singleton '_' <> showb (mkInt u)
Name occ (NameL u) -> occB occ <> singleton '_' <> showb (mkInt u)
#if MIN_VERSION_template_haskell(2,10,0)
mkInt = id
#else
mkInt i# = I# i#
#endif
occB :: OccName -> Builder
occB = fromString . occString
modB :: ModName -> Builder
modB = fromString . modString
pnam :: Bool
pnam = classify $ toLazyText nms
classify :: TL.Text -> Bool
classify t
| TL.null t = False
| otherwise = case fromJust $ uncons t of
(x, xs) -> if isAlpha x || (x `elem` "_[]()")
then let t' = TL.dropWhile (/= '.') xs
in if TL.null t'
then True
else classify $ TL.tail t'
else False
$(deriveTextShow ''Body)
$(deriveTextShow ''Callconv)
$(deriveTextShow ''Clause)
$(deriveTextShow ''Con)
$(deriveTextShow ''Dec)
$(deriveTextShow ''Exp)
$(deriveTextShow ''FamFlavour)
$(deriveTextShow ''Fixity)
$(deriveTextShow ''FixityDirection)
$(deriveTextShow ''Foreign)
$(deriveTextShow ''FunDep)
$(deriveTextShow ''Guard)
$(deriveTextShow ''Info)
$(deriveTextShow ''Lit)
$(deriveTextShow ''Loc)
$(deriveTextShow ''Match)
$(deriveTextShow ''ModName)
instance TextShow Name where
showb = showbName
$(deriveTextShow ''NameFlavour)
$(deriveTextShow ''NameSpace)
$(deriveTextShow ''OccName)
$(deriveTextShow ''Pat)
$(deriveTextShow ''PkgName)
$(deriveTextShow ''Pragma)
$(deriveTextShow ''Range)
$(deriveTextShow ''Safety)
$(deriveTextShow ''Stmt)
$(deriveTextShow ''Type)
$(deriveTextShow ''TyVarBndr)
instance TextShow Doc where
showb = renderB . to_HPJ_Doc
#if MIN_VERSION_template_haskell(2,8,0)
$(deriveTextShow ''Inline)
$(deriveTextShow ''Phases)
$(deriveTextShow ''RuleBndr)
$(deriveTextShow ''RuleMatch)
$(deriveTextShow ''TyLit)
#else
$(deriveTextShow ''InlineSpec)
$(deriveTextShow ''Kind)
#endif
#if MIN_VERSION_template_haskell(2,9,0)
$(deriveTextShow ''AnnLookup)
$(deriveTextShow ''AnnTarget)
$(deriveTextShow ''Module)
$(deriveTextShow ''ModuleInfo)
$(deriveTextShow ''Role)
$(deriveTextShow ''TySynEqn)
#endif
#if !(MIN_VERSION_template_haskell(2,10,0))
$(deriveTextShow ''Pred)
#endif
#if MIN_VERSION_template_haskell(2,11,0)
$(deriveTextShow ''Bang)
$(deriveTextShow ''DecidedStrictness)
$(deriveTextShow ''FamilyResultSig)
$(deriveTextShow ''InjectivityAnn)
$(deriveTextShow ''Overlap)
$(deriveTextShow ''SourceStrictness)
$(deriveTextShow ''SourceUnpackedness)
$(deriveTextShow ''TypeFamilyHead)
#else
$(deriveTextShow ''Strict)
#endif
#if MIN_VERSION_template_haskell(2,12,0)
$(deriveTextShow ''DerivClause)
$(deriveTextShow ''DerivStrategy)
$(deriveTextShow ''PatSynArgs)
$(deriveTextShow ''PatSynDir)
#endif