Skip to content

Commit b9d762c

Browse files
committed
Implement many more visitor methods for ripper
1 parent 4ba9abf commit b9d762c

File tree

1 file changed

+165
-34
lines changed

1 file changed

+165
-34
lines changed

lib/prism/translation/ripper.rb

Lines changed: 165 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,27 @@ def visit_alias_method_node(node)
195195
# alias $foo $bar
196196
# ^^^^^^^^^^^^^^^
197197
def visit_alias_global_variable_node(node)
198-
new_name = visit(node.new_name)
199-
old_name = visit(node.old_name)
198+
new_name = visit_alias_global_variable_node_value(node.new_name)
199+
old_name = visit_alias_global_variable_node_value(node.old_name)
200200

201201
bounds(node.location)
202202
on_var_alias(new_name, old_name)
203203
end
204204

205+
# Visit one side of an alias global variable node.
206+
private def visit_alias_global_variable_node_value(node)
207+
bounds(node.location)
208+
209+
case node
210+
when BackReferenceReadNode
211+
on_backref(node.slice)
212+
when GlobalVariableReadNode
213+
on_gvar(node.name.to_s)
214+
else
215+
raise
216+
end
217+
end
218+
205219
# foo => bar | baz
206220
# ^^^^^^^^^
207221
def visit_alternation_pattern_node(node)
@@ -299,7 +313,8 @@ def visit_block_argument_node(node)
299313
# foo { |; bar| }
300314
# ^^^
301315
def visit_block_local_variable_node(node)
302-
raise NoMethodError, __method__
316+
bounds(node.location)
317+
on_ident(node.name.to_s)
303318
end
304319

305320
# Visit a BlockNode.
@@ -322,12 +337,13 @@ def visit_block_node(node)
322337
visit(node.body)
323338
end
324339

325-
if node.opening == "{"
340+
case node.opening
341+
when "{"
326342
on_brace_block(params_val, body_val)
327-
elsif node.opening == "do"
343+
when "do"
328344
on_do_block(params_val, on_bodystmt(body_val, nil, nil, nil))
329345
else
330-
raise NoMethodError, __method__, "Unexpected Block opening character!"
346+
raise raise
331347
end
332348
end
333349

@@ -339,15 +355,30 @@ def visit_block_parameter_node(node)
339355

340356
# A block's parameters.
341357
def visit_block_parameters_node(node)
342-
on_block_var(visit(node.parameters), visit_block_parameters_node_empty)
358+
parameters =
359+
if node.parameters.nil?
360+
on_params(nil, nil, nil, nil, nil, nil, nil)
361+
else
362+
visit(node.parameters)
363+
end
364+
365+
locals =
366+
if node.locals.any?
367+
visit_all(node.locals)
368+
else
369+
visit_block_parameters_node_empty_locals
370+
end
371+
372+
bounds(node.location)
373+
on_block_var(parameters, locals)
343374
end
344375

345376
if RUBY_ENGINE == "jruby"
346-
# For JRuby, "no block" in an on_block_var is nil
347-
private def visit_block_parameters_node_empty; nil; end
377+
# For JRuby, empty locals in an on_block_var is nil.
378+
private def visit_block_parameters_node_empty_locals; nil; end
348379
else
349-
# For CRuby et al, "no block" in an on_block_var is false
350-
private def visit_block_parameters_node_empty; false; end
380+
# For everyone else, empty locals in an on_block_var is false.
381+
private def visit_block_parameters_node_empty_locals; false; end
351382
end
352383

353384
# break
@@ -458,7 +489,8 @@ def visit_class_node(node)
458489
# @@foo
459490
# ^^^^^
460491
def visit_class_variable_read_node(node)
461-
raise NoMethodError, __method__
492+
bounds(node.location)
493+
on_var_ref(on_cvar(node.slice))
462494
end
463495

464496
# @@foo = 1
@@ -467,25 +499,54 @@ def visit_class_variable_read_node(node)
467499
# @@foo, @@bar = 1
468500
# ^^^^^ ^^^^^
469501
def visit_class_variable_write_node(node)
470-
raise NoMethodError, __method__
502+
bounds(node.name_loc)
503+
target = on_var_field(on_cvar(node.name.to_s))
504+
value = visit(node.value)
505+
506+
bounds(node.location)
507+
on_assign(target, value)
471508
end
472509

