-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathPattern.qll
More file actions
369 lines (306 loc) · 10.4 KB
/
Pattern.qll
File metadata and controls
369 lines (306 loc) · 10.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
overlay[local]
module;
private import codeql.ruby.AST
private import internal.AST
private import internal.Pattern
private import internal.TreeSitter
private import internal.Variable
private import internal.Parameter
private class TPatternNode =
TArrayPattern or TFindPattern or THashPattern or TAlternativePattern or TAsPattern or
TParenthesizedPattern or TExpressionReferencePattern or TVariableReferencePattern;
private class TPattern =
TPatternNode or TLiteral or TLambda or TConstantAccess or TLocalVariableAccess or
TUnaryArithmeticOperation;
/**
* A pattern used in a `case-in` expression. For example
* ```rb
* case expr
* in [ x ] then ...
* in Point(a:, b:) then ...
* in Integer => x then ...
* end
* ```
*/
class CasePattern extends AstNode, TPattern {
CasePattern() {
casePattern(toGenerated(this)) or
synthChild(any(HashPattern p), _, this)
}
}
/**
* An array pattern, for example:
* ```rb
* in []
* in ["first", Integer => x, "last"]
* in ["a", Integer => x, *]
* in ["a", Integer => x, ]
* in [1, 2, *x, 7, 8]
* in [*init, 7, 8]
* in List["a", Integer => x, *tail]
* ```
*/
class ArrayPattern extends CasePattern, TArrayPattern {
private Ruby::ArrayPattern g;
ArrayPattern() { this = TArrayPattern(g) }
/** Gets the class this pattern matches objects against, if any. */
ConstantReadAccess getClass() { toGenerated(result) = g.getClass() }
/**
* Gets the `n`th element of this list pattern's prefix, i.e. the elements `1, ^two, 3`
* in the following examples:
* ```
* in [ 1, ^two, 3 ]
* in [ 1, ^two, 3, ]
* in [ 1, ^two, 3, *, 4 , 5]
* in [ 1, ^two, 3, *more]
* ```
*/
CasePattern getPrefixElement(int n) {
toGenerated(result) = g.getChild(n) and
(
n < this.restIndex()
or
not exists(this.restIndex())
)
}
/**
* Gets the `n`th element of this list pattern's suffix, i.e. the elements `4, 5`
* in the following examples:
* ```
* in [ *, 4, 5 ]
* in [ 1, 2, 3, *middle, 4 , 5]
* ```
*/
CasePattern getSuffixElement(int n) { toGenerated(result) = g.getChild(n + this.restIndex() + 1) }
/**
* Gets the variable of the rest token, if any. For example `middle` in `the following array pattern.
* ```rb
* [ 1, 2, 3, *middle, 4 , 5]
* ```
*/
LocalVariableWriteAccess getRestVariableAccess() {
toGenerated(result) = g.getChild(this.restIndex()).(Ruby::SplatParameter).getName()
}
/**
* Holds if this pattern permits any unmatched remaining elements, i.e. the pattern does not have a trailing `,`
* and does not contain a rest token (`*` or `*name`) either.
*/
predicate allowsUnmatchedElements() { not exists(this.restIndex()) }
private int restIndex() { g.getChild(result) instanceof Ruby::SplatParameter }
final override string getAPrimaryQlClass() { result = "ArrayPattern" }
final override string toString() { result = "[ ..., * ]" }
final override AstNode getAChild(string pred) {
result = super.getAChild(pred)
or
pred = "getClass" and result = this.getClass()
or
pred = "getPrefixElement" and result = this.getPrefixElement(_)
or
pred = "getSuffixElement" and result = this.getSuffixElement(_)
or
pred = "getRestVariableAccess" and result = this.getRestVariableAccess()
}
}
/**
* A find pattern, for example:
* ```rb
* in [*, "a", Integer => x, *]
* in List[*init, "a", Integer => x, *tail]
* in List[*, "a", Integer => x, *]
* ```
*/
class FindPattern extends CasePattern, TFindPattern {
private Ruby::FindPattern g;
FindPattern() { this = TFindPattern(g) }
/** Gets the class this pattern matches objects against, if any. */
ConstantReadAccess getClass() { toGenerated(result) = g.getClass() }
/** Gets the `n`th element of this list pattern. */
CasePattern getElement(int n) { toGenerated(result) = g.getChild(n + 1) }
/** Gets an element of this list pattern. */
CasePattern getAnElement() { result = this.getElement(_) }
/**
* Gets the variable for the prefix of this find pattern, if any. For example `init` in:
* ```rb
* in List[*init, "a", Integer => x, *tail]
* ```
*/
LocalVariableWriteAccess getPrefixVariableAccess() {
toGenerated(result) = g.getChild(0).(Ruby::SplatParameter).getName()
}
/**
* Gets the variable for the suffix of this find pattern, if any. For example `tail` in:
* ```rb
* in List[*init, "a", Integer => x, *tail]
* ```
*/
LocalVariableWriteAccess getSuffixVariableAccess() {
toGenerated(result) = max(int i | | g.getChild(i) order by i).(Ruby::SplatParameter).getName()
}
final override string getAPrimaryQlClass() { result = "FindPattern" }
final override string toString() { result = "[ *,...,* ]" }
final override AstNode getAChild(string pred) {
result = super.getAChild(pred)
or
pred = "getClass" and result = this.getClass()
or
pred = "getElement" and result = this.getElement(_)
or
pred = "getPrefixVariableAccess" and result = this.getPrefixVariableAccess()
or
pred = "getSuffixVariableAccess" and result = this.getSuffixVariableAccess()
}
}
/**
* A hash pattern, for example:
* ```rb
* in {}
* in { a: 1 }
* in { a: 1, **rest }
* in { a: 1, **nil }
* in Node{ label: , children: [] }
* ```
*/
class HashPattern extends CasePattern, THashPattern {
private Ruby::HashPattern g;
HashPattern() { this = THashPattern(g) }
/** Gets the class this pattern matches objects against, if any. */
ConstantReadAccess getClass() { toGenerated(result) = g.getClass() }
private Ruby::KeywordPattern keyValuePair(int n) { result = g.getChild(n) }
/** Gets the key of the `n`th pair. */
StringlikeLiteral getKey(int n) { toGenerated(result) = this.keyValuePair(n).getKey() }
/** Gets the value of the `n`th pair. */
CasePattern getValue(int n) {
toGenerated(result) = this.keyValuePair(n).getValue() or
synthChild(this, n, result)
}
/** Gets the value for a given key name. */
overlay[global]
CasePattern getValueByKey(string key) {
exists(int i |
this.getKey(i).getConstantValue().isStringlikeValue(key) and result = this.getValue(i)
)
}
/**
* Gets the variable of the keyword rest token, if any. For example `rest` in:
* ```rb
* in { a: 1, **rest }
* ```
*/
LocalVariableWriteAccess getRestVariableAccess() {
toGenerated(result) =
max(int i | | g.getChild(i) order by i).(Ruby::HashSplatParameter).getName()
}
/**
* Holds if this pattern is terminated by `**nil` indicating that the pattern does not permit
* any unmatched remaining pairs.
*/
predicate allowsUnmatchedElements() { g.getChild(_) instanceof Ruby::HashSplatNil }
final override string getAPrimaryQlClass() { result = "HashPattern" }
final override string toString() { result = "{ ..., ** }" }
final override AstNode getAChild(string pred) {
result = super.getAChild(pred)
or
pred = "getClass" and result = this.getClass()
or
pred = "getKey" and result = this.getKey(_)
or
pred = "getValue" and result = this.getValue(_)
or
pred = "getRestVariableAccess" and result = this.getRestVariableAccess()
}
}
/**
* A composite pattern matching one of the given sub-patterns, for example:
* ```rb
* in 1 | 2 | 3
* ```
*/
class AlternativePattern extends CasePattern, TAlternativePattern {
private Ruby::AlternativePattern g;
AlternativePattern() { this = TAlternativePattern(g) }
/** Gets the `n`th alternative of this pattern. */
CasePattern getAlternative(int n) { toGenerated(result) = g.getAlternatives(n) }
/** Gets an alternative of this pattern. */
CasePattern getAnAlternative() { result = this.getAlternative(_) }
final override string getAPrimaryQlClass() { result = "AlternativePattern" }
final override string toString() { result = "... | ..." }
final override AstNode getAChild(string pred) {
result = super.getAChild(pred)
or
pred = "getAlternative" and result = this.getAlternative(_)
}
}
/**
* A pattern match that binds to the specified local variable, for example `Integer => a`
* in the following:
* ```rb
* case 1
* in Integer => a then puts "#{a} is an integer value"
* end
* ```
*/
class AsPattern extends CasePattern, TAsPattern {
private Ruby::AsPattern g;
AsPattern() { this = TAsPattern(g) }
/** Gets the underlying pattern. */
CasePattern getPattern() { toGenerated(result) = g.getValue() }
/** Gets the variable access for this pattern. */
LocalVariableWriteAccess getVariableAccess() { toGenerated(result) = g.getName() }
final override string getAPrimaryQlClass() { result = "AsPattern" }
final override string toString() { result = "... => ..." }
final override AstNode getAChild(string pred) {
result = super.getAChild(pred)
or
pred = "getPattern" and result = this.getPattern()
or
pred = "getVariableAccess" and result = this.getVariableAccess()
}
}
/**
* A parenthesized pattern:
* ```rb
* in (1 ..)
* in (0 | "" | [] | {})
* ```
*/
class ParenthesizedPattern extends CasePattern, TParenthesizedPattern {
private Ruby::ParenthesizedPattern g;
ParenthesizedPattern() { this = TParenthesizedPattern(g) }
/** Gets the underlying pattern. */
final CasePattern getPattern() { toGenerated(result) = g.getChild() }
final override string getAPrimaryQlClass() { result = "ParenthesizedPattern" }
final override string toString() { result = "( ... )" }
final override AstNode getAChild(string pred) {
result = super.getAChild(pred)
or
pred = "getPattern" and result = this.getPattern()
}
}
/**
* A variable or value reference in a pattern, i.e. `^x`, and `^(2 * x)` in the following example:
* ```rb
* x = 10
* case expr
* in ^x then puts "ok"
* in ^(2 * x) then puts "ok"
* end
* ```
*/
class ReferencePattern extends CasePattern, TReferencePattern {
private Ruby::AstNode value;
ReferencePattern() {
value = any(Ruby::VariableReferencePattern g | this = TVariableReferencePattern(g)).getName()
or
value =
any(Ruby::ExpressionReferencePattern g | this = TExpressionReferencePattern(g)).getValue()
}
/** Gets the value this reference pattern matches against. For example `2 * x` in `^(2 * x)` */
final Expr getExpr() { toGenerated(result) = value }
final override string getAPrimaryQlClass() { result = "ReferencePattern" }
final override string toString() { result = "^..." }
final override AstNode getAChild(string pred) {
result = super.getAChild(pred)
or
pred = "getExpr" and result = this.getExpr()
}
}