Tidy.Codegen
- Package
- purescript-tidy-codegen
- Repository
- natefaubion/purescript-tidy-codegen
#PrintOptions Source
type PrintOptions = { importWrap :: ImportWrapOption, indentUnit :: String, indentWidth :: Int, operators :: Lazy PrecedenceMap, pageWidth :: Int, ribbonRatio :: Number, typeArrowPlacement :: TypeArrowOption, unicode :: UnicodeOption }#printModule Source
printModule :: Module Void -> StringPretty-prints a module using default format options.
exampleSource = printModule myModule
#printModuleWithOptions Source
printModuleWithOptions :: PrintOptions -> Module Void -> StringPretty-prints a module given a set of options. Use defaultPrintOptions
and override as necessary.
exampleSource = printModule
defaultPrintOptions
{ indentUnit = " "
, indentWidth = 4
, unicode = UnicodeAlways
}
myModule
#DeclInstance Source
class DeclInstance a e | a -> e whereMembers
declInstance :: forall className. ToQualifiedName className Proper => Maybe (Name Ident) -> Array (Type e) -> className -> Array (Type e) -> Array (InstanceBinding e) -> a
Instances
DeclInstance (Instance e) eDeclInstance (Declaration e) e
#TypeVar Source
class TypeVar a e | a -> e whereOverloaded constructors for type variables. This can be used to construct
not only CST.Type, but also type variable bindings in typeForall and
data, newtype, type, and class declarations.
exampleDecl =
declType "Id" [ typeVarKinded "a" (typeCtor "Type") ]
(typeVar "a")
Members
typeVar :: forall name. ToName name Ident => name -> aAn overloaded constructor for type variables.
typeVarKinded :: forall name. ToName name Ident => name -> Type e -> aAn overloaded constructor for kinded type variables.
Instances
#binaryOp Source
binaryOp :: forall name b. ToQualifiedName name Operator => name -> b -> BinaryOp bAn overloaded binary operator constructor. Use with exprOp, typeOp,
or binderOp.
exampleExpr =
exprOp (exprInt 4)
[ binaryOp "+" (exprIdent "a")
, binaryOp "/" (exprIdent "b")
]
#binderArray Source
binderArray :: forall e. Array (Binder e) -> Binder eConstructs an array literal binding pattern.
#binderBool Source
binderBool :: forall e. Boolean -> Binder eConstructs a boolean literal binding pattern.
#binderChar Source
binderChar :: forall e. Char -> Binder eConstructs a char literal binding pattern.
#binderCtor Source
binderCtor :: forall e name. ToQualifiedName name Proper => name -> Array (Binder e) -> Binder eConstructs a constructor binding pattern.
#binderNamed Source
binderNamed :: forall e name. ToName name Ident => name -> Binder e -> Binder eConstructs a named binding pattern (@).
#binderNumber Source
binderNumber :: forall e. Number -> Binder eConstructs a number literal binding pattern.
#binderParens Source
binderParens :: forall e. Binder e -> Binder eWraps a binding pattern in parens.
#binderRecord Source
binderRecord :: forall e field. ToRecordLabeled field (Binder e) => Array field -> Binder eConstructs a record literal binding pattern.
#binderString Source
binderString :: forall e. String -> Binder eConstructs a string literal binding pattern.
#binderTyped Source
binderTyped :: forall e. Binder e -> Type e -> Binder eConstructs a typed binding pattern.
#binderWildcard Source
binderWildcard :: forall e. Binder eConstructs a wildcard (_) binding pattern.
exampleExpr =
exprLambda [ binderWildcard ]
(exprApp (exprIdent "countDown") [ exprInt 100 ])
#blockComment Source
blockComment :: forall a. String -> Array (Comment a)Constructs a block comment.
#caseBranch Source
caseBranch :: forall e f branch. ToNonEmptyArray f => ToGuarded branch e => f (Binder e) -> branch -> Tuple (Separated (Binder e)) (Guarded e)Constructs a case branch for use with exprCase. See exprCase.
#classMember Source
classMember :: forall e name. ToName name Ident => name -> Type e -> ClassMember eConstructs a class member signature. See declClass.
#declClass Source
declClass :: forall e name. ToName name Proper => Array (Type e) -> name -> Array (TypeVarBinding (Name Ident) e) -> Array ClassFundep -> Array (ClassMember e) -> Declaration eConstructs a class declaration.
exampleDecl =
declClass [ typeApp (typeCtor "Eq") [ typeVar "a" ] ] "Ord" [ typeVar "a" ] []
[ classMember "compare"
(typeArrow [ typeVar "a", typeVar "a" ] (typeCtor "Ordering"))
]
#declClassSignature Source
declClassSignature :: forall e a. ToName a Proper => a -> Type e -> Declaration eConstructs a kind signature for a class declaration.
#declData Source
declData :: forall e name. ToName name Proper => name -> Array (TypeVarBinding (Name Ident) e) -> Array (DataCtor e) -> Declaration eConstructs a data declaration.
exampleDecl =
declData "Either" [ typeVar "a", typeVar "b" ]
[ dataCtor "Left" [ typeVar "a" ]
, dataCtor "Right" [ typeVar "b" ]
]
#declDataSignature Source
declDataSignature :: forall e a. ToName a Proper => a -> Type e -> Declaration eConstructs a kind signature for a data declaration.
#declDerive Source
declDerive :: forall e className. ToQualifiedName className Proper => Maybe (Name Ident) -> Array (Type e) -> className -> Array (Type e) -> Declaration eConstructs an instance deriving declaration.
exampleDecl =
declDerive Nothing [] "Eq" [ typeCtor "UserId" ]
#declDeriveNewtype Source
declDeriveNewtype :: forall e className. ToQualifiedName className Proper => Maybe (Name Ident) -> Array (Type e) -> className -> Array (Type e) -> Declaration eConstructs a newtype instance deriving declaration.
exampleDecl =
declDeriveNewtype Nothing [] "Eq" [ typeCtor "UserId" ]
#declForeign Source
declForeign :: forall e name. ToName name Ident => name -> Type e -> Declaration eConstructs a foreign import declaration.
#declForeignData Source
declForeignData :: forall e name. ToName name Proper => name -> Type e -> Declaration eConstructs a foreign data import declaration.
#declImport Source
declImport :: forall e name. ToName name ModuleName => name -> Array (Import e) -> ImportDecl eConstructs an import declaration with selective imports. Providing no named imports results in an open import.
#declImportAs Source
declImportAs :: forall e as name. ToName name ModuleName => ToName as ModuleName => name -> Array (Import e) -> as -> ImportDecl eConstructs a qualified import declaration.
#declImportHiding Source
declImportHiding :: forall e name. ToName name ModuleName => name -> Array (Import e) -> ImportDecl eConstructs a hiding import declaration.
#declImportHidingAs Source
declImportHidingAs :: forall e as name. ToName name ModuleName => ToName as ModuleName => name -> Array (Import e) -> as -> ImportDecl eConstructs a qualified hiding import declaration.
#declInfix Source
declInfix :: forall e name op. ToFixityName name => ToName op Operator => Fixity -> Int -> name -> op -> Declaration eConstructs an fixity declaration for a value operator.
exampleDecl =
declInfix Infixl 4 "map" "<$>"
#declInfixType Source
declInfixType :: forall e name op. ToQualifiedName name Proper => ToName op Operator => Fixity -> Int -> name -> op -> Declaration eConstructs an fixity declaration for a type operator.
exampleDecl =
declInfix Infixr 0 "RowApply" "+"
#declInstanceChain Source
declInstanceChain :: forall e f. ToNonEmptyArray f => f (Instance e) -> Declaration eConstructs an instance chain.
exampleDecl =
declInstanceChain
[ declInstance Nothing [] "IsTypeEqual"
[ typeVar "a", typeVar "a", typeCtor "True" ]
[]
, declInstance Nothing [] "IsTypeEqual"
[ typeVar "a", typeVar "b", typeCtor "False" ]
[]
]
#declNewtype Source
declNewtype :: forall e name ctor. ToName name Proper => ToName ctor Proper => name -> Array (TypeVarBinding (Name Ident) e) -> ctor -> Type e -> Declaration eConstructs a newtype declaration.
exampleDecl =
declNewtype "UserId" [] "UserId" (typeCtor "String")
#declNewtypeSignature Source
declNewtypeSignature :: forall e a. ToName a Proper => a -> Type e -> Declaration eConstructs a kind signature for a newtype declaration.
#declRole Source
declRole :: forall e f name. ToName name Proper => ToNonEmptyArray f => name -> f Role -> Declaration eConstructs a role declaration.
#declSignature Source
declSignature :: forall e a. ToName a Ident => a -> Type e -> Declaration eConstructs a type signature for a value declaration.
#declType Source
declType :: forall e name. ToName name Proper => name -> Array (TypeVarBinding (Name Ident) e) -> Type e -> Declaration eConstructs a type synonym declaration.
exampleDecl =
declType "UserFields" [ typeVar "r" ]
( typeRow
[ Tuple "id" (typeCtor "UserId")
, Tuple "name" (typeCtor "String")
, Tuple "age" (typeCtor "Int")
]
(Just (typeVar "r"))
)
#declTypeSignature Source
declTypeSignature :: forall e a. ToName a Proper => a -> Type e -> Declaration eConstructs a kind signature for a type synonym declaration.
#declValue Source
declValue :: forall e name guards. ToName name Ident => ToGuarded guards e => name -> Array (Binder e) -> guards -> Declaration eConstructs a value declaration.
exampleDecl =
declValue "countDown" [ binderVar "n" ]
[ guardBranch [ guardExpr (exprOp (exprIdent "n") [ binaryOp ">" (exprInt 0) ]) ]
( exprApp (exprIdent "countDown")
[ exprOp (exprIdent "n") [ binaryOp "-" (exprInt 1) ] ]
)
, guardBranch [ guardExpr (exprIdent "otherwise") ]
(exprIdent "n")
]
#doBind Source
doBind :: forall e. Binder e -> Expr e -> DoStatement eConstructs a do statement which binds it's return value.
exampleExpr =
exprDo
[ doBind (binderRecord [ "followers" ])
(exprApp (exprIdent "getUser") [ exprIdent "user" ])
]
( exprApp (exprIdent "pure")
[ exprIdent "followers" ]
)
#doDiscard Source
doDiscard :: forall e. Expr e -> DoStatement eConstructs a do statement which has no binder.
exampleExpr =
exprDo
[ doDiscard
( exprApp (exprIdent "logoutUser")
[ exprIdent "user" ]
)
]
( exprApp (exprIdent "pure")
[ exprApp (exprIdent "httpStatus")
[ exprInt 200 ]
]
)
#doLet Source
doLet :: forall e f. ToNonEmptyArray f => f (LetBinding e) -> DoStatement eConstructs a let binding context within a do expression.
exampleExpr =
exprDo
[ doLet
[ letBinder (binderRecord [ "age" ])
(exprIdent "user")
]
]
(exprIdent "age")
#docComments Source
docComments :: String -> Array (Comment LineFeed)Constructs documentation line comments (-- |).
#exportClass Source
exportClass :: forall e name. ToName name Proper => name -> Export eConstructs a class export.
#exportModule Source
exportModule :: forall e name. ToName name ModuleName => name -> Export eConstructs a module re-export.
#exportOp Source
exportOp :: forall e name. ToName name SymbolName => name -> Export eConstructs a value operator export.
#exportType Source
exportType :: forall e name. ToName name Proper => name -> Export eConstructs a type export.
#exportTypeAll Source
exportTypeAll :: forall e name. ToName name Proper => name -> Export eConstructs a type export with all data members.
#exportTypeOp Source
exportTypeOp :: forall e name. ToName name SymbolName => name -> Export eConstructs a type operator export.
#exportValue Source
exportValue :: forall e name. ToName name Ident => name -> Export eConstructs a value export.
#exprAdo Source
exprAdo :: forall e. Array (DoStatement e) -> Expr e -> Expr eConstructs an ado expression. Works just like exprDo.
#exprCase Source
exprCase :: forall e f g. ToNonEmptyArray f => ToNonEmptyArray g => f (Expr e) -> g (Tuple (Separated (Binder e)) (Guarded e)) -> Expr eConstructs a case expression.
exampleExpr =
exprCase [ exprIdent "xs" ]
[ caseBranch [ binderCtor "List.Cons" [ binderVar "x", binderWildcard ] ]
( exprApp (exprCtor "Just")
[ exprIdent "x"
]
)
, caseBranch [ binderCtor "Nothing" [] ]
(exprCtor "Nothing")
]
#exprCtor Source
exprCtor :: forall e name. ToQualifiedName name Proper => name -> Expr eAn overloaded constructor for a value constructor, which may be qualified.
exampleExpr =
exprApp (exprCtor "List.Cons")
[ exprIdent "a"
, exprCtor "List.Nil"
]
#exprDo Source
exprDo :: forall e. Array (DoStatement e) -> Expr e -> Expr eConstructs a do expression.
exampleExpr =
exprDo
[ doBind (binderVar "followers")
(exprApp (exprIdent "getFollowers") [ exprIdent "user" ])
, doBind (binderVar "favorites")
(exprApp (exprIdent "getFavorites") [ exprIdent "user" ])
]
( exprApp (exprIdent "pure")
[ exprRecord [ "followers", "favorites" ] ]
)
#exprIdent Source
exprIdent :: forall e name. ToQualifiedName name Ident => name -> Expr eAn overloaded constructor for a value identifier, which may be qualified.
exampleExpr =
exprApp (exprIdent "Map.lookup")
[ exprIdent "userId"
, exprIdent "users"
]
#exprIntHex Source
exprIntHex :: forall e. Int -> Expr eConstructs an Int literal using hex notation.
exampleExpr = exprIntHex 0xFF0000
#exprLambda Source
exprLambda :: forall e. Array (Binder e) -> Expr e -> Expr eConstructs a lambda expression.
exampleExpr =
exprLambda [ binderVar "a", binderVar "b" ]
( exprOp (exprIdent "a")
[ binaryOp "<>" (exprIdent "b") ]
)
#exprLet Source
exprLet :: forall e. Array (LetBinding e) -> Expr e -> Expr eConstructs a let expression.
exampleExpr =
exprLet
[ letSignature "countDown" (typeArrow [ typeCtor "Int" ] (typeCtor "Int"))
, letValue "countDown" [ binderVar "n" ]
[ guardBranch [ guardExpr (exprOp (exprIdent "n") [ binaryOp ">" (exprInt 0) ]) ]
( exprApp (exprIdent "countDown")
[ exprOp (exprIdent "n") [ binaryOp "-" (exprInt 1) ] ]
)
, guardBranch [ guardExpr (exprIdent "otherwise") ]
(exprIdent "n")
]
]
(exprApp (exprIdent "countDown") [ exprInt 100 ])
#exprNegate Source
exprNegate :: forall e. Expr e -> Expr eConstructs unary negation.
#exprOpName Source
exprOpName :: forall e name. ToQualifiedName name SymbolName => name -> Expr eOverloaded constructor for an expression operator's symbolic identifier (wrapping it in parens).
exampleExpr = exprOpName "(<>)"
#exprParens Source
exprParens :: forall e. Expr e -> Expr eWraps an expression in parens.
#exprRecord Source
exprRecord :: forall e field. ToRecordLabeled field (Expr e) => Array field -> Expr eConstructs a Record literal.
exampleExpr = exprRecord
[ Tuple "id" (exprIdent "userId")
, Tuple "name" (exprIdent "userName")
, Tuple "age" (exprIdent "userAge")
]
#exprSection Source
exprSection :: forall e. Expr eConstructs an expression section (_). This is meaningless and will
produce invalid syntax when used arbitrarily. Pair with appropriate
constructors like exprCase or exprDot.
#exprString Source
exprString :: forall e. String -> Expr eConstructs a String literal while handling escaping.
exampleExpr = exprString "string"
#exprTypeApp Source
exprTypeApp :: forall e. Expr e -> Array (Type e) -> Array (Expr e) -> Expr eConstructs left-associated applications, with optional type applications.
exampleExpr =
exprTypeApp (exprIdent "Map.lookup")
[ typeCtor "String"
, typeApp (typeCtor "Maybe") [ typeCtor "User" ]
]
[ exprIdent "userId"
, exprIdent "users"
]
#exprUpdate Source
exprUpdate :: forall e. Expr e -> Array (RecordUpdate e) -> Expr eConstructs a record update.
exampleExpr =
exprUpdate (exprIdent "user")
[ update "age" (exprInt 42)
, updateNested "phone"
[ update "countryCode" (exprInt 1)
]
]
#exprWhere Source
exprWhere :: forall e. Expr e -> Array (LetBinding e) -> Where eConstructs a where let binding context. This can be used anywhere that
takes a ToWhere or ToGuarded constraint, such as in value bindings,
case branches, and guards.
exampleDecl =
declValue "getName" [ binderVar "user" ]
( exprWhere (exprIdent "name")
[ letBinder (binderRecord [ "name" ])
(exprIdent "user")
]
)
#guardBinder Source
guardBinder :: forall e. Binder e -> Expr e -> PatternGuard eConstructs a guard expression that binds a value.
#guardBranch Source
guardBranch :: forall e f expr. ToNonEmptyArray f => ToWhere expr e => f (PatternGuard e) -> expr -> GuardedBranch eConstructs a guarded branch in a value binding or case expressions.
This can be used anwhere that takes a ToGuarded constraint.
exampleDecl =
declValue "countDown" [ binderVar "n" ]
[ guardBranch [ guardExpr (exprOp (exprIdent "n") [ binaryOp ">" (exprInt 0) ]) ]
( exprApp (exprIdent "countDown")
[ exprOp (exprIdent "n") [ binaryOp "-" (exprInt 1) ] ]
)
, guardBranch [ guardExpr (exprIdent "otherwise") ]
(exprIdent "n")
]
#guardExpr Source
guardExpr :: forall e. Expr e -> PatternGuard eConstructs a guard expression that does not bind anything.
#importClass Source
importClass :: forall e name. ToName name Proper => name -> Import eConstructs a class import.
#importOp Source
importOp :: forall e name. ToName name SymbolName => name -> Import eConstructs a value operator import.
#importType Source
importType :: forall e name. ToName name Proper => name -> Import eConstructs a type import.
#importTypeAll Source
importTypeAll :: forall e name. ToName name Proper => name -> Import eConstructs a type import with all data members.
#importTypeOp Source
importTypeOp :: forall e name. ToName name SymbolName => name -> Import eConstructs a type operator import.
#importValue Source
importValue :: forall e name. ToName name Ident => name -> Import eConstructs a value import.
#instSignature Source
instSignature :: forall e a. ToName a Ident => a -> Type e -> InstanceBinding eConstructs an signature for an instance binding.
#leading Source
leading :: forall a. OverLeadingComments a => Array (Comment LineFeed) -> a -> aAttaches leading comments to a CST node.
#letBinder Source
letBinder :: forall e rhs. ToWhere rhs e => Binder e -> rhs -> LetBinding eConstructs a value binding with a left-hand-side pattern binder.
exampleExpr =
exprLet
[ letBinder (binderRecord [ "name" ])
(exprIdent "user")
]
(exprIdent "name")
#letSignature Source
letSignature :: forall e name. ToName name Ident => name -> Type e -> LetBinding eConstructs a type signature in a let binding context. See exprLet.
#lineComments Source
lineComments :: String -> Array (Comment LineFeed)Constructs line comments (--).
#module_ Source
module_ :: forall e name. ToName name ModuleName => name -> Array (Export e) -> Array (ImportDecl e) -> Array (Declaration e) -> Module eConstructs a module.
#trailing Source
trailing :: forall a trl. OverTrailingComments a trl => a -> Array (Comment trl) -> aAttaches trailing comments to a CST node.
#typeArrowName Source
typeArrowName :: forall e. Type eThe function arrow's symbolic identifier ((->)).
#typeConstrained Source
typeConstrained :: forall e. Array (Type e) -> Type e -> Type eConstructs right-associated constraint arrows.
exampleType =
typeForall [ typeVar "f", typeVar "a" ]
( typeConstrained
[ typeApp (typeCtor "Functor") [ typeVar "f" ]
, typeApp (typeCtor "Show") [ typeVar "a" ]
]
( typeArrow
[ typeApp (typeVar "f")
[ typeVar "a" ]
]
( typeApp (typeVar "f")
[ typeCtor "String" ]
)
)
)
#typeCtor Source
typeCtor :: forall e name. ToQualifiedName name Proper => name -> Type eAn overloaded constructor for a proper type name.
exampleType = typeApp (typeCtor "Maybe") [ typeCtor "Int" ]
#typeForall Source
typeForall :: forall e. Array (TypeVarBinding (Prefixed (Name Ident)) e) -> Type e -> Type eConstructs a forall given type variable bindings.
exampleType =
typeForall [ typeVar "a" ]
(typeArrow [ typeVar "a" ] (typeVar "a"))
#typeKinded Source
typeKinded :: forall e. Type e -> Type e -> Type eConstructs a type with a kind annotation.
exampleType =
typeKinded (typeCtor "Maybe")
(typeArrow [ typeCtor "Type" ] (typeCtor "Type"))
#typeOpName Source
typeOpName :: forall e name. ToQualifiedName name SymbolName => name -> Type eOverloaded constructor for a type operator's symbolic identifier (wrapping it in parens), which may be qualified.
exampleType = typeOpName "(~>)"
#typeParens Source
typeParens :: forall e. Type e -> Type eWraps a type in parens.
#typeRecordEmpty Source
typeRecordEmpty :: forall e. Type eThe empty record type.
#typeRowEmpty Source
typeRowEmpty :: forall e. Type eThe empty row type.
#typeString Source
typeString :: forall e. String -> Type eConstructs a type-level string while handling escaping.
exampleType = typeString "string"
#typeWildcard Source
typeWildcard :: forall e. Type eA type wildcard (_).
#update Source
update :: forall e a. ToName a Label => a -> Expr e -> RecordUpdate eConstructs an update for a field.
#updateNested Source
updateNested :: forall f e label. ToNonEmptyArray f => ToName label Label => label -> f (RecordUpdate e) -> RecordUpdate eConstructs a nested update for a field.
An overloaded constructor for instances. Can be used to construct a declaration directly, or within an instance chain.