Debian Codegraph @

lib/prism/translation/ripper.rb

raw symbols SCIP
1 # frozen_string_literal: true
2 # :markup: markdown
3
4 module Prism
5 module Translation
6 # This class provides a compatibility layer between prism and Ripper. It
7 # functions by parsing the entire tree first and then walking it and
8 # executing each of the Ripper callbacks as it goes. To use this class, you
9 # treat `Prism::Translation::Ripper` effectively as you would treat the
10 # `Ripper` class.
11 #
12 # Note that this class will serve the most common use cases, but Ripper's
13 # API is extensive and undocumented. It relies on reporting the state of the
14 # parser at any given time. We do our best to replicate that here, but
15 # because it is a different architecture it is not possible to perfectly
16 # replicate the behavior of Ripper.
17 #
18 # The main known difference is that we may omit dispatching some events in
19 # some cases. This impacts the following events:
20 #
21 # - on_assign_error
22 # - on_comma
23 # - on_ignored_nl
24 # - on_ignored_sp
25 # - on_kw
26 # - on_label_end
27 # - on_lbrace
28 # - on_lbracket
29 # - on_lparen
30 # - on_nl
31 # - on_op
32 # - on_operator_ambiguous
33 # - on_rbrace
34 # - on_rbracket
35 # - on_rparen
36 # - on_semicolon
37 # - on_sp
38 # - on_symbeg
39 # - on_tstring_beg
40 # - on_tstring_end
41 #
42 class Ripper < Compiler
43 # Parses the given Ruby program read from +src+.
44 # +src+ must be a String or an IO or a object with a #gets method.
45 def self.parse(src, filename = "(ripper)", lineno = 1)
46 new(src, filename, lineno).parse
47 end
48
49 # Tokenizes the Ruby program and returns an array of an array,
50 # which is formatted like
51 # <code>[[lineno, column], type, token, state]</code>.
52 # The +filename+ argument is mostly ignored.
53 # By default, this method does not handle syntax errors in +src+,
54 # use the +raise_errors+ keyword to raise a SyntaxError for an error in +src+.
55 #
56 # require "ripper"
57 # require "pp"
58 #
59 # pp Ripper.lex("def m(a) nil end")
60 # #=> [[[1, 0], :on_kw, "def", FNAME ],
61 # [[1, 3], :on_sp, " ", FNAME ],
62 # [[1, 4], :on_ident, "m", ENDFN ],
63 # [[1, 5], :on_lparen, "(", BEG|LABEL],
64 # [[1, 6], :on_ident, "a", ARG ],
65 # [[1, 7], :on_rparen, ")", ENDFN ],
66 # [[1, 8], :on_sp, " ", BEG ],
67 # [[1, 9], :on_kw, "nil", END ],
68 # [[1, 12], :on_sp, " ", END ],
69 # [[1, 13], :on_kw, "end", END ]]
70 #
71 def self.lex(src, filename = "-", lineno = 1, raise_errors: false)
72 result = Prism.lex_compat(src, filepath: filename, line: lineno, version: "current")
73
74 if result.failure? && raise_errors
75 raise SyntaxError, result.errors.first.message
76 else
77 result.value
78 end
79 end
80
81 # Tokenizes the Ruby program and returns an array of strings.
82 # The +filename+ and +lineno+ arguments are mostly ignored, since the
83 # return value is just the tokenized input.
84 # By default, this method does not handle syntax errors in +src+,
85 # use the +raise_errors+ keyword to raise a SyntaxError for an error in +src+.
86 #
87 # p Ripper.tokenize("def m(a) nil end")
88 # # => ["def", " ", "m", "(", "a", ")", " ", "nil", " ", "end"]
89 #
90 def self.tokenize(...)
91 lex(...).map(&:value)
92 end
93
94 # This contains a table of all of the parser events and their
95 # corresponding arity.
96 PARSER_EVENT_TABLE = {
97 BEGIN: 1,
98 END: 1,
99 alias: 2,
100 alias_error: 2,
101 aref: 2,
102 aref_field: 2,
103 arg_ambiguous: 1,
104 arg_paren: 1,
105 args_add: 2,
106 args_add_block: 2,
107 args_add_star: 2,
108 args_forward: 0,
109 args_new: 0,
110 array: 1,
111 aryptn: 4,
112 assign: 2,
113 assign_error: 2,
114 assoc_new: 2,
115 assoc_splat: 1,
116 assoclist_from_args: 1,
117 bare_assoc_hash: 1,
118 begin: 1,
119 binary: 3,
120 block_var: 2,
121 blockarg: 1,
122 bodystmt: 4,
123 brace_block: 2,
124 break: 1,
125 call: 3,
126 case: 2,
127 class: 3,
128 class_name_error: 2,
129 command: 2,
130 command_call: 4,
131 const_path_field: 2,
132 const_path_ref: 2,
133 const_ref: 1,
134 def: 3,
135 defined: 1,
136 defs: 5,
137 do_block: 2,
138 dot2: 2,
139 dot3: 2,
140 dyna_symbol: 1,
141 else: 1,
142 elsif: 3,
143 ensure: 1,
144 excessed_comma: 0,
145 fcall: 1,
146 field: 3,
147 fndptn: 4,
148 for: 3,
149 hash: 1,
150 heredoc_dedent: 2,
151 hshptn: 3,
152 if: 3,
153 if_mod: 2,
154 ifop: 3,
155 in: 3,
156 kwrest_param: 1,
157 lambda: 2,
158 magic_comment: 2,
159 massign: 2,
160 method_add_arg: 2,
161 method_add_block: 2,
162 mlhs_add: 2,
163 mlhs_add_post: 2,
164 mlhs_add_star: 2,
165 mlhs_new: 0,
166 mlhs_paren: 1,
167 module: 2,
168 mrhs_add: 2,
169 mrhs_add_star: 2,
170 mrhs_new: 0,
171 mrhs_new_from_args: 1,
172 next: 1,
173 nokw_param: 1,
174 opassign: 3,
175 operator_ambiguous: 2,
176 param_error: 2,
177 params: 7,
178 paren: 1,
179 parse_error: 1,
180 program: 1,
181 qsymbols_add: 2,
182 qsymbols_new: 0,
183 qwords_add: 2,
184 qwords_new: 0,
185 redo: 0,
186 regexp_add: 2,
187 regexp_literal: 2,
188 regexp_new: 0,
189 rescue: 4,
190 rescue_mod: 2,
191 rest_param: 1,
192 retry: 0,
193 return: 1,
194 return0: 0,
195 sclass: 2,
196 stmts_add: 2,
197 stmts_new: 0,
198 string_add: 2,
199 string_concat: 2,
200 string_content: 0,
201 string_dvar: 1,
202 string_embexpr: 1,
203 string_literal: 1,
204 super: 1,
205 symbol: 1,
206 symbol_literal: 1,
207 symbols_add: 2,
208 symbols_new: 0,
209 top_const_field: 1,
210 top_const_ref: 1,
211 unary: 2,
212 undef: 1,
213 unless: 3,
214 unless_mod: 2,
215 until: 2,
216 until_mod: 2,
217 var_alias: 2,
218 var_field: 1,
219 var_ref: 1,
220 vcall: 1,
221 void_stmt: 0,
222 when: 3,
223 while: 2,
224 while_mod: 2,
225 word_add: 2,
226 word_new: 0,
227 words_add: 2,
228 words_new: 0,
229 xstring_add: 2,
230 xstring_literal: 1,
231 xstring_new: 0,
232 yield: 1,
233 yield0: 0,
234 zsuper: 0
235 }
236
237 # This contains a table of all of the scanner events and their
238 # corresponding arity.
239 SCANNER_EVENT_TABLE = {
240 CHAR: 1,
241 __end__: 1,
242 backref: 1,
243 backtick: 1,
244 comma: 1,
245 comment: 1,
246 const: 1,
247 cvar: 1,
248 embdoc: 1,
249 embdoc_beg: 1,
250 embdoc_end: 1,
251 embexpr_beg: 1,
252 embexpr_end: 1,
253 embvar: 1,
254 float: 1,
255 gvar: 1,
256 heredoc_beg: 1,
257 heredoc_end: 1,
258 ident: 1,
259 ignored_nl: 1,
260 imaginary: 1,
261 int: 1,
262 ivar: 1,
263 kw: 1,
264 label: 1,
265 label_end: 1,
266 lbrace: 1,
267 lbracket: 1,
268 lparen: 1,
269 nl: 1,
270 op: 1,
271 period: 1,
272 qsymbols_beg: 1,
273 qwords_beg: 1,
274 rational: 1,
275 rbrace: 1,
276 rbracket: 1,
277 regexp_beg: 1,
278 regexp_end: 1,
279 rparen: 1,
280 semicolon: 1,
281 sp: 1,
282 symbeg: 1,
283 symbols_beg: 1,
284 tlambda: 1,
285 tlambeg: 1,
286 tstring_beg: 1,
287 tstring_content: 1,
288 tstring_end: 1,
289 words_beg: 1,
290 words_sep: 1,
291 ignored_sp: 1
292 }
293
294 # This array contains name of parser events.
295 PARSER_EVENTS = PARSER_EVENT_TABLE.keys
296
297 # This array contains name of scanner events.
298 SCANNER_EVENTS = SCANNER_EVENT_TABLE.keys
299
300 # This array contains name of all ripper events.
301 EVENTS = PARSER_EVENTS + SCANNER_EVENTS
302
303 # A list of all of the Ruby keywords.
304 KEYWORDS = [
305 "alias",
306 "and",
307 "begin",
308 "BEGIN",
309 "break",
310 "case",
311 "class",
312 "def",
313 "defined?",
314 "do",
315 "else",
316 "elsif",
317 "end",
318 "END",
319 "ensure",
320 "false",
321 "for",
322 "if",
323 "in",
324 "module",
325 "next",
326 "nil",
327 "not",
328 "or",
329 "redo",
330 "rescue",
331 "retry",
332 "return",
333 "self",
334 "super",
335 "then",
336 "true",
337 "undef",
338 "unless",
339 "until",
340 "when",
341 "while",
342 "yield",
343 "__ENCODING__",
344 "__FILE__",
345 "__LINE__"
346 ]
347
348 # A list of all of the Ruby binary operators.
349 BINARY_OPERATORS = [
350 :!=,
351 :!~,
352 :=~,
353 :==,
354 :===,
355 :<=>,
356 :>,
357 :>=,
358 :<,
359 :<=,
360 :&,
361 :|,
362 :^,
363 :>>,
364 :<<,
365 :-,
366 :+,
367 :%,
368 :/,
369 :*,
370 :**
371 ]
372
373 private_constant :KEYWORDS, :BINARY_OPERATORS
374
375 # Parses +src+ and create S-exp tree.
376 # Returns more readable tree rather than Ripper.sexp_raw.
377 # This method is mainly for developer use.
378 # The +filename+ argument is mostly ignored.
379 # By default, this method does not handle syntax errors in +src+,
380 # returning +nil+ in such cases. Use the +raise_errors+ keyword
381 # to raise a SyntaxError for an error in +src+.
382 #
383 # require "ripper"
384 # require "pp"
385 #
386 # pp Ripper.sexp("def m(a) nil end")
387 # #=> [:program,
388 # [[:def,
389 # [:@ident, "m", [1, 4]],
390 # [:paren, [:params, [[:@ident, "a", [1, 6]]], nil, nil, nil, nil, nil, nil]],
391 # [:bodystmt, [[:var_ref, [:@kw, "nil", [1, 9]]]], nil, nil, nil]]]]
392 #
393 def self.sexp(src, filename = "-", lineno = 1, raise_errors: false)
394 builder = SexpBuilderPP.new(src, filename, lineno)
395 sexp = builder.parse
396 if builder.error?
397 if raise_errors
398 raise SyntaxError, builder.error
399 end
400 else
401 sexp
402 end
403 end
404
405 # Parses +src+ and create S-exp tree.
406 # This method is mainly for developer use.
407 # The +filename+ argument is mostly ignored.
408 # By default, this method does not handle syntax errors in +src+,
409 # returning +nil+ in such cases. Use the +raise_errors+ keyword
410 # to raise a SyntaxError for an error in +src+.
411 #
412 # require "ripper"
413 # require "pp"
414 #
415 # pp Ripper.sexp_raw("def m(a) nil end")
416 # #=> [:program,
417 # [:stmts_add,
418 # [:stmts_new],
419 # [:def,
420 # [:@ident, "m", [1, 4]],
421 # [:paren, [:params, [[:@ident, "a", [1, 6]]], nil, nil, nil]],
422 # [:bodystmt,
423 # [:stmts_add, [:stmts_new], [:var_ref, [:@kw, "nil", [1, 9]]]],
424 # nil,
425 # nil,
426 # nil]]]]
427 #
428 def self.sexp_raw(src, filename = "-", lineno = 1, raise_errors: false)
429 builder = SexpBuilder.new(src, filename, lineno)
430 sexp = builder.parse
431 if builder.error?
432 if raise_errors
433 raise SyntaxError, builder.error
434 end
435 else
436 sexp
437 end
438 end
439
440 autoload :Filter, "prism/translation/ripper/filter"
441 autoload :Lexer, "prism/translation/ripper/lexer"
442 autoload :SexpBuilder, "prism/translation/ripper/sexp"
443 autoload :SexpBuilderPP, "prism/translation/ripper/sexp"
444
445 # :stopdoc:
446 # This is not part of the public API but used by some gems.
447
448 # Ripper-internal bitflags.
449 LEX_STATE_NAMES = %i[
450 BEG END ENDARG ENDFN ARG CMDARG MID FNAME DOT CLASS LABEL LABELED FITEM
451 ].map.with_index.to_h { |name, i| [2 ** i, name] }.freeze
452 private_constant :LEX_STATE_NAMES
453
454 LEX_STATE_NAMES.each do |value, key|
455 const_set("EXPR_#{key}", value)
456 end
457 EXPR_NONE = 0
458 EXPR_VALUE = EXPR_BEG
459 EXPR_BEG_ANY = EXPR_BEG | EXPR_MID | EXPR_CLASS
460 EXPR_ARG_ANY = EXPR_ARG | EXPR_CMDARG
461 EXPR_END_ANY = EXPR_END | EXPR_ENDARG | EXPR_ENDFN
462
463 def self.lex_state_name(state)
464 LEX_STATE_NAMES.filter_map { |flag, name| name if state & flag != 0 }.join("|")
465 end
466
467 # :startdoc:
468
469 # The source that is being parsed.
470 attr_reader :source
471
472 # The filename of the source being parsed.
473 attr_reader :filename
474
475 # The current line number of the parser.
476 attr_reader :lineno
477
478 # The current column number of the parser.
479 attr_reader :column
480
481 # Create a new Translation::Ripper object with the given source.
482 def initialize(source, filename = "(ripper)", lineno = 1)
483 @source = source
484 @filename = filename
485 @lineno = lineno
486 @column = 0
487 @result = nil
488 end
489
490 ##########################################################################
491 # Public interface
492 ##########################################################################
493
494 # True if the parser encountered an error during parsing.
495 def error?
496 result.failure?
497 end
498
499 # Parse the source and return the result.
500 def parse
501 result.comments.each do |comment|
502 location = comment.location
503 bounds(location)
504
505 if comment.is_a?(InlineComment)
506 on_comment(comment.slice)
507 else
508 offset = location.start_offset
509 lines = comment.slice.lines
510
511 lines.each_with_index do |line, index|
512 bounds(location.copy(start_offset: offset))
513
514 if index == 0
515 on_embdoc_beg(line)
516 elsif index == lines.size - 1
517 on_embdoc_end(line)
518 else
519 on_embdoc(line)
520 end
521
522 offset += line.bytesize
523 end
524 end
525 end
526
527 result.magic_comments.each do |magic_comment|
528 on_magic_comment(magic_comment.key, magic_comment.value)
529 end
530
531 unless result.data_loc.nil?
532 on___end__(result.data_loc.slice.each_line.first)
533 end
534
535 result.warnings.each do |warning|
536 bounds(warning.location)
537
538 if warning.level == :default
539 warning(warning.message)
540 else
541 case warning.type
542 when :ambiguous_first_argument_plus
543 on_arg_ambiguous("+")
544 when :ambiguous_first_argument_minus
545 on_arg_ambiguous("-")
546 when :ambiguous_slash
547 on_arg_ambiguous("/")
548 else
549 warn(warning.message)
550 end
551 end
552 end
553
554 if error?
555 result.errors.each do |error|
556 location = error.location
557 bounds(location)
558
559 case error.type
560 when :alias_argument
561 on_alias_error("can't make alias for the number variables", location.slice)
562 when :argument_formal_class
563 on_param_error("formal argument cannot be a class variable", location.slice)
564 when :argument_format_constant
565 on_param_error("formal argument cannot be a constant", location.slice)
566 when :argument_formal_global
567 on_param_error("formal argument cannot be a global variable", location.slice)
568 when :argument_formal_ivar
569 on_param_error("formal argument cannot be an instance variable", location.slice)
570 when :class_name, :module_name
571 on_class_name_error("class/module name must be CONSTANT", location.slice)
572 else
573 on_parse_error(error.message)
574 end
575 end
576
577 nil
578 else
579 result.value.accept(self)
580 end
581 end
582
583 ##########################################################################
584 # Visitor methods
585 ##########################################################################
586
587 # alias foo bar
588 # ^^^^^^^^^^^^^
589 def visit_alias_method_node(node)
590 new_name = visit(node.new_name)
591 old_name = visit(node.old_name)
592
593 bounds(node.location)
594 on_alias(new_name, old_name)
595 end
596
597 # alias $foo $bar
598 # ^^^^^^^^^^^^^^^
599 def visit_alias_global_variable_node(node)
600 new_name = visit_alias_global_variable_node_value(node.new_name)
601 old_name = visit_alias_global_variable_node_value(node.old_name)
602
603 bounds(node.location)
604 on_var_alias(new_name, old_name)
605 end
606
607 # Visit one side of an alias global variable node.
608 private def visit_alias_global_variable_node_value(node)
609 bounds(node.location)
610
611 case node
612 when BackReferenceReadNode
613 on_backref(node.slice)
614 when GlobalVariableReadNode
615 on_gvar(node.name.to_s)
616 else
617 raise
618 end
619 end
620
621 # foo => bar | baz
622 # ^^^^^^^^^
623 def visit_alternation_pattern_node(node)
624 left = visit_pattern_node(node.left)
625 right = visit_pattern_node(node.right)
626
627 bounds(node.location)
628 on_binary(left, :|, right)
629 end
630
631 # Visit a pattern within a pattern match. This is used to bypass the
632 # parenthesis node that can be used to wrap patterns.
633 private def visit_pattern_node(node)
634 if node.is_a?(ParenthesesNode)
635 visit(node.body)
636 else
637 visit(node)
638 end
639 end
640
641 # a and b
642 # ^^^^^^^
643 def visit_and_node(node)
644 left = visit(node.left)
645 right = visit(node.right)
646
647 bounds(node.location)
648 on_binary(left, node.operator.to_sym, right)
649 end
650
651 # []
652 # ^^
653 def visit_array_node(node)
654 case (opening = node.opening)
655 when /^%w/
656 opening_loc = node.opening_loc
657 bounds(opening_loc)
658 on_qwords_beg(opening)
659
660 elements = on_qwords_new
661 previous = nil
662
663 node.elements.each do |element|
664 visit_words_sep(opening_loc, previous, element)
665
666 bounds(element.location)
667 elements = on_qwords_add(elements, on_tstring_content(element.content))
668
669 previous = element
670 end
671
672 bounds(node.closing_loc)
673 on_tstring_end(node.closing)
674 when /^%i/
675 opening_loc = node.opening_loc
676 bounds(opening_loc)
677 on_qsymbols_beg(opening)
678
679 elements = on_qsymbols_new
680 previous = nil
681
682 node.elements.each do |element|
683 visit_words_sep(opening_loc, previous, element)
684
685 bounds(element.location)
686 elements = on_qsymbols_add(elements, on_tstring_content(element.value))
687
688 previous = element
689 end
690
691 bounds(node.closing_loc)
692 on_tstring_end(node.closing)
693 when /^%W/
694 opening_loc = node.opening_loc
695 bounds(opening_loc)
696 on_words_beg(opening)
697
698 elements = on_words_new
699 previous = nil
700
701 node.elements.each do |element|
702 visit_words_sep(opening_loc, previous, element)
703
704 bounds(element.location)
705 elements =
706 on_words_add(
707 elements,
708 if element.is_a?(StringNode)
709 on_word_add(on_word_new, on_tstring_content(element.content))
710 else
711 element.parts.inject(on_word_new) do |word, part|
712 word_part =
713 if part.is_a?(StringNode)
714 bounds(part.location)
715 on_tstring_content(part.content)
716 else
717 visit(part)
718 end
719
720 on_word_add(word, word_part)
721 end
722 end
723 )
724
725 previous = element
726 end
727
728 bounds(node.closing_loc)
729 on_tstring_end(node.closing)
730 when /^%I/
731 opening_loc = node.opening_loc
732 bounds(opening_loc)
733 on_symbols_beg(opening)
734
735 elements = on_symbols_new
736 previous = nil
737
738 node.elements.each do |element|
739 visit_words_sep(opening_loc, previous, element)
740
741 bounds(element.location)
742 elements =
743 on_symbols_add(
744 elements,
745 if element.is_a?(SymbolNode)
746 on_word_add(on_word_new, on_tstring_content(element.value))
747 else
748 element.parts.inject(on_word_new) do |word, part|
749 word_part =
750 if part.is_a?(StringNode)
751 bounds(part.location)
752 on_tstring_content(part.content)
753 else
754 visit(part)
755 end
756
757 on_word_add(word, word_part)
758 end
759 end
760 )
761
762 previous = element
763 end
764
765 bounds(node.closing_loc)
766 on_tstring_end(node.closing)
767 else
768 bounds(node.opening_loc)
769 on_lbracket(opening)
770
771 elements = visit_arguments(node.elements) unless node.elements.empty?
772
773 bounds(node.closing_loc)
774 on_rbracket(node.closing)
775 end
776
777 bounds(node.location)
778 on_array(elements)
779 end
780
781 # Dispatch a words_sep event that contains the space between the elements
782 # of list literals.
783 private def visit_words_sep(opening_loc, previous, current)
784 end_offset = (previous.nil? ? opening_loc : previous.location).end_offset
785 start_offset = current.location.start_offset
786
787 if end_offset != start_offset
788 bounds(current.location.copy(start_offset: end_offset))
789 on_words_sep(source.byteslice(end_offset...start_offset))
790 end
791 end
792
793 # Visit a list of elements, like the elements of an array or arguments.
794 private def visit_arguments(elements)
795 bounds(elements.first.location)
796 elements.inject(on_args_new) do |args, element|
797 arg = visit(element)
798 bounds(element.location)
799
800 case element
801 when BlockArgumentNode
802 on_args_add_block(args, arg)
803 when SplatNode
804 on_args_add_star(args, arg)
805 else
806 on_args_add(args, arg)
807 end
808 end
809 end
810
811 # foo => [bar]
812 # ^^^^^
813 def visit_array_pattern_node(node)
814 constant = visit(node.constant)
815 requireds = visit_all(node.requireds) if node.requireds.any?
816 rest =
817 if (rest_node = node.rest).is_a?(SplatNode)
818 if rest_node.expression.nil?
819 bounds(rest_node.location)
820 on_var_field(nil)
821 else
822 visit(rest_node.expression)
823 end
824 end
825
826 posts = visit_all(node.posts) if node.posts.any?
827
828 bounds(node.location)
829 on_aryptn(constant, requireds, rest, posts)
830 end
831
832 # foo(bar)
833 # ^^^
834 def visit_arguments_node(node)
835 arguments, _, _ = visit_call_node_arguments(node, nil, false)
836 arguments
837 end
838
839 # { a: 1 }
840 # ^^^^
841 def visit_assoc_node(node)
842 key = visit(node.key)
843 value = visit(node.value)
844
845 bounds(node.location)
846 on_assoc_new(key, value)
847 end
848
849 # def foo(**); bar(**); end
850 # ^^
851 #
852 # { **foo }
853 # ^^^^^
854 def visit_assoc_splat_node(node)
855 value = visit(node.value)
856
857 bounds(node.location)
858 on_assoc_splat(value)
859 end
860
861 # $+
862 # ^^
863 def visit_back_reference_read_node(node)
864 bounds(node.location)
865 on_backref(node.slice)
866 end
867
868 # begin end
869 # ^^^^^^^^^
870 def visit_begin_node(node)
871 clauses = visit_begin_node_clauses(node.begin_keyword_loc, node, false)
872
873 bounds(node.location)
874 on_begin(clauses)
875 end
876
877 # Visit the clauses of a begin node to form an on_bodystmt call.
878 private def visit_begin_node_clauses(location, node, allow_newline)
879 statements =
880 if node.statements.nil?
881 on_stmts_add(on_stmts_new, on_void_stmt)
882 else
883 body = node.statements.body
884 body.unshift(nil) if void_stmt?(location, node.statements.body[0].location, allow_newline)
885
886 bounds(node.statements.location)
887 visit_statements_node_body(body)
888 end
889
890 rescue_clause = visit(node.rescue_clause)
891 else_clause =
892 unless (else_clause_node = node.else_clause).nil?
893 else_statements =
894 if else_clause_node.statements.nil?
895 [nil]
896 else
897 body = else_clause_node.statements.body
898 body.unshift(nil) if void_stmt?(else_clause_node.else_keyword_loc, else_clause_node.statements.body[0].location, allow_newline)
899 body
900 end
901
902 bounds(else_clause_node.location)
903 visit_statements_node_body(else_statements)
904 end
905 ensure_clause = visit(node.ensure_clause)
906
907 bounds(node.location)
908 on_bodystmt(statements, rescue_clause, else_clause, ensure_clause)
909 end
910
911 # Visit the body of a structure that can have either a set of statements
912 # or statements wrapped in rescue/else/ensure.
913 private def visit_body_node(location, node, allow_newline = false)
914 case node
915 when nil
916 bounds(location)
917 on_bodystmt(visit_statements_node_body([nil]), nil, nil, nil)
918 when StatementsNode
919 body = [*node.body]
920 body.unshift(nil) if void_stmt?(location, body[0].location, allow_newline)
921 stmts = visit_statements_node_body(body)
922
923 bounds(node.body.first.location)
924 on_bodystmt(stmts, nil, nil, nil)
925 when BeginNode
926 visit_begin_node_clauses(location, node, allow_newline)
927 else
928 raise
929 end
930 end
931
932 # foo(&bar)
933 # ^^^^
934 def visit_block_argument_node(node)
935 visit(node.expression)
936 end
937
938 # foo { |; bar| }
939 # ^^^
940 def visit_block_local_variable_node(node)
941 bounds(node.location)
942 on_ident(node.name.to_s)
943 end
944
945 # Visit a BlockNode.
946 def visit_block_node(node)
947 braces = node.opening == "{"
948 parameters = visit(node.parameters)
949
950 body =
951 case node.body
952 when nil
953 bounds(node.location)
954 stmts = on_stmts_add(on_stmts_new, on_void_stmt)
955
956 bounds(node.location)
957 braces ? stmts : on_bodystmt(stmts, nil, nil, nil)
958 when StatementsNode
959 stmts = node.body.body
960 stmts.unshift(nil) if void_stmt?(node.parameters&.location || node.opening_loc, node.body.location, false)
961 stmts = visit_statements_node_body(stmts)
962
963 bounds(node.body.location)
964 braces ? stmts : on_bodystmt(stmts, nil, nil, nil)
965 when BeginNode
966 visit_body_node(node.parameters&.location || node.opening_loc, node.body)
967 else
968 raise
969 end
970
971 if braces
972 bounds(node.location)
973 on_brace_block(parameters, body)
974 else
975 bounds(node.location)
976 on_do_block(parameters, body)
977 end
978 end
979
980 # def foo(&bar); end
981 # ^^^^
982 def visit_block_parameter_node(node)
983 if node.name_loc.nil?
984 bounds(node.location)
985 on_blockarg(nil)
986 else
987 bounds(node.name_loc)
988 name = visit_token(node.name.to_s)
989
990 bounds(node.location)
991 on_blockarg(name)
992 end
993 end
994
995 # A block's parameters.
996 def visit_block_parameters_node(node)
997 parameters =
998 if node.parameters.nil?
999 on_params(nil, nil, nil, nil, nil, nil, nil)
1000 else
1001 visit(node.parameters)
1002 end
1003
1004 locals =
1005 if node.locals.any?
1006 visit_all(node.locals)
1007 else
1008 false
1009 end
1010
1011 bounds(node.location)
1012 on_block_var(parameters, locals)
1013 end
1014
1015 # break
1016 # ^^^^^
1017 #
1018 # break foo
1019 # ^^^^^^^^^
1020 def visit_break_node(node)
1021 if node.arguments.nil?
1022 bounds(node.location)
1023 on_break(on_args_new)
1024 else
1025 arguments = visit(node.arguments)
1026
1027 bounds(node.location)
1028 on_break(arguments)
1029 end
1030 end
1031
1032 # foo
1033 # ^^^
1034 #
1035 # foo.bar
1036 # ^^^^^^^
1037 #
1038 # foo.bar() {}
1039 # ^^^^^^^^^^^^
1040 def visit_call_node(node)
1041 if node.call_operator_loc.nil?
1042 case node.name
1043 when :[]
1044 receiver = visit(node.receiver)
1045 arguments, block, has_ripper_block = visit_call_node_arguments(node.arguments, node.block, trailing_comma?(node.arguments&.location || node.location, node.closing_loc))
1046
1047 bounds(node.location)
1048 call = on_aref(receiver, arguments)
1049
1050 if has_ripper_block
1051 bounds(node.location)
1052 on_method_add_block(call, block)
1053 else
1054 call
1055 end
1056 when :[]=
1057 receiver = visit(node.receiver)
1058
1059 *arguments, last_argument = node.arguments.arguments
1060 arguments << node.block if !node.block.nil?
1061
1062 arguments =
1063 if arguments.any?
1064 args = visit_arguments(arguments)
1065
1066 if !node.block.nil?
1067 args
1068 else
1069 bounds(arguments.first.location)
1070 on_args_add_block(args, false)
1071 end
1072 end
1073
1074 bounds(node.location)
1075 call = on_aref_field(receiver, arguments)
1076 value = visit_write_value(last_argument)
1077
1078 bounds(last_argument.location)
1079 on_assign(call, value)
1080 when :-@, :+@, :~
1081 receiver = visit(node.receiver)
1082
1083 bounds(node.location)
1084 on_unary(node.name, receiver)
1085 when :!
1086 if node.message == "not"
1087 receiver =
1088 if !node.receiver.is_a?(ParenthesesNode) || !node.receiver.body.nil?
1089 visit(node.receiver)
1090 end
1091
1092 bounds(node.location)
1093 on_unary(:not, receiver)
1094 else
1095 receiver = visit(node.receiver)
1096
1097 bounds(node.location)
1098 on_unary(:!, receiver)
1099 end
1100 when *BINARY_OPERATORS
1101 receiver = visit(node.receiver)
1102 value = visit(node.arguments.arguments.first)
1103
1104 bounds(node.location)
1105 on_binary(receiver, node.name, value)
1106 else
1107 bounds(node.message_loc)
1108 message = visit_token(node.message, false)
1109
1110 if node.variable_call?
1111 on_vcall(message)
1112 else
1113 arguments, block, has_ripper_block = visit_call_node_arguments(node.arguments, node.block, trailing_comma?(node.arguments&.location || node.location, node.closing_loc || node.location))
1114 call =
1115 if node.opening_loc.nil? && get_arguments_and_block(node.arguments, node.block).first.any?
1116 bounds(node.location)
1117 on_command(message, arguments)
1118 elsif !node.opening_loc.nil?
1119 bounds(node.location)
1120 on_method_add_arg(on_fcall(message), on_arg_paren(arguments))
1121 else
1122 bounds(node.location)
1123 on_method_add_arg(on_fcall(message), on_args_new)
1124 end
1125
1126 if has_ripper_block
1127 bounds(node.block.location)
1128 on_method_add_block(call, block)
1129 else
1130 call
1131 end
1132 end
1133 end
1134 else
1135 receiver = visit(node.receiver)
1136
1137 bounds(node.call_operator_loc)
1138 call_operator = visit_token(node.call_operator)
1139
1140 message =
1141 if node.message_loc.nil?
1142 :call
1143 else
1144 bounds(node.message_loc)
1145 visit_token(node.message, false)
1146 end
1147
1148 if node.name.end_with?("=") && !node.message.end_with?("=") && !node.arguments.nil? && node.block.nil?
1149 value = visit_write_value(node.arguments.arguments.first)
1150
1151 bounds(node.location)
1152 on_assign(on_field(receiver, call_operator, message), value)
1153 else
1154 arguments, block, has_ripper_block = visit_call_node_arguments(node.arguments, node.block, trailing_comma?(node.arguments&.location || node.location, node.closing_loc || node.location))
1155 call =
1156 if node.opening_loc.nil?
1157 bounds(node.location)
1158
1159 if node.arguments.nil? && !node.block.is_a?(BlockArgumentNode)
1160 on_call(receiver, call_operator, message)
1161 else
1162 on_command_call(receiver, call_operator, message, arguments)
1163 end
1164 else
1165 bounds(node.opening_loc)
1166 arguments = on_arg_paren(arguments)
1167
1168 bounds(node.location)
1169 on_method_add_arg(on_call(receiver, call_operator, message), arguments)
1170 end
1171
1172 if has_ripper_block
1173 bounds(node.block.location)
1174 on_method_add_block(call, block)
1175 else
1176 call
1177 end
1178 end
1179 end
1180 end
1181
1182 # Extract the arguments and block Ripper-style, which means if the block
1183 # is like `&b` then it's moved to arguments.
1184 private def get_arguments_and_block(arguments_node, block_node)
1185 arguments = arguments_node&.arguments || []
1186 block = block_node
1187
1188 if block.is_a?(BlockArgumentNode)
1189 arguments += [block]
1190 block = nil
1191 end
1192
1193 [arguments, block]
1194 end
1195
1196 # Visit the arguments and block of a call node and return the arguments
1197 # and block as they should be used.
1198 private def visit_call_node_arguments(arguments_node, block_node, trailing_comma)
1199 arguments, block = get_arguments_and_block(arguments_node, block_node)
1200
1201 [
1202 if arguments.length == 1 && arguments.first.is_a?(ForwardingArgumentsNode)
1203 visit(arguments.first)
1204 elsif arguments.any?
1205 args = visit_arguments(arguments)
1206
1207 if block_node.is_a?(BlockArgumentNode) || arguments.last.is_a?(ForwardingArgumentsNode) || command?(arguments.last) || trailing_comma
1208 args
1209 else
1210 bounds(arguments.first.location)
1211 on_args_add_block(args, false)
1212 end
1213 end,
1214 visit(block),
1215 block != nil,
1216 ]
1217 end
1218
1219 # Returns true if the given node is a command node.
1220 private def command?(node)
1221 node.is_a?(CallNode) &&
1222 node.opening_loc.nil? &&
1223 (!node.arguments.nil? || node.block.is_a?(BlockArgumentNode)) &&
1224 !BINARY_OPERATORS.include?(node.name)
1225 end
1226
1227 # foo.bar += baz
1228 # ^^^^^^^^^^^^^^^
1229 def visit_call_operator_write_node(node)
1230 receiver = visit(node.receiver)
1231
1232 bounds(node.call_operator_loc)
1233 call_operator = visit_token(node.call_operator)
1234
1235 bounds(node.message_loc)
1236 message = visit_token(node.message)
1237
1238 bounds(node.location)
1239 target = on_field(receiver, call_operator, message)
1240
1241 bounds(node.binary_operator_loc)
1242 operator = on_op("#{node.binary_operator}=")
1243 value = visit_write_value(node.value)
1244
1245 bounds(node.location)
1246 on_opassign(target, operator, value)
1247 end
1248
1249 # foo.bar &&= baz
1250 # ^^^^^^^^^^^^^^^
1251 def visit_call_and_write_node(node)
1252 receiver = visit(node.receiver)
1253
1254 bounds(node.call_operator_loc)
1255 call_operator = visit_token(node.call_operator)
1256
1257 bounds(node.message_loc)
1258 message = visit_token(node.message)
1259
1260 bounds(node.location)
1261 target = on_field(receiver, call_operator, message)
1262
1263 bounds(node.operator_loc)
1264 operator = on_op("&&=")
1265 value = visit_write_value(node.value)
1266
1267 bounds(node.location)
1268 on_opassign(target, operator, value)
1269 end
1270
1271 # foo.bar ||= baz
1272 # ^^^^^^^^^^^^^^^
1273 def visit_call_or_write_node(node)
1274 receiver = visit(node.receiver)
1275
1276 bounds(node.call_operator_loc)
1277 call_operator = visit_token(node.call_operator)
1278
1279 bounds(node.message_loc)
1280 message = visit_token(node.message)
1281
1282 bounds(node.location)
1283 target = on_field(receiver, call_operator, message)
1284
1285 bounds(node.operator_loc)
1286 operator = on_op("||=")
1287 value = visit_write_value(node.value)
1288
1289 bounds(node.location)
1290 on_opassign(target, operator, value)
1291 end
1292
1293 # foo.bar, = 1
1294 # ^^^^^^^
1295 def visit_call_target_node(node)
1296 if node.call_operator == "::"
1297 receiver = visit(node.receiver)
1298
1299 bounds(node.message_loc)
1300 message = visit_token(node.message)
1301
1302 bounds(node.location)
1303 on_const_path_field(receiver, message)
1304 else
1305 receiver = visit(node.receiver)
1306
1307 bounds(node.call_operator_loc)
1308 call_operator = visit_token(node.call_operator)
1309
1310 bounds(node.message_loc)
1311 message = visit_token(node.message)
1312
1313 bounds(node.location)
1314 on_field(receiver, call_operator, message)
1315 end
1316 end
1317
1318 # foo => bar => baz
1319 # ^^^^^^^^^^
1320 def visit_capture_pattern_node(node)
1321 value = visit(node.value)
1322 target = visit(node.target)
1323
1324 bounds(node.location)
1325 on_binary(value, :"=>", target)
1326 end
1327
1328 # case foo; when bar; end
1329 # ^^^^^^^^^^^^^^^^^^^^^^^
1330 def visit_case_node(node)
1331 predicate = visit(node.predicate)
1332 clauses =
1333 node.conditions.reverse_each.inject(visit(node.else_clause)) do |current, condition|
1334 on_when(*visit(condition), current)
1335 end
1336
1337 bounds(node.location)
1338 on_case(predicate, clauses)
1339 end
1340
1341 # case foo; in bar; end
1342 # ^^^^^^^^^^^^^^^^^^^^^
1343 def visit_case_match_node(node)
1344 predicate = visit(node.predicate)
1345 clauses =
1346 node.conditions.reverse_each.inject(visit(node.else_clause)) do |current, condition|
1347 on_in(*visit(condition), current)
1348 end
1349
1350 bounds(node.location)
1351 on_case(predicate, clauses)
1352 end
1353
1354 # class Foo; end
1355 # ^^^^^^^^^^^^^^
1356 def visit_class_node(node)
1357 constant_path =
1358 if node.constant_path.is_a?(ConstantReadNode)
1359 bounds(node.constant_path.location)
1360 on_const_ref(on_const(node.constant_path.name.to_s))
1361 else
1362 visit(node.constant_path)
1363 end
1364
1365 superclass = visit(node.superclass)
1366 bodystmt = visit_body_node(node.superclass&.location || node.constant_path.location, node.body, node.superclass.nil?)
1367
1368 bounds(node.location)
1369 on_class(constant_path, superclass, bodystmt)
1370 end
1371
1372 # @@foo
1373 # ^^^^^
1374 def visit_class_variable_read_node(node)
1375 bounds(node.location)
1376 on_var_ref(on_cvar(node.slice))
1377 end
1378
1379 # @@foo = 1
1380 # ^^^^^^^^^
1381 #
1382 # @@foo, @@bar = 1
1383 # ^^^^^ ^^^^^
1384 def visit_class_variable_write_node(node)
1385 bounds(node.name_loc)
1386 target = on_var_field(on_cvar(node.name.to_s))
1387 value = visit_write_value(node.value)
1388
1389 bounds(node.location)
1390 on_assign(target, value)
1391 end
1392
1393 # @@foo += bar
1394 # ^^^^^^^^^^^^
1395 def visit_class_variable_operator_write_node(node)
1396 bounds(node.name_loc)
1397 target = on_var_field(on_cvar(node.name.to_s))
1398
1399 bounds(node.binary_operator_loc)
1400 operator = on_op("#{node.binary_operator}=")
1401 value = visit_write_value(node.value)
1402
1403 bounds(node.location)
1404 on_opassign(target, operator, value)
1405 end
1406
1407 # @@foo &&= bar
1408 # ^^^^^^^^^^^^^
1409 def visit_class_variable_and_write_node(node)
1410 bounds(node.name_loc)
1411 target = on_var_field(on_cvar(node.name.to_s))
1412
1413 bounds(node.operator_loc)
1414 operator = on_op("&&=")
1415 value = visit_write_value(node.value)
1416
1417 bounds(node.location)
1418 on_opassign(target, operator, value)
1419 end
1420
1421 # @@foo ||= bar
1422 # ^^^^^^^^^^^^^
1423 def visit_class_variable_or_write_node(node)
1424 bounds(node.name_loc)
1425 target = on_var_field(on_cvar(node.name.to_s))
1426
1427 bounds(node.operator_loc)
1428 operator = on_op("||=")
1429 value = visit_write_value(node.value)
1430
1431 bounds(node.location)
1432 on_opassign(target, operator, value)
1433 end
1434
1435 # @@foo, = bar
1436 # ^^^^^
1437 def visit_class_variable_target_node(node)
1438 bounds(node.location)
1439 on_var_field(on_cvar(node.name.to_s))
1440 end
1441
1442 # Foo
1443 # ^^^
1444 def visit_constant_read_node(node)
1445 bounds(node.location)
1446 on_var_ref(on_const(node.name.to_s))
1447 end
1448
1449 # Foo = 1
1450 # ^^^^^^^
1451 #
1452 # Foo, Bar = 1
1453 # ^^^ ^^^
1454 def visit_constant_write_node(node)
1455 bounds(node.name_loc)
1456 target = on_var_field(on_const(node.name.to_s))
1457 value = visit_write_value(node.value)
1458
1459 bounds(node.location)
1460 on_assign(target, value)
1461 end
1462
1463 # Foo += bar
1464 # ^^^^^^^^^^^
1465 def visit_constant_operator_write_node(node)
1466 bounds(node.name_loc)
1467 target = on_var_field(on_const(node.name.to_s))
1468
1469 bounds(node.binary_operator_loc)
1470 operator = on_op("#{node.binary_operator}=")
1471 value = visit_write_value(node.value)
1472
1473 bounds(node.location)
1474 on_opassign(target, operator, value)
1475 end
1476
1477 # Foo &&= bar
1478 # ^^^^^^^^^^^^
1479 def visit_constant_and_write_node(node)
1480 bounds(node.name_loc)
1481 target = on_var_field(on_const(node.name.to_s))
1482
1483 bounds(node.operator_loc)
1484 operator = on_op("&&=")
1485 value = visit_write_value(node.value)
1486
1487 bounds(node.location)
1488 on_opassign(target, operator, value)
1489 end
1490
1491 # Foo ||= bar
1492 # ^^^^^^^^^^^^
1493 def visit_constant_or_write_node(node)
1494 bounds(node.name_loc)
1495 target = on_var_field(on_const(node.name.to_s))
1496
1497 bounds(node.operator_loc)
1498 operator = on_op("||=")
1499 value = visit_write_value(node.value)
1500
1501 bounds(node.location)
1502 on_opassign(target, operator, value)
1503 end
1504
1505 # Foo, = bar
1506 # ^^^
1507 def visit_constant_target_node(node)
1508 bounds(node.location)
1509 on_var_field(on_const(node.name.to_s))
1510 end
1511
1512 # Foo::Bar
1513 # ^^^^^^^^
1514 def visit_constant_path_node(node)
1515 if node.parent.nil?
1516 bounds(node.name_loc)
1517 child = on_const(node.name.to_s)
1518
1519 bounds(node.location)
1520 on_top_const_ref(child)
1521 else
1522 parent = visit(node.parent)
1523
1524 bounds(node.name_loc)
1525 child = on_const(node.name.to_s)
1526
1527 bounds(node.location)
1528 on_const_path_ref(parent, child)
1529 end
1530 end
1531
1532 # Foo::Bar = 1
1533 # ^^^^^^^^^^^^
1534 #
1535 # Foo::Foo, Bar::Bar = 1
1536 # ^^^^^^^^ ^^^^^^^^
1537 def visit_constant_path_write_node(node)
1538 target = visit_constant_path_write_node_target(node.target)
1539 value = visit_write_value(node.value)
1540
1541 bounds(node.location)
1542 on_assign(target, value)
1543 end
1544
1545 # Visit a constant path that is part of a write node.
1546 private def visit_constant_path_write_node_target(node)
1547 if node.parent.nil?
1548 bounds(node.name_loc)
1549 child = on_const(node.name.to_s)
1550
1551 bounds(node.location)
1552 on_top_const_field(child)
1553 else
1554 parent = visit(node.parent)
1555
1556 bounds(node.name_loc)
1557 child = on_const(node.name.to_s)
1558
1559 bounds(node.location)
1560 on_const_path_field(parent, child)
1561 end
1562 end
1563
1564 # Foo::Bar += baz
1565 # ^^^^^^^^^^^^^^^
1566 def visit_constant_path_operator_write_node(node)
1567 target = visit_constant_path_write_node_target(node.target)
1568 value = visit(node.value)
1569
1570 bounds(node.binary_operator_loc)
1571 operator = on_op("#{node.binary_operator}=")
1572 value = visit_write_value(node.value)
1573
1574 bounds(node.location)
1575 on_opassign(target, operator, value)
1576 end
1577
1578 # Foo::Bar &&= baz
1579 # ^^^^^^^^^^^^^^^^
1580 def visit_constant_path_and_write_node(node)
1581 target = visit_constant_path_write_node_target(node.target)
1582 value = visit(node.value)
1583
1584 bounds(node.operator_loc)
1585 operator = on_op("&&=")
1586 value = visit_write_value(node.value)
1587
1588 bounds(node.location)
1589 on_opassign(target, operator, value)
1590 end
1591
1592 # Foo::Bar ||= baz
1593 # ^^^^^^^^^^^^^^^^
1594 def visit_constant_path_or_write_node(node)
1595 target = visit_constant_path_write_node_target(node.target)
1596 value = visit(node.value)
1597
1598 bounds(node.operator_loc)
1599 operator = on_op("||=")
1600 value = visit_write_value(node.value)
1601
1602 bounds(node.location)
1603 on_opassign(target, operator, value)
1604 end
1605
1606 # Foo::Bar, = baz
1607 # ^^^^^^^^
1608 def visit_constant_path_target_node(node)
1609 visit_constant_path_write_node_target(node)
1610 end
1611
1612 # def foo; end
1613 # ^^^^^^^^^^^^
1614 #
1615 # def self.foo; end
1616 # ^^^^^^^^^^^^^^^^^
1617 def visit_def_node(node)
1618 receiver = visit(node.receiver)
1619 operator =
1620 if !node.operator_loc.nil?
1621 bounds(node.operator_loc)
1622 visit_token(node.operator)
1623 end
1624
1625 bounds(node.name_loc)
1626 name = visit_token(node.name_loc.slice)
1627
1628 parameters =
1629 if node.parameters.nil?
1630 bounds(node.location)
1631 on_params(nil, nil, nil, nil, nil, nil, nil)
1632 else
1633 visit(node.parameters)
1634 end
1635
1636 if !node.lparen_loc.nil?
1637 bounds(node.lparen_loc)
1638 parameters = on_paren(parameters)
1639 end
1640
1641 bodystmt =
1642 if node.equal_loc.nil?
1643 visit_body_node(node.rparen_loc || node.end_keyword_loc, node.body)
1644 else
1645 body = visit(node.body.body.first)
1646
1647 bounds(node.body.location)
1648 on_bodystmt(body, nil, nil, nil)
1649 end
1650
1651 bounds(node.location)
1652 if receiver
1653 on_defs(receiver, operator, name, parameters, bodystmt)
1654 else
1655 on_def(name, parameters, bodystmt)
1656 end
1657 end
1658
1659 # defined? a
1660 # ^^^^^^^^^^
1661 #
1662 # defined?(a)
1663 # ^^^^^^^^^^^
1664 def visit_defined_node(node)
1665 expression = visit(node.value)
1666
1667 # Very weird circumstances here where something like:
1668 #
1669 # defined?
1670 # (1)
1671 #
1672 # gets parsed in Ruby as having only the `1` expression but in Ripper it
1673 # gets parsed as having a parentheses node. In this case we need to
1674 # synthesize that node to match Ripper's behavior.
1675 if node.lparen_loc && node.keyword_loc.join(node.lparen_loc).slice.include?("\n")
1676 bounds(node.lparen_loc.join(node.rparen_loc))
1677 expression = on_paren(on_stmts_add(on_stmts_new, expression))
1678 end
1679
1680 bounds(node.location)
1681 on_defined(expression)
1682 end
1683
1684 # if foo then bar else baz end
1685 # ^^^^^^^^^^^^
1686 def visit_else_node(node)
1687 statements =
1688 if node.statements.nil?
1689 [nil]
1690 else
1691 body = node.statements.body
1692 body.unshift(nil) if void_stmt?(node.else_keyword_loc, node.statements.body[0].location, false)
1693 body
1694 end
1695
1696 bounds(node.location)
1697 on_else(visit_statements_node_body(statements))
1698 end
1699
1700 # "foo #{bar}"
1701 # ^^^^^^
1702 def visit_embedded_statements_node(node)
1703 bounds(node.opening_loc)
1704 on_embexpr_beg(node.opening)
1705
1706 statements =
1707 if node.statements.nil?
1708 bounds(node.location)
1709 on_stmts_add(on_stmts_new, on_void_stmt)
1710 else
1711 visit(node.statements)
1712 end
1713
1714 bounds(node.closing_loc)
1715 on_embexpr_end(node.closing)
1716
1717 bounds(node.location)
1718 on_string_embexpr(statements)
1719 end
1720
1721 # "foo #@bar"
1722 # ^^^^^
1723 def visit_embedded_variable_node(node)
1724 bounds(node.operator_loc)
1725 on_embvar(node.operator)
1726
1727 variable = visit(node.variable)
1728
1729 bounds(node.location)
1730 on_string_dvar(variable)
1731 end
1732
1733 # Visit an EnsureNode node.
1734 def visit_ensure_node(node)
1735 statements =
1736 if node.statements.nil?
1737 [nil]
1738 else
1739 body = node.statements.body
1740 body.unshift(nil) if void_stmt?(node.ensure_keyword_loc, body[0].location, false)
1741 body
1742 end
1743
1744 statements = visit_statements_node_body(statements)
1745
1746 bounds(node.location)
1747 on_ensure(statements)
1748 end
1749
1750 # false
1751 # ^^^^^
1752 def visit_false_node(node)
1753 bounds(node.location)
1754 on_var_ref(on_kw("false"))
1755 end
1756
1757 # foo => [*, bar, *]
1758 # ^^^^^^^^^^^
1759 def visit_find_pattern_node(node)
1760 constant = visit(node.constant)
1761 left =
1762 if node.left.expression.nil?
1763 bounds(node.left.location)
1764 on_var_field(nil)
1765 else
1766 visit(node.left.expression)
1767 end
1768
1769 requireds = visit_all(node.requireds) if node.requireds.any?
1770 right =
1771 if node.right.expression.nil?
1772 bounds(node.right.location)
1773 on_var_field(nil)
1774 else
1775 visit(node.right.expression)
1776 end
1777
1778 bounds(node.location)
1779 on_fndptn(constant, left, requireds, right)
1780 end
1781
1782 # if foo .. bar; end
1783 # ^^^^^^^^^^
1784 def visit_flip_flop_node(node)
1785 left = visit(node.left)
1786 right = visit(node.right)
1787
1788 bounds(node.location)
1789 if node.exclude_end?
1790 on_dot3(left, right)
1791 else
1792 on_dot2(left, right)
1793 end
1794 end
1795
1796 # 1.0
1797 # ^^^
1798 def visit_float_node(node)
1799 visit_number_node(node) { |text| on_float(text) }
1800 end
1801
1802 # for foo in bar do end
1803 # ^^^^^^^^^^^^^^^^^^^^^
1804 def visit_for_node(node)
1805 index = visit(node.index)
1806 collection = visit(node.collection)
1807 statements =
1808 if node.statements.nil?
1809 bounds(node.location)
1810 on_stmts_add(on_stmts_new, on_void_stmt)
1811 else
1812 visit(node.statements)
1813 end
1814
1815 bounds(node.location)
1816 on_for(index, collection, statements)
1817 end
1818
1819 # def foo(...); bar(...); end
1820 # ^^^
1821 def visit_forwarding_arguments_node(node)
1822 bounds(node.location)
1823 on_args_forward
1824 end
1825
1826 # def foo(...); end
1827 # ^^^
1828 def visit_forwarding_parameter_node(node)
1829 bounds(node.location)
1830 on_args_forward
1831 end
1832
1833 # super
1834 # ^^^^^
1835 #
1836 # super {}
1837 # ^^^^^^^^
1838 def visit_forwarding_super_node(node)
1839 if node.block.nil?
1840 bounds(node.location)
1841 on_zsuper
1842 else
1843 block = visit(node.block)
1844
1845 bounds(node.location)
1846 on_method_add_block(on_zsuper, block)
1847 end
1848 end
1849
1850 # $foo
1851 # ^^^^
1852 def visit_global_variable_read_node(node)
1853 bounds(node.location)
1854 on_var_ref(on_gvar(node.name.to_s))
1855 end
1856
1857 # $foo = 1
1858 # ^^^^^^^^
1859 #
1860 # $foo, $bar = 1
1861 # ^^^^ ^^^^
1862 def visit_global_variable_write_node(node)
1863 bounds(node.name_loc)
1864 target = on_var_field(on_gvar(node.name.to_s))
1865 value = visit_write_value(node.value)
1866
1867 bounds(node.location)
1868 on_assign(target, value)
1869 end
1870
1871 # $foo += bar
1872 # ^^^^^^^^^^^
1873 def visit_global_variable_operator_write_node(node)
1874 bounds(node.name_loc)
1875 target = on_var_field(on_gvar(node.name.to_s))
1876
1877 bounds(node.binary_operator_loc)
1878 operator = on_op("#{node.binary_operator}=")
1879 value = visit_write_value(node.value)
1880
1881 bounds(node.location)
1882 on_opassign(target, operator, value)
1883 end
1884
1885 # $foo &&= bar
1886 # ^^^^^^^^^^^^
1887 def visit_global_variable_and_write_node(node)
1888 bounds(node.name_loc)
1889 target = on_var_field(on_gvar(node.name.to_s))
1890
1891 bounds(node.operator_loc)
1892 operator = on_op("&&=")
1893 value = visit_write_value(node.value)
1894
1895 bounds(node.location)
1896 on_opassign(target, operator, value)
1897 end
1898
1899 # $foo ||= bar
1900 # ^^^^^^^^^^^^
1901 def visit_global_variable_or_write_node(node)
1902 bounds(node.name_loc)
1903 target = on_var_field(on_gvar(node.name.to_s))
1904
1905 bounds(node.operator_loc)
1906 operator = on_op("||=")
1907 value = visit_write_value(node.value)
1908
1909 bounds(node.location)
1910 on_opassign(target, operator, value)
1911 end
1912
1913 # $foo, = bar
1914 # ^^^^
1915 def visit_global_variable_target_node(node)
1916 bounds(node.location)
1917 on_var_field(on_gvar(node.name.to_s))
1918 end
1919
1920 # {}
1921 # ^^
1922 def visit_hash_node(node)
1923 elements =
1924 if node.elements.any?
1925 args = visit_all(node.elements)
1926
1927 bounds(node.elements.first.location)
1928 on_assoclist_from_args(args)
1929 end
1930
1931 bounds(node.location)
1932 on_hash(elements)
1933 end
1934
1935 # foo => {}
1936 # ^^
1937 def visit_hash_pattern_node(node)
1938 constant = visit(node.constant)
1939 elements =
1940 if node.elements.any? || !node.rest.nil?
1941 node.elements.map do |element|
1942 [
1943 if (key = element.key).opening_loc.nil?
1944 visit(key)
1945 else
1946 bounds(key.value_loc)
1947 if (value = key.value).empty?
1948 on_string_content
1949 else
1950 on_string_add(on_string_content, on_tstring_content(value))
1951 end
1952 end,
1953 visit(element.value)
1954 ]
1955 end
1956 end
1957
1958 rest =
1959 case node.rest
1960 when AssocSplatNode
1961 visit(node.rest.value)
1962 when NoKeywordsParameterNode
1963 bounds(node.rest.location)
1964 on_var_field(visit(node.rest))
1965 end
1966
1967 bounds(node.location)
1968 on_hshptn(constant, elements, rest)
1969 end
1970
1971 # if foo then bar end
1972 # ^^^^^^^^^^^^^^^^^^^
1973 #
1974 # bar if foo
1975 # ^^^^^^^^^^
1976 #
1977 # foo ? bar : baz
1978 # ^^^^^^^^^^^^^^^
1979 def visit_if_node(node)
1980 if node.then_keyword == "?"
1981 predicate = visit(node.predicate)
1982 truthy = visit(node.statements.body.first)
1983 falsy = visit(node.subsequent.statements.body.first)
1984
1985 bounds(node.location)
1986 on_ifop(predicate, truthy, falsy)
1987 elsif node.statements.nil? || (node.predicate.location.start_offset < node.statements.location.start_offset)
1988 predicate = visit(node.predicate)
1989 statements =
1990 if node.statements.nil?
1991 bounds(node.location)
1992 on_stmts_add(on_stmts_new, on_void_stmt)
1993 else
1994 visit(node.statements)
1995 end
1996 subsequent = visit(node.subsequent)
1997
1998 bounds(node.location)
1999 if node.if_keyword == "if"
2000 on_if(predicate, statements, subsequent)
2001 else
2002 on_elsif(predicate, statements, subsequent)
2003 end
2004 else
2005 statements = visit(node.statements.body.first)
2006 predicate = visit(node.predicate)
2007
2008 bounds(node.location)
2009 on_if_mod(predicate, statements)
2010 end
2011 end
2012
2013 # 1i
2014 # ^^
2015 def visit_imaginary_node(node)
2016 visit_number_node(node) { |text| on_imaginary(text) }
2017 end
2018
2019 # { foo: }
2020 # ^^^^
2021 def visit_implicit_node(node)
2022 end
2023
2024 # foo { |bar,| }
2025 # ^
2026 def visit_implicit_rest_node(node)
2027 bounds(node.location)
2028 on_excessed_comma
2029 end
2030
2031 # case foo; in bar; end
2032 # ^^^^^^^^^^^^^^^^^^^^^
2033 def visit_in_node(node)
2034 # This is a special case where we're not going to call on_in directly
2035 # because we don't have access to the subsequent. Instead, we'll return
2036 # the component parts and let the parent node handle it.
2037 pattern = visit_pattern_node(node.pattern)
2038 statements =
2039 if node.statements.nil?
2040 bounds(node.location)
2041 on_stmts_add(on_stmts_new, on_void_stmt)
2042 else
2043 visit(node.statements)
2044 end
2045
2046 [pattern, statements]
2047 end
2048
2049 # foo[bar] += baz
2050 # ^^^^^^^^^^^^^^^
2051 def visit_index_operator_write_node(node)
2052 receiver = visit(node.receiver)
2053 arguments, _, _ = visit_call_node_arguments(node.arguments, node.block, trailing_comma?(node.arguments&.location || node.location, node.closing_loc))
2054
2055 bounds(node.location)
2056 target = on_aref_field(receiver, arguments)
2057
2058 bounds(node.binary_operator_loc)
2059 operator = on_op("#{node.binary_operator}=")
2060 value = visit_write_value(node.value)
2061
2062 bounds(node.location)
2063 on_opassign(target, operator, value)
2064 end
2065
2066 # foo[bar] &&= baz
2067 # ^^^^^^^^^^^^^^^^
2068 def visit_index_and_write_node(node)
2069 receiver = visit(node.receiver)
2070 arguments, _, _ = visit_call_node_arguments(node.arguments, node.block, trailing_comma?(node.arguments&.location || node.location, node.closing_loc))
2071
2072 bounds(node.location)
2073 target = on_aref_field(receiver, arguments)
2074
2075 bounds(node.operator_loc)
2076 operator = on_op("&&=")
2077 value = visit_write_value(node.value)
2078
2079 bounds(node.location)
2080 on_opassign(target, operator, value)
2081 end
2082
2083 # foo[bar] ||= baz
2084 # ^^^^^^^^^^^^^^^^
2085 def visit_index_or_write_node(node)
2086 receiver = visit(node.receiver)
2087 arguments, _, _ = visit_call_node_arguments(node.arguments, node.block, trailing_comma?(node.arguments&.location || node.location, node.closing_loc))
2088
2089 bounds(node.location)
2090 target = on_aref_field(receiver, arguments)
2091
2092 bounds(node.operator_loc)
2093 operator = on_op("||=")
2094 value = visit_write_value(node.value)
2095
2096 bounds(node.location)
2097 on_opassign(target, operator, value)
2098 end
2099
2100 # foo[bar], = 1
2101 # ^^^^^^^^
2102 def visit_index_target_node(node)
2103 receiver = visit(node.receiver)
2104 arguments, _, _ = visit_call_node_arguments(node.arguments, node.block, trailing_comma?(node.arguments&.location || node.location, node.closing_loc))
2105
2106 bounds(node.location)
2107 on_aref_field(receiver, arguments)
2108 end
2109
2110 # @foo
2111 # ^^^^
2112 def visit_instance_variable_read_node(node)
2113 bounds(node.location)
2114 on_var_ref(on_ivar(node.name.to_s))
2115 end
2116
2117 # @foo = 1
2118 # ^^^^^^^^
2119 def visit_instance_variable_write_node(node)
2120 bounds(node.name_loc)
2121 target = on_var_field(on_ivar(node.name.to_s))
2122 value = visit_write_value(node.value)
2123
2124 bounds(node.location)
2125 on_assign(target, value)
2126 end
2127
2128 # @foo += bar
2129 # ^^^^^^^^^^^
2130 def visit_instance_variable_operator_write_node(node)
2131 bounds(node.name_loc)
2132 target = on_var_field(on_ivar(node.name.to_s))
2133
2134 bounds(node.binary_operator_loc)
2135 operator = on_op("#{node.binary_operator}=")
2136 value = visit_write_value(node.value)
2137
2138 bounds(node.location)
2139 on_opassign(target, operator, value)
2140 end
2141
2142 # @foo &&= bar
2143 # ^^^^^^^^^^^^
2144 def visit_instance_variable_and_write_node(node)
2145 bounds(node.name_loc)
2146 target = on_var_field(on_ivar(node.name.to_s))
2147
2148 bounds(node.operator_loc)
2149 operator = on_op("&&=")
2150 value = visit_write_value(node.value)
2151
2152 bounds(node.location)
2153 on_opassign(target, operator, value)
2154 end
2155
2156 # @foo ||= bar
2157 # ^^^^^^^^^^^^
2158 def visit_instance_variable_or_write_node(node)
2159 bounds(node.name_loc)
2160 target = on_var_field(on_ivar(node.name.to_s))
2161
2162 bounds(node.operator_loc)
2163 operator = on_op("||=")
2164 value = visit_write_value(node.value)
2165
2166 bounds(node.location)
2167 on_opassign(target, operator, value)
2168 end
2169
2170 # @foo, = bar
2171 # ^^^^
2172 def visit_instance_variable_target_node(node)
2173 bounds(node.location)
2174 on_var_field(on_ivar(node.name.to_s))
2175 end
2176
2177 # 1
2178 # ^
2179 def visit_integer_node(node)
2180 visit_number_node(node) { |text| on_int(text) }
2181 end
2182
2183 # if /foo #{bar}/ then end
2184 # ^^^^^^^^^^^^
2185 def visit_interpolated_match_last_line_node(node)
2186 bounds(node.opening_loc)
2187 on_regexp_beg(node.opening)
2188
2189 bounds(node.parts.first.location)
2190 parts =
2191 node.parts.inject(on_regexp_new) do |content, part|
2192 on_regexp_add(content, visit_string_content(part))
2193 end
2194
2195 bounds(node.closing_loc)
2196 closing = on_regexp_end(node.closing)
2197
2198 bounds(node.location)
2199 on_regexp_literal(parts, closing)
2200 end
2201
2202 # /foo #{bar}/
2203 # ^^^^^^^^^^^^
2204 def visit_interpolated_regular_expression_node(node)
2205 bounds(node.opening_loc)
2206 on_regexp_beg(node.opening)
2207
2208 bounds(node.parts.first.location)
2209 parts =
2210 node.parts.inject(on_regexp_new) do |content, part|
2211 on_regexp_add(content, visit_string_content(part))
2212 end
2213
2214 bounds(node.closing_loc)
2215 closing = on_regexp_end(node.closing)
2216
2217 bounds(node.location)
2218 on_regexp_literal(parts, closing)
2219 end
2220
2221 # "foo #{bar}"
2222 # ^^^^^^^^^^^^
2223 def visit_interpolated_string_node(node)
2224 if node.opening&.start_with?("<<~")
2225 heredoc = visit_heredoc_string_node(node)
2226
2227 bounds(node.location)
2228 on_string_literal(heredoc)
2229 elsif !node.heredoc? && node.parts.length > 1 && node.parts.any? { |part| (part.is_a?(StringNode) || part.is_a?(InterpolatedStringNode)) && !part.opening_loc.nil? }
2230 first, *rest = node.parts
2231 rest.inject(visit(first)) do |content, part|
2232 concat = visit(part)
2233
2234 bounds(part.location)
2235 on_string_concat(content, concat)
2236 end
2237 else
2238 bounds(node.parts.first.location)
2239 parts =
2240 node.parts.inject(on_string_content) do |content, part|
2241 on_string_add(content, visit_string_content(part))
2242 end
2243
2244 bounds(node.location)
2245 on_string_literal(parts)
2246 end
2247 end
2248
2249 # :"foo #{bar}"
2250 # ^^^^^^^^^^^^^
2251 def visit_interpolated_symbol_node(node)
2252 bounds(node.parts.first.location)
2253 parts =
2254 node.parts.inject(on_string_content) do |content, part|
2255 on_string_add(content, visit_string_content(part))
2256 end
2257
2258 bounds(node.location)
2259 on_dyna_symbol(parts)
2260 end
2261
2262 # `foo #{bar}`
2263 # ^^^^^^^^^^^^
2264 def visit_interpolated_x_string_node(node)
2265 if node.opening.start_with?("<<~")
2266 heredoc = visit_heredoc_x_string_node(node)
2267
2268 bounds(node.location)
2269 on_xstring_literal(heredoc)
2270 else
2271 bounds(node.parts.first.location)
2272 parts =
2273 node.parts.inject(on_xstring_new) do |content, part|
2274 on_xstring_add(content, visit_string_content(part))
2275 end
2276
2277 bounds(node.location)
2278 on_xstring_literal(parts)
2279 end
2280 end
2281
2282 # Visit an individual part of a string-like node.
2283 private def visit_string_content(part)
2284 if part.is_a?(StringNode)
2285 bounds(part.content_loc)
2286 on_tstring_content(part.content)
2287 else
2288 visit(part)
2289 end
2290 end
2291
2292 # -> { it }
2293 # ^^
2294 def visit_it_local_variable_read_node(node)
2295 bounds(node.location)
2296 on_vcall(on_ident(node.slice))
2297 end
2298
2299 # -> { it }
2300 # ^^^^^^^^^
2301 def visit_it_parameters_node(node)
2302 end
2303
2304 # foo(bar: baz)
2305 # ^^^^^^^^
2306 def visit_keyword_hash_node(node)
2307 elements = visit_all(node.elements)
2308
2309 bounds(node.location)
2310 on_bare_assoc_hash(elements)
2311 end
2312
2313 # def foo(**bar); end
2314 # ^^^^^
2315 #
2316 # def foo(**); end
2317 # ^^
2318 def visit_keyword_rest_parameter_node(node)
2319 if node.name_loc.nil?
2320 bounds(node.location)
2321 on_kwrest_param(nil)
2322 else
2323 bounds(node.name_loc)
2324 name = on_ident(node.name.to_s)
2325
2326 bounds(node.location)
2327 on_kwrest_param(name)
2328 end
2329 end
2330
2331 # -> {}
2332 def visit_lambda_node(node)
2333 bounds(node.operator_loc)
2334 on_tlambda(node.operator)
2335
2336 parameters =
2337 if node.parameters.is_a?(BlockParametersNode)
2338 # Ripper does not track block-locals within lambdas, so we skip
2339 # directly to the parameters here.
2340 params =
2341 if node.parameters.parameters.nil?
2342 bounds(node.location)
2343 on_params(nil, nil, nil, nil, nil, nil, nil)
2344 else
2345 visit(node.parameters.parameters)
2346 end
2347
2348 if node.parameters.opening_loc.nil?
2349 params
2350 else
2351 bounds(node.parameters.opening_loc)
2352 on_paren(params)
2353 end
2354 else
2355 bounds(node.location)
2356 on_params(nil, nil, nil, nil, nil, nil, nil)
2357 end
2358
2359 braces = node.opening == "{"
2360 if braces
2361 bounds(node.opening_loc)
2362 on_tlambeg(node.opening)
2363 end
2364
2365 body =
2366 case node.body
2367 when nil
2368 bounds(node.location)
2369 stmts = on_stmts_add(on_stmts_new, on_void_stmt)
2370
2371 bounds(node.location)
2372 braces ? stmts : on_bodystmt(stmts, nil, nil, nil)
2373 when StatementsNode
2374 stmts = node.body.body
2375 stmts.unshift(nil) if void_stmt?(node.parameters&.location || node.opening_loc, node.body.location, false)
2376 stmts = visit_statements_node_body(stmts)
2377
2378 bounds(node.body.location)
2379 braces ? stmts : on_bodystmt(stmts, nil, nil, nil)
2380 when BeginNode
2381 visit_body_node(node.opening_loc, node.body)
2382 else
2383 raise
2384 end
2385
2386 bounds(node.location)
2387 on_lambda(parameters, body)
2388 end
2389
2390 # foo
2391 # ^^^
2392 def visit_local_variable_read_node(node)
2393 bounds(node.location)
2394 on_var_ref(on_ident(node.slice))
2395 end
2396
2397 # foo = 1
2398 # ^^^^^^^
2399 def visit_local_variable_write_node(node)
2400 bounds(node.name_loc)
2401 target = on_var_field(on_ident(node.name_loc.slice))
2402 value = visit_write_value(node.value)
2403
2404 bounds(node.location)
2405 on_assign(target, value)
2406 end
2407
2408 # foo += bar
2409 # ^^^^^^^^^^
2410 def visit_local_variable_operator_write_node(node)
2411 bounds(node.name_loc)
2412 target = on_var_field(on_ident(node.name_loc.slice))
2413
2414 bounds(node.binary_operator_loc)
2415 operator = on_op("#{node.binary_operator}=")
2416 value = visit_write_value(node.value)
2417
2418 bounds(node.location)
2419 on_opassign(target, operator, value)
2420 end
2421
2422 # foo &&= bar
2423 # ^^^^^^^^^^^
2424 def visit_local_variable_and_write_node(node)
2425 bounds(node.name_loc)
2426 target = on_var_field(on_ident(node.name_loc.slice))
2427
2428 bounds(node.operator_loc)
2429 operator = on_op("&&=")
2430 value = visit_write_value(node.value)
2431
2432 bounds(node.location)
2433 on_opassign(target, operator, value)
2434 end
2435
2436 # foo ||= bar
2437 # ^^^^^^^^^^^
2438 def visit_local_variable_or_write_node(node)
2439 bounds(node.name_loc)
2440 target = on_var_field(on_ident(node.name_loc.slice))
2441
2442 bounds(node.operator_loc)
2443 operator = on_op("||=")
2444 value = visit_write_value(node.value)
2445
2446 bounds(node.location)
2447 on_opassign(target, operator, value)
2448 end
2449
2450 # foo, = bar
2451 # ^^^
2452 def visit_local_variable_target_node(node)
2453 bounds(node.location)
2454 on_var_field(on_ident(node.name.to_s))
2455 end
2456
2457 # if /foo/ then end
2458 # ^^^^^
2459 def visit_match_last_line_node(node)
2460 bounds(node.opening_loc)
2461 on_regexp_beg(node.opening)
2462
2463 bounds(node.content_loc)
2464 tstring_content = on_tstring_content(node.content)
2465
2466 bounds(node.closing_loc)
2467 closing = on_regexp_end(node.closing)
2468
2469 on_regexp_literal(on_regexp_add(on_regexp_new, tstring_content), closing)
2470 end
2471
2472 # foo in bar
2473 # ^^^^^^^^^^
2474 def visit_match_predicate_node(node)
2475 value = visit(node.value)
2476 pattern = on_in(visit_pattern_node(node.pattern), nil, nil)
2477
2478 on_case(value, pattern)
2479 end
2480
2481 # foo => bar
2482 # ^^^^^^^^^^
2483 def visit_match_required_node(node)
2484 value = visit(node.value)
2485 pattern = on_in(visit_pattern_node(node.pattern), nil, nil)
2486
2487 on_case(value, pattern)
2488 end
2489
2490 # /(?<foo>foo)/ =~ bar
2491 # ^^^^^^^^^^^^^^^^^^^^
2492 def visit_match_write_node(node)
2493 visit(node.call)
2494 end
2495
2496 # A node that is missing from the syntax tree. This is only used in the
2497 # case of a syntax error.
2498 def visit_missing_node(node)
2499 raise "Cannot visit missing nodes directly."
2500 end
2501
2502 # module Foo; end
2503 # ^^^^^^^^^^^^^^^
2504 def visit_module_node(node)
2505 constant_path =
2506 if node.constant_path.is_a?(ConstantReadNode)
2507 bounds(node.constant_path.location)
2508 on_const_ref(on_const(node.constant_path.name.to_s))
2509 else
2510 visit(node.constant_path)
2511 end
2512
2513 bodystmt = visit_body_node(node.constant_path.location, node.body, true)
2514
2515 bounds(node.location)
2516 on_module(constant_path, bodystmt)
2517 end
2518
2519 # (foo, bar), bar = qux
2520 # ^^^^^^^^^^
2521 def visit_multi_target_node(node)
2522 bounds(node.location)
2523 targets = visit_multi_target_node_targets(node.lefts, node.rest, node.rights, true)
2524
2525 if node.lparen_loc.nil?
2526 targets
2527 else
2528 bounds(node.lparen_loc)
2529 on_mlhs_paren(targets)
2530 end
2531 end
2532
2533 # Visit the targets of a multi-target node.
2534 private def visit_multi_target_node_targets(lefts, rest, rights, skippable)
2535 if skippable && lefts.length == 1 && lefts.first.is_a?(MultiTargetNode) && rest.nil? && rights.empty?
2536 return visit(lefts.first)
2537 end
2538
2539 mlhs = on_mlhs_new
2540
2541 lefts.each do |left|
2542 bounds(left.location)
2543 mlhs = on_mlhs_add(mlhs, visit(left))
2544 end
2545
2546 case rest
2547 when nil
2548 # do nothing
2549 when ImplicitRestNode
2550 # these do not get put into the generated tree
2551 bounds(rest.location)
2552 on_excessed_comma
2553 else
2554 bounds(rest.location)
2555 mlhs = on_mlhs_add_star(mlhs, visit(rest))
2556 end
2557
2558 if rights.any?
2559 bounds(rights.first.location)
2560 post = on_mlhs_new
2561
2562 rights.each do |right|
2563 bounds(right.location)
2564 post = on_mlhs_add(post, visit(right))
2565 end
2566
2567 mlhs = on_mlhs_add_post(mlhs, post)
2568 end
2569
2570 mlhs
2571 end
2572
2573 # foo, bar = baz
2574 # ^^^^^^^^^^^^^^
2575 def visit_multi_write_node(node)
2576 bounds(node.location)
2577 targets = visit_multi_target_node_targets(node.lefts, node.rest, node.rights, true)
2578
2579 unless node.lparen_loc.nil?
2580 bounds(node.lparen_loc)
2581 targets = on_mlhs_paren(targets)
2582 end
2583
2584 value = visit_write_value(node.value)
2585
2586 bounds(node.location)
2587 on_massign(targets, value)
2588 end
2589
2590 # next
2591 # ^^^^
2592 #
2593 # next foo
2594 # ^^^^^^^^
2595 def visit_next_node(node)
2596 if node.arguments.nil?
2597 bounds(node.location)
2598 on_next(on_args_new)
2599 else
2600 arguments = visit(node.arguments)
2601
2602 bounds(node.location)
2603 on_next(arguments)
2604 end
2605 end
2606
2607 # nil
2608 # ^^^
2609 def visit_nil_node(node)
2610 bounds(node.location)
2611 on_var_ref(on_kw("nil"))
2612 end
2613
2614 # def foo(**nil); end
2615 # ^^^^^
2616 def visit_no_keywords_parameter_node(node)
2617 bounds(node.location)
2618 on_nokw_param(nil)
2619
2620 :nil
2621 end
2622
2623 # -> { _1 + _2 }
2624 # ^^^^^^^^^^^^^^
2625 def visit_numbered_parameters_node(node)
2626 end
2627
2628 # $1
2629 # ^^
2630 def visit_numbered_reference_read_node(node)
2631 bounds(node.location)
2632 on_backref(node.slice)
2633 end
2634
2635 # def foo(bar: baz); end
2636 # ^^^^^^^^
2637 def visit_optional_keyword_parameter_node(node)
2638 bounds(node.name_loc)
2639 name = on_label("#{node.name}:")
2640 value = visit(node.value)
2641
2642 [name, value]
2643 end
2644
2645 # def foo(bar = 1); end
2646 # ^^^^^^^
2647 def visit_optional_parameter_node(node)
2648 bounds(node.name_loc)
2649 name = visit_token(node.name.to_s)
2650 value = visit(node.value)
2651
2652 [name, value]
2653 end
2654
2655 # a or b
2656 # ^^^^^^
2657 def visit_or_node(node)
2658 left = visit(node.left)
2659 right = visit(node.right)
2660
2661 bounds(node.location)
2662 on_binary(left, node.operator.to_sym, right)
2663 end
2664
2665 # def foo(bar, *baz); end
2666 # ^^^^^^^^^
2667 def visit_parameters_node(node)
2668 requireds = node.requireds.map { |required| required.is_a?(MultiTargetNode) ? visit_destructured_parameter_node(required) : visit(required) } if node.requireds.any?
2669 optionals = visit_all(node.optionals) if node.optionals.any?
2670 rest = visit(node.rest)
2671 posts = node.posts.map { |post| post.is_a?(MultiTargetNode) ? visit_destructured_parameter_node(post) : visit(post) } if node.posts.any?
2672 keywords = visit_all(node.keywords) if node.keywords.any?
2673 keyword_rest = visit(node.keyword_rest)
2674 block = visit(node.block)
2675
2676 bounds(node.location)
2677 on_params(requireds, optionals, rest, posts, keywords, keyword_rest, block)
2678 end
2679
2680 # Visit a destructured positional parameter node.
2681 private def visit_destructured_parameter_node(node)
2682 bounds(node.location)
2683 targets = visit_multi_target_node_targets(node.lefts, node.rest, node.rights, false)
2684
2685 bounds(node.lparen_loc)
2686 on_mlhs_paren(targets)
2687 end
2688
2689 # ()
2690 # ^^
2691 #
2692 # (1)
2693 # ^^^
2694 def visit_parentheses_node(node)
2695 body =
2696 if node.body.nil?
2697 on_stmts_add(on_stmts_new, on_void_stmt)
2698 else
2699 visit(node.body)
2700 end
2701
2702 bounds(node.location)
2703 on_paren(body)
2704 end
2705
2706 # foo => ^(bar)
2707 # ^^^^^^
2708 def visit_pinned_expression_node(node)
2709 expression = visit(node.expression)
2710
2711 bounds(node.location)
2712 on_begin(expression)
2713 end
2714
2715 # foo = 1 and bar => ^foo
2716 # ^^^^
2717 def visit_pinned_variable_node(node)
2718 visit(node.variable)
2719 end
2720
2721 # END {}
2722 # ^^^^^^
2723 def visit_post_execution_node(node)
2724 statements =
2725 if node.statements.nil?
2726 bounds(node.location)
2727 on_stmts_add(on_stmts_new, on_void_stmt)
2728 else
2729 visit(node.statements)
2730 end
2731
2732 bounds(node.location)
2733 on_END(statements)
2734 end
2735
2736 # BEGIN {}
2737 # ^^^^^^^^
2738 def visit_pre_execution_node(node)
2739 statements =
2740 if node.statements.nil?
2741 bounds(node.location)
2742 on_stmts_add(on_stmts_new, on_void_stmt)
2743 else
2744 visit(node.statements)
2745 end
2746
2747 bounds(node.location)
2748 on_BEGIN(statements)
2749 end
2750
2751 # The top-level program node.
2752 def visit_program_node(node)
2753 body = node.statements.body
2754 body << nil if body.empty?
2755 statements = visit_statements_node_body(body)
2756
2757 bounds(node.location)
2758 on_program(statements)
2759 end
2760
2761 # 0..5
2762 # ^^^^
2763 def visit_range_node(node)
2764 left = visit(node.left)
2765 right = visit(node.right)
2766
2767 bounds(node.location)
2768 if node.exclude_end?
2769 on_dot3(left, right)
2770 else
2771 on_dot2(left, right)
2772 end
2773 end
2774
2775 # 1r
2776 # ^^
2777 def visit_rational_node(node)
2778 visit_number_node(node) { |text| on_rational(text) }
2779 end
2780
2781 # redo
2782 # ^^^^
2783 def visit_redo_node(node)
2784 bounds(node.location)
2785 on_redo
2786 end
2787
2788 # /foo/
2789 # ^^^^^
2790 def visit_regular_expression_node(node)
2791 bounds(node.opening_loc)
2792 on_regexp_beg(node.opening)
2793
2794 if node.content.empty?
2795 bounds(node.closing_loc)
2796 closing = on_regexp_end(node.closing)
2797
2798 on_regexp_literal(on_regexp_new, closing)
2799 else
2800 bounds(node.content_loc)
2801 tstring_content = on_tstring_content(node.content)
2802
2803 bounds(node.closing_loc)
2804 closing = on_regexp_end(node.closing)
2805
2806 on_regexp_literal(on_regexp_add(on_regexp_new, tstring_content), closing)
2807 end
2808 end
2809
2810 # def foo(bar:); end
2811 # ^^^^
2812 def visit_required_keyword_parameter_node(node)
2813 bounds(node.name_loc)
2814 [on_label("#{node.name}:"), false]
2815 end
2816
2817 # def foo(bar); end
2818 # ^^^
2819 def visit_required_parameter_node(node)
2820 bounds(node.location)
2821 on_ident(node.name.to_s)
2822 end
2823
2824 # foo rescue bar
2825 # ^^^^^^^^^^^^^^
2826 def visit_rescue_modifier_node(node)
2827 expression = visit_write_value(node.expression)
2828 rescue_expression = visit(node.rescue_expression)
2829
2830 bounds(node.location)
2831 on_rescue_mod(expression, rescue_expression)
2832 end
2833
2834 # begin; rescue; end
2835 # ^^^^^^^
2836 def visit_rescue_node(node)
2837 exceptions =
2838 case node.exceptions.length
2839 when 0
2840 nil
2841 when 1
2842 if (exception = node.exceptions.first).is_a?(SplatNode)
2843 bounds(exception.location)
2844 on_mrhs_add_star(on_mrhs_new, visit(exception))
2845 else
2846 [visit(node.exceptions.first)]
2847 end
2848 else
2849 bounds(node.location)
2850 length = node.exceptions.length
2851
2852 node.exceptions.each_with_index.inject(on_args_new) do |mrhs, (exception, index)|
2853 arg = visit(exception)
2854
2855 bounds(exception.location)
2856 mrhs = on_mrhs_new_from_args(mrhs) if index == length - 1
2857
2858 if exception.is_a?(SplatNode)
2859 if index == length - 1
2860 on_mrhs_add_star(mrhs, arg)
2861 else
2862 on_args_add_star(mrhs, arg)
2863 end
2864 else
2865 if index == length - 1
2866 on_mrhs_add(mrhs, arg)
2867 else
2868 on_args_add(mrhs, arg)
2869 end
2870 end
2871 end
2872 end
2873
2874 reference = visit(node.reference)
2875 statements =
2876 if node.statements.nil?
2877 bounds(node.location)
2878 on_stmts_add(on_stmts_new, on_void_stmt)
2879 else
2880 visit(node.statements)
2881 end
2882
2883 subsequent = visit(node.subsequent)
2884
2885 bounds(node.location)
2886 on_rescue(exceptions, reference, statements, subsequent)
2887 end
2888
2889 # def foo(*bar); end
2890 # ^^^^
2891 #
2892 # def foo(*); end
2893 # ^
2894 def visit_rest_parameter_node(node)
2895 if node.name_loc.nil?
2896 bounds(node.location)
2897 on_rest_param(nil)
2898 else
2899 bounds(node.name_loc)
2900 on_rest_param(visit_token(node.name.to_s))
2901 end
2902 end
2903
2904 # retry
2905 # ^^^^^
2906 def visit_retry_node(node)
2907 bounds(node.location)
2908 on_retry
2909 end
2910
2911 # return
2912 # ^^^^^^
2913 #
2914 # return 1
2915 # ^^^^^^^^
2916 def visit_return_node(node)
2917 if node.arguments.nil?
2918 bounds(node.location)
2919 on_return0
2920 else
2921 arguments = visit(node.arguments)
2922
2923 bounds(node.location)
2924 on_return(arguments)
2925 end
2926 end
2927
2928 # self
2929 # ^^^^
2930 def visit_self_node(node)
2931 bounds(node.location)
2932 on_var_ref(on_kw("self"))
2933 end
2934
2935 # A shareable constant.
2936 def visit_shareable_constant_node(node)
2937 visit(node.write)
2938 end
2939
2940 # class << self; end
2941 # ^^^^^^^^^^^^^^^^^^
2942 def visit_singleton_class_node(node)
2943 expression = visit(node.expression)
2944 bodystmt = visit_body_node(node.body&.location || node.end_keyword_loc, node.body)
2945
2946 bounds(node.location)
2947 on_sclass(expression, bodystmt)
2948 end
2949
2950 # __ENCODING__
2951 # ^^^^^^^^^^^^
2952 def visit_source_encoding_node(node)
2953 bounds(node.location)
2954 on_var_ref(on_kw("__ENCODING__"))
2955 end
2956
2957 # __FILE__
2958 # ^^^^^^^^
2959 def visit_source_file_node(node)
2960 bounds(node.location)
2961 on_var_ref(on_kw("__FILE__"))
2962 end
2963
2964 # __LINE__
2965 # ^^^^^^^^
2966 def visit_source_line_node(node)
2967 bounds(node.location)
2968 on_var_ref(on_kw("__LINE__"))
2969 end
2970
2971 # foo(*bar)
2972 # ^^^^
2973 #
2974 # def foo((bar, *baz)); end
2975 # ^^^^
2976 #
2977 # def foo(*); bar(*); end
2978 # ^
2979 def visit_splat_node(node)
2980 visit(node.expression)
2981 end
2982
2983 # A list of statements.
2984 def visit_statements_node(node)
2985 bounds(node.location)
2986 visit_statements_node_body(node.body)
2987 end
2988
2989 # Visit the list of statements of a statements node. We support nil
2990 # statements in the list. This would normally not be allowed by the
2991 # structure of the prism parse tree, but we manually add them here so that
2992 # we can mirror Ripper's void stmt.
2993 private def visit_statements_node_body(body)
2994 body.inject(on_stmts_new) do |stmts, stmt|
2995 on_stmts_add(stmts, stmt.nil? ? on_void_stmt : visit(stmt))
2996 end
2997 end
2998
2999 # "foo"
3000 # ^^^^^
3001 def visit_string_node(node)
3002 if (content = node.content).empty?
3003 bounds(node.location)
3004 on_string_literal(on_string_content)
3005 elsif (opening = node.opening) == "?"
3006 bounds(node.location)
3007 on_CHAR("?#{node.content}")
3008 elsif opening.start_with?("<<~")
3009 heredoc = visit_heredoc_string_node(node.to_interpolated)
3010
3011 bounds(node.location)
3012 on_string_literal(heredoc)
3013 else
3014 bounds(node.content_loc)
3015 tstring_content = on_tstring_content(content)
3016
3017 bounds(node.location)
3018 on_string_literal(on_string_add(on_string_content, tstring_content))
3019 end
3020 end
3021
3022 # Ripper gives back the escaped string content but strips out the common
3023 # leading whitespace. Prism gives back the unescaped string content and
3024 # a location for the escaped string content. Unfortunately these don't
3025 # work well together, so here we need to re-derive the common leading
3026 # whitespace.
3027 private def visit_heredoc_node_whitespace(parts)
3028 common_whitespace = nil
3029 dedent_next = true
3030
3031 parts.each do |part|
3032 if part.is_a?(StringNode)
3033 if dedent_next && !(content = part.content).chomp.empty?
3034 common_whitespace = [
3035 common_whitespace || Float::INFINITY,
3036 content[/\A\s*/].each_char.inject(0) do |part_whitespace, char|
3037 char == "\t" ? ((part_whitespace / 8 + 1) * 8) : (part_whitespace + 1)
3038 end
3039 ].min
3040 end
3041
3042 dedent_next = true
3043 else
3044 dedent_next = false
3045 end
3046 end
3047
3048 common_whitespace || 0
3049 end
3050
3051 # Visit a string that is expressed using a <<~ heredoc.
3052 private def visit_heredoc_node(parts, base)
3053 common_whitespace = visit_heredoc_node_whitespace(parts)
3054
3055 if common_whitespace == 0
3056 bounds(parts.first.location)
3057
3058 string = []
3059 result = base
3060
3061 parts.each do |part|
3062 if part.is_a?(StringNode)
3063 if string.empty?
3064 string = [part]
3065 else
3066 string << part
3067 end
3068 else
3069 unless string.empty?
3070 bounds(string[0].location)
3071 result = yield result, on_tstring_content(string.map(&:content).join)
3072 string = []
3073 end
3074
3075 result = yield result, visit(part)
3076 end
3077 end
3078
3079 unless string.empty?
3080 bounds(string[0].location)
3081 result = yield result, on_tstring_content(string.map(&:content).join)
3082 end
3083
3084 result
3085 else
3086 bounds(parts.first.location)
3087 result =
3088 parts.inject(base) do |string_content, part|
3089 yield string_content, visit_string_content(part)
3090 end
3091
3092 bounds(parts.first.location)
3093 on_heredoc_dedent(result, common_whitespace)
3094 end
3095 end
3096
3097 # Visit a heredoc node that is representing a string.
3098 private def visit_heredoc_string_node(node)
3099 bounds(node.opening_loc)
3100 on_heredoc_beg(node.opening)
3101
3102 bounds(node.location)
3103 result =
3104 visit_heredoc_node(node.parts, on_string_content) do |parts, part|
3105 on_string_add(parts, part)
3106 end
3107
3108 bounds(node.closing_loc)
3109 on_heredoc_end(node.closing)
3110
3111 result
3112 end
3113
3114 # Visit a heredoc node that is representing an xstring.
3115 private def visit_heredoc_x_string_node(node)
3116 bounds(node.opening_loc)
3117 on_heredoc_beg(node.opening)
3118
3119 bounds(node.location)
3120 result =
3121 visit_heredoc_node(node.parts, on_xstring_new) do |parts, part|
3122 on_xstring_add(parts, part)
3123 end
3124
3125 bounds(node.closing_loc)
3126 on_heredoc_end(node.closing)
3127
3128 result
3129 end
3130
3131 # super(foo)
3132 # ^^^^^^^^^^
3133 def visit_super_node(node)
3134 arguments, block, has_ripper_block = visit_call_node_arguments(node.arguments, node.block, trailing_comma?(node.arguments&.location || node.location, node.rparen_loc || node.location))
3135
3136 if !node.lparen_loc.nil?
3137 bounds(node.lparen_loc)
3138 arguments = on_arg_paren(arguments)
3139 end
3140
3141 bounds(node.location)
3142 call = on_super(arguments)
3143
3144 if has_ripper_block
3145 bounds(node.block.location)
3146 on_method_add_block(call, block)
3147 else
3148 call
3149 end
3150 end
3151
3152 # :foo
3153 # ^^^^
3154 def visit_symbol_node(node)
3155 if (opening = node.opening)&.match?(/^%s|['"]:?$/)
3156 bounds(node.value_loc)
3157 content = on_string_content
3158
3159 if !(value = node.value).empty?
3160 content = on_string_add(content, on_tstring_content(value))
3161 end
3162
3163 on_dyna_symbol(content)
3164 elsif (closing = node.closing) == ":"
3165 bounds(node.location)
3166 on_label("#{node.value}:")
3167 elsif opening.nil? && node.closing_loc.nil?
3168 bounds(node.value_loc)
3169 on_symbol_literal(visit_token(node.value))
3170 else
3171 bounds(node.value_loc)
3172 on_symbol_literal(on_symbol(visit_token(node.value)))
3173 end
3174 end
3175
3176 # true
3177 # ^^^^
3178 def visit_true_node(node)
3179 bounds(node.location)
3180 on_var_ref(on_kw("true"))
3181 end
3182
3183 # undef foo
3184 # ^^^^^^^^^
3185 def visit_undef_node(node)
3186 names = visit_all(node.names)
3187
3188 bounds(node.location)
3189 on_undef(names)
3190 end
3191
3192 # unless foo; bar end
3193 # ^^^^^^^^^^^^^^^^^^^
3194 #
3195 # bar unless foo
3196 # ^^^^^^^^^^^^^^
3197 def visit_unless_node(node)
3198 if node.statements.nil? || (node.predicate.location.start_offset < node.statements.location.start_offset)
3199 predicate = visit(node.predicate)
3200 statements =
3201 if node.statements.nil?
3202 bounds(node.location)
3203 on_stmts_add(on_stmts_new, on_void_stmt)
3204 else
3205 visit(node.statements)
3206 end
3207 else_clause = visit(node.else_clause)
3208
3209 bounds(node.location)
3210 on_unless(predicate, statements, else_clause)
3211 else
3212 statements = visit(node.statements.body.first)
3213 predicate = visit(node.predicate)
3214
3215 bounds(node.location)
3216 on_unless_mod(predicate, statements)
3217 end
3218 end
3219
3220 # until foo; bar end
3221 # ^^^^^^^^^^^^^^^^^
3222 #
3223 # bar until foo
3224 # ^^^^^^^^^^^^^
3225 def visit_until_node(node)
3226 if node.statements.nil? || (node.predicate.location.start_offset < node.statements.location.start_offset)
3227 predicate = visit(node.predicate)
3228 statements =
3229 if node.statements.nil?
3230 bounds(node.location)
3231 on_stmts_add(on_stmts_new, on_void_stmt)
3232 else
3233 visit(node.statements)
3234 end
3235
3236 bounds(node.location)
3237 on_until(predicate, statements)
3238 else
3239 statements = visit(node.statements.body.first)
3240 predicate = visit(node.predicate)
3241
3242 bounds(node.location)
3243 on_until_mod(predicate, statements)
3244 end
3245 end
3246
3247 # case foo; when bar; end
3248 # ^^^^^^^^^^^^^
3249 def visit_when_node(node)
3250 # This is a special case where we're not going to call on_when directly
3251 # because we don't have access to the subsequent. Instead, we'll return
3252 # the component parts and let the parent node handle it.
3253 conditions = visit_arguments(node.conditions)
3254 statements =
3255 if node.statements.nil?
3256 bounds(node.location)
3257 on_stmts_add(on_stmts_new, on_void_stmt)
3258 else
3259 visit(node.statements)
3260 end
3261
3262 [conditions, statements]
3263 end
3264
3265 # while foo; bar end
3266 # ^^^^^^^^^^^^^^^^^^
3267 #
3268 # bar while foo
3269 # ^^^^^^^^^^^^^
3270 def visit_while_node(node)
3271 if node.statements.nil? || (node.predicate.location.start_offset < node.statements.location.start_offset)
3272 predicate = visit(node.predicate)
3273 statements =
3274 if node.statements.nil?
3275 bounds(node.location)
3276 on_stmts_add(on_stmts_new, on_void_stmt)
3277 else
3278 visit(node.statements)
3279 end
3280
3281 bounds(node.location)
3282 on_while(predicate, statements)
3283 else
3284 statements = visit(node.statements.body.first)
3285 predicate = visit(node.predicate)
3286
3287 bounds(node.location)
3288 on_while_mod(predicate, statements)
3289 end
3290 end
3291
3292 # `foo`
3293 # ^^^^^
3294 def visit_x_string_node(node)
3295 if node.unescaped.empty?
3296 bounds(node.location)
3297 on_xstring_literal(on_xstring_new)
3298 elsif node.opening.start_with?("<<~")
3299 heredoc = visit_heredoc_x_string_node(node.to_interpolated)
3300
3301 bounds(node.location)
3302 on_xstring_literal(heredoc)
3303 else
3304 bounds(node.content_loc)
3305 content = on_tstring_content(node.content)
3306
3307 bounds(node.location)
3308 on_xstring_literal(on_xstring_add(on_xstring_new, content))
3309 end
3310 end
3311
3312 # yield
3313 # ^^^^^
3314 #
3315 # yield 1
3316 # ^^^^^^^
3317 def visit_yield_node(node)
3318 if node.arguments.nil? && node.lparen_loc.nil?
3319 bounds(node.location)
3320 on_yield0
3321 else
3322 arguments =
3323 if node.arguments.nil?
3324 bounds(node.location)
3325 on_args_new
3326 else
3327 visit(node.arguments)
3328 end
3329
3330 unless node.lparen_loc.nil?
3331 bounds(node.lparen_loc)
3332 arguments = on_paren(arguments)
3333 end
3334
3335 bounds(node.location)
3336 on_yield(arguments)
3337 end
3338 end
3339
3340 private
3341
3342 # Lazily initialize the parse result.
3343 def result
3344 @result ||= Prism.parse(source, partial_script: true, version: "current")
3345 end
3346
3347 ##########################################################################
3348 # Helpers
3349 ##########################################################################
3350
3351 # Returns true if there is a comma between the two locations.
3352 def trailing_comma?(left, right)
3353 source.byteslice(left.end_offset...right.start_offset).include?(",")
3354 end
3355
3356 # Returns true if there is a semicolon between the two locations.
3357 def void_stmt?(left, right, allow_newline)
3358 pattern = allow_newline ? /[;\n]/ : /;/
3359 source.byteslice(left.end_offset...right.start_offset).match?(pattern)
3360 end
3361
3362 # Visit the string content of a particular node. This method is used to
3363 # split into the various token types.
3364 def visit_token(token, allow_keywords = true)
3365 case token
3366 when "."
3367 on_period(token)
3368 when "`"
3369 on_backtick(token)
3370 when *(allow_keywords ? KEYWORDS : [])
3371 on_kw(token)
3372 when /^_/
3373 on_ident(token)
3374 when /^[[:upper:]]\w*$/
3375 on_const(token)
3376 when /^@@/
3377 on_cvar(token)
3378 when /^@/
3379 on_ivar(token)
3380 when /^\$/
3381 on_gvar(token)
3382 when /^[[:punct:]]/
3383 on_op(token)
3384 else
3385 on_ident(token)
3386 end
3387 end
3388
3389 # Visit a node that represents a number. We need to explicitly handle the
3390 # unary - operator.
3391 def visit_number_node(node)
3392 slice = node.slice
3393 location = node.location
3394
3395 if slice[0] == "-"
3396 bounds(location.copy(start_offset: location.start_offset + 1))
3397 value = yield slice[1..-1]
3398
3399 bounds(node.location)
3400 on_unary(:-@, value)
3401 else
3402 bounds(location)
3403 yield slice
3404 end
3405 end
3406
3407 # Visit a node that represents a write value. This is used to handle the
3408 # special case of an implicit array that is generated without brackets.
3409 def visit_write_value(node)
3410 if node.is_a?(ArrayNode) && node.opening_loc.nil?
3411 elements = node.elements
3412 length = elements.length
3413
3414 bounds(elements.first.location)
3415 elements.each_with_index.inject((elements.first.is_a?(SplatNode) && length == 1) ? on_mrhs_new : on_args_new) do |args, (element, index)|
3416 arg = visit(element)
3417 bounds(element.location)
3418
3419 if index == length - 1
3420 if element.is_a?(SplatNode)
3421 mrhs = index == 0 ? args : on_mrhs_new_from_args(args)
3422 on_mrhs_add_star(mrhs, arg)
3423 else
3424 on_mrhs_add(on_mrhs_new_from_args(args), arg)
3425 end
3426 else
3427 case element
3428 when BlockArgumentNode
3429 on_args_add_block(args, arg)
3430 when SplatNode
3431 on_args_add_star(args, arg)
3432 else
3433 on_args_add(args, arg)
3434 end
3435 end
3436 end
3437 else
3438 visit(node)
3439 end
3440 end
3441
3442 # This method is responsible for updating lineno and column information
3443 # to reflect the current node.
3444 #
3445 # This method could be drastically improved with some caching on the start
3446 # of every line, but for now it's good enough.
3447 def bounds(location)
3448 @lineno = location.start_line
3449 @column = location.start_column
3450 end
3451
3452 ##########################################################################
3453 # Ripper interface
3454 ##########################################################################
3455
3456 # :stopdoc:
3457 def _dispatch_0; end
3458 def _dispatch_1(arg); arg end
3459 def _dispatch_2(arg, _); arg end
3460 def _dispatch_3(arg, _, _); arg end
3461 def _dispatch_4(arg, _, _, _); arg end
3462 def _dispatch_5(arg, _, _, _, _); arg end
3463 def _dispatch_7(arg, _, _, _, _, _, _); arg end
3464 # :startdoc:
3465
3466 #
3467 # Parser Events
3468 #
3469
3470 PARSER_EVENT_TABLE.each do |id, arity|
3471 alias_method "on_#{id}", "_dispatch_#{arity}"
3472 end
3473
3474 # This method is called when weak warning is produced by the parser.
3475 # +fmt+ and +args+ is printf style.
3476 def warn(fmt, *args)
3477 end
3478
3479 # This method is called when strong warning is produced by the parser.
3480 # +fmt+ and +args+ is printf style.
3481 def warning(fmt, *args)
3482 end
3483
3484 # This method is called when the parser found syntax error.
3485 def compile_error(msg)
3486 end
3487
3488 #
3489 # Scanner Events
3490 #
3491
3492 SCANNER_EVENTS.each do |id|
3493 alias_method "on_#{id}", :_dispatch_1
3494 end
3495
3496 # This method is provided by the Ripper C extension. It is called when a
3497 # string needs to be dedented because of a tilde heredoc. It is expected
3498 # that it will modify the string in place and return the number of bytes
3499 # that were removed.
3500 def dedent_string(string, width)
3501 whitespace = 0
3502 cursor = 0
3503
3504 while cursor < string.length && string[cursor].match?(/\s/) && whitespace < width
3505 if string[cursor] == "\t"
3506 whitespace = ((whitespace / 8 + 1) * 8)
3507 break if whitespace > width
3508 else
3509 whitespace += 1
3510 end
3511
3512 cursor += 1
3513 end
3514
3515 string.replace(string[cursor..])
3516 cursor
3517 end
3518 end
3519 end
3520 end