-
-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathAst.hs
More file actions
314 lines (259 loc) · 8.4 KB
/
Ast.hs
File metadata and controls
314 lines (259 loc) · 8.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
{-# LANGUAGE CPP #-}
{- |
Copyright: (c) 2020 Kowainik
SPDX-License-Identifier: MPL-2.0
Maintainer: Kowainik <xrom.xkov@gmail.com>
Patterns for AST and syntax tree nodes search.
-}
module Stan.Pattern.Ast
( -- * Type
PatternAst (..)
, Literal (..)
-- * Helpers
, namesToPatternAst
, anyNamesToPatternAst
-- * eDSL
, app
, opApp
, constructor
, constructorNameIdentifier
, dataDecl
, fixity
, specializePragma
, fun
, guardBranch
, lazyField
, range
, rhs
, tuple
, typeSig
-- * Pattern matching
, case'
, lambdaCase
, patternMatchBranch
, patternMatchArrow
, patternMatch_
, literalPat
, wildPat
-- * More low-level interface
, literalAnns
) where
import Stan.Ghc.Compat (FastString)
import Stan.Hie.Compat (DeclType (..))
import Stan.NameMeta (NameMeta (..))
import Stan.Pattern.Edsl (PatternBool (..))
import Stan.Pattern.Type (PatternType)
import qualified Data.Set as Set
{- | Query pattern used to search AST nodes in HIE AST. This data type
tries to mirror HIE AST to each future matching, so it's quite
low-level, but helper functions are provided.
-}
data PatternAst
-- | Integer constant in code.
= PatternAstConstant !Literal
-- | Name of a specific function, variable or data type.
| PatternAstName !NameMeta !PatternType
-- | Variable name.
| PatternAstVarName !String
-- | AST node with tags for current node and any children.
| PatternAstNode
!(Set (FastString, FastString)) -- ^ Set of context info (pairs of tags)
-- | AST node with tags for current node and children
-- patterns. This pattern should match the node exactly.
| PatternAstNodeExact
!(Set (FastString, FastString)) -- ^ Set of context info (pairs of tags)
![PatternAst] -- ^ Node children
-- | AST wildcard, matches anything.
| PatternAstAnything
-- | Choice between patterns. Should match either of them.
| PatternAstOr !PatternAst !PatternAst
-- | Union of patterns. Should match both of them.
| PatternAstAnd !PatternAst !PatternAst
-- | Negation of pattern. Should match everything except this pattern.
| PatternAstNeg !PatternAst
-- | AST node with the specified Identifier details (only 'DeclType')
| PatternAstIdentifierDetailsDecl !DeclType
deriving stock (Show, Eq)
instance PatternBool PatternAst where
(?) :: PatternAst
(?) = PatternAstAnything
neg :: PatternAst -> PatternAst
neg = PatternAstNeg
(|||) :: PatternAst -> PatternAst -> PatternAst
(|||) = PatternAstOr
(&&&) :: PatternAst -> PatternAst -> PatternAst
(&&&) = PatternAstAnd
data Literal
= ExactNum !Int
| ExactStr !ByteString
| PrefixStr !ByteString
| ContainStr !ByteString
| AnyLiteral
deriving stock (Show, Eq)
{- | Function that creates 'PatternAst' from the given non-empty list of pairs
'NameMeta' and 'PatternType'.
If the list contains only one 'PatternType' then it is simple 'PatternAstName'.
Else it is 'PatternAstOr' of all such 'PatternAstName's.
-}
namesToPatternAst :: NonEmpty (NameMeta, PatternType) -> PatternAst
namesToPatternAst ((nm, pat) :| []) = PatternAstName nm pat
namesToPatternAst ((nm, pat) :| x:rest) = PatternAstOr
(PatternAstName nm pat)
(namesToPatternAst $ x :| rest)
-- | Like 'namesToPatternAst' but doesn't care about types.
anyNamesToPatternAst :: NonEmpty NameMeta -> PatternAst
anyNamesToPatternAst = namesToPatternAst . fmap (, (?))
-- | @app f x@ is a pattern for function application @f x@.
app :: PatternAst -> PatternAst -> PatternAst
app f x = PatternAstNodeExact (one ("HsApp", "HsExpr")) [f, x]
-- | @opApp x op y@ is a pattern for operator application @x `op` y@.
opApp :: PatternAst -> PatternAst -> PatternAst -> PatternAst
opApp x op y = PatternAstNodeExact (one ("OpApp", "HsExpr")) [x, op, y]
-- | @range a b@ is a pattern for @[a .. b]@
range :: PatternAst -> PatternAst -> PatternAst
range from to = PatternAstNodeExact (one ("ArithSeq", "HsExpr")) [from, to]
-- | 'lambdaCase' is a pattern for @\case@ expression (not considering branches).
lambdaCase :: PatternAst
lambdaCase = PatternAstNode (one ("HsLamCase", "HsExpr"))
-- | 'case'' is a pattern for @case EXP of@ expression (not considering branches).
case' :: PatternAst
case' = PatternAstNode (one ("HsCase", "HsExpr"))
-- | Pattern to represent one pattern matching branch.
patternMatchBranch :: PatternAst
patternMatchBranch = PatternAstNode (one ("Match", "Match"))
{- | Pattern for @_@ in pattern matching.
__Note:__ presents on GHC >=8.10 only.
-}
wildPat :: PatternAst
wildPat = PatternAstNode (one ("WildPat", "Pat"))
{- | Pattern for literals in pattern matching.
__Note:__ presents on GHC >=8.10 only.
-}
literalPat :: PatternAst
literalPat = PatternAstNode (one ("NPat", "Pat"))
||| PatternAstNode (one ("LitPat", "Pat"))
-- | Pattern to represent one pattern matching branch on @_@.
patternMatch_ :: PatternAst -> PatternAst
patternMatch_ val = PatternAstNodeExact (one ("Match", "Match"))
#if __GLASGOW_HASKELL__ >= 810
$ wildPat :
#endif
[patternMatchArrow val]
-- | Pattern to represent right side of the pattern matching, e.g. @-> "foo"@.
patternMatchArrow :: PatternAst -> PatternAst
patternMatchArrow x = PatternAstNodeExact (one ("GRHS", "GRHS")) [x]
{- | Pattern for the top-level fixity declaration:
@
infixr 7 ***, +++, ???
@
-}
fixity :: PatternAst
fixity = PatternAstNode $ one ("FixitySig", "FixitySig")
{- | Pattern for the top-level specialize pragmas declaration:
@
{-# SPECIALIZE foo :: ... #-}
@
-}
specializePragma :: PatternAst
specializePragma = PatternAstNode $ one ("SpecSig", "Sig")
{- | Pattern for the function type signature declaration:
@
foo :: Some -> Type
@
-}
typeSig :: PatternAst
typeSig = PatternAstNode $ one ("TypeSig", "Sig")
{- | Pattern for the function definition:
@
foo x y = ...
@
-}
fun :: PatternAst
fun = PatternAstNode $ Set.fromList
[ ("AbsBinds", "HsBindLR")
, ("FunBind", "HsBindLR")
, ("Match", "Match")
]
{- | @data@ or @newtype@ declaration.
-}
dataDecl :: PatternAst
dataDecl = PatternAstNode $ one ("DataDecl", "TyClDecl")
{- | Constructor of a plain data type or newtype. Children of node
that matches this pattern are constructor fields.
-}
constructor :: PatternAst
constructor = PatternAstNode $ one ("ConDeclH98", "ConDecl")
{- | Constructor name Identifier info
-}
constructorNameIdentifier :: PatternAst
constructorNameIdentifier = PatternAstIdentifierDetailsDecl ConDec
{- | Lazy data type field. Comes in two shapes:
1. Record field, like: @foo :: Text@
2. Simple type: @Int@
-}
lazyField :: PatternAst
lazyField = lazyRecordField ||| type_
{- | Pattern for any occurrence of a plain type. Covers the following
cases:
* Simple type: Int, Bool, a
* Higher-kinded type: Maybe Int, Either String a
* Type in parenthesis: (Int)
* Tuples: (Int, Bool)
* List type: [Int]
* Function type: Int -> Bool
-}
type_ :: PatternAst
type_ =
PatternAstNode (one ("HsTyVar", "HsType")) -- simple type: Int, Bool
|||
PatternAstNode (one ("HsAppTy", "HsType")) -- composite: Maybe Int
|||
PatternAstNode (one ("HsParTy", "HsType")) -- type in ()
|||
PatternAstNode (one ("HsTupleTy", "HsType")) -- tuple types: (Int, Bool)
|||
PatternAstNode (one ("HsListTy", "HsType")) -- list types: [Int]
|||
PatternAstNode (one ("HsFunTy", "HsType")) -- function types: Int -> Bool
{- | Pattern for the field without the explicit bang pattern:
@
someField :: Int
@
-}
lazyRecordField :: PatternAst
lazyRecordField = PatternAstNodeExact
(one ("ConDeclField", "ConDeclField"))
[ PatternAstNode
(fromList
[ ("AbsBinds", "HsBindLR")
, ("FunBind", "HsBindLR")
]
)
, type_
]
{- | Pattern for tuples:
* Type signatures: foo :: (Int, Int, Int, Int)
* Literals: (True, 0, [], Nothing)
-}
tuple :: PatternAst
tuple =
PatternAstNode (one ("HsTupleTy", "HsType")) -- tuple type
|||
PatternAstNode (one ("ExplicitTuple", "HsExpr")) -- tuple literal
{- | Pattern for a single @guard@ branch:
@
| x < y = ...
@
-}
guardBranch :: PatternAst
guardBranch = PatternAstNode $ one ("BodyStmt", "StmtLR")
{- | Pattern for the right-hand-side. Usually an equality sign.
@
foo = baz
@
-}
rhs :: PatternAst
rhs = PatternAstNode $ one ("GRHS", "GRHS")
-- | Annotations for constants: 0, "foo", etc.
literalAnns :: (FastString, FastString)
literalAnns = ("HsOverLit", "HsExpr")