/
ripper.rb
2618 lines (2200 loc) · 69.9 KB
/
ripper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# frozen_string_literal: true
require "ripper"
module Prism
module Translation
# Note: This integration is not finished, and therefore still has many
# inconsistencies with Ripper. If you'd like to help out, pull requests
# would be greatly appreciated!
#
# This class is meant to provide a compatibility layer between prism and
# Ripper. It functions by parsing the entire tree first and then walking it
# and executing each of the Ripper callbacks as it goes.
#
# This class is going to necessarily be slower than the native Ripper API.
# It is meant as a stopgap until developers migrate to using prism. It is
# also meant as a test harness for the prism parser.
#
# To use this class, you treat `Prism::Translation::Ripper` effectively as
# you would treat the `Ripper` class.
class Ripper < Compiler
# This class mirrors the ::Ripper::SexpBuilder subclass of ::Ripper that
# returns the arrays of [type, *children].
class SexpBuilder < Ripper
private
::Ripper::PARSER_EVENTS.each do |event|
define_method(:"on_#{event}") do |*args|
[event, *args]
end
end
::Ripper::SCANNER_EVENTS.each do |event|
define_method(:"on_#{event}") do |value|
[:"@#{event}", value, [lineno, column]]
end
end
end
# This class mirrors the ::Ripper::SexpBuilderPP subclass of ::Ripper that
# returns the same values as ::Ripper::SexpBuilder except with a couple of
# niceties that flatten linked lists into arrays.
class SexpBuilderPP < SexpBuilder
private
def _dispatch_event_new # :nodoc:
[]
end
def _dispatch_event_push(list, item) # :nodoc:
list << item
list
end
::Ripper::PARSER_EVENT_TABLE.each do |event, arity|
case event
when /_new\z/
alias_method :"on_#{event}", :_dispatch_event_new if arity == 0
when /_add\z/
alias_method :"on_#{event}", :_dispatch_event_push
end
end
end
# In an alias statement Ripper will emit @kw instead of @ident if the
# object being aliased is a Ruby keyword. For instance, in the line
# "alias :foo :if", the :if is treated as a lexer keyword. So we need to
# know what symbols are also keywords.
RUBY_KEYWORDS = [
"alias",
"and",
"begin",
"BEGIN",
"break",
"case",
"class",
"def",
"defined?",
"do",
"else",
"elsif",
"end",
"END",
"ensure",
"false",
"for",
"if",
"in",
"module",
"next",
"nil",
"not",
"or",
"redo",
"rescue",
"retry",
"return",
"self",
"super",
"then",
"true",
"undef",
"unless",
"until",
"when",
"while",
"yield",
"__ENCODING__",
"__FILE__",
"__LINE__"
]
private_constant :RUBY_KEYWORDS
# The source that is being parsed.
attr_reader :source
# The current line number of the parser.
attr_reader :lineno
# The current column number of the parser.
attr_reader :column
# Create a new Translation::Ripper object with the given source.
def initialize(source)
@source = source
@result = nil
@lineno = nil
@column = nil
end
##########################################################################
# Public interface
##########################################################################
# True if the parser encountered an error during parsing.
def error?
result.failure?
end
# Parse the source and return the result.
def parse
result.magic_comments.each do |magic_comment|
on_magic_comment(magic_comment.key, magic_comment.value)
end
if error?
result.errors.each do |error|
on_parse_error(error.message)
end
nil
else
result.value.accept(self)
end
end
##########################################################################
# Entrypoints for subclasses
##########################################################################
# This is a convenience method that runs the SexpBuilder subclass parser.
def self.sexp_raw(source)
SexpBuilder.new(source).parse
end
# This is a convenience method that runs the SexpBuilderPP subclass parser.
def self.sexp(source)
SexpBuilderPP.new(source).parse
end
##########################################################################
# Visitor methods
##########################################################################
# alias foo bar
# ^^^^^^^^^^^^^
def visit_alias_method_node(node)
new_name = visit_alias_method_node_value(node.new_name)
old_name = visit_alias_method_node_value(node.old_name)
bounds(node.location)
on_alias(new_name, old_name)
end
# Visit one side of an alias method node.
private def visit_alias_method_node_value(node)
if node.is_a?(SymbolNode) && node.opening_loc.nil?
visit_symbol_literal_node(node, no_symbol_wrapper: true)
else
visit(node)
end
end
# alias $foo $bar
# ^^^^^^^^^^^^^^^
def visit_alias_global_variable_node(node)
new_name = visit_alias_global_variable_node_value(node.new_name)
old_name = visit_alias_global_variable_node_value(node.old_name)
bounds(node.location)
on_var_alias(new_name, old_name)
end
# Visit one side of an alias global variable node.
private def visit_alias_global_variable_node_value(node)
bounds(node.location)
case node
when BackReferenceReadNode
on_backref(node.slice)
when GlobalVariableReadNode
on_gvar(node.name.to_s)
else
raise
end
end
# foo => bar | baz
# ^^^^^^^^^
def visit_alternation_pattern_node(node)
left = visit(node.left)
right = visit(node.right)
bounds(node.location)
on_binary(left, :|, right)
end
# a and b
# ^^^^^^^
def visit_and_node(node)
left = visit(node.left)
right = visit(node.right)
bounds(node.location)
on_binary(left, node.operator.to_sym, right)
end
# []
# ^^
def visit_array_node(node)
elements = visit_arguments(node.elements) unless node.elements.empty?
bounds(node.location)
on_array(elements)
end
# Visit a list of elements, like the elements of an array or arguments.
private def visit_arguments(elements)
bounds(elements.first.location)
elements.inject(on_args_new) do |args, element|
arg = visit(element)
bounds(element.location)
case element
when BlockArgumentNode
on_args_add_block(args, arg)
when SplatNode
on_args_add_star(args, arg)
else
on_args_add(args, arg)
end
end
end
# foo => [bar]
# ^^^^^
def visit_array_pattern_node(node)
constant = visit(node.constant)
requireds = visit_all(node.requireds) if node.requireds.any?
rest =
if !node.rest.nil?
if !node.rest.expression.nil?
visit(node.rest.expression)
else
bounds(node.rest.location)
on_var_field(nil)
end
end
posts = visit_all(node.posts) if node.posts.any?
bounds(node.location)
on_aryptn(constant, requireds, rest, posts)
end
# foo(bar)
# ^^^
def visit_arguments_node(node)
bounds(node.location)
on_args_add_block(visit_arguments(node.arguments), false)
end
# { a: 1 }
# ^^^^
def visit_assoc_node(node)
key =
if node.key.is_a?(SymbolNode) && node.operator_loc.nil?
bounds(node.key.location)
on_label(node.key.slice)
else
visit(node.key)
end
value = visit(node.value)
bounds(node.location)
on_assoc_new(key, value)
end
# def foo(**); bar(**); end
# ^^
#
# { **foo }
# ^^^^^
def visit_assoc_splat_node(node)
value = visit(node.value)
bounds(node.location)
on_assoc_splat(value)
end
# $+
# ^^
def visit_back_reference_read_node(node)
bounds(node.location)
on_backref(node.slice)
end
# begin end
# ^^^^^^^^^
def visit_begin_node(node)
clauses = visit_begin_node_clauses(node)
bounds(node.location)
on_begin(clauses)
end
# Visit the clauses of a begin node to form an on_bodystmt call.
private def visit_begin_node_clauses(node)
statements =
if node.statements.nil?
on_stmts_add(on_stmts_new, on_void_stmt)
else
body = node.statements.body
body.unshift(nil) if source.byteslice(node.begin_keyword_loc.end_offset...node.statements.body[0].location.start_offset).include?(";")
bounds(node.statements.location)
visit_statements_node_body(body)
end
rescue_clause = visit(node.rescue_clause)
else_clause = visit(node.else_clause)
ensure_clause = visit(node.ensure_clause)
bounds(node.location)
on_bodystmt(statements, rescue_clause, else_clause, ensure_clause)
end
# Visit the body of a structure that can have either a set of statements
# or statements wrapped in rescue/else/ensure.
private def visit_body_node(node, location)
case node
when nil
bounds(location)
on_bodystmt(visit_statements_node_body([nil]), nil, nil, nil)
when StatementsNode
body = visit(node)
bounds(node.location)
on_bodystmt(body, nil, nil, nil)
when BeginNode
visit_begin_node_clauses(node)
else
raise
end
end
# foo(&bar)
# ^^^^
def visit_block_argument_node(node)
visit(node.expression)
end
# foo { |; bar| }
# ^^^
def visit_block_local_variable_node(node)
bounds(node.location)
on_ident(node.name.to_s)
end
# Visit a BlockNode.
def visit_block_node(node)
braces = node.opening == "{"
parameters = visit(node.parameters)
body =
case node.body
when nil
bounds(node.location)
stmts = on_stmts_add(on_stmts_new, on_void_stmt)
bounds(node.location)
braces ? stmts : on_bodystmt(stmts, nil, nil, nil)
when StatementsNode
stmts = node.body.body
stmts.unshift(nil) if source.byteslice((node.parameters&.location || node.opening_loc).end_offset...node.body.location.start_offset).include?(";")
stmts = visit_statements_node_body(stmts)
bounds(node.body.location)
braces ? stmts : on_bodystmt(stmts, nil, nil, nil)
when BeginNode
visit_body_node(node.body, node.location)
else
raise
end
if braces
bounds(node.location)
on_brace_block(parameters, body)
else
bounds(node.location)
on_do_block(parameters, body)
end
end
# def foo(&bar); end
# ^^^^
def visit_block_parameter_node(node)
if node.name_loc.nil?
bounds(node.location)
on_blockarg(nil)
else
bounds(node.name_loc)
name = visit_token(node.name.to_s)
bounds(node.location)
on_blockarg(name)
end
end
# A block's parameters.
def visit_block_parameters_node(node)
parameters =
if node.parameters.nil?
on_params(nil, nil, nil, nil, nil, nil, nil)
else
visit(node.parameters)
end
locals =
if node.locals.any?
visit_all(node.locals)
else
visit_block_parameters_node_empty_locals
end
bounds(node.location)
on_block_var(parameters, locals)
end
if RUBY_ENGINE == "jruby"
# For JRuby, empty locals in an on_block_var is nil.
private def visit_block_parameters_node_empty_locals; nil; end
else
# For everyone else, empty locals in an on_block_var is false.
private def visit_block_parameters_node_empty_locals; false; end
end
# break
# ^^^^^
#
# break foo
# ^^^^^^^^^
def visit_break_node(node)
if node.arguments.nil?
bounds(node.location)
on_break(on_args_new)
else
arguments = visit_arguments(node.arguments.arguments)
bounds(node.location)
on_break(on_args_add_block(arguments, false))
end
end
# foo
# ^^^
#
# foo.bar
# ^^^^^^^
#
# foo.bar() {}
# ^^^^^^^^^^^^
def visit_call_node(node)
if node.call_operator_loc.nil?
case node.name
when :[]
receiver = visit(node.receiver)
arguments, block = visit_call_node_arguments(node.arguments, node.block)
bounds(node.location)
call = on_aref(receiver, arguments)
if block.nil?
call
else
bounds(node.location)
on_method_add_block(call, block)
end
when :[]=
receiver = visit(node.receiver)
*arguments, last_argument = node.arguments.arguments
arguments << node.block if !node.block.nil?
arguments =
if arguments.any?
args = visit_arguments(arguments)
if !node.block.nil?
args
else
bounds(arguments.first.location)
on_args_add_block(args, false)
end
end
bounds(node.location)
call = on_aref_field(receiver, arguments)
value = visit(last_argument)
bounds(last_argument.location)
on_assign(call, value)
when :-@, :+@, :~@
receiver = visit(node.receiver)
bounds(node.location)
on_unary(node.name, receiver)
when :!@
if node.message == "not"
receiver =
case node.receiver
when nil
nil
when ParenthesesNode
body = visit(node.receiver.body&.body&.first) || false
bounds(node.receiver.location)
on_paren(body)
else
visit(node.receiver)
end
bounds(node.location)
on_unary(:not, receiver)
else
receiver = visit(node.receiver)
bounds(node.location)
on_unary(:!@, receiver)
end
when :!=, :!~, :=~, :==, :===, :<=>, :>, :>=, :<, :<=, :&, :|, :^, :>>, :<<, :-, :+, :%, :/, :*, :**
receiver = visit(node.receiver)
value = visit(node.arguments.arguments.first)
bounds(node.location)
on_binary(receiver, node.name, value)
else
bounds(node.message_loc)
message = on_ident(node.message)
if node.variable_call?
on_vcall(message)
else
arguments, block = visit_call_node_arguments(node.arguments, node.block)
call =
if node.opening_loc.nil? && (arguments&.any? || block.nil?)
bounds(node.location)
on_command(message, arguments)
elsif !node.opening_loc.nil?
bounds(node.location)
on_method_add_arg(on_fcall(message), on_arg_paren(arguments))
else
bounds(node.location)
on_method_add_arg(on_fcall(message), on_args_new)
end
if block.nil?
call
else
bounds(node.block.location)
on_method_add_block(call, block)
end
end
end
else
receiver = visit(node.receiver)
bounds(node.call_operator_loc)
call_operator = visit_token(node.call_operator)
bounds(node.message_loc)
message = visit_token(node.message)
if node.name.end_with?("=") && !node.message.end_with?("=") && !node.arguments.nil? && node.block.nil?
bounds(node.arguments.location)
value = visit(node.arguments.arguments.first)
bounds(node.location)
on_assign(on_field(receiver, call_operator, message), value)
else
arguments, block = visit_call_node_arguments(node.arguments, node.block)
call =
if node.opening_loc.nil?
bounds(node.location)
if !arguments || arguments.empty?
on_call(receiver, call_operator, message)
else
on_command_call(receiver, call_operator, message, arguments)
end
else
bounds(node.opening_loc)
arguments = on_arg_paren(arguments)
bounds(node.location)
on_method_add_arg(on_call(receiver, call_operator, message), arguments)
end
if block.nil?
call
else
bounds(node.block.location)
on_method_add_block(call, block)
end
end
end
end
# Visit the arguments and block of a call node and return the arguments
# and block as they should be used.
private def visit_call_node_arguments(arguments_node, block_node)
arguments = arguments_node&.arguments || []
block = block_node
if block.is_a?(BlockArgumentNode)
arguments << block
block = nil
end
arguments =
if arguments.length == 1 && arguments.first.is_a?(ForwardingArgumentsNode)
visit(arguments.first)
elsif arguments.any?
args = visit_arguments(arguments)
if block.is_a?(BlockArgumentNode) || arguments.last.is_a?(ForwardingArgumentsNode)
args
else
bounds(arguments.first.location)
on_args_add_block(args, false)
end
end
block = visit(block) if !block.nil?
[arguments, block]
end
# foo.bar += baz
# ^^^^^^^^^^^^^^^
def visit_call_operator_write_node(node)
receiver = visit(node.receiver)
bounds(node.call_operator_loc)
call_operator = visit_token(node.call_operator)
bounds(node.message_loc)
message = visit_token(node.message)
bounds(node.location)
target = on_field(receiver, call_operator, message)
bounds(node.operator_loc)
operator = on_op("#{node.operator}=")
value = visit(node.value)
on_opassign(target, operator, value)
end
# foo.bar &&= baz
# ^^^^^^^^^^^^^^^
def visit_call_and_write_node(node)
receiver = visit(node.receiver)
bounds(node.call_operator_loc)
call_operator = visit_token(node.call_operator)
bounds(node.message_loc)
message = visit_token(node.message)
bounds(node.location)
target = on_field(receiver, call_operator, message)
bounds(node.operator_loc)
operator = on_op("&&=")
value = visit(node.value)
on_opassign(target, operator, value)
end
# foo.bar ||= baz
# ^^^^^^^^^^^^^^^
def visit_call_or_write_node(node)
receiver = visit(node.receiver)
bounds(node.call_operator_loc)
call_operator = visit_token(node.call_operator)
bounds(node.message_loc)
message = visit_token(node.message)
bounds(node.location)
target = on_field(receiver, call_operator, message)
bounds(node.operator_loc)
operator = on_op("||=")
value = visit(node.value)
on_opassign(target, operator, value)
end
# foo.bar, = 1
# ^^^^^^^
def visit_call_target_node(node)
receiver = visit(node.receiver)
bounds(node.call_operator_loc)
call_operator = visit_token(node.call_operator)
bounds(node.message_loc)
message = visit_token(node.message)
bounds(node.location)
on_field(receiver, call_operator, message)
end
# foo => bar => baz
# ^^^^^^^^^^
def visit_capture_pattern_node(node)
value = visit(node.value)
target = visit(node.target)
bounds(node.location)
on_binary(value, :"=>", target)
end
# case foo; when bar; end
# ^^^^^^^^^^^^^^^^^^^^^^^
def visit_case_node(node)
predicate = visit(node.predicate)
clauses =
node.conditions.reverse_each.inject(nil) do |consequent, condition|
on_when(*visit(condition), consequent)
end
bounds(node.location)
on_case(predicate, clauses)
end
# case foo; in bar; end
# ^^^^^^^^^^^^^^^^^^^^^
def visit_case_match_node(node)
predicate = visit(node.predicate)
clauses =
node.conditions.reverse_each.inject(nil) do |consequent, condition|
on_in(*visit(condition), consequent)
end
bounds(node.location)
on_case(predicate, clauses)
end
# class Foo; end
# ^^^^^^^^^^^^^^
def visit_class_node(node)
constant_path =
if node.constant_path.is_a?(ConstantReadNode)
bounds(node.constant_path.location)
on_const_ref(on_const(node.constant_path.name.to_s))
else
visit(node.constant_path)
end
superclass = visit(node.superclass)
bodystmt = visit_body_node(node.body, node.location)
bounds(node.location)
on_class(constant_path, superclass, bodystmt)
end
# @@foo
# ^^^^^
def visit_class_variable_read_node(node)
bounds(node.location)
on_var_ref(on_cvar(node.slice))
end
# @@foo = 1
# ^^^^^^^^^
#
# @@foo, @@bar = 1
# ^^^^^ ^^^^^
def visit_class_variable_write_node(node)
bounds(node.name_loc)
target = on_var_field(on_cvar(node.name.to_s))
value = visit(node.value)
bounds(node.location)
on_assign(target, value)
end
# @@foo += bar
# ^^^^^^^^^^^^
def visit_class_variable_operator_write_node(node)
bounds(node.name_loc)
target = on_var_field(on_cvar(node.name.to_s))
bounds(node.operator_loc)
operator = on_op("#{node.operator}=")
value = visit(node.value)
bounds(node.location)
on_opassign(target, operator, value)
end
# @@foo &&= bar
# ^^^^^^^^^^^^^
def visit_class_variable_and_write_node(node)
bounds(node.name_loc)
target = on_var_field(on_cvar(node.name.to_s))
bounds(node.operator_loc)
operator = on_op("&&=")
value = visit(node.value)
bounds(node.location)
on_opassign(target, operator, value)
end
# @@foo ||= bar
# ^^^^^^^^^^^^^
def visit_class_variable_or_write_node(node)
bounds(node.name_loc)
target = on_var_field(on_cvar(node.name.to_s))
bounds(node.operator_loc)
operator = on_op("||=")
value = visit(node.value)
bounds(node.location)
on_opassign(target, operator, value)
end
# @@foo, = bar
# ^^^^^
def visit_class_variable_target_node(node)
bounds(node.location)
on_var_field(on_cvar(node.name.to_s))
end
# Foo
# ^^^
def visit_constant_read_node(node)
bounds(node.location)
on_var_ref(on_const(node.name.to_s))
end
# Foo = 1
# ^^^^^^^
#
# Foo, Bar = 1
# ^^^ ^^^
def visit_constant_write_node(node)
bounds(node.name_loc)
target = on_var_field(on_const(node.name.to_s))
value = visit(node.value)
bounds(node.location)
on_assign(target, value)
end
# Foo += bar
# ^^^^^^^^^^^
def visit_constant_operator_write_node(node)
bounds(node.name_loc)
target = on_var_field(on_const(node.name.to_s))
bounds(node.operator_loc)
operator = on_op("#{node.operator}=")
value = visit(node.value)
bounds(node.location)
on_opassign(target, operator, value)
end
# Foo &&= bar
# ^^^^^^^^^^^^
def visit_constant_and_write_node(node)
bounds(node.name_loc)
target = on_var_field(on_const(node.name.to_s))
bounds(node.operator_loc)
operator = on_op("&&=")
value = visit(node.value)
bounds(node.location)
on_opassign(target, operator, value)
end
# Foo ||= bar
# ^^^^^^^^^^^^
def visit_constant_or_write_node(node)
bounds(node.name_loc)
target = on_var_field(on_const(node.name.to_s))
bounds(node.operator_loc)
operator = on_op("||=")
value = visit(node.value)
bounds(node.location)
on_opassign(target, operator, value)
end
# Foo, = bar
# ^^^
def visit_constant_target_node(node)
bounds(node.location)
on_var_field(on_const(node.name.to_s))
end
# Foo::Bar
# ^^^^^^^^
def visit_constant_path_node(node)
if node.parent.nil?
bounds(node.child.location)
child = on_const(node.child.name.to_s)
bounds(node.location)
on_top_const_ref(child)
else
parent = visit(node.parent)
bounds(node.child.location)
child = on_const(node.child.name.to_s)
bounds(node.location)
on_const_path_ref(parent, child)
end
end
# Foo::Bar = 1
# ^^^^^^^^^^^^
#
# Foo::Foo, Bar::Bar = 1
# ^^^^^^^^ ^^^^^^^^
def visit_constant_path_write_node(node)
target = visit_constant_path_write_node_target(node.target)
value = visit(node.value)
bounds(node.location)
on_assign(target, value)
end
# Visit a constant path that is part of a write node.
private def visit_constant_path_write_node_target(node)
if node.parent.nil?
bounds(node.child.location)
child = on_const(node.child.name.to_s)
bounds(node.location)
on_top_const_field(child)
else
parent = visit(node.parent)
bounds(node.child.location)
child = on_const(node.child.name.to_s)
bounds(node.location)
on_const_path_field(parent, child)
end
end
# Foo::Bar += baz
# ^^^^^^^^^^^^^^^
def visit_constant_path_operator_write_node(node)
target = visit_constant_path_write_node_target(node.target)
value = visit(node.value)
bounds(node.operator_loc)
operator = on_op("#{node.operator}=")