module Language.PureScript.AST.Binders where
import Prelude.Compat
import Language.PureScript.AST.SourcePos
import Language.PureScript.AST.Literals
import Language.PureScript.Names
import Language.PureScript.Comments
import Language.PureScript.Types
data Binder
= NullBinder
| LiteralBinder (Literal Binder)
| VarBinder Ident
| ConstructorBinder (Qualified (ProperName 'ConstructorName)) [Binder]
| OpBinder (Qualified (OpName 'ValueOpName))
| BinaryNoParensBinder Binder Binder Binder
| ParensInBinder Binder
| NamedBinder Ident Binder
| PositionedBinder SourceSpan [Comment] Binder
| TypedBinder Type Binder
deriving (Show, Eq, Ord)
binderNames :: Binder -> [Ident]
binderNames = go []
where
go ns (LiteralBinder b) = lit ns b
go ns (VarBinder name) = name : ns
go ns (ConstructorBinder _ bs) = foldl go ns bs
go ns (BinaryNoParensBinder b1 b2 b3) = foldl go ns [b1, b2, b3]
go ns (ParensInBinder b) = go ns b
go ns (NamedBinder name b) = go (name : ns) b
go ns (PositionedBinder _ _ b) = go ns b
go ns (TypedBinder _ b) = go ns b
go ns _ = ns
lit ns (ObjectLiteral bs) = foldl go ns (map snd bs)
lit ns (ArrayLiteral bs) = foldl go ns bs
lit ns _ = ns
isIrrefutable :: Binder -> Bool
isIrrefutable NullBinder = True
isIrrefutable (VarBinder _) = True
isIrrefutable (PositionedBinder _ _ b) = isIrrefutable b
isIrrefutable (TypedBinder _ b) = isIrrefutable b
isIrrefutable _ = False