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>attribute)\s+(?<value>.*?))|
	(@define\s+(?<kind>)\s+(?<value>.*?))
\Z/x
SCOPE_ATTRIBUTE =
/\A
	(@scope\s+(?<names>.*?))
\Z/x

Instance Method Summary collapse

Constructor Details

#initialize(language) ⇒ Parser

Returns a new instance of Parser.



26
27
28
# File 'lib/decode/language/ruby/parser.rb', line 26

def initialize(language)
	@language = language
end

Instance Method Details

#definitions_for(source, &block) ⇒ Object

Extract definitions from the given input file.



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

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



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

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



238
239
240
241
242
243
244
245
246
# File 'lib/decode/language/ruby/parser.rb', line 238

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



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/decode/language/ruby/parser.rb', line 199

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



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

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



252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/decode/language/ruby/parser.rb', line 252

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.



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

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



224
225
226
227
228
229
230
231
# File 'lib/decode/language/ruby/parser.rb', line 224

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.



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

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
		)
		
		yield definition
		
		if children = node.children[1]
			walk_definitions(children, comments, definition, &block)
		end
	when :class
		definition = Class.new(
			node, nested_name_for(node.children[0]),
			comments: extract_comments_for(node, comments),
			parent: parent, language: @language
		)
		
		yield definition
		
		if children = node.children[2]
			walk_definitions(children, comments, definition, &block)
		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
			)
			
			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
		)
		
		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
		)
		
		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 :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



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/decode/language/ruby/parser.rb', line 286

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.expand(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