{-# LANGUAGE CPP #-}
module Stan.Pattern.Ast
(
PatternAst (..)
, Literal (..)
, namesToPatternAst
, anyNamesToPatternAst
, app
, opApp
, constructor
, constructorNameIdentifier
, dataDecl
, fixity
, fun
, guardBranch
, lazyField
, range
, rhs
, tuple
, typeSig
, case'
, lambdaCase
, patternMatchBranch
, patternMatchArrow
, patternMatch_
, literalPat
, wildPat
, literalAnns
) where
import Stan.Hie.Compat (DeclType, NodeAnnotation, mkNodeAnnotation, conDec)
import Stan.NameMeta (NameMeta (..))
import Stan.Pattern.Edsl (PatternBool (..))
import Stan.Pattern.Type (PatternType)
import qualified Data.Set as Set
data PatternAst
= PatternAstConstant !Literal
| PatternAstName !NameMeta !PatternType
| PatternAstVarName !String
| PatternAstNode
!(Set NodeAnnotation)
| PatternAstNodeExact
!(Set NodeAnnotation)
![PatternAst]
| PatternAstAnything
| PatternAstOr !PatternAst !PatternAst
| PatternAstAnd !PatternAst !PatternAst
| PatternAstNeg !PatternAst
| PatternAstIdentifierDetailsDecl !DeclType
deriving stock (Int -> PatternAst -> ShowS
[PatternAst] -> ShowS
PatternAst -> String
(Int -> PatternAst -> ShowS)
-> (PatternAst -> String)
-> ([PatternAst] -> ShowS)
-> Show PatternAst
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PatternAst -> ShowS
showsPrec :: Int -> PatternAst -> ShowS
$cshow :: PatternAst -> String
show :: PatternAst -> String
$cshowList :: [PatternAst] -> ShowS
showList :: [PatternAst] -> ShowS
Show, PatternAst -> PatternAst -> Bool
(PatternAst -> PatternAst -> Bool)
-> (PatternAst -> PatternAst -> Bool) -> Eq PatternAst
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PatternAst -> PatternAst -> Bool
== :: PatternAst -> PatternAst -> Bool
$c/= :: PatternAst -> PatternAst -> Bool
/= :: PatternAst -> PatternAst -> Bool
Eq)
instance PatternBool PatternAst where
(?) :: PatternAst
? :: PatternAst
(?) = PatternAst
PatternAstAnything
neg :: PatternAst -> PatternAst
neg :: PatternAst -> PatternAst
neg = PatternAst -> PatternAst
PatternAstNeg
(|||) :: PatternAst -> PatternAst -> PatternAst
||| :: PatternAst -> PatternAst -> PatternAst
(|||) = PatternAst -> PatternAst -> PatternAst
PatternAstOr
(&&&) :: PatternAst -> PatternAst -> PatternAst
&&& :: PatternAst -> PatternAst -> PatternAst
(&&&) = PatternAst -> PatternAst -> PatternAst
PatternAstAnd
data Literal
= ExactNum !Int
| ExactStr !ByteString
| PrefixStr !ByteString
| ContainStr !ByteString
| AnyLiteral
deriving stock (Int -> Literal -> ShowS
[Literal] -> ShowS
Literal -> String
(Int -> Literal -> ShowS)
-> (Literal -> String) -> ([Literal] -> ShowS) -> Show Literal
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Literal -> ShowS
showsPrec :: Int -> Literal -> ShowS
$cshow :: Literal -> String
show :: Literal -> String
$cshowList :: [Literal] -> ShowS
showList :: [Literal] -> ShowS
Show, Literal -> Literal -> Bool
(Literal -> Literal -> Bool)
-> (Literal -> Literal -> Bool) -> Eq Literal
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Literal -> Literal -> Bool
== :: Literal -> Literal -> Bool
$c/= :: Literal -> Literal -> Bool
/= :: Literal -> Literal -> Bool
Eq)
namesToPatternAst :: NonEmpty (NameMeta, PatternType) -> PatternAst
namesToPatternAst :: NonEmpty (NameMeta, PatternType) -> PatternAst
namesToPatternAst ((NameMeta
nm, PatternType
pat) :| []) = NameMeta -> PatternType -> PatternAst
PatternAstName NameMeta
nm PatternType
pat
namesToPatternAst ((NameMeta
nm, PatternType
pat) :| (NameMeta, PatternType)
x:[(NameMeta, PatternType)]
rest) = PatternAst -> PatternAst -> PatternAst
PatternAstOr
(NameMeta -> PatternType -> PatternAst
PatternAstName NameMeta
nm PatternType
pat)
(NonEmpty (NameMeta, PatternType) -> PatternAst
namesToPatternAst (NonEmpty (NameMeta, PatternType) -> PatternAst)
-> NonEmpty (NameMeta, PatternType) -> PatternAst
forall a b. (a -> b) -> a -> b
$ (NameMeta, PatternType)
x (NameMeta, PatternType)
-> [(NameMeta, PatternType)] -> NonEmpty (NameMeta, PatternType)
forall a. a -> [a] -> NonEmpty a
:| [(NameMeta, PatternType)]
rest)
anyNamesToPatternAst :: NonEmpty NameMeta -> PatternAst
anyNamesToPatternAst :: NonEmpty NameMeta -> PatternAst
anyNamesToPatternAst = NonEmpty (NameMeta, PatternType) -> PatternAst
namesToPatternAst (NonEmpty (NameMeta, PatternType) -> PatternAst)
-> (NonEmpty NameMeta -> NonEmpty (NameMeta, PatternType))
-> NonEmpty NameMeta
-> PatternAst
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (NameMeta -> (NameMeta, PatternType))
-> NonEmpty NameMeta -> NonEmpty (NameMeta, PatternType)
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (, PatternType
forall a. PatternBool a => a
(?))
app :: PatternAst -> PatternAst -> PatternAst
app :: PatternAst -> PatternAst -> PatternAst
app PatternAst
f PatternAst
x = Set NodeAnnotation -> [PatternAst] -> PatternAst
PatternAstNodeExact (OneItem (Set NodeAnnotation) -> Set NodeAnnotation
forall x. One x => OneItem x -> x
one (FastString -> FastString -> NodeAnnotation
mkNodeAnnotation FastString
"HsApp" FastString
"HsExpr")) [PatternAst
f, PatternAst
x]
opApp :: PatternAst -> PatternAst -> PatternAst -> PatternAst
opApp :: PatternAst -> PatternAst -> PatternAst -> PatternAst
opApp PatternAst
x PatternAst
op PatternAst
y = Set NodeAnnotation -> [PatternAst] -> PatternAst
PatternAstNodeExact (OneItem (Set NodeAnnotation) -> Set NodeAnnotation
forall x. One x => OneItem x -> x
one (FastString -> FastString -> NodeAnnotation
mkNodeAnnotation FastString
"OpApp" FastString
"HsExpr")) [PatternAst
x, PatternAst
op, PatternAst
y]
range :: PatternAst -> PatternAst -> PatternAst
range :: PatternAst -> PatternAst -> PatternAst
range PatternAst
from PatternAst
to = Set NodeAnnotation -> [PatternAst] -> PatternAst
PatternAstNodeExact (OneItem (Set NodeAnnotation) -> Set NodeAnnotation
forall x. One x => OneItem x -> x
one (FastString -> FastString -> NodeAnnotation
mkNodeAnnotation FastString
"ArithSeq" FastString
"HsExpr")) [PatternAst
from, PatternAst
to]
lambdaCase :: PatternAst
lambdaCase :: PatternAst
lambdaCase = Set NodeAnnotation -> PatternAst
PatternAstNode (OneItem (Set NodeAnnotation) -> Set NodeAnnotation
forall x. One x => OneItem x -> x
one (FastString -> FastString -> NodeAnnotation
mkNodeAnnotation
#if __GLASGOW_HASKELL__ < 910
FastString
"HsLamCase"
#else
"HsLam"
#endif
FastString
"HsExpr"))
case' :: PatternAst
case' :: PatternAst
case' = Set NodeAnnotation -> PatternAst
PatternAstNode (OneItem (Set NodeAnnotation) -> Set NodeAnnotation
forall x. One x => OneItem x -> x
one (FastString -> FastString -> NodeAnnotation
mkNodeAnnotation FastString
"HsCase" FastString
"HsExpr"))
patternMatchBranch :: PatternAst
patternMatchBranch :: PatternAst
patternMatchBranch = Set NodeAnnotation -> PatternAst
PatternAstNode (OneItem (Set NodeAnnotation) -> Set NodeAnnotation
forall x. One x => OneItem x -> x
one (FastString -> FastString -> NodeAnnotation
mkNodeAnnotation FastString
"Match" FastString
"Match"))
wildPat :: PatternAst
wildPat :: PatternAst
wildPat = Set NodeAnnotation -> PatternAst
PatternAstNode (OneItem (Set NodeAnnotation) -> Set NodeAnnotation
forall x. One x => OneItem x -> x
one (FastString -> FastString -> NodeAnnotation
mkNodeAnnotation FastString
"WildPat" FastString
"Pat"))
literalPat :: PatternAst
literalPat :: PatternAst
literalPat = Set NodeAnnotation -> PatternAst
PatternAstNode (OneItem (Set NodeAnnotation) -> Set NodeAnnotation
forall x. One x => OneItem x -> x
one (FastString -> FastString -> NodeAnnotation
mkNodeAnnotation FastString
"NPat" FastString
"Pat"))
PatternAst -> PatternAst -> PatternAst
forall a. PatternBool a => a -> a -> a
||| Set NodeAnnotation -> PatternAst
PatternAstNode (OneItem (Set NodeAnnotation) -> Set NodeAnnotation
forall x. One x => OneItem x -> x
one (FastString -> FastString -> NodeAnnotation
mkNodeAnnotation FastString
"LitPat" FastString
"Pat"))
patternMatch_ :: PatternAst -> PatternAst
patternMatch_ :: PatternAst -> PatternAst
patternMatch_ PatternAst
val = Set NodeAnnotation -> [PatternAst] -> PatternAst
PatternAstNodeExact (OneItem (Set NodeAnnotation) -> Set NodeAnnotation
forall x. One x => OneItem x -> x
one (FastString -> FastString -> NodeAnnotation
mkNodeAnnotation FastString
"Match" FastString
"Match"))
#if __GLASGOW_HASKELL__ >= 810
([PatternAst] -> PatternAst) -> [PatternAst] -> PatternAst
forall a b. (a -> b) -> a -> b
$ PatternAst
wildPat PatternAst -> [PatternAst] -> [PatternAst]
forall a. a -> [a] -> [a]
:
#endif
[PatternAst -> PatternAst
patternMatchArrow PatternAst
val]
patternMatchArrow :: PatternAst -> PatternAst
patternMatchArrow :: PatternAst -> PatternAst
patternMatchArrow PatternAst
x = Set NodeAnnotation -> [PatternAst] -> PatternAst
PatternAstNodeExact (OneItem (Set NodeAnnotation) -> Set NodeAnnotation
forall x. One x => OneItem x -> x
one (FastString -> FastString -> NodeAnnotation
mkNodeAnnotation FastString
"GRHS" FastString
"GRHS")) [PatternAst
x]
fixity :: PatternAst
fixity :: PatternAst
fixity = Set NodeAnnotation -> PatternAst
PatternAstNode (Set NodeAnnotation -> PatternAst)
-> Set NodeAnnotation -> PatternAst
forall a b. (a -> b) -> a -> b
$ OneItem (Set NodeAnnotation) -> Set NodeAnnotation
forall x. One x => OneItem x -> x
one (FastString -> FastString -> NodeAnnotation
mkNodeAnnotation FastString
"FixitySig" FastString
"FixitySig")
typeSig :: PatternAst
typeSig :: PatternAst
typeSig = Set NodeAnnotation -> PatternAst
PatternAstNode (Set NodeAnnotation -> PatternAst)
-> Set NodeAnnotation -> PatternAst
forall a b. (a -> b) -> a -> b
$ OneItem (Set NodeAnnotation) -> Set NodeAnnotation
forall x. One x => OneItem x -> x
one (FastString -> FastString -> NodeAnnotation
mkNodeAnnotation FastString
"TypeSig" FastString
"Sig")
absBinds :: NodeAnnotation
absBinds :: NodeAnnotation
absBinds =
#if __GLASGOW_HASKELL__ < 904
mkNodeAnnotation "AbsBinds" "HsBindLR"
#else
FastString -> FastString -> NodeAnnotation
mkNodeAnnotation FastString
"XHsBindsLR" FastString
"HsBindLR"
#endif
fun :: PatternAst
fun :: PatternAst
fun = Set NodeAnnotation -> PatternAst
PatternAstNode (Set NodeAnnotation -> PatternAst)
-> Set NodeAnnotation -> PatternAst
forall a b. (a -> b) -> a -> b
$ [NodeAnnotation] -> Set NodeAnnotation
forall a. Ord a => [a] -> Set a
Set.fromList
[ NodeAnnotation
absBinds
, FastString -> FastString -> NodeAnnotation
mkNodeAnnotation FastString
"FunBind" FastString
"HsBindLR"
, FastString -> FastString -> NodeAnnotation
mkNodeAnnotation FastString
"Match" FastString
"Match"
]
dataDecl :: PatternAst
dataDecl :: PatternAst
dataDecl = Set NodeAnnotation -> PatternAst
PatternAstNode (Set NodeAnnotation -> PatternAst)
-> Set NodeAnnotation -> PatternAst
forall a b. (a -> b) -> a -> b
$ OneItem (Set NodeAnnotation) -> Set NodeAnnotation
forall x. One x => OneItem x -> x
one (FastString -> FastString -> NodeAnnotation
mkNodeAnnotation FastString
"DataDecl" FastString
"TyClDecl")
constructor :: PatternAst
constructor :: PatternAst
constructor = Set NodeAnnotation -> PatternAst
PatternAstNode (Set NodeAnnotation -> PatternAst)
-> Set NodeAnnotation -> PatternAst
forall a b. (a -> b) -> a -> b
$ OneItem (Set NodeAnnotation) -> Set NodeAnnotation
forall x. One x => OneItem x -> x
one (FastString -> FastString -> NodeAnnotation
mkNodeAnnotation FastString
"ConDeclH98" FastString
"ConDecl")
constructorNameIdentifier :: PatternAst
constructorNameIdentifier :: PatternAst
constructorNameIdentifier = DeclType -> PatternAst
PatternAstIdentifierDetailsDecl DeclType
conDec
lazyField :: PatternAst
lazyField :: PatternAst
lazyField = PatternAst
lazyRecordField PatternAst -> PatternAst -> PatternAst
forall a. PatternBool a => a -> a -> a
||| PatternAst
type_
type_ :: PatternAst
type_ :: PatternAst
type_ =
Set NodeAnnotation -> PatternAst
PatternAstNode (OneItem (Set NodeAnnotation) -> Set NodeAnnotation
forall x. One x => OneItem x -> x
one (FastString -> FastString -> NodeAnnotation
mkNodeAnnotation FastString
"HsTyVar" FastString
"HsType"))
PatternAst -> PatternAst -> PatternAst
forall a. PatternBool a => a -> a -> a
|||
Set NodeAnnotation -> PatternAst
PatternAstNode (OneItem (Set NodeAnnotation) -> Set NodeAnnotation
forall x. One x => OneItem x -> x
one (FastString -> FastString -> NodeAnnotation
mkNodeAnnotation FastString
"HsAppTy" FastString
"HsType"))
PatternAst -> PatternAst -> PatternAst
forall a. PatternBool a => a -> a -> a
|||
Set NodeAnnotation -> PatternAst
PatternAstNode (OneItem (Set NodeAnnotation) -> Set NodeAnnotation
forall x. One x => OneItem x -> x
one (FastString -> FastString -> NodeAnnotation
mkNodeAnnotation FastString
"HsParTy" FastString
"HsType"))
PatternAst -> PatternAst -> PatternAst
forall a. PatternBool a => a -> a -> a
|||
Set NodeAnnotation -> PatternAst
PatternAstNode (OneItem (Set NodeAnnotation) -> Set NodeAnnotation
forall x. One x => OneItem x -> x
one (FastString -> FastString -> NodeAnnotation
mkNodeAnnotation FastString
"HsTupleTy" FastString
"HsType"))
PatternAst -> PatternAst -> PatternAst
forall a. PatternBool a => a -> a -> a
|||
Set NodeAnnotation -> PatternAst
PatternAstNode (OneItem (Set NodeAnnotation) -> Set NodeAnnotation
forall x. One x => OneItem x -> x
one (FastString -> FastString -> NodeAnnotation
mkNodeAnnotation FastString
"HsListTy" FastString
"HsType"))
PatternAst -> PatternAst -> PatternAst
forall a. PatternBool a => a -> a -> a
|||
Set NodeAnnotation -> PatternAst
PatternAstNode (OneItem (Set NodeAnnotation) -> Set NodeAnnotation
forall x. One x => OneItem x -> x
one (FastString -> FastString -> NodeAnnotation
mkNodeAnnotation FastString
"HsFunTy" FastString
"HsType"))
lazyRecordField :: PatternAst
lazyRecordField :: PatternAst
lazyRecordField = Set NodeAnnotation -> [PatternAst] -> PatternAst
PatternAstNodeExact
(OneItem (Set NodeAnnotation) -> Set NodeAnnotation
forall x. One x => OneItem x -> x
one (FastString -> FastString -> NodeAnnotation
mkNodeAnnotation FastString
"ConDeclField" FastString
"ConDeclField"))
[ Set NodeAnnotation -> PatternAst
PatternAstNode
([Item (Set NodeAnnotation)] -> Set NodeAnnotation
forall l. IsList l => [Item l] -> l
fromList
[ Item (Set NodeAnnotation)
NodeAnnotation
absBinds
, FastString -> FastString -> NodeAnnotation
mkNodeAnnotation FastString
"FunBind" FastString
"HsBindLR"
]
)
, PatternAst
type_
]
tuple :: PatternAst
tuple :: PatternAst
tuple =
Set NodeAnnotation -> PatternAst
PatternAstNode (OneItem (Set NodeAnnotation) -> Set NodeAnnotation
forall x. One x => OneItem x -> x
one (FastString -> FastString -> NodeAnnotation
mkNodeAnnotation FastString
"HsTupleTy" FastString
"HsType"))
PatternAst -> PatternAst -> PatternAst
forall a. PatternBool a => a -> a -> a
|||
Set NodeAnnotation -> PatternAst
PatternAstNode (OneItem (Set NodeAnnotation) -> Set NodeAnnotation
forall x. One x => OneItem x -> x
one (FastString -> FastString -> NodeAnnotation
mkNodeAnnotation FastString
"ExplicitTuple" FastString
"HsExpr"))
guardBranch :: PatternAst
guardBranch :: PatternAst
guardBranch = Set NodeAnnotation -> PatternAst
PatternAstNode (Set NodeAnnotation -> PatternAst)
-> Set NodeAnnotation -> PatternAst
forall a b. (a -> b) -> a -> b
$ OneItem (Set NodeAnnotation) -> Set NodeAnnotation
forall x. One x => OneItem x -> x
one (FastString -> FastString -> NodeAnnotation
mkNodeAnnotation FastString
"BodyStmt" FastString
"StmtLR")
rhs :: PatternAst
rhs :: PatternAst
rhs = Set NodeAnnotation -> PatternAst
PatternAstNode (Set NodeAnnotation -> PatternAst)
-> Set NodeAnnotation -> PatternAst
forall a b. (a -> b) -> a -> b
$ OneItem (Set NodeAnnotation) -> Set NodeAnnotation
forall x. One x => OneItem x -> x
one (FastString -> FastString -> NodeAnnotation
mkNodeAnnotation FastString
"GRHS" FastString
"GRHS")
literalAnns :: NodeAnnotation
literalAnns :: NodeAnnotation
literalAnns = FastString -> FastString -> NodeAnnotation
mkNodeAnnotation FastString
"HsOverLit" FastString
"HsExpr"