/
node.rb
237 lines (212 loc) · 4.84 KB
/
node.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
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
231
232
233
234
235
236
237
# encoding: utf-8
module Mutant
# Generator for mutations
class Mutator
# Abstract base class for node mutators
class Node < self
include AbstractType, NodeHelpers, Unparser::Constants
# Return identity of node
#
# @param [Parser::AST::Node] node
#
# @return [String]
#
# @api private
#
def self.identity(node)
Unparser.unparse(node)
end
# Define named child
#
# @param [Symbol] name
# @param [Fixnum] index
#
# @return [undefined]
#
# @api private
#
def self.define_named_child(name, index)
define_method(name) do
children.at(index)
end
define_method("emit_#{name}_mutations") do
mutate_child(index)
end
define_method("emit_#{name}") do |node|
emit_child_update(index, node)
end
end
private_class_method :define_named_child
# Define remaining children
#
# @param [Array<Symbol>] names
#
# @return [undefined]
#
# @api private
#
def self.define_remaining_children(names)
define_method(:remaining_children_with_index) do
children.each_with_index.drop(names.length)
end
define_method(:remaining_children) do
children.drop(names.length)
end
end
private_class_method :define_remaining_children
# Create name helpers
#
# @return [undefined]
#
# @api private
#
def self.children(*names)
names.each_with_index do |name, index|
define_named_child(name, index)
end
define_remaining_children(names)
end
private_class_method :children
private
# Return mutated node
#
# @return [Parser::AST::Node]
#
# @api private
#
alias_method :node, :input
# Return duplicated node
#
# @return [Parser::AST::Node]
#
# @api private
#
alias_method :dup_node, :dup_input
# Emit children mutations
#
# @return [undefined]
#
# @api private
#
def emit_children_mutations
Mutator::Util::Array.each(inherit_context(children)) do |children|
emit_self(*children)
end
end
# Return children
#
# @return [Array<Parser::AST::Node>]
#
# @api private
#
def children
node.children
end
# Dispatch on child index
#
# @param [Fixnum] index
#
# @return [undefined]
#
# @api private
#
def mutate_child(index, mutator = Mutator)
child = children.at(index)
mutator.each(inherit_context(child)) do |mutation|
emit_child_update(index, mutation)
end
end
# Emit delete child mutation
#
# @param [Fixnum] index
#
# @return [undefined]
#
# @api private
#
def delete_child(index)
dup_children = children.dup
dup_children.delete_at(index)
emit_self(*dup_children)
end
# Emit updated child
#
# @param [Fixnum] index
# @param [Parser::AST::Node] node
#
# @return [undefined]
#
# @api private
#
def emit_child_update(index, node)
new_children = children.dup
new_children[index] = node
emit_self(*new_children)
end
# Emit a new AST node with same class as wrapped node
#
# @param [Array<Parser::AST::Node>] children
#
# @return [undefined]
#
# @api private
#
def emit_self(*children)
emit(new_self(*children))
end
# Emit a new AST node with NilLiteral class
#
# @return [undefined]
#
# @api private
#
def emit_nil
emit(N_NIL) unless agsn_left?
end
# Return new self typed child
#
# @return [Parser::AST::Node]
#
# @api private
#
def new_self(*children)
Parser::AST::Node.new(node.type, children)
end
# Emit values
#
# @param [Array<Object>] values
#
# @return [undefined]
#
# @api private
#
def emit_values(values)
values.each do |value|
emit_self(value)
end
end
# Return parent type
#
# @return [Symbol] type
# if parent with type is presnet
#
# @return [nil]
# otherwise
#
# @api private
#
def parent_type
parent && parent.node.type
end
# Test if the node is the left of an or_asgn or op_asgn
#
# @return [Boolean]
#
# @api private
#
def agsn_left?
OP_ASSIGN.include?(parent_type) && parent.left.equal?(node)
end
end # Node
end # Mutator
end # Mutant