names-th (empty) → 0.0.1.0
raw patch · 5 files changed
+277/−0 lines, 5 filesdep +basedep +template-haskellsetup-changed
Dependencies added: base, template-haskell
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- names-th.cabal +33/−0
- src/Language/Haskell/TH/Lib/Extra.hs +98/−0
- src/Language/Haskell/TH/Name/CamelCase.hs +114/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ names-th.cabal view
@@ -0,0 +1,33 @@+name: names-th+version: 0.0.1.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://twitter.com/khibino+license: BSD3+license-file: LICENSE+author: Kei Hibino+maintainer: ex8k.hibino@gmail.com+copyright: Copyright (c) 2013 Kei Hibino+category: Development+build-type: Simple+cabal-version: >=1.10++library+ exposed-modules:+ Language.Haskell.TH.Name.CamelCase+ Language.Haskell.TH.Lib.Extra+ build-depends: base <5, 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
+ src/Language/Haskell/TH/Lib/Extra.hs view
@@ -0,0 +1,98 @@+-- |+-- 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+ reportMessage, reportWarning, reportError,+ ) where++import System.IO (hPutStrLn, stderr)+import System.Environment (getEnvironment)++import Language.Haskell.TH+ (Ppr, ppr, Q, runQ, runIO,+ 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 message from TH code.+-- Only display when TH_EXTRA_MESSAGE_OUTPUT environment variable is set.+-- When variable value string is 'as_warn' or 'as_warning' and+-- using 'Q' monad action, Output is put into ghc logger as warning.+-- Other cases are normal standard error output.+reportMessage :: Quasi m => String -> m ()+reportMessage s = runQ . runIO $ do+ let lookupEnv n = lookup n `fmap` getEnvironment {- for base-4.5 -}+ mayOut <- lookupEnv "TH_EXTRA_MESSAGE_OUTPUT"+ case mayOut of+ Just out+ | out `elem` ["as_warn", "as_warning"] -> qReport False s+ | otherwise -> hPutStrLn stderr s+ Nothing -> return ()++-- | Print compile warnings from TH code.+reportWarning :: String -> Q ()+reportWarning = qReport False++-- | Print compile errors from TH code.+reportError :: String -> Q ()+reportError = qReport True
+ src/Language/Haskell/TH/Name/CamelCase.hs view
@@ -0,0 +1,114 @@+-- |+-- Module : Language.Haskell.TH.Name.CamelCase+-- Copyright : 2013 Kei Hibino+-- 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,++ varNameWithPrefix,++ -- * Functions to generate haskell template from names+ -- $makeTemplates+ toTypeCon, toDataCon,++ toVarExp, toVarPat+ ) where++import Data.Char (toUpper, toLower)+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 "" = ""++{- $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 . 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 . 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++-- | Make camel-cased variable name with prefix like below.+--+-- > name `varNamePrefix` prefix+--+varNameWithPrefix :: String -> String -> VarName+varNameWithPrefix n p = toVarName $ p ++ camelcaseUpper n++{- $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