473510
# @@foo += bar
474511
# ^^^^^^^^^^^^
475512
def visit_class_variable_operator_write_node(node)
476-
raise NoMethodError, __method__
513+
bounds(node.name_loc)
514+
target = on_var_field(on_cvar(node.name.to_s))
515+
516+
bounds(node.operator_loc)
517+
operator = on_op("#{node.operator}=")
518+
value = visit(node.value)
519+
520+
bounds(node.location)
521+
on_opassign(target, operator, value)
477522
end
478523

479524
# @@foo &&= bar
480525
# ^^^^^^^^^^^^^
481526
def visit_class_variable_and_write_node(node)
482-
raise NoMethodError, __method__
527+
bounds(node.name_loc)
528+
target = on_var_field(on_cvar(node.name.to_s))
529+
530+
bounds(node.operator_loc)
531+
operator = on_op("&&=")
532+
value = visit(node.value)
533+
534+
bounds(node.location)
535+
on_opassign(target, operator, value)
483536
end
484537

485538
# @@foo ||= bar
486539
# ^^^^^^^^^^^^^
487540
def visit_class_variable_or_write_node(node)
488-
raise NoMethodError, __method__
541+
bounds(node.name_loc)
542+
target = on_var_field(on_cvar(node.name.to_s))
543+
544+
bounds(node.operator_loc)
545+
operator = on_op("||=")
546+
value = visit(node.value)
547+
548+
bounds(node.location)
549+
on_opassign(target, operator, value)
489550
end
490551

491552
# @@foo, = bar
@@ -518,19 +579,43 @@ def visit_constant_write_node(node)
518579
# Foo += bar
519580
# ^^^^^^^^^^^
520581
def visit_constant_operator_write_node(node)
521-
raise NoMethodError, __method__
582+
bounds(node.name_loc)
583+
target = on_var_field(on_const(node.name.to_s))
584+
585+
bounds(node.operator_loc)
586+
operator = on_op("#{node.operator}=")
587+
value = visit(node.value)
588+
589+
bounds(node.location)
590+
on_opassign(target, operator, value)
522591
end
523592

524593
# Foo &&= bar
525594
# ^^^^^^^^^^^^
526595
def visit_constant_and_write_node(node)
527-
raise NoMethodError, __method__
596+
bounds(node.name_loc)
597+
target = on_var_field(on_const(node.name.to_s))
598+
599+
bounds(node.operator_loc)
600+
operator = on_op("&&=")
601+
value = visit(node.value)
602+
603+
bounds(node.location)
604+
on_opassign(target, operator, value)
528605
end
529606

530607
# Foo ||= bar
531608
# ^^^^^^^^^^^^
532609
def visit_constant_or_write_node(node)
533-
raise NoMethodError, __method__
610+
bounds(node.name_loc)
611+
target = on_var_field(on_const(node.name.to_s))
612+
613+
bounds(node.operator_loc)
614+
operator = on_op("||=")
615+
value = visit(node.value)
616+
617+
bounds(node.location)
618+
on_opassign(target, operator, value)
534619
end
535620

536621
# Foo, = bar
@@ -542,7 +627,13 @@ def visit_constant_target_node(node)
542627
# Foo::Bar
543628
# ^^^^^^^^
544629
def visit_constant_path_node(node)
545-
raise NoMethodError, __method__
630+
parent = visit(node.parent)
631+
632+
bounds(node.child.location)
633+
child = on_const(node.child.name.to_s)
634+
635+
bounds(node.location)
636+
on_const_path_ref(parent, child)
546637
end
547638

548639
# Foo::Bar = 1
@@ -551,7 +642,17 @@ def visit_constant_path_node(node)
551642
# Foo::Foo, Bar::Bar = 1
552643
# ^^^^^^^^ ^^^^^^^^
553644
def visit_constant_path_write_node(node)
554-
raise NoMethodError, __method__
645+
parent = visit(node.target.parent)
646+
647+
bounds(node.target.child.location)
648+
child = on_const(node.target.child.name.to_s)
649+
650+
bounds(node.target.location)
651+
target = on_const_path_field(parent, child)
652+
value = visit(node.value)
653+
654+
bounds(node.location)
655+
on_assign(target, value)
555656
end
556657

557658
# Foo::Bar += baz
@@ -593,7 +694,8 @@ def visit_def_node(node)
593694
# defined?(a)
594695
# ^^^^^^^^^^^
595696
def visit_defined_node(node)
596-
raise NoMethodError, __method__
697+
bounds(node.location)
698+
on_defined(visit(node.value))
597699
end
598700

