|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module SyntaxTree |
| 4 | + # This class can be used to build an index of the structure of Ruby files. We |
| 5 | + # define an index as the list of constants and methods defined within a file. |
| 6 | + # |
| 7 | + # This index strives to be as fast as possible to better support tools like |
| 8 | + # IDEs. Because of that, it has different backends depending on what |
| 9 | + # functionality is available. |
| 10 | + module Index |
| 11 | + # This is a location for an index entry. |
| 12 | + class Location |
| 13 | + attr_reader :line, :column |
| 14 | + |
| 15 | + def initialize(line, column) |
| 16 | + @line = line |
| 17 | + @column = column |
| 18 | + end |
| 19 | + end |
| 20 | + |
| 21 | + # This entry represents a class definition using the class keyword. |
| 22 | + class ClassDefinition |
| 23 | + attr_reader :nesting, :name, :location |
| 24 | + |
| 25 | + def initialize(nesting, name, location) |
| 26 | + @nesting = nesting |
| 27 | + @name = name |
| 28 | + @location = location |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + # This entry represents a module definition using the module keyword. |
| 33 | + class ModuleDefinition |
| 34 | + attr_reader :nesting, :name, :location |
| 35 | + |
| 36 | + def initialize(nesting, name, location) |
| 37 | + @nesting = nesting |
| 38 | + @name = name |
| 39 | + @location = location |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + # This entry represents a method definition using the def keyword. |
| 44 | + class MethodDefinition |
| 45 | + attr_reader :nesting, :name, :location |
| 46 | + |
| 47 | + def initialize(nesting, name, location) |
| 48 | + @nesting = nesting |
| 49 | + @name = name |
| 50 | + @location = location |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + # This entry represents a singleton method definition using the def keyword |
| 55 | + # with a specified target. |
| 56 | + class SingletonMethodDefinition |
| 57 | + attr_reader :nesting, :name, :location |
| 58 | + |
| 59 | + def initialize(nesting, name, location) |
| 60 | + @nesting = nesting |
| 61 | + @name = name |
| 62 | + @location = location |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + # This backend creates the index using RubyVM::InstructionSequence, which is |
| 67 | + # faster than using the Syntax Tree parser, but is not available on all |
| 68 | + # runtimes. |
| 69 | + class ISeqBackend |
| 70 | + VM_DEFINECLASS_TYPE_CLASS = 0x00 |
| 71 | + VM_DEFINECLASS_TYPE_SINGLETON_CLASS = 0x01 |
| 72 | + VM_DEFINECLASS_TYPE_MODULE = 0x02 |
| 73 | + VM_DEFINECLASS_FLAG_SCOPED = 0x08 |
| 74 | + VM_DEFINECLASS_FLAG_HAS_SUPERCLASS = 0x10 |
| 75 | + |
| 76 | + def index(source) |
| 77 | + index_iseq(RubyVM::InstructionSequence.compile(source).to_a) |
| 78 | + end |
| 79 | + |
| 80 | + def index_file(filepath) |
| 81 | + index_iseq(RubyVM::InstructionSequence.compile_file(filepath).to_a) |
| 82 | + end |
| 83 | + |
| 84 | + private |
| 85 | + |
| 86 | + def index_iseq(iseq) |
| 87 | + results = [] |
| 88 | + queue = [[iseq, []]] |
| 89 | + |
| 90 | + while (current_iseq, current_nesting = queue.shift) |
| 91 | + current_iseq[13].each_with_index do |insn, index| |
| 92 | + next unless insn.is_a?(Array) |
| 93 | + |
| 94 | + case insn[0] |
| 95 | + when :defineclass |
| 96 | + _, name, class_iseq, flags = insn |
| 97 | + |
| 98 | + if flags == VM_DEFINECLASS_TYPE_SINGLETON_CLASS |
| 99 | + # At the moment, we don't support singletons that aren't |
| 100 | + # defined on self. We could, but it would require more |
| 101 | + # emulation. |
| 102 | + if current_iseq[13][index - 2] != [:putself] |
| 103 | + raise NotImplementedError, |
| 104 | + "singleton class with non-self receiver" |
| 105 | + end |
| 106 | + elsif flags & VM_DEFINECLASS_TYPE_MODULE > 0 |
| 107 | + code_location = class_iseq[4][:code_location] |
| 108 | + location = Location.new(code_location[0], code_location[1]) |
| 109 | + results << ModuleDefinition.new(current_nesting, name, location) |
| 110 | + else |
| 111 | + code_location = class_iseq[4][:code_location] |
| 112 | + location = Location.new(code_location[0], code_location[1]) |
| 113 | + results << ClassDefinition.new(current_nesting, name, location) |
| 114 | + end |
| 115 | + |
| 116 | + queue << [class_iseq, current_nesting + [name]] |
| 117 | + when :definemethod |
| 118 | + _, name, method_iseq = insn |
| 119 | + |
| 120 | + code_location = method_iseq[4][:code_location] |
| 121 | + location = Location.new(code_location[0], code_location[1]) |
| 122 | + results << SingletonMethodDefinition.new( |
| 123 | + current_nesting, |
| 124 | + name, |
| 125 | + location |
| 126 | + ) |
| 127 | + when :definesmethod |
| 128 | + _, name, method_iseq = insn |
| 129 | + |
| 130 | + code_location = method_iseq[4][:code_location] |
| 131 | + location = Location.new(code_location[0], code_location[1]) |
| 132 | + results << MethodDefinition.new(current_nesting, name, location) |
| 133 | + end |
| 134 | + end |
| 135 | + end |
| 136 | + |
| 137 | + results |
| 138 | + end |
| 139 | + end |
| 140 | + |
| 141 | + # This backend creates the index using the Syntax Tree parser and a visitor. |
| 142 | + # It is not as fast as using the instruction sequences directly, but is |
| 143 | + # supported on all runtimes. |
| 144 | + class ParserBackend |
| 145 | + class IndexVisitor < Visitor |
| 146 | + attr_reader :results, :nesting |
| 147 | + |
| 148 | + def initialize |
| 149 | + @results = [] |
| 150 | + @nesting = [] |
| 151 | + end |
| 152 | + |
| 153 | + def visit_class(node) |
| 154 | + name = visit(node.constant).to_sym |
| 155 | + location = |
| 156 | + Location.new(node.location.start_line, node.location.start_column) |
| 157 | + |
| 158 | + results << ClassDefinition.new(nesting.dup, name, location) |
| 159 | + nesting << name |
| 160 | + |
| 161 | + super |
| 162 | + nesting.pop |
| 163 | + end |
| 164 | + |
| 165 | + def visit_const_ref(node) |
| 166 | + node.constant.value |
| 167 | + end |
| 168 | + |
| 169 | + def visit_def(node) |
| 170 | + name = node.name.value.to_sym |
| 171 | + location = |
| 172 | + Location.new(node.location.start_line, node.location.start_column) |
| 173 | + |
| 174 | + results << if node.target.nil? |
| 175 | + MethodDefinition.new(nesting.dup, name, location) |
| 176 | + else |
| 177 | + SingletonMethodDefinition.new(nesting.dup, name, location) |
| 178 | + end |
| 179 | + end |
| 180 | + |
| 181 | + def visit_module(node) |
| 182 | + name = visit(node.constant).to_sym |
| 183 | + location = |
| 184 | + Location.new(node.location.start_line, node.location.start_column) |
| 185 | + |
| 186 | + results << ModuleDefinition.new(nesting.dup, name, location) |
| 187 | + nesting << name |
| 188 | + |
| 189 | + super |
| 190 | + nesting.pop |
| 191 | + end |
| 192 | + |
| 193 | + def visit_program(node) |
| 194 | + super |
| 195 | + results |
| 196 | + end |
| 197 | + end |
| 198 | + |
| 199 | + def index(source) |
| 200 | + SyntaxTree.parse(source).accept(IndexVisitor.new) |
| 201 | + end |
| 202 | + |
| 203 | + def index_file(filepath) |
| 204 | + index(SyntaxTree.read(filepath)) |
| 205 | + end |
| 206 | + end |
| 207 | + |
| 208 | + # The class defined here is used to perform the indexing, depending on what |
| 209 | + # functionality is available from the runtime. |
| 210 | + INDEX_BACKEND = |
| 211 | + defined?(RubyVM::InstructionSequence) ? ISeqBackend : ParserBackend |
| 212 | + |
| 213 | + # This method accepts source code and then indexes it. |
| 214 | + def self.index(source) |
| 215 | + INDEX_BACKEND.new.index(source) |
| 216 | + end |
| 217 | + |
| 218 | + # This method accepts a filepath and then indexes it. |
| 219 | + def self.index_file(filepath) |
| 220 | + INDEX_BACKEND.new.index_file(filepath) |
| 221 | + end |
| 222 | + end |
| 223 | +end |
0 commit comments