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.
Instance Method Summary collapse
- #extract_comments_for(node, comments) ⇒ Object
- #name_for(node) ⇒ Object
-
#segments_for(input, &block) ⇒ Object
Extract segments from the given input file.
-
#symbols_for(input, &block) ⇒ Object
Extract symbols from the given input file.
- #walk_segments(node, comments, &block) ⇒ Object
-
#walk_symbols(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
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/decode/language/ruby/parser.rb', line 46 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 |
#name_for(node) ⇒ Object
153 154 155 156 157 158 |
# File 'lib/decode/language/ruby/parser.rb', line 153 def name_for(node) case node.type when :sym return node.children[0] end end |
#segments_for(input, &block) ⇒ Object
Extract segments from the given input file.
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/decode/language/ruby/parser.rb', line 161 def segments_for(input, &block) top, comments = ::Parser::CurrentRuby.parse_with_comments(input.read) # 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 |
#symbols_for(input, &block) ⇒ Object
Extract symbols from the given input file.
38 39 40 41 42 43 44 |
# File 'lib/decode/language/ruby/parser.rb', line 38 def symbols_for(input, &block) top, comments = ::Parser::CurrentRuby.parse_with_comments(input.read) if top walk_symbols(top, comments, &block) end end |
#walk_segments(node, comments, &block) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/decode/language/ruby/parser.rb', line 180 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), Ruby, child ) elsif next_comments = extract_comments_for(child, comments) yield segment if segment segment = Segment.new(next_comments, Ruby, child) else segment.(child) end end yield segment if segment end end |
#walk_symbols(node, comments, parent = nil, &block) ⇒ Object
Walk over the syntax tree and extract relevant definitions with their associated comments.
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 |
# File 'lib/decode/language/ruby/parser.rb', line 72 def walk_symbols(node, comments, parent = nil, &block) case node.type when :begin node.children.each do |child| walk_symbols(child, comments, parent, &block) end when :module definition = Module.new( :module, node.children[0].children[1], extract_comments_for(node, comments), node, parent: parent, language: Ruby ) yield definition if children = node.children[1] walk_symbols(children, comments, definition, &block) end when :class definition = Class.new( :class, node.children[0].children[1], extract_comments_for(node, comments), node, parent: parent, language: Ruby ) yield definition if children = node.children[2] walk_symbols(children, comments, definition, &block) end when :sclass definition = Singleton.new( :class, node.children[0], extract_comments_for(node, comments), node, parent: parent, language: Ruby ) yield definition if children = node.children[1] walk_symbols(children, comments, definition, &block) end when :def definition = Method.new( :def, node.children[0], extract_comments_for(node, comments), node, parent: parent, language: Ruby ) yield definition when :defs definition = Function.new( :defs, node.children[1], extract_comments_for(node, comments), node, parent: parent, language: Ruby ) yield definition when :casgn definition = Constant.new( :constant, node.children[1], extract_comments_for(node, comments), node, parent: parent, language: Ruby ) yield definition when :send name = node.children[1] case name when :attr, :attr_reader, :attr_writer, :attr_accessor definition = Attribute.new( :def, name_for(node.children[2]), extract_comments_for(node, comments), node, parent: parent, language: Ruby ) yield definition end end end |