599701
# if foo then bar else baz end
@@ -648,13 +750,21 @@ def visit_find_pattern_node(node)
648750
# if foo .. bar; end
649751
# ^^^^^^^^^^
650752
def visit_flip_flop_node(node)
651-
raise NoMethodError, __method__
753+
left = visit(node.left)
754+
right = visit(node.right)
755+
756+
bounds(node.location)
757+
if node.exclude_end?
758+
on_dot3(left, right)
759+
else
760+
on_dot2(left, right)
761+
end
652762
end
653763

654764
# 1.0
655765
# ^^^
656766
def visit_float_node(node)
657-
visit_number(node) { |text| on_float(text) }
767+
visit_number_node(node) { |text| on_float(text) }
658768
end
659769

660770
# for foo in bar do end
@@ -681,14 +791,22 @@ def visit_forwarding_parameter_node(node)
681791
# super {}
682792
# ^^^^^^^^
683793
def visit_forwarding_super_node(node)
684-
raise NoMethodError, __method__
794+
if node.block.nil?
795+
bounds(node.location)
796+
on_zsuper
797+
else
798+
block = visit(node.block)
799+
800+
bounds(node.location)
801+
on_method_add_block(on_zsuper, block)
802+
end
685803
end
686804

687805
# $foo
688806
# ^^^^
689807
def visit_global_variable_read_node(node)
690808
bounds(node.location)
691-
on_gvar(node.name.to_s)
809+
on_var_ref(on_gvar(node.name.to_s))
692810
end
693811

694812
# $foo = 1
@@ -697,13 +815,26 @@ def visit_global_variable_read_node(node)
697815
# $foo, $bar = 1
698816
# ^^^^ ^^^^
699817
def visit_global_variable_write_node(node)
700-
raise NoMethodError, __method__
818+
bounds(node.name_loc)
819+
target = on_var_field(on_gvar(node.name.to_s))
820+
value = visit(node.value)
821+
822+
bounds(node.location)
823+
on_assign(target, value)
701824
end
702825

703826
# $foo += bar
704827
# ^^^^^^^^^^^
705828
def visit_global_variable_operator_write_node(node)
706-
raise NoMethodError, __method__
829+
bounds(node.name_loc)
830+
target = on_var_field(on_gvar(node.name.to_s))
831+
832+
bounds(node.operator_loc)
833+
operator = on_op("#{node.operator}=")
834+
value = visit(node.value)
835+
836+
bounds(node.location)
837+
on_opassign(target, operator, value)
707838
end
708839

709840
# $foo &&= bar
@@ -751,7 +882,7 @@ def visit_if_node(node)
751882
# 1i
752883
# ^^
753884
def visit_imaginary_node(node)
754-
visit_number(node) { |text| on_imaginary(text) }
885+
visit_number_node(node) { |text| on_imaginary(text) }
755886
end
756887

757888
# { foo: }
@@ -835,7 +966,7 @@ def visit_instance_variable_target_node(node)
835966
# 1
836967
# ^
837968
def visit_integer_node(node)
838-
visit_number(node) { |text| on_int(text) }
969+
visit_number_node(node) { |text| on_int(text) }
839970
end
840971

841972
# if /foo #{bar}/ then end
@@ -1049,13 +1180,13 @@ def visit_no_keywords_parameter_node(node)
10491180

10501181
# -> { _1 + _2 }
10511182
# ^^^^^^^^^^^^^^
1052-
def visit_numbered_parameters_node(node)
1183+
def visit_number_nodeed_parameters_node(node)
10531184
raise NoMethodError, __method__
10541185
end
10551186

10561187
# $1
10571188
# ^^
1058-
def visit_numbered_reference_read_node(node)
1189+
def visit_number_nodeed_reference_read_node(node)
10591190
raise NoMethodError, __method__
10601191
end
10611192

@@ -1155,7 +1286,7 @@ def visit_range_node(node)
11551286
# 1r
11561287
# ^^
11571288
def visit_rational_node(node)
1158-
visit_number(node) { |text| on_rational(text) }
1289+
visit_number_node(node) { |text| on_rational(text) }
11591290
end
11601291

11611292
# redo
@@ -1518,7 +1649,7 @@ def visit_symbol_literal_node(node, no_symbol_wrapper: false)
15181649

15191650
# Visit a node that represents a number. We need to explicitly handle the
15201651
# unary - operator.
1521-
def visit_number(node)
1652+
def visit_number_node(node)
15221653
slice = node.slice
15231654
location = node.location
15241655

0 commit comments

Comments
 (0)