Class: RubyNext::Language::Rewriters::PatternMatching
- Inherits:
-
Base
- Object
- Parser::TreeRewriter
- Abstract
- Base
- RubyNext::Language::Rewriters::PatternMatching
show all
- Defined in:
- lib/ruby-next/language/rewriters/2.7/pattern_matching.rb
Constant Summary
collapse
- NAME =
"pattern-matching"
- SYNTAX_PROBE =
"case 0; in 0; true; else; 1; end"
- MIN_SUPPORTED_VERSION =
Gem::Version.new("2.7.0")
- MATCHEE =
:__m__
- MATCHEE_ARR =
:__m_arr__
- MATCHEE_HASH =
:__m_hash__
- ALTERNATION_MARKER =
:__alt__
- CURRENT_HASH_KEY =
:__chk__
Instance Attribute Summary
Attributes inherited from Base
#locals
Instance Method Summary
collapse
Methods inherited from Base
ast?, #initialize, #s
Methods inherited from Abstract
ast?, #initialize, text?, unsupported_syntax?, unsupported_version?
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(mid, *args, &block) ⇒ Object
1043
1044
1045
1046
1047
1048
1049
|
# File 'lib/ruby-next/language/rewriters/2.7/pattern_matching.rb', line 1043
def method_missing(mid, *args, &block)
mid = mid.to_s
return case_eq_clause(*args) if mid.match?(/_clause$/)
return case_eq_array_element(*args) if mid.match?(/_array_element$/)
return case_eq_hash_element(*args) if mid.match?(/_hash_element$/)
super
end
|
Instance Method Details
#on_case_match(node) ⇒ Object
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
|
# File 'lib/ruby-next/language/rewriters/2.7/pattern_matching.rb', line 240
def on_case_match(node)
context.track! self
@deconstructed_keys = {}
@predicates = Predicates::CaseIn.new
@lvars = []
matchee_ast =
s(:begin, s(:lvasgn, MATCHEE, node.children[0]))
patterns = locals.with(
matchee: MATCHEE,
arr: MATCHEE_ARR,
hash: MATCHEE_HASH
) do
build_case_when(node.children[1..-1])
end
case_clause = predicates.process(s(:case, *patterns))
rewrite_case_in! node, matchee_ast, case_clause
node.updated(
:kwbegin,
[
matchee_ast, case_clause
]
)
end
|
#on_match_pattern(node) ⇒ Object
Also known as:
on_in_match
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
|
# File 'lib/ruby-next/language/rewriters/2.7/pattern_matching.rb', line 270
def on_match_pattern(node)
context.track! self
@deconstructed_keys = {}
@predicates = Predicates::Noop.new
@lvars = []
matchee =
s(:begin, s(:lvasgn, MATCHEE, node.children[0]))
pattern =
locals.with(
matchee: MATCHEE,
arr: MATCHEE_ARR,
hash: MATCHEE_HASH
) do
with_declared_locals do
send(
:"#{node.children[1].type}_clause",
node.children[1]
)
end.then do |node|
s(:begin,
s(:or,
node,
no_matching_pattern))
end
end
node.updated(
:and,
[
matchee,
pattern
]
).tap do |new_node|
replace(node.loc.expression, inline_blocks(unparse(new_node)))
end
end
|
#on_match_pattern_p(node) ⇒ Object
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
|
# File 'lib/ruby-next/language/rewriters/2.7/pattern_matching.rb', line 312
def on_match_pattern_p(node)
context.track! self
@deconstructed_keys = {}
@predicates = Predicates::Noop.new
@lvars = []
matchee =
s(:begin, s(:lvasgn, MATCHEE, node.children[0]))
pattern =
locals.with(
matchee: MATCHEE,
arr: MATCHEE_ARR,
hash: MATCHEE_HASH
) do
with_declared_locals do
send(
:"#{node.children[1].type}_clause",
node.children[1]
)
end
end
node.updated(
:and,
[
matchee,
pattern
]
).tap do |new_node|
replace(node.loc.expression, inline_blocks(unparse(new_node)))
end
end
|