forked from purescript/purescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinders.hs
More file actions
91 lines (85 loc) · 2.4 KB
/
Binders.hs
File metadata and controls
91 lines (85 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
-- |
-- Case binders
--
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 type for binders
--
data Binder
-- |
-- Wildcard binder
--
= NullBinder
-- |
-- A binder which matches a literal
--
| LiteralBinder (Literal Binder)
-- |
-- A binder which binds an identifier
--
| VarBinder Ident
-- |
-- A binder which matches a data constructor
--
| ConstructorBinder (Qualified (ProperName 'ConstructorName)) [Binder]
-- |
-- A operator alias binder. During the rebracketing phase of desugaring,
-- this data constructor will be removed.
--
| OpBinder (Qualified (OpName 'ValueOpName))
-- |
-- Binary operator application. During the rebracketing phase of desugaring,
-- this data constructor will be removed.
--
| BinaryNoParensBinder Binder Binder Binder
-- |
-- Explicit parentheses. During the rebracketing phase of desugaring, this
-- data constructor will be removed.
--
-- Note: although it seems this constructor is not used, it _is_ useful,
-- since it prevents certain traversals from matching.
--
| ParensInBinder Binder
-- |
-- A binder which binds its input to an identifier
--
| NamedBinder Ident Binder
-- |
-- A binder with source position information
--
| PositionedBinder SourceSpan [Comment] Binder
-- |
-- A binder with a type annotation
--
| TypedBinder Type Binder
deriving (Show, Eq)
-- |
-- Collect all names introduced in binders in an expression
--
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