names-th-0.3.0.0/0000755000000000000000000000000013313147464011560 5ustar0000000000000000names-th-0.3.0.0/Setup.hs0000644000000000000000000000005613313147464013215 0ustar0000000000000000import Distribution.Simple main = defaultMain names-th-0.3.0.0/names-th.cabal0000644000000000000000000000303713313147464014263 0ustar0000000000000000name: names-th version: 0.3.0.0 synopsis: Manipulate name strings for TH description: This package includes functions to manipulate name string and extra library functions for Template Haskell. homepage: http://khibino.github.io/haskell-relational-record/ license: BSD3 license-file: LICENSE author: Kei Hibino maintainer: ex8k.hibino@gmail.com copyright: Copyright (c) 2013-2018 Kei Hibino, 2015 Shohei Murayama category: Development build-type: Simple cabal-version: >=1.10 tested-with: GHC == 8.4.1, GHC == 8.4.2, GHC == 8.4.3 , GHC == 8.2.1, GHC == 8.2.2 , GHC == 8.0.1, GHC == 8.0.2 , GHC == 7.10.1, GHC == 7.10.2, GHC == 7.10.3 , GHC == 7.8.1, GHC == 7.8.2, GHC == 7.8.3, GHC == 7.8.4 , GHC == 7.6.1, GHC == 7.6.2, GHC == 7.6.3 , GHC == 7.4.1, GHC == 7.4.2 library exposed-modules: Language.Haskell.TH.Name.CamelCase Language.Haskell.TH.Lib.Extra build-depends: base <5, containers, template-haskell hs-source-dirs: src ghc-options: -Wall default-language: Haskell2010 source-repository head type: git location: https://github.com/khibino/haskell-relational-record source-repository head type: mercurial location: https://bitbucket.org/khibino/haskell-relational-record names-th-0.3.0.0/LICENSE0000644000000000000000000000275613313147464012577 0ustar0000000000000000Copyright (c) 2013, Kei Hibino All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Kei Hibino nor the names of other contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. names-th-0.3.0.0/src/0000755000000000000000000000000013313147464012347 5ustar0000000000000000names-th-0.3.0.0/src/Language/0000755000000000000000000000000013313147464014072 5ustar0000000000000000names-th-0.3.0.0/src/Language/Haskell/0000755000000000000000000000000013313147464015455 5ustar0000000000000000names-th-0.3.0.0/src/Language/Haskell/TH/0000755000000000000000000000000013313147464015770 5ustar0000000000000000names-th-0.3.0.0/src/Language/Haskell/TH/Name/0000755000000000000000000000000013313147464016650 5ustar0000000000000000names-th-0.3.0.0/src/Language/Haskell/TH/Name/CamelCase.hs0000644000000000000000000000707313313147464021030 0ustar0000000000000000-- | -- Module : Language.Haskell.TH.Name.CamelCase -- Copyright : 2013-2018 Kei Hibino, 2015 Shohei Murayama -- License : BSD3 -- -- Maintainer : ex8k.hibino@gmail.com -- Stability : experimental -- Portability : unknown -- -- This module provides camelcased 'Name' for Template Haskell module Language.Haskell.TH.Name.CamelCase ( -- * Types to wrap 'Name' -- $nameTypes ConName (ConName, conName), toConName, VarName (VarName, varName), toVarName, -- * Functions to make camel-cased names -- $makeNames conCamelcaseName, varCamelcaseName, -- * Functions to generate haskell template from names -- $makeTemplates toTypeCon, toDataCon, toVarExp, toVarPat, ) where import Data.Char (toUpper, toLower) import Data.Set (Set, fromList, member) import Language.Haskell.TH (Name, mkName, TypeQ, conT, ExpQ, conE, varE, PatQ, varP) capitalize :: String -> String capitalize (c:cs) = toUpper c : cs capitalize "" = "" unCapitalize :: String -> String unCapitalize (c:cs) = toLower c : cs unCapitalize "" = "" -- | rename the string that equals to reserved identifiers. rename :: String -> String rename cs | cs `member` reservedIds = cs ++ "_" | otherwise = cs {-# INLINE rename #-} -- | All reserved identifiers. Taken from section 2.4 of the 2010 Report. reservedIds :: Set String reservedIds = fromList [ "case", "class", "data", "default", "deriving" , "do", "else", "foreign", "if", "import", "in" , "infix", "infixl", "infixr", "instance", "let" , "module", "newtype", "of", "then", "type", "where" , "_" ] {-# INLINE reservedIds #-} {- $nameTypes Wrap 'Name' to distinguish constructor names and variable names. -} -- | Type to wrap constructor\'s 'Name'. newtype ConName = ConName { conName :: Name {- ^ Get wrapped 'Name' -} } -- | Make constructor name from 'String'. toConName :: String -> ConName toConName = ConName . mkName . rename . capitalize -- | Type to wrap variable\'s 'Name'. newtype VarName = VarName { varName :: Name {- ^ Get wrapped 'Name' -} } -- | Make variable name from 'String'. toVarName :: String -> VarName toVarName = VarName . mkName . rename . unCapitalize -- | 'Char' set used from camel-cased names. nameChars :: String nameChars = '\'' : ['0' .. '9'] ++ ['A' .. 'Z'] ++ ['a' .. 'z'] -- | Split into chunks to generate camel-cased 'String'. splitForName :: String -> [String] splitForName str | rest /= [] = tk : splitForName (tail rest) | otherwise = [tk] where (tk, rest) = span (`elem` nameChars) str {- $makeNames Make camel-cased names. -} -- | Convert into camel-cased 'String'. -- First 'Char' of result is upper case. camelcaseUpper :: String -> String camelcaseUpper = concatMap capitalize . splitForName -- | Make camel-cased constructor name from 'String'. conCamelcaseName :: String -> ConName conCamelcaseName = toConName . camelcaseUpper -- | Make camel-cased variable name from 'String'. varCamelcaseName :: String -> VarName varCamelcaseName = toVarName . camelcaseUpper {- $makeTemplates Make haskell templates from names. -} -- | Make type constructor 'TypeQ' monad from constructor name type. toTypeCon :: ConName -> TypeQ toTypeCon = conT . conName -- | Make data constructor 'ExpQ' monad from constructor name type. toDataCon :: ConName -> ExpQ toDataCon = conE . conName -- | Make variable 'ExpQ' monad from variable name type. toVarExp :: VarName -> ExpQ toVarExp = varE . varName -- | Make pattern 'PatQ' monad from variable name type. toVarPat :: VarName -> PatQ toVarPat = varP . varName names-th-0.3.0.0/src/Language/Haskell/TH/Lib/0000755000000000000000000000000013313147464016476 5ustar0000000000000000names-th-0.3.0.0/src/Language/Haskell/TH/Lib/Extra.hs0000644000000000000000000000407713313147464020125 0ustar0000000000000000-- | -- Module : Language.Haskell.TH.Lib.Extra -- Copyright : 2013 Kei Hibino -- License : BSD3 -- -- Maintainer : ex8k.hibino@gmail.com -- Stability : experimental -- Portability : unknown -- -- This module provides extra helper functions -- complementing "Language.Haskell.TH.Lib" module Language.Haskell.TH.Lib.Extra ( -- * Extra template functions -- $extraTemplateFunctions integralE, simpleValD, maybeD, -- * Pretty printing for 'Q' monad -- $prettyPrint pprQ, -- * Functions to print message or errors when compile time -- $compileMessage reportWarning, reportError, ) where import Language.Haskell.TH (Ppr, ppr, Q, runQ, Name, Dec, sigD, valD, TypeQ, varP, normalB, ExpQ, litE, integerL) import Language.Haskell.TH.PprLib (Doc) import Language.Haskell.TH.Syntax (Quasi, qReport) {- $extraTemplateFunctions Extra functions to generate haskell templates. -} -- | Integer literal template from 'Integral' types. integralE :: Integral a => a -> ExpQ integralE = litE . integerL . toInteger -- | Generate declaration template from name, type and expression. simpleValD :: Name -> TypeQ -> ExpQ -> Q [Dec] simpleValD var typ expr = do sig <- sigD var typ val <- valD (varP var) (normalB expr) [] return [sig, val] -- | May generate declaration template. maybeD :: (a -> Q [Dec]) -> Maybe a -> Q [Dec] maybeD = maybe (return []) {- $prettyPrint Pretty printing for haskell templates. -} -- | Helper function for pretty printing 'Q' Monad. pprQ :: (Functor m, Quasi m, Ppr a) => Q a -> m Doc pprQ = fmap ppr . runQ {- $compileMessage Functions to display or to raise compile messages from codes which generating haskell templates. Only messages directly generated by 'Q' monad report actions are handled by ghc loggers. > -- Handled by ghc logger > qReport False "Foo" > -- Not handled by ghc logger > runIO . runQ $ qReport False "Foo" -} -- | Print compile warnings from TH code. reportWarning :: String -> Q () reportWarning = qReport False -- | Print compile errors from TH code. reportError :: String -> Q () reportError = qReport True