Debian Codegraph @

lib/prism/translation/ruby_parser.rb

raw symbols SCIP
1 # frozen_string_literal: true
2 # :markup: markdown
3
4 begin
5 require "sexp"
6 rescue LoadError
7 warn(%q{Error: Unable to load sexp. Add `gem "sexp_processor"` to your Gemfile.})
8 exit(1)
9 end
10
11 class RubyParser # :nodoc:
12 class SyntaxError < RuntimeError # :nodoc:
13 end
14 end
15
16 module Prism
17 module Translation
18 # This module is the entry-point for converting a prism syntax tree into the
19 # seattlerb/ruby_parser gem's syntax tree.
20 class RubyParser
21 # A prism visitor that builds Sexp objects.
22 class Compiler < ::Prism::Compiler
23 # This is the name of the file that we are compiling. We set it on every
24 # Sexp object that is generated, and also use it to compile `__FILE__`
25 # nodes.
26 attr_reader :file
27
28 # Class variables will change their type based on if they are inside of
29 # a method definition or not, so we need to track that state.
30 attr_reader :in_def
31
32 # Some nodes will change their representation if they are inside of a
33 # pattern, so we need to track that state.
34 attr_reader :in_pattern
35
36 # Initialize a new compiler with the given file name.
37 def initialize(file, in_def: false, in_pattern: false)
38 @file = file
39 @in_def = in_def
40 @in_pattern = in_pattern
41 end
42
43 # ```
44 # alias foo bar
45 # ^^^^^^^^^^^^^
46 # ```
47 def visit_alias_method_node(node)
48 s(node, :alias, visit(node.new_name), visit(node.old_name))
49 end
50
51 # ```
52 # alias $foo $bar
53 # ^^^^^^^^^^^^^^^
54 # ```
55 def visit_alias_global_variable_node(node)
56 s(node, :valias, node.new_name.name, node.old_name.name)
57 end
58
59 # ```
60 # foo => bar | baz
61 # ^^^^^^^^^
62 # ```
63 def visit_alternation_pattern_node(node)
64 s(node, :or, visit(node.left), visit(node.right))
65 end
66
67 # ```
68 # a and b
69 # ^^^^^^^
70 # ```
71 def visit_and_node(node)
72 left = visit(node.left)
73
74 if left[0] == :and
75 # ruby_parser has the and keyword as right-associative as opposed to
76 # prism which has it as left-associative. We reverse that
77 # associativity here.
78 nest = left
79 nest = nest[2] while nest[2][0] == :and
80 nest[2] = s(node, :and, nest[2], visit(node.right))
81 left
82 else
83 s(node, :and, left, visit(node.right))
84 end
85 end
86
87 # ```
88 # []
89 # ^^
90 # ```
91 def visit_array_node(node)
92 if in_pattern
93 s(node, :array_pat, nil).concat(visit_all(node.elements))
94 else
95 s(node, :array).concat(visit_all(node.elements))
96 end
97 end
98
99 # ```
100 # foo => [bar]
101 # ^^^^^
102 # ```
103 def visit_array_pattern_node(node)
104 if node.constant.nil? && node.requireds.empty? && node.rest.nil? && node.posts.empty?
105 s(node, :array_pat)
106 else
107 result = s(node, :array_pat, visit_pattern_constant(node.constant)).concat(visit_all(node.requireds))
108
109 case node.rest
110 when SplatNode
111 result << :"*#{node.rest.expression&.name}"
112 when ImplicitRestNode
113 result << :*
114
115 # This doesn't make any sense at all, but since we're trying to
116 # replicate the behavior directly, we'll copy it.
117 result.line(666)
118 end
119
120 result.concat(visit_all(node.posts))
121 end
122 end
123
124 # ```
125 # foo(bar)
126 # ^^^
127 # ```
128 def visit_arguments_node(node)
129 raise "Cannot visit arguments directly"
130 end
131
132 # ```
133 # { a: 1 }
134 # ^^^^
135 # ```
136 def visit_assoc_node(node)
137 [visit(node.key), visit(node.value)]
138 end
139
140 # ```
141 # def foo(**); bar(**); end
142 # ^^
143 #
144 # { **foo }
145 # ^^^^^
146 # ```
147 def visit_assoc_splat_node(node)
148 if node.value.nil?
149 [s(node, :kwsplat)]
150 else
151 [s(node, :kwsplat, visit(node.value))]
152 end
153 end
154
155 # ```
156 # $+
157 # ^^
158 # ```
159 def visit_back_reference_read_node(node)
160 s(node, :back_ref, node.name.to_s.delete_prefix("$").to_sym)
161 end
162
163 # ```
164 # begin end
165 # ^^^^^^^^^
166 # ```
167 def visit_begin_node(node)
168 result = node.statements.nil? ? s(node, :nil) : visit(node.statements)
169
170 if !node.rescue_clause.nil?
171 if !node.statements.nil?
172 result = s(node.statements, :rescue, result, visit(node.rescue_clause))
173 else
174 result = s(node.rescue_clause, :rescue, visit(node.rescue_clause))
175 end
176
177 current = node.rescue_clause
178 until (current = current.subsequent).nil?
179 result << visit(current)
180 end
181 end
182
183 if !node.else_clause&.statements.nil?
184 result << visit(node.else_clause)
185 end
186
187 if !node.ensure_clause.nil?
188 if !node.statements.nil? || !node.rescue_clause.nil? || !node.else_clause.nil?
189 result = s(node.statements || node.rescue_clause || node.else_clause || node.ensure_clause, :ensure, result, visit(node.ensure_clause))
190 else
191 result = s(node.ensure_clause, :ensure, visit(node.ensure_clause))
192 end
193 end
194
195 result
196 end
197
198 # ```
199 # foo(&bar)
200 # ^^^^
201 # ```
202 def visit_block_argument_node(node)
203 s(node, :block_pass).tap do |result|
204 result << visit(node.expression) unless node.expression.nil?
205 end
206 end
207
208 # ```
209 # foo { |; bar| }
210 # ^^^
211 # ```
212 def visit_block_local_variable_node(node)
213 node.name
214 end
215
216 # A block on a keyword or method call.
217 def visit_block_node(node)
218 s(node, :block_pass, visit(node.expression))
219 end
220
221 # ```
222 # def foo(&bar); end
223 # ^^^^
224 # ```
225 def visit_block_parameter_node(node)
226 :"&#{node.name}"
227 end
228
229 # A block's parameters.
230 def visit_block_parameters_node(node)
231 # If this block parameters has no parameters and is using pipes, then
232 # it inherits its location from its shadow locals, even if they're not
233 # on the same lines as the pipes.
234 shadow_loc = true
235
236 result =
237 if node.parameters.nil?
238 s(node, :args)
239 else
240 shadow_loc = false
241 visit(node.parameters)
242 end
243
244 if node.opening == "("
245 result.line = node.opening_loc.start_line
246 result.line_max = node.closing_loc.end_line
247 shadow_loc = false
248 end
249
250 if node.locals.any?
251 shadow = s(node, :shadow).concat(visit_all(node.locals))
252 shadow.line = node.locals.first.location.start_line
253 shadow.line_max = node.locals.last.location.end_line
254 result << shadow
255
256 if shadow_loc
257 result.line = shadow.line
258 result.line_max = shadow.line_max
259 end
260 end
261
262 result
263 end
264
265 # ```
266 # break
267 # ^^^^^
268 #
269 # break foo
270 # ^^^^^^^^^
271 # ```
272 def visit_break_node(node)
273 if node.arguments.nil?
274 s(node, :break)
275 elsif node.arguments.arguments.length == 1
276 s(node, :break, visit(node.arguments.arguments.first))
277 else
278 s(node, :break, s(node.arguments, :array).concat(visit_all(node.arguments.arguments)))
279 end
280 end
281
282 # ```
283 # foo
284 # ^^^
285 #
286 # foo.bar
287 # ^^^^^^^
288 #
289 # foo.bar() {}
290 # ^^^^^^^^^^^^
291 # ```
292 def visit_call_node(node)
293 case node.name
294 when :!~
295 return s(node, :not, visit(node.copy(name: :"=~")))
296 when :=~
297 if node.arguments&.arguments&.length == 1 && node.block.nil?
298 case node.receiver
299 when StringNode
300 return s(node, :match3, visit(node.arguments.arguments.first), visit(node.receiver))
301 when RegularExpressionNode, InterpolatedRegularExpressionNode
302 return s(node, :match2, visit(node.receiver), visit(node.arguments.arguments.first))
303 end
304
305 case node.arguments.arguments.first
306 when RegularExpressionNode, InterpolatedRegularExpressionNode
307 return s(node, :match3, visit(node.arguments.arguments.first), visit(node.receiver))
308 end
309 end
310 end
311
312 type = node.attribute_write? ? :attrasgn : :call
313 type = :"safe_#{type}" if node.safe_navigation?
314
315 arguments = node.arguments&.arguments || []
316 write_value = arguments.pop if type == :attrasgn
317 block = node.block
318
319 if block.is_a?(BlockArgumentNode)
320 arguments << block
321 block = nil
322 end
323
324 result = s(node, type, visit(node.receiver), node.name).concat(visit_all(arguments))
325 result << visit_write_value(write_value) unless write_value.nil?
326
327 visit_block(node, result, block)
328 end
329
330 # ```
331 # foo.bar += baz
332 # ^^^^^^^^^^^^^^^
333 # ```
334 def visit_call_operator_write_node(node)
335 if op_asgn?(node)
336 s(node, op_asgn_type(node, :op_asgn), visit(node.receiver), visit_write_value(node.value), node.read_name, node.binary_operator)
337 else
338 s(node, op_asgn_type(node, :op_asgn2), visit(node.receiver), node.write_name, node.binary_operator, visit_write_value(node.value))
339 end
340 end
341
342 # ```
343 # foo.bar &&= baz
344 # ^^^^^^^^^^^^^^^
345 # ```
346 def visit_call_and_write_node(node)
347 if op_asgn?(node)
348 s(node, op_asgn_type(node, :op_asgn), visit(node.receiver), visit_write_value(node.value), node.read_name, :"&&")
349 else
350 s(node, op_asgn_type(node, :op_asgn2), visit(node.receiver), node.write_name, :"&&", visit_write_value(node.value))
351 end
352 end
353
354 # ```
355 # foo.bar ||= baz
356 # ^^^^^^^^^^^^^^^
357 # ```
358 def visit_call_or_write_node(node)
359 if op_asgn?(node)
360 s(node, op_asgn_type(node, :op_asgn), visit(node.receiver), visit_write_value(node.value), node.read_name, :"||")
361 else
362 s(node, op_asgn_type(node, :op_asgn2), visit(node.receiver), node.write_name, :"||", visit_write_value(node.value))
363 end
364 end
365
366 # Call nodes with operators following them will either be op_asgn or
367 # op_asgn2 nodes. That is determined by their call operator and their
368 # right-hand side.
369 private def op_asgn?(node)
370 node.call_operator == "::" || (node.value.is_a?(CallNode) && node.value.opening_loc.nil? && !node.value.arguments.nil?)
371 end
372
373 # Call nodes with operators following them can use &. as an operator,
374 # which changes their type by prefixing "safe_".
375 private def op_asgn_type(node, type)
376 node.safe_navigation? ? :"safe_#{type}" : type
377 end
378
379 # ```
380 # foo.bar, = 1
381 # ^^^^^^^
382 # ```
383 def visit_call_target_node(node)
384 s(node, :attrasgn, visit(node.receiver), node.name)
385 end
386
387 # ```
388 # foo => bar => baz
389 # ^^^^^^^^^^
390 # ```
391 def visit_capture_pattern_node(node)
392 visit(node.target) << visit(node.value)
393 end
394
395 # ```
396 # case foo; when bar; end
397 # ^^^^^^^^^^^^^^^^^^^^^^^
398 # ```
399 def visit_case_node(node)
400 s(node, :case, visit(node.predicate)).concat(visit_all(node.conditions)) << visit(node.else_clause)
401 end
402
403 # ```
404 # case foo; in bar; end
405 # ^^^^^^^^^^^^^^^^^^^^^
406 # ```
407 def visit_case_match_node(node)
408 s(node, :case, visit(node.predicate)).concat(visit_all(node.conditions)) << visit(node.else_clause)
409 end
410
411 # ```
412 # class Foo; end
413 # ^^^^^^^^^^^^^^
414 # ```
415 def visit_class_node(node)
416 name =
417 if node.constant_path.is_a?(ConstantReadNode)
418 node.name
419 else
420 visit(node.constant_path)
421 end
422
423 result =
424 if node.body.nil?
425 s(node, :class, name, visit(node.superclass))
426 elsif node.body.is_a?(StatementsNode)
427 compiler = copy_compiler(in_def: false)
428 s(node, :class, name, visit(node.superclass)).concat(node.body.body.map { |child| child.accept(compiler) })
429 else
430 s(node, :class, name, visit(node.superclass), node.body.accept(copy_compiler(in_def: false)))
431 end
432
433 attach_comments(result, node)
434 result
435 end
436
437 # ```
438 # @@foo
439 # ^^^^^
440 # ```
441 def visit_class_variable_read_node(node)
442 s(node, :cvar, node.name)
443 end
444
445 # ```
446 # @@foo = 1
447 # ^^^^^^^^^
448 #
449 # @@foo, @@bar = 1
450 # ^^^^^ ^^^^^
451 # ```
452 def visit_class_variable_write_node(node)
453 s(node, class_variable_write_type, node.name, visit_write_value(node.value))
454 end
455
456 # ```
457 # @@foo += bar
458 # ^^^^^^^^^^^^
459 # ```
460 def visit_class_variable_operator_write_node(node)
461 s(node, class_variable_write_type, node.name, s(node, :call, s(node, :cvar, node.name), node.binary_operator, visit_write_value(node.value)))
462 end
463
464 # ```
465 # @@foo &&= bar
466 # ^^^^^^^^^^^^^
467 # ```
468 def visit_class_variable_and_write_node(node)
469 s(node, :op_asgn_and, s(node, :cvar, node.name), s(node, class_variable_write_type, node.name, visit_write_value(node.value)))
470 end
471
472 # ```
473 # @@foo ||= bar
474 # ^^^^^^^^^^^^^
475 # ```
476 def visit_class_variable_or_write_node(node)
477 s(node, :op_asgn_or, s(node, :cvar, node.name), s(node, class_variable_write_type, node.name, visit_write_value(node.value)))
478 end
479
480 # ```
481 # @@foo, = bar
482 # ^^^^^
483 # ```
484 def visit_class_variable_target_node(node)
485 s(node, class_variable_write_type, node.name)
486 end
487
488 # If a class variable is written within a method definition, it has a
489 # different type than everywhere else.
490 private def class_variable_write_type
491 in_def ? :cvasgn : :cvdecl
492 end
493
494 # ```
495 # Foo
496 # ^^^
497 # ```
498 def visit_constant_read_node(node)
499 s(node, :const, node.name)
500 end
501
502 # ```
503 # Foo = 1
504 # ^^^^^^^
505 #
506 # Foo, Bar = 1
507 # ^^^ ^^^
508 # ```
509 def visit_constant_write_node(node)
510 s(node, :cdecl, node.name, visit_write_value(node.value))
511 end
512
513 # ```
514 # Foo += bar
515 # ^^^^^^^^^^^
516 # ```
517 def visit_constant_operator_write_node(node)
518 s(node, :cdecl, node.name, s(node, :call, s(node, :const, node.name), node.binary_operator, visit_write_value(node.value)))
519 end
520
521 # ```
522 # Foo &&= bar
523 # ^^^^^^^^^^^^
524 # ```
525 def visit_constant_and_write_node(node)
526 s(node, :op_asgn_and, s(node, :const, node.name), s(node, :cdecl, node.name, visit(node.value)))
527 end
528
529 # ```
530 # Foo ||= bar
531 # ^^^^^^^^^^^^
532 # ```
533 def visit_constant_or_write_node(node)
534 s(node, :op_asgn_or, s(node, :const, node.name), s(node, :cdecl, node.name, visit(node.value)))
535 end
536
537 # ```
538 # Foo, = bar
539 # ^^^
540 # ```
541 def visit_constant_target_node(node)
542 s(node, :cdecl, node.name)
543 end
544
545 # ```
546 # Foo::Bar
547 # ^^^^^^^^
548 # ```
549 def visit_constant_path_node(node)
550 if node.parent.nil?
551 s(node, :colon3, node.name)
552 else
553 s(node, :colon2, visit(node.parent), node.name)
554 end
555 end
556
557 # ```
558 # Foo::Bar = 1
559 # ^^^^^^^^^^^^
560 #
561 # Foo::Foo, Bar::Bar = 1
562 # ^^^^^^^^ ^^^^^^^^
563 # ```
564 def visit_constant_path_write_node(node)
565 s(node, :cdecl, visit(node.target), visit_write_value(node.value))
566 end
567
568 # ```
569 # Foo::Bar += baz
570 # ^^^^^^^^^^^^^^^
571 # ```
572 def visit_constant_path_operator_write_node(node)
573 s(node, :op_asgn, visit(node.target), node.binary_operator, visit_write_value(node.value))
574 end
575
576 # ```
577 # Foo::Bar &&= baz
578 # ^^^^^^^^^^^^^^^^
579 # ```
580 def visit_constant_path_and_write_node(node)
581 s(node, :op_asgn_and, visit(node.target), visit_write_value(node.value))
582 end
583
584 # ```
585 # Foo::Bar ||= baz
586 # ^^^^^^^^^^^^^^^^
587 # ```
588 def visit_constant_path_or_write_node(node)
589 s(node, :op_asgn_or, visit(node.target), visit_write_value(node.value))
590 end
591
592 # ```
593 # Foo::Bar, = baz
594 # ^^^^^^^^
595 # ```
596 def visit_constant_path_target_node(node)
597 inner =
598 if node.parent.nil?
599 s(node, :colon3, node.name)
600 else
601 s(node, :colon2, visit(node.parent), node.name)
602 end
603
604 s(node, :const, inner)
605 end
606
607 # ```
608 # def foo; end
609 # ^^^^^^^^^^^^
610 #
611 # def self.foo; end
612 # ^^^^^^^^^^^^^^^^^
613 # ```
614 def visit_def_node(node)
615 name = node.name_loc.slice.to_sym
616 result =
617 if node.receiver.nil?
618 s(node, :defn, name)
619 else
620 s(node, :defs, visit(node.receiver), name)
621 end
622
623 attach_comments(result, node)
624 result.line(node.name_loc.start_line)
625
626 if node.parameters.nil?
627 result << s(node, :args).line(node.name_loc.start_line)
628 else
629 result << visit(node.parameters)
630 end
631
632 if node.body.nil?
633 result << s(node, :nil)
634 elsif node.body.is_a?(StatementsNode)
635 compiler = copy_compiler(in_def: true)
636 result.concat(node.body.body.map { |child| child.accept(compiler) })
637 else
638 result << node.body.accept(copy_compiler(in_def: true))
639 end
640 end
641
642 # ```
643 # defined? a
644 # ^^^^^^^^^^
645 #
646 # defined?(a)
647 # ^^^^^^^^^^^
648 # ```
649 def visit_defined_node(node)
650 s(node, :defined, visit(node.value))
651 end
652
653 # ```
654 # if foo then bar else baz end
655 # ^^^^^^^^^^^^
656 # ```
657 def visit_else_node(node)
658 visit(node.statements)
659 end
660
661 # ```
662 # "foo #{bar}"
663 # ^^^^^^
664 # ```
665 def visit_embedded_statements_node(node)
666 result = s(node, :evstr)
667 result << visit(node.statements) unless node.statements.nil?
668 result
669 end
670
671 # ```
672 # "foo #@bar"
673 # ^^^^^
674 # ```
675 def visit_embedded_variable_node(node)
676 s(node, :evstr, visit(node.variable))
677 end
678
679 # ```
680 # begin; foo; ensure; bar; end
681 # ^^^^^^^^^^^^
682 # ```
683 def visit_ensure_node(node)
684 node.statements.nil? ? s(node, :nil) : visit(node.statements)
685 end
686
687 # ```
688 # false
689 # ^^^^^
690 # ```
691 def visit_false_node(node)
692 s(node, :false)
693 end
694
695 # ```
696 # foo => [*, bar, *]
697 # ^^^^^^^^^^^
698 # ```
699 def visit_find_pattern_node(node)
700 s(node, :find_pat, visit_pattern_constant(node.constant), :"*#{node.left.expression&.name}", *visit_all(node.requireds), :"*#{node.right.expression&.name}")
701 end
702
703 # ```
704 # if foo .. bar; end
705 # ^^^^^^^^^^
706 # ```
707 def visit_flip_flop_node(node)
708 if node.left.is_a?(IntegerNode) && node.right.is_a?(IntegerNode)
709 s(node, :lit, Range.new(node.left.value, node.right.value, node.exclude_end?))
710 else
711 s(node, node.exclude_end? ? :flip3 : :flip2, visit(node.left), visit(node.right))
712 end
713 end
714
715 # ```
716 # 1.0
717 # ^^^
718 # ```
719 def visit_float_node(node)
720 s(node, :lit, node.value)
721 end
722
723 # ```
724 # for foo in bar do end
725 # ^^^^^^^^^^^^^^^^^^^^^
726 # ```
727 def visit_for_node(node)
728 s(node, :for, visit(node.collection), visit(node.index), visit(node.statements))
729 end
730
731 # ```
732 # def foo(...); bar(...); end
733 # ^^^
734 # ```
735 def visit_forwarding_arguments_node(node)
736 s(node, :forward_args)
737 end
738
739 # ```
740 # def foo(...); end
741 # ^^^
742 # ```
743 def visit_forwarding_parameter_node(node)
744 s(node, :forward_args)
745 end
746
747 # ```
748 # super
749 # ^^^^^
750 #
751 # super {}
752 # ^^^^^^^^
753 # ```
754 def visit_forwarding_super_node(node)
755 visit_block(node, s(node, :zsuper), node.block)
756 end
757
758 # ```
759 # $foo
760 # ^^^^
761 # ```
762 def visit_global_variable_read_node(node)
763 s(node, :gvar, node.name)
764 end
765
766 # ```
767 # $foo = 1
768 # ^^^^^^^^
769 #
770 # $foo, $bar = 1
771 # ^^^^ ^^^^
772 # ```
773 def visit_global_variable_write_node(node)
774 s(node, :gasgn, node.name, visit_write_value(node.value))
775 end
776
777 # ```
778 # $foo += bar
779 # ^^^^^^^^^^^
780 # ```
781 def visit_global_variable_operator_write_node(node)
782 s(node, :gasgn, node.name, s(node, :call, s(node, :gvar, node.name), node.binary_operator, visit(node.value)))
783 end
784
785 # ```
786 # $foo &&= bar
787 # ^^^^^^^^^^^^
788 # ```
789 def visit_global_variable_and_write_node(node)
790 s(node, :op_asgn_and, s(node, :gvar, node.name), s(node, :gasgn, node.name, visit_write_value(node.value)))
791 end
792
793 # ```
794 # $foo ||= bar
795 # ^^^^^^^^^^^^
796 # ```
797 def visit_global_variable_or_write_node(node)
798 s(node, :op_asgn_or, s(node, :gvar, node.name), s(node, :gasgn, node.name, visit_write_value(node.value)))
799 end
800
801 # ```
802 # $foo, = bar
803 # ^^^^
804 # ```
805 def visit_global_variable_target_node(node)
806 s(node, :gasgn, node.name)
807 end
808
809 # ```
810 # {}
811 # ^^
812 # ```
813 def visit_hash_node(node)
814 s(node, :hash).concat(node.elements.flat_map { |element| visit(element) })
815 end
816
817 # ```
818 # foo => {}
819 # ^^
820 # ```
821 def visit_hash_pattern_node(node)
822 result = s(node, :hash_pat, visit_pattern_constant(node.constant)).concat(node.elements.flat_map { |element| visit(element) })
823
824 case node.rest
825 when AssocSplatNode
826 result << s(node.rest, :kwrest, :"**#{node.rest.value&.name}")
827 when NoKeywordsParameterNode
828 result << visit(node.rest)
829 end
830
831 result
832 end
833
834 # ```
835 # if foo then bar end
836 # ^^^^^^^^^^^^^^^^^^^
837 #
838 # bar if foo
839 # ^^^^^^^^^^
840 #
841 # foo ? bar : baz
842 # ^^^^^^^^^^^^^^^
843 # ```
844 def visit_if_node(node)
845 s(node, :if, visit(node.predicate), visit(node.statements), visit(node.subsequent))
846 end
847
848 # 1i
849 def visit_imaginary_node(node)
850 s(node, :lit, node.value)
851 end
852
853 # ```
854 # { foo: }
855 # ^^^^
856 # ```
857 def visit_implicit_node(node)
858 end
859
860 # ```
861 # foo { |bar,| }
862 # ^
863 # ```
864 def visit_implicit_rest_node(node)
865 end
866
867 # ```
868 # case foo; in bar; end
869 # ^^^^^^^^^^^^^^^^^^^^^
870 # ```
871 def visit_in_node(node)
872 pattern =
873 if node.pattern.is_a?(ConstantPathNode)
874 s(node.pattern, :const, visit(node.pattern))
875 else
876 node.pattern.accept(copy_compiler(in_pattern: true))
877 end
878
879 s(node, :in, pattern).concat(node.statements.nil? ? [nil] : visit_all(node.statements.body))
880 end
881
882 # ```
883 # foo[bar] += baz
884 # ^^^^^^^^^^^^^^^
885 # ```
886 def visit_index_operator_write_node(node)
887 arglist = nil
888
889 if !node.arguments.nil? || !node.block.nil?
890 arglist = s(node, :arglist).concat(visit_all(node.arguments&.arguments || []))
891 arglist << visit(node.block) if !node.block.nil?
892 end
893
894 s(node, :op_asgn1, visit(node.receiver), arglist, node.binary_operator, visit_write_value(node.value))
895 end
896
897 # ```
898 # foo[bar] &&= baz
899 # ^^^^^^^^^^^^^^^^
900 # ```
901 def visit_index_and_write_node(node)
902 arglist = nil
903
904 if !node.arguments.nil? || !node.block.nil?
905 arglist = s(node, :arglist).concat(visit_all(node.arguments&.arguments || []))
906 arglist << visit(node.block) if !node.block.nil?
907 end
908
909 s(node, :op_asgn1, visit(node.receiver), arglist, :"&&", visit_write_value(node.value))
910 end
911
912 # ```
913 # foo[bar] ||= baz
914 # ^^^^^^^^^^^^^^^^
915 # ```
916 def visit_index_or_write_node(node)
917 arglist = nil
918
919 if !node.arguments.nil? || !node.block.nil?
920 arglist = s(node, :arglist).concat(visit_all(node.arguments&.arguments || []))
921 arglist << visit(node.block) if !node.block.nil?
922 end
923
924 s(node, :op_asgn1, visit(node.receiver), arglist, :"||", visit_write_value(node.value))
925 end
926
927 # ```
928 # foo[bar], = 1
929 # ^^^^^^^^
930 # ```
931 def visit_index_target_node(node)
932 arguments = visit_all(node.arguments&.arguments || [])
933 arguments << visit(node.block) unless node.block.nil?
934
935 s(node, :attrasgn, visit(node.receiver), :[]=).concat(arguments)
936 end
937
938 # ```
939 # @foo
940 # ^^^^
941 # ```
942 def visit_instance_variable_read_node(node)
943 s(node, :ivar, node.name)
944 end
945
946 # ```
947 # @foo = 1
948 # ^^^^^^^^
949 #
950 # @foo, @bar = 1
951 # ^^^^ ^^^^
952 # ```
953 def visit_instance_variable_write_node(node)
954 s(node, :iasgn, node.name, visit_write_value(node.value))
955 end
956
957 # ```
958 # @foo += bar
959 # ^^^^^^^^^^^
960 # ```
961 def visit_instance_variable_operator_write_node(node)
962 s(node, :iasgn, node.name, s(node, :call, s(node, :ivar, node.name), node.binary_operator, visit_write_value(node.value)))
963 end
964
965 # ```
966 # @foo &&= bar
967 # ^^^^^^^^^^^^
968 # ```
969 def visit_instance_variable_and_write_node(node)
970 s(node, :op_asgn_and, s(node, :ivar, node.name), s(node, :iasgn, node.name, visit(node.value)))
971 end
972
973 # ```
974 # @foo ||= bar
975 # ^^^^^^^^^^^^
976 # ```
977 def visit_instance_variable_or_write_node(node)
978 s(node, :op_asgn_or, s(node, :ivar, node.name), s(node, :iasgn, node.name, visit(node.value)))
979 end
980
981 # ```
982 # @foo, = bar
983 # ^^^^
984 # ```
985 def visit_instance_variable_target_node(node)
986 s(node, :iasgn, node.name)
987 end
988
989 # ```
990 # 1
991 # ^
992 # ```
993 def visit_integer_node(node)
994 s(node, :lit, node.value)
995 end
996
997 # ```
998 # if /foo #{bar}/ then end
999 # ^^^^^^^^^^^^
1000 # ```
1001 def visit_interpolated_match_last_line_node(node)
1002 parts = visit_interpolated_parts(node.parts)
1003 regexp =
1004 if parts.length == 1
1005 s(node, :lit, Regexp.new(parts.first, node.options))
1006 else
1007 s(node, :dregx).concat(parts).tap do |result|
1008 options = node.options
1009 result << options if options != 0
1010 end
1011 end
1012
1013 s(node, :match, regexp)
1014 end
1015
1016 # ```
1017 # /foo #{bar}/
1018 # ^^^^^^^^^^^^
1019 # ```
1020 def visit_interpolated_regular_expression_node(node)
1021 parts = visit_interpolated_parts(node.parts)
1022
1023 if parts.length == 1
1024 s(node, :lit, Regexp.new(parts.first, node.options))
1025 else
1026 s(node, :dregx).concat(parts).tap do |result|
1027 options = node.options
1028 result << options if options != 0
1029 end
1030 end
1031 end
1032
1033 # ```
1034 # "foo #{bar}"
1035 # ^^^^^^^^^^^^
1036 # ```
1037 def visit_interpolated_string_node(node)
1038 parts = visit_interpolated_parts(node.parts)
1039 parts.length == 1 ? s(node, :str, parts.first) : s(node, :dstr).concat(parts)
1040 end
1041
1042 # ```
1043 # :"foo #{bar}"
1044 # ^^^^^^^^^^^^^
1045 # ```
1046 def visit_interpolated_symbol_node(node)
1047 parts = visit_interpolated_parts(node.parts)
1048 parts.length == 1 ? s(node, :lit, parts.first.to_sym) : s(node, :dsym).concat(parts)
1049 end
1050
1051 # ```
1052 # `foo #{bar}`
1053 # ^^^^^^^^^^^^
1054 # ```
1055 def visit_interpolated_x_string_node(node)
1056 source = node.heredoc? ? node.parts.first : node
1057 parts = visit_interpolated_parts(node.parts)
1058 parts.length == 1 ? s(source, :xstr, parts.first) : s(source, :dxstr).concat(parts)
1059 end
1060
1061 # Visit the interpolated content of the string-like node.
1062 private def visit_interpolated_parts(parts)
1063 visited = []
1064
1065 parts.each do |part|
1066 result = visit(part)
1067
1068 if result[0] == :evstr && result[1]
1069 if result[1][0] == :str
1070 visited << result[1]
1071 elsif result[1][0] == :dstr
1072 visited.concat(result[1][1..-1])
1073 else
1074 visited << result
1075 end
1076 visited << :space
1077 elsif result[0] == :dstr
1078 if !visited.empty? && part.parts[0].is_a?(StringNode)
1079 # If we are in the middle of an implicitly concatenated string,
1080 # we should not have a bare string as the first part. In this
1081 # case we need to visit just that first part and then we can
1082 # push the rest of the parts onto the visited array.
1083 result[1] = visit(part.parts[0])
1084 end
1085 visited.concat(result[1..-1])
1086 else
1087 visited << result
1088 end
1089 end
1090
1091 state = :beginning #: :beginning | :string_content | :interpolated_content
1092 results = []
1093
1094 visited.each_with_index do |result, index|
1095 case state
1096 when :beginning
1097 if result.is_a?(String)
1098 results << result
1099 state = :string_content
1100 elsif result.is_a?(Array) && result[0] == :str
1101 results << result[1]
1102 state = :string_content
1103 else
1104 results << ""
1105 results << result
1106 state = :interpolated_content
1107 end
1108 when :string_content
1109 if result == :space
1110 # continue
1111 elsif result.is_a?(String)
1112 results[0] = "#{results[0]}#{result}"
1113 elsif result.is_a?(Array) && result[0] == :str
1114 results[0] = "#{results[0]}#{result[1]}"
1115 else
1116 results << result
1117 state = :interpolated_content
1118 end
1119 when :interpolated_content
1120 if result == :space
1121 # continue
1122 elsif visited[index - 1] != :space && result.is_a?(Array) && result[0] == :str && results[-1][0] == :str && (results[-1].line_max == result.line)
1123 results[-1][1] = "#{results[-1][1]}#{result[1]}"
1124 results[-1].line_max = result.line_max
1125 else
1126 results << result
1127 end
1128 end
1129 end
1130
1131 results
1132 end
1133
1134 # ```
1135 # -> { it }
1136 # ^^
1137 # ```
1138 def visit_it_local_variable_read_node(node)
1139 s(node, :call, nil, :it)
1140 end
1141
1142 # ```
1143 # foo(bar: baz)
1144 # ^^^^^^^^
1145 # ```
1146 def visit_keyword_hash_node(node)
1147 s(node, :hash).concat(node.elements.flat_map { |element| visit(element) })
1148 end
1149
1150 # ```
1151 # def foo(**bar); end
1152 # ^^^^^
1153 #
1154 # def foo(**); end
1155 # ^^
1156 # ```
1157 def visit_keyword_rest_parameter_node(node)
1158 :"**#{node.name}"
1159 end
1160
1161 # -> {}
1162 def visit_lambda_node(node)
1163 parameters =
1164 case node.parameters
1165 when nil, ItParametersNode, NumberedParametersNode
1166 0
1167 else
1168 visit(node.parameters)
1169 end
1170
1171 if node.body.nil?
1172 s(node, :iter, s(node, :lambda), parameters)
1173 else
1174 s(node, :iter, s(node, :lambda), parameters, visit(node.body))
1175 end
1176 end
1177
1178 # ```
1179 # foo
1180 # ^^^
1181 # ```
1182 def visit_local_variable_read_node(node)
1183 if node.name.match?(/^_\d$/)
1184 s(node, :call, nil, node.name)
1185 else
1186 s(node, :lvar, node.name)
1187 end
1188 end
1189
1190 # ```
1191 # foo = 1
1192 # ^^^^^^^
1193 #
1194 # foo, bar = 1
1195 # ^^^ ^^^
1196 # ```
1197 def visit_local_variable_write_node(node)
1198 s(node, :lasgn, node.name, visit_write_value(node.value))
1199 end
1200
1201 # ```
1202 # foo += bar
1203 # ^^^^^^^^^^
1204 # ```
1205 def visit_local_variable_operator_write_node(node)
1206 s(node, :lasgn, node.name, s(node, :call, s(node, :lvar, node.name), node.binary_operator, visit_write_value(node.value)))
1207 end
1208
1209 # ```
1210 # foo &&= bar
1211 # ^^^^^^^^^^^
1212 # ```
1213 def visit_local_variable_and_write_node(node)
1214 s(node, :op_asgn_and, s(node, :lvar, node.name), s(node, :lasgn, node.name, visit_write_value(node.value)))
1215 end
1216
1217 # ```
1218 # foo ||= bar
1219 # ^^^^^^^^^^^
1220 # ```
1221 def visit_local_variable_or_write_node(node)
1222 s(node, :op_asgn_or, s(node, :lvar, node.name), s(node, :lasgn, node.name, visit_write_value(node.value)))
1223 end
1224
1225 # ```
1226 # foo, = bar
1227 # ^^^
1228 # ```
1229 def visit_local_variable_target_node(node)
1230 s(node, :lasgn, node.name)
1231 end
1232
1233 # ```
1234 # if /foo/ then end
1235 # ^^^^^
1236 # ```
1237 def visit_match_last_line_node(node)
1238 s(node, :match, s(node, :lit, Regexp.new(node.unescaped, node.options)))
1239 end
1240
1241 # ```
1242 # foo in bar
1243 # ^^^^^^^^^^
1244 # ```
1245 def visit_match_predicate_node(node)
1246 s(node, :case, visit(node.value), s(node, :in, node.pattern.accept(copy_compiler(in_pattern: true)), nil), nil)
1247 end
1248
1249 # ```
1250 # foo => bar
1251 # ^^^^^^^^^^
1252 # ```
1253 def visit_match_required_node(node)
1254 s(node, :case, visit(node.value), s(node, :in, node.pattern.accept(copy_compiler(in_pattern: true)), nil), nil)
1255 end
1256
1257 # ```
1258 # /(?<foo>foo)/ =~ bar
1259 # ^^^^^^^^^^^^^^^^^^^^
1260 # ```
1261 def visit_match_write_node(node)
1262 s(node, :match2, visit(node.call.receiver), visit(node.call.arguments.arguments.first))
1263 end
1264
1265 # A node that is missing from the syntax tree. This is only used in the
1266 # case of a syntax error. The parser gem doesn't have such a concept, so
1267 # we invent our own here.
1268 def visit_missing_node(node)
1269 raise "Cannot visit missing node directly"
1270 end
1271
1272 # ```
1273 # module Foo; end
1274 # ^^^^^^^^^^^^^^^
1275 # ```
1276 def visit_module_node(node)
1277 name =
1278 if node.constant_path.is_a?(ConstantReadNode)
1279 node.name
1280 else
1281 visit(node.constant_path)
1282 end
1283
1284 result =
1285 if node.body.nil?
1286 s(node, :module, name)
1287 elsif node.body.is_a?(StatementsNode)
1288 compiler = copy_compiler(in_def: false)
1289 s(node, :module, name).concat(node.body.body.map { |child| child.accept(compiler) })
1290 else
1291 s(node, :module, name, node.body.accept(copy_compiler(in_def: false)))
1292 end
1293
1294 attach_comments(result, node)
1295 result
1296 end
1297
1298 # ```
1299 # foo, bar = baz
1300 # ^^^^^^^^
1301 # ```
1302 def visit_multi_target_node(node)
1303 targets = [*node.lefts]
1304 targets << node.rest if !node.rest.nil? && !node.rest.is_a?(ImplicitRestNode)
1305 targets.concat(node.rights)
1306
1307 s(node, :masgn, s(node, :array).concat(visit_all(targets)))
1308 end
1309
1310 # ```
1311 # foo, bar = baz
1312 # ^^^^^^^^^^^^^^
1313 # ```
1314 def visit_multi_write_node(node)
1315 targets = [*node.lefts]
1316 targets << node.rest if !node.rest.nil? && !node.rest.is_a?(ImplicitRestNode)
1317 targets.concat(node.rights)
1318
1319 value =
1320 if node.value.is_a?(ArrayNode) && node.value.opening_loc.nil?
1321 if node.value.elements.length == 1 && node.value.elements.first.is_a?(SplatNode)
1322 visit(node.value.elements.first)
1323 else
1324 visit(node.value)
1325 end
1326 else
1327 s(node.value, :to_ary, visit(node.value))
1328 end
1329
1330 s(node, :masgn, s(node, :array).concat(visit_all(targets)), value)
1331 end
1332
1333 # ```
1334 # next
1335 # ^^^^
1336 #
1337 # next foo
1338 # ^^^^^^^^
1339 # ```
1340 def visit_next_node(node)
1341 if node.arguments.nil?
1342 s(node, :next)
1343 elsif node.arguments.arguments.length == 1
1344 argument = node.arguments.arguments.first
1345 s(node, :next, argument.is_a?(SplatNode) ? s(node, :svalue, visit(argument)) : visit(argument))
1346 else
1347 s(node, :next, s(node, :array).concat(visit_all(node.arguments.arguments)))
1348 end
1349 end
1350
1351 # ```
1352 # nil
1353 # ^^^
1354 # ```
1355 def visit_nil_node(node)
1356 s(node, :nil)
1357 end
1358
1359 # ```
1360 # def foo(**nil); end
1361 # ^^^^^
1362 # ```
1363 def visit_no_keywords_parameter_node(node)
1364 in_pattern ? s(node, :kwrest, :"**nil") : :"**nil"
1365 end
1366
1367 # ```
1368 # -> { _1 + _2 }
1369 # ^^^^^^^^^^^^^^
1370 # ```
1371 def visit_numbered_parameters_node(node)
1372 raise "Cannot visit numbered parameters directly"
1373 end
1374
1375 # ```
1376 # $1
1377 # ^^
1378 # ```
1379 def visit_numbered_reference_read_node(node)
1380 s(node, :nth_ref, node.number)
1381 end
1382
1383 # ```
1384 # def foo(bar: baz); end
1385 # ^^^^^^^^
1386 # ```
1387 def visit_optional_keyword_parameter_node(node)
1388 s(node, :kwarg, node.name, visit(node.value))
1389 end
1390
1391 # ```
1392 # def foo(bar = 1); end
1393 # ^^^^^^^
1394 # ```
1395 def visit_optional_parameter_node(node)
1396 s(node, :lasgn, node.name, visit(node.value))
1397 end
1398
1399 # ```
1400 # a or b
1401 # ^^^^^^
1402 # ```
1403 def visit_or_node(node)
1404 left = visit(node.left)
1405
1406 if left[0] == :or
1407 # ruby_parser has the or keyword as right-associative as opposed to
1408 # prism which has it as left-associative. We reverse that
1409 # associativity here.
1410 nest = left
1411 nest = nest[2] while nest[2][0] == :or
1412 nest[2] = s(node, :or, nest[2], visit(node.right))
1413 left
1414 else
1415 s(node, :or, left, visit(node.right))
1416 end
1417 end
1418
1419 # ```
1420 # def foo(bar, *baz); end
1421 # ^^^^^^^^^
1422 # ```
1423 def visit_parameters_node(node)
1424 children =
1425 node.each_child_node.map do |element|
1426 if element.is_a?(MultiTargetNode)
1427 visit_destructured_parameter(element)
1428 else
1429 visit(element)
1430 end
1431 end
1432
1433 s(node, :args).concat(children)
1434 end
1435
1436 # ```
1437 # def foo((bar, baz)); end
1438 # ^^^^^^^^^^
1439 # ```
1440 private def visit_destructured_parameter(node)
1441 children =
1442 [*node.lefts, *node.rest, *node.rights].map do |child|
1443 case child
1444 when RequiredParameterNode
1445 visit(child)
1446 when MultiTargetNode
1447 visit_destructured_parameter(child)
1448 when SplatNode
1449 :"*#{child.expression&.name}"
1450 else
1451 raise
1452 end
1453 end
1454
1455 s(node, :masgn).concat(children)
1456 end
1457
1458 # ```
1459 # ()
1460 # ^^
1461 #
1462 # (1)
1463 # ^^^
1464 # ```
1465 def visit_parentheses_node(node)
1466 if node.body.nil?
1467 s(node, :nil)
1468 else
1469 visit(node.body)
1470 end
1471 end
1472
1473 # ```
1474 # foo => ^(bar)
1475 # ^^^^^^
1476 # ```
1477 def visit_pinned_expression_node(node)
1478 node.expression.accept(copy_compiler(in_pattern: false))
1479 end
1480
1481 # ```
1482 # foo = 1 and bar => ^foo
1483 # ^^^^
1484 # ```
1485 def visit_pinned_variable_node(node)
1486 if node.variable.is_a?(LocalVariableReadNode) && node.variable.name.match?(/^_\d$/)
1487 s(node, :lvar, node.variable.name)
1488 else
1489 visit(node.variable)
1490 end
1491 end
1492
1493 # END {}
1494 def visit_post_execution_node(node)
1495 s(node, :iter, s(node, :postexe), 0, visit(node.statements))
1496 end
1497
1498 # BEGIN {}
1499 def visit_pre_execution_node(node)
1500 s(node, :iter, s(node, :preexe), 0, visit(node.statements))
1501 end
1502
1503 # The top-level program node.
1504 def visit_program_node(node)
1505 visit(node.statements)
1506 end
1507
1508 # ```
1509 # 0..5
1510 # ^^^^
1511 # ```
1512 def visit_range_node(node)
1513 if !in_pattern && !node.left.nil? && !node.right.nil? && ([node.left.type, node.right.type] - %i[nil_node integer_node]).empty?
1514 left = node.left.value if node.left.is_a?(IntegerNode)
1515 right = node.right.value if node.right.is_a?(IntegerNode)
1516 s(node, :lit, Range.new(left, right, node.exclude_end?))
1517 else
1518 s(node, node.exclude_end? ? :dot3 : :dot2, visit_range_bounds_node(node.left), visit_range_bounds_node(node.right))
1519 end
1520 end
1521
1522 # If the bounds of a range node are empty parentheses, then they do not
1523 # get replaced by their usual s(:nil), but instead are s(:begin).
1524 private def visit_range_bounds_node(node)
1525 if node.is_a?(ParenthesesNode) && node.body.nil?
1526 s(node, :begin)
1527 else
1528 visit(node)
1529 end
1530 end
1531
1532 # ```
1533 # 1r
1534 # ^^
1535 # ```
1536 def visit_rational_node(node)
1537 s(node, :lit, node.value)
1538 end
1539
1540 # ```
1541 # redo
1542 # ^^^^
1543 # ```
1544 def visit_redo_node(node)
1545 s(node, :redo)
1546 end
1547
1548 # ```
1549 # /foo/
1550 # ^^^^^
1551 # ```
1552 def visit_regular_expression_node(node)
1553 s(node, :lit, Regexp.new(node.unescaped, node.options))
1554 end
1555
1556 # ```
1557 # def foo(bar:); end
1558 # ^^^^
1559 # ```
1560 def visit_required_keyword_parameter_node(node)
1561 s(node, :kwarg, node.name)
1562 end
1563
1564 # ```
1565 # def foo(bar); end
1566 # ^^^
1567 # ```
1568 def visit_required_parameter_node(node)
1569 node.name
1570 end
1571
1572 # ```
1573 # foo rescue bar
1574 # ^^^^^^^^^^^^^^
1575 # ```
1576 def visit_rescue_modifier_node(node)
1577 s(node, :rescue, visit(node.expression), s(node.rescue_expression, :resbody, s(node.rescue_expression, :array), visit(node.rescue_expression)))
1578 end
1579
1580 # ```
1581 # begin; rescue; end
1582 # ^^^^^^^
1583 # ```
1584 def visit_rescue_node(node)
1585 exceptions =
1586 if node.exceptions.length == 1 && node.exceptions.first.is_a?(SplatNode)
1587 visit(node.exceptions.first)
1588 else
1589 s(node, :array).concat(visit_all(node.exceptions))
1590 end
1591
1592 if !node.reference.nil?
1593 exceptions << (visit(node.reference) << s(node.reference, :gvar, :"$!"))
1594 end
1595
1596 s(node, :resbody, exceptions).concat(node.statements.nil? ? [nil] : visit_all(node.statements.body))
1597 end
1598
1599 # ```
1600 # def foo(*bar); end
1601 # ^^^^
1602 #
1603 # def foo(*); end
1604 # ^
1605 # ```
1606 def visit_rest_parameter_node(node)
1607 :"*#{node.name}"
1608 end
1609
1610 # ```
1611 # retry
1612 # ^^^^^
1613 # ```
1614 def visit_retry_node(node)
1615 s(node, :retry)
1616 end
1617
1618 # ```
1619 # return
1620 # ^^^^^^
1621 #
1622 # return 1
1623 # ^^^^^^^^
1624 # ```
1625 def visit_return_node(node)
1626 if node.arguments.nil?
1627 s(node, :return)
1628 elsif node.arguments.arguments.length == 1
1629 argument = node.arguments.arguments.first
1630 s(node, :return, argument.is_a?(SplatNode) ? s(node, :svalue, visit(argument)) : visit(argument))
1631 else
1632 s(node, :return, s(node, :array).concat(visit_all(node.arguments.arguments)))
1633 end
1634 end
1635
1636 # ```
1637 # self
1638 # ^^^^
1639 # ```
1640 def visit_self_node(node)
1641 s(node, :self)
1642 end
1643
1644 # A shareable constant.
1645 def visit_shareable_constant_node(node)
1646 visit(node.write)
1647 end
1648
1649 # ```
1650 # class << self; end
1651 # ^^^^^^^^^^^^^^^^^^
1652 # ```
1653 def visit_singleton_class_node(node)
1654 s(node, :sclass, visit(node.expression)).tap do |sexp|
1655 sexp << node.body.accept(copy_compiler(in_def: false)) unless node.body.nil?
1656 end
1657 end
1658
1659 # ```
1660 # __ENCODING__
1661 # ^^^^^^^^^^^^
1662 # ```
1663 def visit_source_encoding_node(node)
1664 # TODO
1665 s(node, :colon2, s(node, :const, :Encoding), :UTF_8)
1666 end
1667
1668 # ```
1669 # __FILE__
1670 # ^^^^^^^^
1671 # ```
1672 def visit_source_file_node(node)
1673 s(node, :str, node.filepath)
1674 end
1675
1676 # ```
1677 # __LINE__
1678 # ^^^^^^^^
1679 # ```
1680 def visit_source_line_node(node)
1681 s(node, :lit, node.location.start_line)
1682 end
1683
1684 # ```
1685 # foo(*bar)
1686 # ^^^^
1687 #
1688 # def foo((bar, *baz)); end
1689 # ^^^^
1690 #
1691 # def foo(*); bar(*); end
1692 # ^
1693 # ```
1694 def visit_splat_node(node)
1695 if node.expression.nil?
1696 s(node, :splat)
1697 else
1698 s(node, :splat, visit(node.expression))
1699 end
1700 end
1701
1702 # A list of statements.
1703 def visit_statements_node(node)
1704 first, *rest = node.body
1705
1706 if rest.empty?
1707 visit(first)
1708 else
1709 s(node, :block).concat(visit_all(node.body))
1710 end
1711 end
1712
1713 # ```
1714 # "foo"
1715 # ^^^^^
1716 # ```
1717 def visit_string_node(node)
1718 unescaped = node.unescaped
1719
1720 if node.forced_binary_encoding?
1721 unescaped = unescaped.dup
1722 unescaped.force_encoding(Encoding::BINARY)
1723 end
1724
1725 s(node, :str, unescaped)
1726 end
1727
1728 # ```
1729 # super(foo)
1730 # ^^^^^^^^^^
1731 # ```
1732 def visit_super_node(node)
1733 arguments = node.arguments&.arguments || []
1734 block = node.block
1735
1736 if block.is_a?(BlockArgumentNode)
1737 arguments << block
1738 block = nil
1739 end
1740
1741 visit_block(node, s(node, :super).concat(visit_all(arguments)), block)
1742 end
1743
1744 # ```
1745 # :foo
1746 # ^^^^
1747 # ```
1748 def visit_symbol_node(node)
1749 node.value == "!@" ? s(node, :lit, :"!@") : s(node, :lit, node.unescaped.to_sym)
1750 end
1751
1752 # ```
1753 # true
1754 # ^^^^
1755 # ```
1756 def visit_true_node(node)
1757 s(node, :true)
1758 end
1759
1760 # ```
1761 # undef foo
1762 # ^^^^^^^^^
1763 # ```
1764 def visit_undef_node(node)
1765 names = node.names.map { |name| s(node, :undef, visit(name)) }
1766 names.length == 1 ? names.first : s(node, :block).concat(names)
1767 end
1768
1769 # ```
1770 # unless foo; bar end
1771 # ^^^^^^^^^^^^^^^^^^^
1772 #
1773 # bar unless foo
1774 # ^^^^^^^^^^^^^^
1775 # ```
1776 def visit_unless_node(node)
1777 s(node, :if, visit(node.predicate), visit(node.else_clause), visit(node.statements))
1778 end
1779
1780 # ```
1781 # until foo; bar end
1782 # ^^^^^^^^^^^^^^^^^
1783 #
1784 # bar until foo
1785 # ^^^^^^^^^^^^^
1786 # ```
1787 def visit_until_node(node)
1788 s(node, :until, visit(node.predicate), visit(node.statements), !node.begin_modifier?)
1789 end
1790
1791 # ```
1792 # case foo; when bar; end
1793 # ^^^^^^^^^^^^^
1794 # ```
1795 def visit_when_node(node)
1796 s(node, :when, s(node, :array).concat(visit_all(node.conditions))).concat(node.statements.nil? ? [nil] : visit_all(node.statements.body))
1797 end
1798
1799 # ```
1800 # while foo; bar end
1801 # ^^^^^^^^^^^^^^^^^^
1802 #
1803 # bar while foo
1804 # ^^^^^^^^^^^^^
1805 # ```
1806 def visit_while_node(node)
1807 s(node, :while, visit(node.predicate), visit(node.statements), !node.begin_modifier?)
1808 end
1809
1810 # ```
1811 # `foo`
1812 # ^^^^^
1813 # ```
1814 def visit_x_string_node(node)
1815 result = s(node, :xstr, node.unescaped)
1816
1817 if node.heredoc?
1818 result.line = node.content_loc.start_line
1819 result.line_max = node.content_loc.end_line
1820 end
1821
1822 result
1823 end
1824
1825 # ```
1826 # yield
1827 # ^^^^^
1828 #
1829 # yield 1
1830 # ^^^^^^^
1831 # ```
1832 def visit_yield_node(node)
1833 s(node, :yield).concat(visit_all(node.arguments&.arguments || []))
1834 end
1835
1836 private
1837
1838 # Attach prism comments to the given sexp.
1839 def attach_comments(sexp, node)
1840 return unless node.comments
1841 return if node.comments.empty?
1842
1843 extra = node.location.start_line - node.comments.last.location.start_line
1844 comments = node.comments.map(&:slice)
1845 comments.concat([nil] * [0, extra].max)
1846 sexp.comments = comments.join("\n")
1847 end
1848
1849 # Create a new compiler with the given options.
1850 def copy_compiler(in_def: self.in_def, in_pattern: self.in_pattern)
1851 Compiler.new(file, in_def: in_def, in_pattern: in_pattern)
1852 end
1853
1854 # Create a new Sexp object from the given prism node and arguments.
1855 def s(node, *arguments)
1856 result = Sexp.new(*arguments)
1857 result.file = file
1858 result.line = node.location.start_line
1859 result.line_max = node.location.end_line
1860 result
1861 end
1862
1863 # Visit a block node, which will modify the AST by wrapping the given
1864 # visited node in an iter node.
1865 def visit_block(node, sexp, block)
1866 if block.nil?
1867 sexp
1868 else
1869 parameters =
1870 case block.parameters
1871 when nil, ItParametersNode, NumberedParametersNode
1872 0
1873 else
1874 visit(block.parameters)
1875 end
1876
1877 if block.body.nil?
1878 s(node, :iter, sexp, parameters)
1879 else
1880 s(node, :iter, sexp, parameters, visit(block.body))
1881 end
1882 end
1883 end
1884
1885 # Pattern constants get wrapped in another layer of :const.
1886 def visit_pattern_constant(node)
1887 case node
1888 when nil
1889 # nothing
1890 when ConstantReadNode
1891 visit(node)
1892 else
1893 s(node, :const, visit(node))
1894 end
1895 end
1896
1897 # Visit the value of a write, which will be on the right-hand side of
1898 # a write operator. Because implicit arrays can have splats, those could
1899 # potentially be wrapped in an svalue node.
1900 def visit_write_value(node)
1901 if node.is_a?(ArrayNode) && node.opening_loc.nil?
1902 if node.elements.length == 1 && node.elements.first.is_a?(SplatNode)
1903 s(node, :svalue, visit(node.elements.first))
1904 else
1905 s(node, :svalue, visit(node))
1906 end
1907 else
1908 visit(node)
1909 end
1910 end
1911 end
1912
1913 private_constant :Compiler
1914
1915 # Parse the given source and translate it into the seattlerb/ruby_parser
1916 # gem's Sexp format.
1917 def parse(source, filepath = "(string)")
1918 translate(Prism.parse(source, filepath: filepath, partial_script: true), filepath)
1919 end
1920
1921 # Parse the given file and translate it into the seattlerb/ruby_parser
1922 # gem's Sexp format.
1923 def parse_file(filepath)
1924 translate(Prism.parse_file(filepath, partial_script: true), filepath)
1925 end
1926
1927 # Parse the give file and translate it into the
1928 # seattlerb/ruby_parser gem's Sexp format. This method is
1929 # provided for API compatibility to RubyParser and takes an
1930 # optional +timeout+ argument.
1931 def process(ruby, file = "(string)", timeout = nil)
1932 Timeout.timeout(timeout) { parse(ruby, file) }
1933 end
1934
1935 class << self
1936 # Parse the given source and translate it into the seattlerb/ruby_parser
1937 # gem's Sexp format.
1938 def parse(source, filepath = "(string)")
1939 new.parse(source, filepath)
1940 end
1941
1942 # Parse the given file and translate it into the seattlerb/ruby_parser
1943 # gem's Sexp format.
1944 def parse_file(filepath)
1945 new.parse_file(filepath)
1946 end
1947 end
1948
1949 private
1950
1951 # Translate the given parse result and filepath into the
1952 # seattlerb/ruby_parser gem's Sexp format.
1953 def translate(result, filepath)
1954 if result.failure?
1955 error = result.errors.first
1956 raise ::RubyParser::SyntaxError, "#{filepath}:#{error.location.start_line} :: #{error.message}"
1957 end
1958
1959 result.attach_comments!
1960 result.value.accept(Compiler.new(filepath))
1961 end
1962 end
1963 end
1964 end