-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
index.rb
683 lines (577 loc) · 19.9 KB
/
index.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
# frozen_string_literal: true
module SyntaxTree
# This class can be used to build an index of the structure of Ruby files. We
# define an index as the list of constants and methods defined within a file.
#
# This index strives to be as fast as possible to better support tools like
# IDEs. Because of that, it has different backends depending on what
# functionality is available.
module Index
# This is a location for an index entry.
class Location
attr_reader :line, :column
def initialize(line, column)
@line = line
@column = column
end
end
# This entry represents a class definition using the class keyword.
class ClassDefinition
attr_reader :nesting, :name, :superclass, :location, :comments
def initialize(nesting, name, superclass, location, comments)
@nesting = nesting
@name = name
@superclass = superclass
@location = location
@comments = comments
end
end
# This entry represents a constant assignment.
class ConstantDefinition
attr_reader :nesting, :name, :location, :comments
def initialize(nesting, name, location, comments)
@nesting = nesting
@name = name
@location = location
@comments = comments
end
end
# This entry represents a module definition using the module keyword.
class ModuleDefinition
attr_reader :nesting, :name, :location, :comments
def initialize(nesting, name, location, comments)
@nesting = nesting
@name = name
@location = location
@comments = comments
end
end
# This entry represents a method definition using the def keyword.
class MethodDefinition
attr_reader :nesting, :name, :location, :comments
def initialize(nesting, name, location, comments)
@nesting = nesting
@name = name
@location = location
@comments = comments
end
end
# This entry represents a singleton method definition using the def keyword
# with a specified target.
class SingletonMethodDefinition
attr_reader :nesting, :name, :location, :comments
def initialize(nesting, name, location, comments)
@nesting = nesting
@name = name
@location = location
@comments = comments
end
end
# This entry represents a method definition that was created using the alias
# keyword.
class AliasMethodDefinition
attr_reader :nesting, :name, :location, :comments
def initialize(nesting, name, location, comments)
@nesting = nesting
@name = name
@location = location
@comments = comments
end
end
# When you're using the instruction sequence backend, this class is used to
# lazily parse comments out of the source code.
class FileComments
# We use the ripper library to pull out source comments.
class Parser < Ripper
attr_reader :comments
def initialize(*)
super
@comments = {}
end
def on_comment(value)
comments[lineno] = value.chomp
end
end
# This represents the Ruby source in the form of a file. When it needs to
# be read we'll read the file.
class FileSource
attr_reader :filepath
def initialize(filepath)
@filepath = filepath
end
def source
File.read(filepath)
end
end
# This represents the Ruby source in the form of a string. When it needs
# to be read the string is returned.
class StringSource
attr_reader :source
def initialize(source)
@source = source
end
end
attr_reader :source
def initialize(source)
@source = source
end
def comments
@comments ||= Parser.new(source.source).tap(&:parse).comments
end
end
# This class handles parsing comments from Ruby source code in the case that
# we use the instruction sequence backend. Because the instruction sequence
# backend doesn't provide comments (since they are dropped) we provide this
# interface to lazily parse them out.
class EntryComments
include Enumerable
attr_reader :file_comments, :location
def initialize(file_comments, location)
@file_comments = file_comments
@location = location
end
def each(&block)
line = location.line - 1
result = []
while line >= 0 && (comment = file_comments.comments[line])
result.unshift(comment)
line -= 1
end
result.each(&block)
end
end
# This backend creates the index using RubyVM::InstructionSequence, which is
# faster than using the Syntax Tree parser, but is not available on all
# runtimes.
class ISeqBackend
VM_DEFINECLASS_TYPE_CLASS = 0x00
VM_DEFINECLASS_TYPE_SINGLETON_CLASS = 0x01
VM_DEFINECLASS_TYPE_MODULE = 0x02
VM_DEFINECLASS_FLAG_SCOPED = 0x08
VM_DEFINECLASS_FLAG_HAS_SUPERCLASS = 0x10
def index(source)
index_iseq(
RubyVM::InstructionSequence.compile(source).to_a,
FileComments.new(FileComments::StringSource.new(source))
)
end
def index_file(filepath)
index_iseq(
RubyVM::InstructionSequence.compile_file(filepath).to_a,
FileComments.new(FileComments::FileSource.new(filepath))
)
end
private
def location_for(iseq)
code_location = iseq[4][:code_location]
Location.new(code_location[0], code_location[1])
end
def find_constant_path(insns, index)
index -= 1 while index >= 0 &&
(
insns[index].is_a?(Integer) ||
(
insns[index].is_a?(Array) &&
%i[swap topn].include?(insns[index][0])
)
)
insn = insns[index]
if insn.is_a?(Array) && insn[0] == :opt_getconstant_path
# In this case we're on Ruby 3.2+ and we have an opt_getconstant_path
# instruction, so we already know all of the symbols in the nesting.
[index - 1, insn[1]]
elsif insn.is_a?(Symbol) && insn.match?(/\Alabel_\d+/)
# Otherwise, if we have a label then this is very likely the
# destination of an opt_getinlinecache instruction, in which case
# we'll walk backwards to grab up all of the constants.
names = []
index -= 1
until insns[index].is_a?(Array) &&
insns[index][0] == :opt_getinlinecache
if insns[index].is_a?(Array) && insns[index][0] == :getconstant
names.unshift(insns[index][1])
end
index -= 1
end
[index - 1, names]
else
[index, []]
end
end
def find_attr_arguments(insns, index)
orig_argc = insns[index][1][:orig_argc]
names = []
current = index - 1
while current >= 0 && names.length < orig_argc
if insns[current].is_a?(Array) && insns[current][0] == :putobject
names.unshift(insns[current][1])
end
current -= 1
end
names if insns[current] == [:putself] && names.length == orig_argc
end
def method_definition(nesting, name, location, file_comments)
comments = EntryComments.new(file_comments, location)
if nesting.last == [:singletonclass]
SingletonMethodDefinition.new(
nesting[0...-1],
name,
location,
comments
)
else
MethodDefinition.new(nesting, name, location, comments)
end
end
def index_iseq(iseq, file_comments)
results = []
queue = [[iseq, []]]
while (current_iseq, current_nesting = queue.shift)
file = current_iseq[5]
line = current_iseq[8]
insns = current_iseq[13]
insns.each_with_index do |insn, index|
case insn
when Integer
line = insn
next
when Array
# continue on
else
# skip everything else
next
end
case insn[0]
when :defineclass
_, name, class_iseq, flags = insn
next_nesting = current_nesting.dup
# This is the index we're going to search for the nested constant
# path within the declaration name.
constant_index = index - 2
# This is the superclass of the class being defined.
superclass = []
# If there is a superclass, then we're going to find it here and
# then update the constant_index as necessary.
if flags & VM_DEFINECLASS_FLAG_HAS_SUPERCLASS > 0
constant_index, superclass =
find_constant_path(insns, index - 1)
if superclass.empty?
warn("#{file}:#{line}: superclass with non constant path")
next
end
end
if (_, nesting = find_constant_path(insns, constant_index))
# If there is a constant path in the class name, then we need to
# handle that by updating the nesting.
next_nesting << (nesting << name)
else
# Otherwise we'll add the class name to the nesting.
next_nesting << [name]
end
if flags == VM_DEFINECLASS_TYPE_SINGLETON_CLASS
# At the moment, we don't support singletons that aren't
# defined on self. We could, but it would require more
# emulation.
if insns[index - 2] != [:putself]
warn(
"#{file}:#{line}: singleton class with non-self receiver"
)
next
end
elsif flags & VM_DEFINECLASS_TYPE_MODULE > 0
location = location_for(class_iseq)
results << ModuleDefinition.new(
next_nesting,
name,
location,
EntryComments.new(file_comments, location)
)
else
location = location_for(class_iseq)
results << ClassDefinition.new(
next_nesting,
name,
superclass,
location,
EntryComments.new(file_comments, location)
)
end
queue << [class_iseq, next_nesting]
when :definemethod
location = location_for(insn[2])
results << method_definition(
current_nesting,
insn[1],
location,
file_comments
)
when :definesmethod
if insns[index - 1] != [:putself]
warn("#{file}:#{line}: singleton method with non-self receiver")
next
end
location = location_for(insn[2])
results << SingletonMethodDefinition.new(
current_nesting,
insn[1],
location,
EntryComments.new(file_comments, location)
)
when :setconstant
next_nesting = current_nesting.dup
name = insn[1]
_, nesting = find_constant_path(insns, index - 1)
next_nesting << nesting if nesting.any?
location = Location.new(line, :unknown)
results << ConstantDefinition.new(
next_nesting,
name,
location,
EntryComments.new(file_comments, location)
)
when :opt_send_without_block, :send
case insn[1][:mid]
when :attr_reader, :attr_writer, :attr_accessor
attr_names = find_attr_arguments(insns, index)
next unless attr_names
location = Location.new(line, :unknown)
attr_names.each do |attr_name|
if insn[1][:mid] != :attr_writer
results << method_definition(
current_nesting,
attr_name,
location,
file_comments
)
end
if insn[1][:mid] != :attr_reader
results << method_definition(
current_nesting,
:"#{attr_name}=",
location,
file_comments
)
end
end
when :"core#set_method_alias"
# Now we have to validate that the alias is happening with a
# non-interpolated value. To do this we'll match the specific
# pattern we're expecting.
values =
insns[(index - 4)...index].map do |previous|
previous.is_a?(Array) ? previous[0] : previous
end
if values !=
%i[putspecialobject putspecialobject putobject putobject]
next
end
# Now that we know it's in the structure we want it, we can use
# the values of the putobject to determine the alias.
location = Location.new(line, :unknown)
results << AliasMethodDefinition.new(
current_nesting,
insns[index - 2][1],
location,
EntryComments.new(file_comments, location)
)
end
end
end
end
results
end
end
# This backend creates the index using the Syntax Tree parser and a visitor.
# It is not as fast as using the instruction sequences directly, but is
# supported on all runtimes.
class ParserBackend
class ConstantNameVisitor < Visitor
def visit_const_ref(node)
[node.constant.value.to_sym]
end
def visit_const_path_ref(node)
visit(node.parent) << node.constant.value.to_sym
end
def visit_var_ref(node)
[node.value.value.to_sym]
end
end
class IndexVisitor < Visitor
attr_reader :results, :nesting, :statements
def initialize
@results = []
@nesting = []
@statements = nil
end
visit_methods do
def visit_alias(node)
if node.left.is_a?(SymbolLiteral) && node.right.is_a?(SymbolLiteral)
location =
Location.new(
node.location.start_line,
node.location.start_column
)
results << AliasMethodDefinition.new(
nesting.dup,
node.left.value.value.to_sym,
location,
comments_for(node)
)
end
super
end
def visit_assign(node)
if node.target.is_a?(VarField) && node.target.value.is_a?(Const)
location =
Location.new(
node.location.start_line,
node.location.start_column
)
results << ConstantDefinition.new(
nesting.dup,
node.target.value.value.to_sym,
location,
comments_for(node)
)
end
super
end
def visit_class(node)
names = node.constant.accept(ConstantNameVisitor.new)
nesting << names
location =
Location.new(node.location.start_line, node.location.start_column)
superclass =
if node.superclass
visited = node.superclass.accept(ConstantNameVisitor.new)
if visited == [[]]
raise NotImplementedError, "superclass with non constant path"
end
visited
else
[]
end
results << ClassDefinition.new(
nesting.dup,
names.last,
superclass,
location,
comments_for(node)
)
super
nesting.pop
end
def visit_command(node)
case node.message.value
when "attr_reader", "attr_writer", "attr_accessor"
comments = comments_for(node)
location =
Location.new(
node.location.start_line,
node.location.start_column
)
node.arguments.parts.each do |argument|
next unless argument.is_a?(SymbolLiteral)
name = argument.value.value.to_sym
if node.message.value != "attr_writer"
results << MethodDefinition.new(
nesting.dup,
name,
location,
comments
)
end
if node.message.value != "attr_reader"
results << MethodDefinition.new(
nesting.dup,
:"#{name}=",
location,
comments
)
end
end
end
super
end
def visit_def(node)
name = node.name.value.to_sym
location =
Location.new(node.location.start_line, node.location.start_column)
results << if node.target.nil?
MethodDefinition.new(
nesting.dup,
name,
location,
comments_for(node)
)
else
SingletonMethodDefinition.new(
nesting.dup,
name,
location,
comments_for(node)
)
end
super
end
def visit_module(node)
names = node.constant.accept(ConstantNameVisitor.new)
nesting << names
location =
Location.new(node.location.start_line, node.location.start_column)
results << ModuleDefinition.new(
nesting.dup,
names.last,
location,
comments_for(node)
)
super
nesting.pop
end
def visit_program(node)
super
results
end
def visit_statements(node)
@statements = node
super
end
end
private
def comments_for(node)
comments = []
body = statements.body
line = node.location.start_line - 1
index = body.index(node)
return comments if index.nil?
index -= 1
while index >= 0 && body[index].is_a?(Comment) &&
(line - body[index].location.start_line < 2)
comments.unshift(body[index].value)
line = body[index].location.start_line
index -= 1
end
comments
end
end
def index(source)
SyntaxTree.parse(source).accept(IndexVisitor.new)
end
def index_file(filepath)
index(SyntaxTree.read(filepath))
end
end
# The class defined here is used to perform the indexing, depending on what
# functionality is available from the runtime.
INDEX_BACKEND =
defined?(RubyVM::InstructionSequence) ? ISeqBackend : ParserBackend
# This method accepts source code and then indexes it.
def self.index(source, backend: INDEX_BACKEND.new)
backend.index(source)
end
# This method accepts a filepath and then indexes it.
def self.index_file(filepath, backend: INDEX_BACKEND.new)
backend.index_file(filepath)
end
end
end