forked from ruby-syntax-tree/syntax_tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpattern.rb
287 lines (244 loc) · 8.02 KB
/
pattern.rb
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
# frozen_string_literal: true
module SyntaxTree
# A pattern is an object that wraps a Ruby pattern matching expression. The
# expression would normally be passed to an `in` clause within a `case`
# expression or a rightward assignment expression. For example, in the
# following snippet:
#
# case node
# in Const[value: "SyntaxTree"]
# end
#
# the pattern is the `Const[value: "SyntaxTree"]` expression. Within Syntax
# Tree, every node generates these kinds of expressions using the
# #construct_keys method.
#
# The pattern gets compiled into an object that responds to call by running
# the #compile method. This method itself will run back through Syntax Tree to
# parse the expression into a tree, then walk the tree to generate the
# necessary callable objects. For example, if you wanted to compile the
# expression above into a callable, you would:
#
# callable = SyntaxTree::Pattern.new("Const[value: 'SyntaxTree']").compile
# callable.call(node)
#
# The callable object returned by #compile is guaranteed to respond to #call
# with a single argument, which is the node to match against. It also is
# guaranteed to respond to #===, which means it itself can be used in a `case`
# expression, as in:
#
# case node
# when callable
# end
#
# If the query given to the initializer cannot be compiled into a valid
# matcher (either because of a syntax error or because it is using syntax we
# do not yet support) then a SyntaxTree::Pattern::CompilationError will be
# raised.
class Pattern
# Raised when the query given to a pattern is either invalid Ruby syntax or
# is using syntax that we don't yet support.
class CompilationError < StandardError
def initialize(repr)
super(<<~ERROR)
Syntax Tree was unable to compile the pattern you provided to search
into a usable expression. It failed on to understand the node
represented by:
#{repr}
Note that not all syntax supported by Ruby's pattern matching syntax
is also supported by Syntax Tree's code search. If you're using some
syntax that you believe should be supported, please open an issue on
GitHub at https://github.com/ruby-syntax-tree/syntax_tree/issues/new.
ERROR
end
end
attr_reader :query
def initialize(query)
@query = query
end
def compile
program =
begin
SyntaxTree.parse("case nil\nin #{query}\nend")
rescue Parser::ParseError
raise CompilationError, query
end
compile_node(program.statements.body.first.consequent.pattern)
end
private
# Shortcut for combining two procs into one that returns true if both return
# true.
def combine_and(left, right)
->(other) { left.call(other) && right.call(other) }
end
# Shortcut for combining two procs into one that returns true if either
# returns true.
def combine_or(left, right)
->(other) { left.call(other) || right.call(other) }
end
# Raise an error because the given node is not supported.
def compile_error(node)
raise CompilationError, PP.pp(node, +"").chomp
end
# There are a couple of nodes (string literals, dynamic symbols, and regexp)
# that contain list of parts. This can include plain string content,
# interpolated expressions, and interpolated variables. We only support
# plain string content, so this method will extract out the plain string
# content if it is the only element in the list.
def extract_string(node)
parts = node.parts
if parts.length == 1 && (part = parts.first) && part.is_a?(TStringContent)
part.value
end
end
# in [foo, bar, baz]
def compile_aryptn(node)
compile_error(node) if !node.rest.nil? || node.posts.any?
constant = node.constant
compiled_constant = compile_node(constant) if constant
preprocessed = node.requireds.map { |required| compile_node(required) }
compiled_requireds = ->(other) do
deconstructed = other.deconstruct
deconstructed.length == preprocessed.length &&
preprocessed
.zip(deconstructed)
.all? { |(matcher, value)| matcher.call(value) }
end
if compiled_constant
combine_and(compiled_constant, compiled_requireds)
else
compiled_requireds
end
end
# in foo | bar
def compile_binary(node)
compile_error(node) if node.operator != :|
combine_or(compile_node(node.left), compile_node(node.right))
end
# in Ident
# in String
def compile_const(node)
value = node.value
if SyntaxTree.const_defined?(value, false)
clazz = SyntaxTree.const_get(value)
->(other) { clazz === other }
elsif Object.const_defined?(value, false)
clazz = Object.const_get(value)
->(other) { clazz === other }
else
compile_error(node)
end
end
# in SyntaxTree::Ident
def compile_const_path_ref(node)
parent = node.parent
compile_error(node) if !parent.is_a?(VarRef) || !parent.value.is_a?(Const)
if parent.value.value == "SyntaxTree"
compile_node(node.constant)
else
compile_error(node)
end
end
# in :""
# in :"foo"
def compile_dyna_symbol(node)
if node.parts.empty?
symbol = :""
->(other) { symbol === other }
elsif (value = extract_string(node))
symbol = value.to_sym
->(other) { symbol === other }
else
compile_error(node)
end
end
# in Ident[value: String]
# in { value: String }
def compile_hshptn(node)
compile_error(node) unless node.keyword_rest.nil?
compiled_constant = compile_node(node.constant) if node.constant
preprocessed =
node.keywords.to_h do |keyword, value|
compile_error(node) unless keyword.is_a?(Label)
[keyword.value.chomp(":").to_sym, compile_node(value)]
end
compiled_keywords = ->(other) do
deconstructed = other.deconstruct_keys(preprocessed.keys)
preprocessed.all? do |keyword, matcher|
matcher.call(deconstructed[keyword])
end
end
if compiled_constant
combine_and(compiled_constant, compiled_keywords)
else
compiled_keywords
end
end
# in /foo/
def compile_regexp_literal(node)
if (value = extract_string(node))
regexp = /#{value}/
->(attribute) { regexp === attribute }
else
compile_error(node)
end
end
# in ""
# in "foo"
def compile_string_literal(node)
if node.parts.empty?
->(attribute) { "" === attribute }
elsif (value = extract_string(node))
->(attribute) { value === attribute }
else
compile_error(node)
end
end
# in :+
# in :foo
def compile_symbol_literal(node)
symbol = node.value.value.to_sym
->(attribute) { symbol === attribute }
end
# in Foo
# in nil
def compile_var_ref(node)
value = node.value
if value.is_a?(Const)
compile_node(value)
elsif value.is_a?(Kw) && value.value.nil?
->(attribute) { nil === attribute }
else
compile_error(node)
end
end
# Compile any kind of node. Dispatch out to the individual compilation
# methods based on the type of node.
def compile_node(node)
case node
when AryPtn
compile_aryptn(node)
when Binary
compile_binary(node)
when Const
compile_const(node)
when ConstPathRef
compile_const_path_ref(node)
when DynaSymbol
compile_dyna_symbol(node)
when HshPtn
compile_hshptn(node)
when RegexpLiteral
compile_regexp_literal(node)
when StringLiteral
compile_string_literal(node)
when SymbolLiteral
compile_symbol_literal(node)
when VarRef
compile_var_ref(node)
else
compile_error(node)
end
end
end
end