Class: CodeTools::AST::Node
- Inherits:
-
Object
- Object
- CodeTools::AST::Node
show all
- Defined in:
- lib/rubinius/code/ast/node.rb
Direct Known Subclasses
Alias, And, Arguments, ArrayLiteral, BackRef, Begin, Block, BlockArgument, BlockPass, Break, Case, Class, ClassName, ClosedScope, CollectSplat, ConcatArgs, ConstantAccess, ConstantAssignment, CurrentException, DefaultArguments, DefineFunction, DefineSingleton, Defined, EmptyArray, EmptyBody, EmptySplat, Encoding, EndData, Ensure, FalseLiteral, File, Flip2, FloatLiteral, ForParameters, HashLiteral, If, Iter, IterArguments, KeywordParameters, Lambda, Match, Match2, Match3, MirrorConstant, Module, ModuleName, MultipleAssignment, Negate, NilLiteral, Not, NthRef, NumberLiteral, OpAssignAnd, OpAssignAttribute, OpAssignElement, PEGLiteral, Parameters, PostArg, PreExe, PushArgs, PushArguments, Range, RegexLiteral, Rescue, RescueCondition, RescueSplat, Return, SClass, SValue, ScopedConstant, Self, Send, SplatAssignment, SplatValue, SplatWhen, StringLiteral, SymbolLiteral, ToArray, ToString, ToplevelConstant, TrueLiteral, TypeConstant, Undef, VariableAccess, VariableAssignment, When, While
Defined Under Namespace
Classes: TransformState
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#ascii_graph ⇒ Object
-
#attributes ⇒ Object
Yields each attribute and its name to the block.
-
#bytecode(g) ⇒ Object
-
#children ⇒ Object
Yields each child of this Node to the block.
-
#defined(g) ⇒ Object
-
#initialize(line) ⇒ Node
constructor
-
#new_block_generator(g, arguments) ⇒ Object
-
#new_generator(g, name, arguments = nil) ⇒ Object
-
#node_name ⇒ Object
The equivalent of Some::Module.demodulize.underscore in ActiveSupport.
-
#or_bytecode(g) ⇒ Object
Called if used as the lhs of an ||=.
-
#pos(g) ⇒ Object
-
#set_child(name, node) ⇒ Object
Called by #transform to update the child of a Node instance.
-
#to_sexp ⇒ Object
-
#transform(visitor, parent = nil, state = nil) ⇒ Object
A fixed-point algorithm for transforming an AST with a visitor.
-
#value_defined(g, f) ⇒ Object
-
#visit(visitor, parent = nil) ⇒ Object
Supports the Visitor pattern on a tree of Nodes.
-
#walk(arg = true, &block) ⇒ Object
This method implements a sort of tree iterator, yielding each Node instance to the provided block with the first argument to #walk.
Constructor Details
#initialize(line) ⇒ Node
48
49
50
|
# File 'lib/rubinius/code/ast/node.rb', line 48
def initialize(line)
@line = line
end
|
Instance Attribute Details
#line ⇒ Object
Returns the value of attribute line.
6
7
8
|
# File 'lib/rubinius/code/ast/node.rb', line 6
def line
@line
end
|
Class Method Details
.match_arguments?(arguments, count) ⇒ Boolean
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/rubinius/code/ast/node.rb', line 37
def self.match_arguments?(arguments, count)
case arguments
when ArrayLiteral
arguments.body.size == count
when nil
count == 0
else
false
end
end
|
.match_send?(node, receiver, method, name) ⇒ Boolean
31
32
33
34
35
|
# File 'lib/rubinius/code/ast/node.rb', line 31
def self.match_send?(node, receiver, method, name)
node.kind_of? ConstantAccess and
node.name == receiver and
method == name
end
|
8
9
10
11
12
13
|
# File 'lib/rubinius/code/ast/node.rb', line 8
def self.transform(category, name, )
Transforms.register category, name, self
@transform_name = name
=
@transform_kind = :call
end
|
19
20
21
|
# File 'lib/rubinius/code/ast/node.rb', line 19
def self.
end
|
23
24
25
|
# File 'lib/rubinius/code/ast/node.rb', line 23
def self.transform_kind
@transform_kind
end
|
27
28
29
|
# File 'lib/rubinius/code/ast/node.rb', line 27
def self.transform_kind=(k)
@transform_kind = k
end
|
15
16
17
|
# File 'lib/rubinius/code/ast/node.rb', line 15
def self.transform_name
@transform_name
end
|
Instance Method Details
#ascii_graph ⇒ Object
123
124
125
|
# File 'lib/rubinius/code/ast/node.rb', line 123
def ascii_graph
AsciiGrapher.new(self).print
end
|
#attributes ⇒ Object
Yields each attribute and its name to the block.
188
189
190
191
192
193
194
|
# File 'lib/rubinius/code/ast/node.rb', line 188
def attributes
instance_variables.each do |var|
child = instance_variable_get var
name = var.to_s[1..-1]
yield child, name
end
end
|
#bytecode(g) ⇒ Object
93
94
|
# File 'lib/rubinius/code/ast/node.rb', line 93
def bytecode(g)
end
|
#children ⇒ Object
Yields each child of this Node to the block. Additionally, for any attribute that is an Array, yields each element that is a Node.
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/rubinius/code/ast/node.rb', line 145
def children
instance_variables.each do |var|
child = instance_variable_get var
if child.kind_of? Node
yield child
elsif child.kind_of? Array
child.each { |x| yield x if x.kind_of? Node }
end
end
end
|
#defined(g) ⇒ Object
96
97
98
99
100
101
102
103
104
|
# File 'lib/rubinius/code/ast/node.rb', line 96
def defined(g)
g.push_rubinius
g.push_scope
g.send :active_path, 0
g.push_int @line
g.send :unrecognized_defined, 2
g.pop
g.push_nil
end
|
#new_block_generator(g, arguments) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/rubinius/code/ast/node.rb', line 56
def new_block_generator(g, arguments)
blk = g.class.new
blk.name = g.state.name || :__block__
blk.file = g.file
blk.for_block = true
blk.required_args = arguments.required_args
blk.post_args = arguments.post_args
blk.total_args = arguments.total_args
blk.splat_index = arguments.splat_index
blk.block_index = arguments.block_index
blk.arity = arguments.arity
blk.keywords = arguments.keywords.entries if arguments.keywords
blk.kwrest_index = arguments.kwrest_index
blk
end
|
#new_generator(g, name, arguments = nil) ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/rubinius/code/ast/node.rb', line 74
def new_generator(g, name, arguments=nil)
meth = g.class.new
meth.name = name
meth.file = g.file
if arguments
meth.required_args = arguments.required_args
meth.post_args = arguments.post_args
meth.total_args = arguments.total_args
meth.splat_index = arguments.splat_index
meth.block_index = arguments.block_index
meth.arity = arguments.arity
meth.keywords = arguments.keywords.entries if arguments.keywords
meth.kwrest_index = arguments.kwrest_index
end
meth
end
|
#node_name ⇒ Object
The equivalent of Some::Module.demodulize.underscore in ActiveSupport. The code is shamelessly borrowed as well.
158
159
160
161
162
163
164
|
# File 'lib/rubinius/code/ast/node.rb', line 158
def node_name
name = self.class.name.gsub(/^.*::/, '')
name.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
name.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
name.downcase!
name
end
|
#or_bytecode(g) ⇒ Object
Called if used as the lhs of an ||=. Expected to yield if the value was not found, so the bytecode for it to be emitted.
129
130
131
132
133
134
135
136
137
|
# File 'lib/rubinius/code/ast/node.rb', line 129
def or_bytecode(g)
found = g.new_label
bytecode(g)
g.dup
g.goto_if_true found
g.pop
yield
found.set!
end
|
#pos(g) ⇒ Object
52
53
54
|
# File 'lib/rubinius/code/ast/node.rb', line 52
def pos(g)
g.set_line @line
end
|
#set_child(name, node) ⇒ Object
Called by #transform to update the child of a Node instance. The default just calls the attr_accessor for the child. However, Node subclasses that must synchronize other internal state can override this method.
183
184
185
|
# File 'lib/rubinius/code/ast/node.rb', line 183
def set_child(name, node)
send :"#{name}=", node
end
|
#to_sexp ⇒ Object
139
140
141
|
# File 'lib/rubinius/code/ast/node.rb', line 139
def to_sexp
[:node, self.class.name]
end
|
A fixed-point algorithm for transforming an AST with a visitor. The traversal is top-down. The visitor object’s method corresponding to each node (see #node_name) is called for each node, passing the node and its parent.
To replace the node in the tree, the visitor method should return a new node; otherwise, return the existing node. The visitor is free to change values in the node, but substituting a node causes the entire tree to be walked repeatedly until no modifications are made.
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
238
|
# File 'lib/rubinius/code/ast/node.rb', line 205
def transform(visitor, parent=nil, state=nil)
state ||= TransformState.new
node = visitor.send :"node_#{node_name}", self, parent
state.modify unless equal? node
node.attributes do |attr, name|
if attr.kind_of? Node
child = attr.transform visitor, node, state
unless attr.equal? child
state.modify
node.set_child name, child
end
elsif attr.kind_of? Array
attr.each_with_index do |x, i|
if x.kind_of? Node
child = x.transform visitor, node, state
unless x.equal? child
state.modify
attr[i] = child
end
end
end
end
end
if parent.nil? and state.modified?
state.unmodify
node = transform visitor, nil, state
end
node
end
|
#value_defined(g, f) ⇒ Object
106
107
108
|
# File 'lib/rubinius/code/ast/node.rb', line 106
def value_defined(g, f)
bytecode(g)
end
|
#visit(visitor, parent = nil) ⇒ Object
Supports the Visitor pattern on a tree of Nodes. The visitor should be an object that responds to methods named after the Node subclasses. The method called is determined by the #node_name method. Passes both the node and its parent so that the visitor can maintain nesting information if desired.
The #visit implements a read-only traversal of the tree. To modify the tree, see the #transform methed.
174
175
176
177
|
# File 'lib/rubinius/code/ast/node.rb', line 174
def visit(visitor, parent=nil)
visitor.__send__ self.node_name, self, parent
children { |c| c.visit visitor, self }
end
|
#walk(arg = true, &block) ⇒ Object
This method implements a sort of tree iterator, yielding each Node instance to the provided block with the first argument to #walk. If the block returns a non-true value, the walk is terminated.
This method is really an iterator, not a Visitor pattern.
115
116
117
118
119
120
121
|
# File 'lib/rubinius/code/ast/node.rb', line 115
def walk(arg=true, &block)
children do |child|
if ch_arg = block.call(arg, child)
child.walk(ch_arg, &block)
end
end
end
|