Class: Decode::Language::Ruby::Parser

Inherits:
Object
  • Object
show all
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>attr)\s+(?<value>.*?))|
  (@define\s+(?<kind>)\s+(?<value>.*?))
\Z/x
SCOPE_ATTRIBUTE =
/\A
  (@scope\s+(?<names>.*?))
\Z/x

Instance Method Summary collapse

Instance Method Details

#definitions_for(input, &block) ⇒ Object

Extract definitions from the given input file.



42
43
44
45
46
47
48
# File 'lib/decode/language/ruby/parser.rb', line 42

def definitions_for(input, &block)
  top, comments = ::Parser::CurrentRuby.parse_with_comments(input.read)
  
  if top
    walk_definitions(top, comments, &block)
  end
end

#extract_comments_for(node, comments) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/decode/language/ruby/parser.rb', line 50

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



214
215
216
217
218
219
220
221
222
# File 'lib/decode/language/ruby/parser.rb', line 214

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



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/decode/language/ruby/parser.rb', line 192

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

#scope_for(comments, parent = nil, &block) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/decode/language/ruby/parser.rb', line 228

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: Ruby)
        yield scope
        scope
      end
    end
  end
  
  return parent
end

#segments_for(input, &block) ⇒ Object

Extract segments from the given input file.



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/decode/language/ruby/parser.rb', line 243

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

#walk_definitions(node, comments, parent = nil, &block) ⇒ Object

Walk over the syntax tree and extract relevant definitions with their associated comments.



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
# File 'lib/decode/language/ruby/parser.rb', line 76

def walk_definitions(node, comments, parent = nil, &block)
  case node.type
  when :begin
    node.children.each do |child|
      walk_definitions(child, comments, parent, &block)
    end
  when :module
    definition = Module.new(
      node, node.children[0].children[1],
      comments: extract_comments_for(node, comments),
      parent: parent,
      language: Ruby
    )
    
    yield definition
    
    if children = node.children[1]
      walk_definitions(children, comments, definition, &block)
    end
  when :class
    definition = Class.new(
      node, node.children[0].children[1],
      comments: extract_comments_for(node, comments),
      parent: parent, language: Ruby
    )
    
    yield definition
    
    if children = node.children[2]
      walk_definitions(children, comments, definition, &block)
    end
  when :sclass
    definition = Singleton.new(
      node, node.children[0],
      comments: extract_comments_for(node, comments),
      parent: parent, language: Ruby
    )
    
    yield definition
    
    if children = node.children[1]
      walk_definitions(children, comments, definition, &block)
    end
  when :def
    definition = Method.new(
      node, node.children[0],
      comments: extract_comments_for(node, comments),
      parent: parent, language: Ruby
    )
    
    yield definition
  when :defs
    definition = Function.new(
      node, node.children[1],
      comments: extract_comments_for(node, comments),
      parent: parent, language: Ruby
    )
    
    yield definition
  when :casgn
    definition = Constant.new(
      node, node.children[1],
      comments: extract_comments_for(node, comments),
      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(
        node, name_for(node.children[2]),
        comments: extract_comments_for(node, comments),
        parent: parent, language: Ruby
      )
      
      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: Ruby
        )
        
        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: Ruby
      )
      
      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
  end
end

#walk_segments(node, comments, &block) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/decode/language/ruby/parser.rb', line 262

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.expand(child)
      end
    end
    
    yield segment if segment
  end
end