-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Expand file tree
/
Copy pathripper.rb
More file actions
3805 lines (3234 loc) · 104 KB
/
ripper.rb
File metadata and controls
3805 lines (3234 loc) · 104 KB
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
# :markup: markdown
module Prism
module Translation
# This class provides 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. To use this class, you
# treat `Prism::Translation::Ripper` effectively as you would treat the
# `Ripper` class.
#
# Note that this class will serve the most common use cases, but Ripper's
# API is extensive and undocumented. It relies on reporting the state of the
# parser at any given time. We do our best to replicate that here, but
# because it is a different architecture it is not possible to perfectly
# replicate the behavior of Ripper.
#
# The main known difference is that we may omit dispatching some events in
# some cases. This impacts the following events:
#
# - on_assign_error
# - on_comma
# - on_ignored_nl
# - on_ignored_sp
# - on_kw
# - on_label_end
# - on_lbrace
# - on_lbracket
# - on_lparen
# - on_nl
# - on_op
# - on_operator_ambiguous
# - on_rbrace
# - on_rbracket
# - on_rparen
# - on_semicolon
# - on_sp
#
class Ripper < Compiler
# Parses the given Ruby program read from +src+.
# +src+ must be a String or an IO or a object with a #gets method.
def self.parse(src, filename = "(ripper)", lineno = 1)
new(src, filename, lineno).parse
end
# Tokenizes the Ruby program and returns an array of an array,
# which is formatted like
# <code>[[lineno, column], type, token, state]</code>.
# The +filename+ argument is mostly ignored.
# By default, this method does not handle syntax errors in +src+,
# use the +raise_errors+ keyword to raise a SyntaxError for an error in +src+.
#
# require "ripper"
# require "pp"
#
# pp Ripper.lex("def m(a) nil end")
# #=> [[[1, 0], :on_kw, "def", FNAME ],
# [[1, 3], :on_sp, " ", FNAME ],
# [[1, 4], :on_ident, "m", ENDFN ],
# [[1, 5], :on_lparen, "(", BEG|LABEL],
# [[1, 6], :on_ident, "a", ARG ],
# [[1, 7], :on_rparen, ")", ENDFN ],
# [[1, 8], :on_sp, " ", BEG ],
# [[1, 9], :on_kw, "nil", END ],
# [[1, 12], :on_sp, " ", END ],
# [[1, 13], :on_kw, "end", END ]]
#
def self.lex(src, filename = "-", lineno = 1, raise_errors: false)
result = Prism.lex_compat(coerce_source(src), filepath: filename, line: lineno, version: "current")
if result.failure? && raise_errors
raise SyntaxError, result.errors.first.message
else
result.value
end
end
# Tokenizes the Ruby program and returns an array of strings.
# The +filename+ and +lineno+ arguments are mostly ignored, since the
# return value is just the tokenized input.
# By default, this method does not handle syntax errors in +src+,
# use the +raise_errors+ keyword to raise a SyntaxError for an error in +src+.
#
# p Ripper.tokenize("def m(a) nil end")
# # => ["def", " ", "m", "(", "a", ")", " ", "nil", " ", "end"]
#
def self.tokenize(...)
lex(...).map { |token| token[2] }
end
# Mirros the various lex_types that ripper supports
def self.coerce_source(source) # :nodoc:
if source.is_a?(IO)
source.read
elsif source.respond_to?(:gets)
src = +""
while line = source.gets
src << line
end
src
else
source.to_str
end
end
# This contains a table of all of the parser events and their
# corresponding arity.
PARSER_EVENT_TABLE = {
BEGIN: 1,
END: 1,
alias: 2,
alias_error: 2,
aref: 2,
aref_field: 2,
arg_ambiguous: 1,
arg_paren: 1,
args_add: 2,
args_add_block: 2,
args_add_star: 2,
args_forward: 0,
args_new: 0,
array: 1,
aryptn: 4,
assign: 2,
assign_error: 2,
assoc_new: 2,
assoc_splat: 1,
assoclist_from_args: 1,
bare_assoc_hash: 1,
begin: 1,
binary: 3,
block_var: 2,
blockarg: 1,
bodystmt: 4,
brace_block: 2,
break: 1,
call: 3,
case: 2,
class: 3,
class_name_error: 2,
command: 2,
command_call: 4,
const_path_field: 2,
const_path_ref: 2,
const_ref: 1,
def: 3,
defined: 1,
defs: 5,
do_block: 2,
dot2: 2,
dot3: 2,
dyna_symbol: 1,
else: 1,
elsif: 3,
ensure: 1,
excessed_comma: 0,
fcall: 1,
field: 3,
fndptn: 4,
for: 3,
hash: 1,
heredoc_dedent: 2,
hshptn: 3,
if: 3,
if_mod: 2,
ifop: 3,
in: 3,
kwrest_param: 1,
lambda: 2,
magic_comment: 2,
massign: 2,
method_add_arg: 2,
method_add_block: 2,
mlhs_add: 2,
mlhs_add_post: 2,
mlhs_add_star: 2,
mlhs_new: 0,
mlhs_paren: 1,
module: 2,
mrhs_add: 2,
mrhs_add_star: 2,
mrhs_new: 0,
mrhs_new_from_args: 1,
next: 1,
nokw_param: 1,
opassign: 3,
operator_ambiguous: 2,
param_error: 2,
params: 7,
paren: 1,
parse_error: 1,
program: 1,
qsymbols_add: 2,
qsymbols_new: 0,
qwords_add: 2,
qwords_new: 0,
redo: 0,
regexp_add: 2,
regexp_literal: 2,
regexp_new: 0,
rescue: 4,
rescue_mod: 2,
rest_param: 1,
retry: 0,
return: 1,
return0: 0,
sclass: 2,
stmts_add: 2,
stmts_new: 0,
string_add: 2,
string_concat: 2,
string_content: 0,
string_dvar: 1,
string_embexpr: 1,
string_literal: 1,
super: 1,
symbol: 1,
symbol_literal: 1,
symbols_add: 2,
symbols_new: 0,
top_const_field: 1,
top_const_ref: 1,
unary: 2,
undef: 1,
unless: 3,
unless_mod: 2,
until: 2,
until_mod: 2,
var_alias: 2,
var_field: 1,
var_ref: 1,
vcall: 1,
void_stmt: 0,
when: 3,
while: 2,
while_mod: 2,
word_add: 2,
word_new: 0,
words_add: 2,
words_new: 0,
xstring_add: 2,
xstring_literal: 1,
xstring_new: 0,
yield: 1,
yield0: 0,
zsuper: 0
}
# This contains a table of all of the scanner events and their
# corresponding arity.
SCANNER_EVENT_TABLE = {
CHAR: 1,
__end__: 1,
backref: 1,
backtick: 1,
comma: 1,
comment: 1,
const: 1,
cvar: 1,
embdoc: 1,
embdoc_beg: 1,
embdoc_end: 1,
embexpr_beg: 1,
embexpr_end: 1,
embvar: 1,
float: 1,
gvar: 1,
heredoc_beg: 1,
heredoc_end: 1,
ident: 1,
ignored_nl: 1,
imaginary: 1,
int: 1,
ivar: 1,
kw: 1,
label: 1,
label_end: 1,
lbrace: 1,
lbracket: 1,
lparen: 1,
nl: 1,
op: 1,
period: 1,
qsymbols_beg: 1,
qwords_beg: 1,
rational: 1,
rbrace: 1,
rbracket: 1,
regexp_beg: 1,
regexp_end: 1,
rparen: 1,
semicolon: 1,
sp: 1,
symbeg: 1,
symbols_beg: 1,
tlambda: 1,
tlambeg: 1,
tstring_beg: 1,
tstring_content: 1,
tstring_end: 1,
words_beg: 1,
words_sep: 1,
ignored_sp: 1
}
# This array contains name of parser events.
PARSER_EVENTS = PARSER_EVENT_TABLE.keys
# This array contains name of scanner events.
SCANNER_EVENTS = SCANNER_EVENT_TABLE.keys
# This array contains name of all ripper events.
EVENTS = PARSER_EVENTS + SCANNER_EVENTS
# A list of all of the Ruby keywords.
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__"
]
# A list of all of the Ruby binary operators.
BINARY_OPERATORS = [
:!=,
:!~,
:=~,
:==,
:===,
:<=>,
:>,
:>=,
:<,
:<=,
:&,
:|,
:^,
:>>,
:<<,
:-,
:+,
:%,
:/,
:*,
:**
]
private_constant :KEYWORDS, :BINARY_OPERATORS
# Parses +src+ and create S-exp tree.
# Returns more readable tree rather than Ripper.sexp_raw.
# This method is mainly for developer use.
# The +filename+ argument is mostly ignored.
# By default, this method does not handle syntax errors in +src+,
# returning +nil+ in such cases. Use the +raise_errors+ keyword
# to raise a SyntaxError for an error in +src+.
#
# require "ripper"
# require "pp"
#
# pp Ripper.sexp("def m(a) nil end")
# #=> [:program,
# [[:def,
# [:@ident, "m", [1, 4]],
# [:paren, [:params, [[:@ident, "a", [1, 6]]], nil, nil, nil, nil, nil, nil]],
# [:bodystmt, [[:var_ref, [:@kw, "nil", [1, 9]]]], nil, nil, nil]]]]
#
def self.sexp(src, filename = "-", lineno = 1, raise_errors: false)
builder = SexpBuilderPP.new(src, filename, lineno)
sexp = builder.parse
if builder.error?
if raise_errors
raise SyntaxError, builder.error
end
else
sexp
end
end
# Parses +src+ and create S-exp tree.
# This method is mainly for developer use.
# The +filename+ argument is mostly ignored.
# By default, this method does not handle syntax errors in +src+,
# returning +nil+ in such cases. Use the +raise_errors+ keyword
# to raise a SyntaxError for an error in +src+.
#
# require "ripper"
# require "pp"
#
# pp Ripper.sexp_raw("def m(a) nil end")
# #=> [:program,
# [:stmts_add,
# [:stmts_new],
# [:def,
# [:@ident, "m", [1, 4]],
# [:paren, [:params, [[:@ident, "a", [1, 6]]], nil, nil, nil]],
# [:bodystmt,
# [:stmts_add, [:stmts_new], [:var_ref, [:@kw, "nil", [1, 9]]]],
# nil,
# nil,
# nil]]]]
#
def self.sexp_raw(src, filename = "-", lineno = 1, raise_errors: false)
builder = SexpBuilder.new(src, filename, lineno)
sexp = builder.parse
if builder.error?
if raise_errors
raise SyntaxError, builder.error
end
else
sexp
end
end
autoload :Filter, "prism/translation/ripper/filter"
autoload :Lexer, "prism/translation/ripper/lexer"
autoload :SexpBuilder, "prism/translation/ripper/sexp"
autoload :SexpBuilderPP, "prism/translation/ripper/sexp"
# :stopdoc:
# This is not part of the public API but used by some gems.
# Ripper-internal bitflags.
LEX_STATE_NAMES = %i[
BEG END ENDARG ENDFN ARG CMDARG MID FNAME DOT CLASS LABEL LABELED FITEM
].map.with_index.to_h { |name, i| [2 ** i, name] }.freeze
private_constant :LEX_STATE_NAMES
LEX_STATE_NAMES.each do |value, key|
const_set("EXPR_#{key}", value)
end
EXPR_NONE = 0
EXPR_VALUE = EXPR_BEG
EXPR_BEG_ANY = EXPR_BEG | EXPR_MID | EXPR_CLASS
EXPR_ARG_ANY = EXPR_ARG | EXPR_CMDARG
EXPR_END_ANY = EXPR_END | EXPR_ENDARG | EXPR_ENDFN
def self.lex_state_name(state)
LEX_STATE_NAMES.filter_map { |flag, name| name if state & flag != 0 }.join("|")
end
# :startdoc:
# The source that is being parsed.
attr_reader :source
# The filename of the source being parsed.
attr_reader :filename
# The current line number of the parser.
attr_reader :lineno
# The current column in bytes of the parser.
attr_reader :column
# Create a new Translation::Ripper object with the given source.
def initialize(source, filename = "(ripper)", lineno = 1)
@source = Ripper.coerce_source(source)
@filename = filename
@lineno = lineno
@column = 0
@result = 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.comments.each do |comment|
location = comment.location
bounds(location)
if comment.is_a?(InlineComment)
# Inline comments always contain a newline if the line itself contains it
if result.source.source.bytesize > comment.location.end_offset
on_comment("#{comment.slice}\n")
else
on_comment(comment.slice)
end
else
offset = location.start_offset
lines = comment.slice.lines
lines.each_with_index do |line, index|
bounds(location.copy(start_offset: offset))
if index == 0
on_embdoc_beg(line)
elsif index == lines.size - 1
on_embdoc_end(line)
else
on_embdoc(line)
end
offset += line.bytesize
end
end
end
result.magic_comments.each do |magic_comment|
on_magic_comment(magic_comment.key, magic_comment.value)
end
unless result.data_loc.nil?
on___end__(result.data_loc.slice.each_line.first)
end
result.warnings.each do |warning|
bounds(warning.location)
if warning.level == :default
warning(warning.message)
else
case warning.type
when :ambiguous_first_argument_plus
on_arg_ambiguous("+")
when :ambiguous_first_argument_minus
on_arg_ambiguous("-")
when :ambiguous_slash
on_arg_ambiguous("/")
else
warn(warning.message)
end
end
end
if error?
result.errors.each do |error|
location = error.location
bounds(location)
case error.type
when :alias_argument
on_alias_error("can't make alias for the number variables", location.slice)
when :argument_formal_class
on_param_error("formal argument cannot be a class variable", location.slice)
when :argument_format_constant
on_param_error("formal argument cannot be a constant", location.slice)
when :argument_formal_global
on_param_error("formal argument cannot be a global variable", location.slice)
when :argument_formal_ivar
on_param_error("formal argument cannot be an instance variable", location.slice)
when :class_name, :module_name
on_class_name_error("class/module name must be CONSTANT", location.slice)
else
on_parse_error(error.message)
end
end
nil
else
result.value.accept(self)
end
end
##########################################################################
# Visitor methods
##########################################################################
# :stopdoc:
# alias foo bar
# ^^^^^^^^^^^^^
def visit_alias_method_node(node)
bounds(node.keyword_loc)
on_kw("alias")
new_name = visit(node.new_name)
old_name = visit(node.old_name)
bounds(node.location)
on_alias(new_name, old_name)
end
# alias $foo $bar
# ^^^^^^^^^^^^^^^
def visit_alias_global_variable_node(node)
bounds(node.keyword_loc)
on_kw("alias")
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_pattern_node(node.left)
right = visit_pattern_node(node.right)
bounds(node.location)
on_binary(left, :|, right)
end
# Visit a pattern within a pattern match. This is used to bypass the
# parenthesis node that can be used to wrap patterns.
private def visit_pattern_node(node)
if node.is_a?(ParenthesesNode)
visit(node.body)
else
visit(node)
end
end
# a and b
# ^^^^^^^
def visit_and_node(node)
left = visit(node.left)
if node.operator == "and"
bounds(node.operator_loc)
on_kw("and")
end
right = visit(node.right)
bounds(node.location)
on_binary(left, node.operator.to_sym, right)
end
# []
# ^^
def visit_array_node(node)
case (opening = node.opening)
when /^%w/
opening_loc = node.opening_loc
bounds(opening_loc)
on_qwords_beg(opening)
elements = on_qwords_new
previous = nil
node.elements.each do |element|
visit_words_sep(opening_loc, previous, element)
bounds(element.location)
elements = on_qwords_add(elements, on_tstring_content(element.content))
previous = element
end
bounds(node.closing_loc)
on_tstring_end(node.closing)
when /^%i/
opening_loc = node.opening_loc
bounds(opening_loc)
on_qsymbols_beg(opening)
elements = on_qsymbols_new
previous = nil
node.elements.each do |element|
visit_words_sep(opening_loc, previous, element)
bounds(element.location)
elements = on_qsymbols_add(elements, on_tstring_content(element.value))
previous = element
end
bounds(node.closing_loc)
on_tstring_end(node.closing)
when /^%W/
opening_loc = node.opening_loc
bounds(opening_loc)
on_words_beg(opening)
elements = on_words_new
previous = nil
node.elements.each do |element|
visit_words_sep(opening_loc, previous, element)
bounds(element.location)
elements =
on_words_add(
elements,
if element.is_a?(StringNode)
on_word_add(on_word_new, on_tstring_content(element.content))
else
element.parts.inject(on_word_new) do |word, part|
word_part =
if part.is_a?(StringNode)
bounds(part.location)
on_tstring_content(part.content)
else
visit(part)
end
on_word_add(word, word_part)
end
end
)
previous = element
end
bounds(node.closing_loc)
on_tstring_end(node.closing)
when /^%I/
opening_loc = node.opening_loc
bounds(opening_loc)
on_symbols_beg(opening)
elements = on_symbols_new
previous = nil
node.elements.each do |element|
visit_words_sep(opening_loc, previous, element)
bounds(element.location)
elements =
on_symbols_add(
elements,
if element.is_a?(SymbolNode)
on_word_add(on_word_new, on_tstring_content(element.value))
else
element.parts.inject(on_word_new) do |word, part|
word_part =
if part.is_a?(StringNode)
bounds(part.location)
on_tstring_content(part.content)
else
visit(part)
end
on_word_add(word, word_part)
end
end
)
previous = element
end
bounds(node.closing_loc)
on_tstring_end(node.closing)
else
bounds(node.opening_loc)
on_lbracket(opening)
elements = visit_arguments(node.elements) unless node.elements.empty?
bounds(node.closing_loc)
on_rbracket(node.closing)
end
bounds(node.location)
on_array(elements)
end
# Dispatch a words_sep event that contains the space between the elements
# of list literals.
private def visit_words_sep(opening_loc, previous, current)
end_offset = (previous.nil? ? opening_loc : previous.location).end_offset
start_offset = current.location.start_offset
if end_offset != start_offset
bounds(current.location.copy(start_offset: end_offset))
on_words_sep(source.byteslice(end_offset...start_offset))
end
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 (rest_node = node.rest).is_a?(SplatNode)
if rest_node.expression.nil?
bounds(rest_node.location)
on_var_field(nil)
else
visit(rest_node.expression)
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)
arguments, _, _ = visit_call_node_arguments(node, nil, false)
arguments
end
# { a: 1 }
# ^^^^
def visit_assoc_node(node)
key = visit(node.key)
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)
if node.begin_keyword_loc
bounds(node.begin_keyword_loc)
on_kw("begin")
end
clauses = visit_begin_node_clauses(node.begin_keyword_loc, node, false)
if node.end_keyword_loc
bounds(node.end_keyword_loc)
on_kw("end")
end
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(location, node, allow_newline)
statements =
if node.statements.nil?
on_stmts_add(on_stmts_new, on_void_stmt)
else
body = node.statements.body
body.unshift(nil) if void_stmt?(location, node.statements.body[0].location, allow_newline)
bounds(node.statements.location)
visit_statements_node_body(body)
end
rescue_clause = visit(node.rescue_clause)
else_clause =
unless (else_clause_node = node.else_clause).nil?
bounds(else_clause_node.else_keyword_loc)
on_kw("else")
else_statements =
if else_clause_node.statements.nil?
[nil]
else
body = else_clause_node.statements.body
body.unshift(nil) if void_stmt?(else_clause_node.else_keyword_loc, else_clause_node.statements.body[0].location, allow_newline)
body
end
bounds(else_clause_node.location)
visit_statements_node_body(else_statements)
end
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(location, node, allow_newline = false)
case node
when nil
bounds(location)
on_bodystmt(visit_statements_node_body([nil]), nil, nil, nil)
when StatementsNode
body = [*node.body]
body.unshift(nil) if void_stmt?(location, body[0].location, allow_newline)
stmts = visit_statements_node_body(body)
bounds(node.body.first.location)
on_bodystmt(stmts, nil, nil, nil)
when BeginNode
visit_begin_node_clauses(location, node, allow_newline)
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)
unless braces
bounds(node.opening_loc)
on_kw("do")
end
body =
case node.body
when nil
bounds(node.location)