Class: Woods::Ast::Node
- Inherits:
-
Struct
- Object
- Struct
- Woods::Ast::Node
- Defined in:
- lib/woods/ast/node.rb
Overview
Normalized AST node struct used by all consumers.
Provides a parser-independent representation of Ruby AST nodes. Both Prism and the parser gem are normalized to this common structure.
Instance Attribute Summary collapse
-
#arguments ⇒ Object
Returns the value of attribute arguments.
-
#children ⇒ Object
Returns the value of attribute children.
-
#end_line ⇒ Object
Returns the value of attribute end_line.
-
#line ⇒ Object
Returns the value of attribute line.
-
#method_name ⇒ Object
Returns the value of attribute method_name.
-
#receiver ⇒ Object
Returns the value of attribute receiver.
-
#source ⇒ Object
Returns the value of attribute source.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
-
#find_all(target_type) ⇒ Array<Ast::Node>
Find all descendant nodes matching a type.
-
#find_first(target_type) ⇒ Ast::Node?
Find the first descendant node matching a type (depth-first).
-
#to_source ⇒ String
Return source text representation.
Instance Attribute Details
#arguments ⇒ Object
Returns the value of attribute arguments
20 21 22 |
# File 'lib/woods/ast/node.rb', line 20 def arguments @arguments end |
#children ⇒ Object
Returns the value of attribute children
20 21 22 |
# File 'lib/woods/ast/node.rb', line 20 def children @children end |
#end_line ⇒ Object
Returns the value of attribute end_line
20 21 22 |
# File 'lib/woods/ast/node.rb', line 20 def end_line @end_line end |
#line ⇒ Object
Returns the value of attribute line
20 21 22 |
# File 'lib/woods/ast/node.rb', line 20 def line @line end |
#method_name ⇒ Object
Returns the value of attribute method_name
20 21 22 |
# File 'lib/woods/ast/node.rb', line 20 def method_name @method_name end |
#receiver ⇒ Object
Returns the value of attribute receiver
20 21 22 |
# File 'lib/woods/ast/node.rb', line 20 def receiver @receiver end |
#source ⇒ Object
Returns the value of attribute source
20 21 22 |
# File 'lib/woods/ast/node.rb', line 20 def source @source end |
#type ⇒ Object
Returns the value of attribute type
20 21 22 |
# File 'lib/woods/ast/node.rb', line 20 def type @type end |
Instance Method Details
#find_all(target_type) ⇒ Array<Ast::Node>
Find all descendant nodes matching a type.
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/woods/ast/node.rb', line 35 def find_all(target_type) results = [] queue = [self] while (current = queue.shift) results << current if current.type == target_type (current.children || []).each do |child| queue << child if child.is_a?(Ast::Node) end end results end |
#find_first(target_type) ⇒ Ast::Node?
Find the first descendant node matching a type (depth-first).
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/woods/ast/node.rb', line 51 def find_first(target_type) return self if type == target_type (children || []).each do |child| next unless child.is_a?(Ast::Node) result = child.find_first(target_type) return result if result end nil end |
#to_source ⇒ String
Return source text representation.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/woods/ast/node.rb', line 66 def to_source return source if source case type when :send parts = [] parts << receiver if receiver parts << method_name if method_name parts.join('.') when :const parts = [] parts << receiver if receiver parts << method_name if method_name parts.join('::') when :def, :defs "def #{method_name}" else type.to_s end end |