Class: Decode::Language::Ruby::Parser
- Inherits:
-
Object
- Object
- Decode::Language::Ruby::Parser
- Defined in:
- lib/decode/language/ruby/parser.rb
Overview
The Ruby source code parser.
Constant Summary collapse
- NAME_ATTRIBUTE =
/\A@name\s+(?<value>.*?)\Z/- KIND_ATTRIBUTE =
/\A (@(?<kind>attribute)\s+(?<value>.*?))| (@define\s+(?<kind>)\s+(?<value>.*?)) \Z/x- SCOPE_ATTRIBUTE =
/\A (@scope\s+(?<names>.*?)) \Z/x
Instance Method Summary collapse
- #constant_names_for(children) ⇒ Object
-
#definitions_for(source, &block) ⇒ Object
Extract definitions from the given input file.
- #extract_comments_for(node, comments) ⇒ Object
-
#initialize(language) ⇒ Parser
constructor
A new instance of Parser.
- #kind_for(node, comments = nil) ⇒ Object
- #name_for(node, comments = nil) ⇒ Object
- #nested_name_for(node) ⇒ Object
- #scope_for(comments, parent = nil, &block) ⇒ Object
-
#segments_for(source, &block) ⇒ Object
Extract segments from the given input file.
- #singleton_name_for(node) ⇒ Object
-
#walk_definitions(node, comments, parent = nil, &block) ⇒ Object
Walk over the syntax tree and extract relevant definitions with their associated comments.
- #walk_segments(node, comments, &block) ⇒ Object
- #with_visibility(visibility = :public, &block) ⇒ Object
Constructor Details
#initialize(language) ⇒ Parser
Returns a new instance of Parser.
26 27 28 29 30 31 |
# File 'lib/decode/language/ruby/parser.rb', line 26 def initialize(language) @language = language @visibility = :public @definitions = Hash.new.compare_by_identity end |
Instance Method Details
#constant_names_for(children) ⇒ Object
301 302 303 304 305 306 307 |
# File 'lib/decode/language/ruby/parser.rb', line 301 def constant_names_for(children) children.each do |node| if node.type == :sym yield node.children[0] end end end |
#definitions_for(source, &block) ⇒ Object
Extract definitions from the given input file.
52 53 54 55 56 57 58 |
# File 'lib/decode/language/ruby/parser.rb', line 52 def definitions_for(source, &block) top, comments = self.parse_source(source) if top walk_definitions(top, comments, &block) end end |
#extract_comments_for(node, comments) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/decode/language/ruby/parser.rb', line 60 def extract_comments_for(node, comments) prefix = [] while comment = comments.first break if comment.location.line >= node.location.line if last_comment = prefix.last if last_comment.location.line != (comment.location.line - 1) prefix.clear end end prefix << comments.shift end # The last comment must butt up against the node: if comment = prefix.last if comment.location.line == (node.location.line - 1) return prefix.map do |comment| comment.text.sub(/\A\#\s?/, '') end end end end |
#kind_for(node, comments = nil) ⇒ Object
273 274 275 276 277 278 279 280 281 |
# File 'lib/decode/language/ruby/parser.rb', line 273 def kind_for(node, comments = nil) comments&.each do |comment| if match = comment.match(KIND_ATTRIBUTE) return match[:kind].to_sym end end return nil end |
#name_for(node, comments = nil) ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/decode/language/ruby/parser.rb', line 234 def name_for(node, comments = nil) comments&.each do |comment| if match = comment.match(NAME_ATTRIBUTE) return match[:value].to_sym end end case node.type when :sym return node.children[0] when :send return node.children[1] when :block return node.children[0].children[1] end end |
#nested_name_for(node) ⇒ Object
251 252 253 254 255 256 257 |
# File 'lib/decode/language/ruby/parser.rb', line 251 def nested_name_for(node) if prefix = node.children[0] "#{nested_name_for(prefix)}::#{node.children[1]}".to_sym else node.children[1] end end |
#scope_for(comments, parent = nil, &block) ⇒ Object
287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/decode/language/ruby/parser.rb', line 287 def scope_for(comments, parent = nil, &block) comments&.each do |comment| if match = comment.match(SCOPE_ATTRIBUTE) return match[:names].split(/\s+/).map(&:to_sym).inject(nil) do |memo, name| scope = Scope.new(name, parent: memo, language: @language) yield scope scope end end end return parent end |
#segments_for(source, &block) ⇒ Object
Extract segments from the given input file.
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/decode/language/ruby/parser.rb', line 310 def segments_for(source, &block) top, comments = self.parse_source(source) # We delete any leading comments: line = 0 while comment = comments.first if comment.location.line == line comments.pop line += 1 else break end end # Now we iterate over the syntax tree and generate segments: walk_segments(top, comments, &block) end |
#singleton_name_for(node) ⇒ Object
259 260 261 262 263 264 265 266 |
# File 'lib/decode/language/ruby/parser.rb', line 259 def singleton_name_for(node) case node.type when :const nested_name_for(node) when :self :'self' end end |
#walk_definitions(node, comments, parent = nil, &block) ⇒ Object
Walk over the syntax tree and extract relevant definitions with their associated comments.
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 |
# File 'lib/decode/language/ruby/parser.rb', line 94 def walk_definitions(node, comments, parent = nil, &block) case node.type when :module definition = Module.new( node, nested_name_for(node.children[0]), comments: extract_comments_for(node, comments), parent: parent, language: @language, visibility: :public ) assign_definition(parent, definition) yield definition if children = node.children[1] with_visibility do walk_definitions(children, comments, definition, &block) end end when :class definition = Class.new( node, nested_name_for(node.children[0]), comments: extract_comments_for(node, comments), parent: parent, language: @language, visibility: :public ) assign_definition(parent, definition) yield definition if children = node.children[2] with_visibility do walk_definitions(children, comments, definition, &block) end end when :sclass if name = singleton_name_for(node.children[0]) definition = Singleton.new( node, name, comments: extract_comments_for(node, comments), parent: parent, language: @language, visibility: :public ) yield definition if children = node.children[1] walk_definitions(children, comments, definition, &block) end end when :def definition = Method.new( node, node.children[0], comments: extract_comments_for(node, comments), parent: parent, language: @language, visibility: @visibility ) yield definition when :defs extracted_comments = extract_comments_for(node, comments) definition = Function.new( node, node.children[1], comments: extracted_comments, parent: scope_for(extracted_comments, parent, &block), language: @language, visibility: @visibility ) yield definition when :casgn definition = Constant.new( node, node.children[1], comments: extract_comments_for(node, comments), parent: parent, language: @language ) yield definition when :send name = node.children[1] case name when :public, :protected, :private @visibility = name when :private_constant constant_names_for(node.children[2..]) do |name| if definition = lookup_definition(parent, name) definition.visibility = :private end end when :attr, :attr_reader, :attr_writer, :attr_accessor definition = Attribute.new( node, name_for(node.children[2]), comments: extract_comments_for(node, comments), parent: parent, language: @language ) yield definition else extracted_comments = extract_comments_for(node, comments) if kind = kind_for(node, extracted_comments) definition = Call.new( node, name_for(node, extracted_comments), comments: extracted_comments, parent: parent, language: @language ) yield definition end end when :block extracted_comments = extract_comments_for(node, comments) if name = name_for(node, extracted_comments) definition = Block.new( node, name, comments: extracted_comments, parent: scope_for(extracted_comments, parent, &block), language: @language ) if kind = kind_for(node, extracted_comments) definition = definition.convert(kind) end yield definition if children = node.children[2] walk_definitions(children, comments, definition, &block) end end else node.children.each do |child| if child.is_a?(::Parser::AST::Node) walk_definitions(child, comments, parent, &block) if child end end end end |
#walk_segments(node, comments, &block) ⇒ Object
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 |
# File 'lib/decode/language/ruby/parser.rb', line 329 def walk_segments(node, comments, &block) case node.type when :begin segment = nil node.children.each do |child| if segment.nil? segment = Segment.new( extract_comments_for(child, comments), @language, child ) elsif next_comments = extract_comments_for(child, comments) yield segment if segment segment = Segment.new(next_comments, @language, child) else segment.(child) end end yield segment if segment else # One top level segment: segment = Segment.new( extract_comments_for(node, comments), @language, node ) yield segment end end |
#with_visibility(visibility = :public, &block) ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/decode/language/ruby/parser.rb', line 85 def with_visibility(visibility = :public, &block) saved_visibility = @visibility @visibility = visibility yield ensure @visibility = saved_visibility end |