-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Better interface to smart constructor generation with decapitalized…
… names
- Loading branch information
Showing
11 changed files
with
215 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
-- | | ||
-- Module : Grisette.Internal.TH.Ctor.Common | ||
-- Copyright : (c) Sirui Lu 2024 | ||
-- License : BSD-3-Clause (see the LICENSE file) | ||
-- | ||
-- Maintainer : siruilu@cs.washington.edu | ||
-- Stability : Experimental | ||
-- Portability : GHC only | ||
module Grisette.Internal.TH.Ctor.Common | ||
( withNameTransformer, | ||
prefixTransformer, | ||
decapitalizeTransformer, | ||
) | ||
where | ||
|
||
import Control.Monad (unless) | ||
import Data.Char (isAlphaNum, toLower) | ||
import Data.Foldable (traverse_) | ||
import Grisette.Internal.TH.Util (occName) | ||
import Language.Haskell.TH (Dec, Name, Q) | ||
import Language.Haskell.TH.Datatype | ||
( ConstructorInfo (constructorName), | ||
DatatypeInfo (datatypeCons), | ||
reifyDatatype, | ||
) | ||
|
||
checkName :: String -> Q () | ||
checkName name = | ||
unless (all (\x -> isAlphaNum x || x == '\'' || x == '_') name) $ | ||
fail | ||
( "Constructor name contain invalid characters, consider providing a " | ||
++ "custom name: " | ||
++ show name | ||
) | ||
|
||
-- | Generate smart constructor given a type name, using a name transformer | ||
-- to transform constructor names. | ||
withNameTransformer :: | ||
-- | A function that generates decs given a list of constructor names and a | ||
-- type name | ||
([String] -> Name -> Q [Dec]) -> | ||
-- | A function that transforms constructor names | ||
(String -> String) -> | ||
-- | The type to generate the wrappers for | ||
Name -> | ||
Q [Dec] | ||
withNameTransformer namedGen nameTransformer typName = do | ||
d <- reifyDatatype typName | ||
let constructorNames = occName . constructorName <$> datatypeCons d | ||
let transformedNames = nameTransformer <$> constructorNames | ||
traverse_ checkName transformedNames | ||
namedGen transformedNames typName | ||
|
||
-- | A name transformer that prefixes a string to the constructor name | ||
prefixTransformer :: String -> String -> String | ||
prefixTransformer = (++) | ||
|
||
-- | A name transformer that converts the first character to lowercase | ||
decapitalizeTransformer :: String -> String | ||
decapitalizeTransformer (x : xs) = toLower x : xs | ||
decapitalizeTransformer [] = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.