Class: Decode::Language::Ruby
- Inherits:
-
Object
- Object
- Decode::Language::Ruby
- Defined in:
- lib/decode/language/ruby.rb
Defined Under Namespace
Classes: Reference
Constant Summary collapse
- PREFIX =
The symbol which is used to separate the specified definition from the parent scope.
{ class: '::', module: '::', def: ':', constant: '::', defs: '.', }.freeze
- KIND =
{ ':' => :def, '.' => :defs, }.freeze
Instance Method Summary collapse
- #extract_comments_for(node, comments) ⇒ Object
- #join(symbols, absolute = true) ⇒ Object
- #parse(input, &block) ⇒ Object
-
#walk(node, comments, parent = nil, &block) ⇒ Object
Walk over the syntax tree and extract relevant definitions with their associated comments.
Instance Method Details
#extract_comments_for(node, comments) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/decode/language/ruby.rb', line 115 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(&:text) end end end |
#join(symbols, absolute = true) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/decode/language/ruby.rb', line 88 def join(symbols, absolute = true) buffer = String.new symbols.each do |symbol| if absolute == false absolute = true else buffer << PREFIX[symbol.kind] end buffer << symbol.name.to_s end return buffer end |
#parse(input, &block) ⇒ Object
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/decode/language/ruby.rb', line 104 def parse(input, &block) parser = ::Parser::CurrentRuby.new buffer = ::Parser::Source::Buffer.new('(input)') buffer.source = input.read top, comments = parser.parse_with_comments(buffer) walk(top, comments, &block) end |
#walk(node, comments, parent = nil, &block) ⇒ Object
Walk over the syntax tree and extract relevant definitions with their associated comments.
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 |
# File 'lib/decode/language/ruby.rb', line 139 def walk(node, comments, parent = nil, &block) case node.type when :begin node.children.each do |child| walk(child, comments, parent, &block) end when :class definition = Definition.new( :class, node.children[0].children[1], node, extract_comments_for(node, comments), parent: parent, language: self ) yield definition walk(node.children[2], comments, definition, &block) when :module definition = Definition.new( :module, node.children[0].children[1], node, extract_comments_for(node, comments), parent: parent, language: self ) yield definition walk(node.children[1], comments, definition, &block) when :def definition = Definition.new( :def, node.children[0], node, extract_comments_for(node, comments), parent: parent, language: self ) yield definition # if body = node.children[2] # walk(body, comments, definition, &block) # end when :defs definition = Definition.new( :defs, node.children[1], node, extract_comments_for(node, comments), parent: parent, language: self ) yield definition # if body = node.children[2] # walk(body, comments, definition, &block) # end when :casgn definition = Definition.new( :constant, node.children[1], node, extract_comments_for(node, comments), parent: parent, language: self ) yield definition end end |