Class: Rattler::Util::Node
- Inherits:
-
Object
- Object
- Rattler::Util::Node
- Includes:
- Enumerable
- Defined in:
- lib/rattler/util/node.rb
Overview
A Node
is a node that can be used as-is to compose tree structures or as a base class for nodes. It is the base class used for all tree structures in the Rattler
framework.
Direct Known Subclasses
Grammar::Grammar, Parsers::Parser, Parsers::Rule, Parsers::RuleSet, Runtime::ParseNode, Runtime::Parser
Class Method Summary collapse
-
.[](*args) ⇒ Node
Create a
Node
object.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Return
true
if the node is equal toother
. -
#[](*args) ⇒ Object
Access the node’s children as if the node were an array of its children.
-
#attrs ⇒ Hash
Return a the node’s attributes.
- #can_equal?(other) ⇒ Boolean
-
#child(index = 0) ⇒ Object
Return the node’s child at
index
, or the first/only child if no index is given. -
#children ⇒ Array
Return an array of the node’s children.
-
#each {|child| ... } ⇒ Object
Call block once for each child, passing that child as an argument.
-
#empty? ⇒ Boolean
Return
true
if the node has no children. -
#eql?(other) ⇒ Boolean
Return
true
if the node has the same value asother
, i.e. -
#initialize(*args) ⇒ Node
constructor
Create a
Node
object. - #inspect ⇒ Object
-
#method_missing(symbol, *args) ⇒ Object
Allow attributes to be accessed as methods.
-
#name ⇒ Object
Return the node’s name, which is the node’s
name
attribute if it has one, otherwise the name of the node’s class. - #respond_to?(symbol) ⇒ Boolean
- #same_contents?(other) ⇒ Boolean
- #to_graphviz ⇒ Object
- #with_attrs(new_attrs) ⇒ Object
- #with_attrs!(new_attrs) ⇒ Object
- #with_children(new_children) ⇒ Object (also: #with_child)
Constructor Details
#initialize ⇒ Node #initialize(child...) ⇒ Node #initialize(attribute...) ⇒ Node #initialize(child..., attribute...) ⇒ Node
Create a Node
object.
46 47 48 49 |
# File 'lib/rattler/util/node.rb', line 46 def initialize(*args) @attrs = args.last.respond_to?(:to_hash) ? args.pop : {} @__children__ = args end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args) ⇒ Object
Allow attributes to be accessed as methods.
161 162 163 |
# File 'lib/rattler/util/node.rb', line 161 def method_missing(symbol, *args) (args.empty? and attrs.has_key?(symbol)) ? attrs[symbol] : super end |
Class Method Details
Instance Method Details
#==(other) ⇒ Boolean
Return true
if the node is equal to other
. Normally this means other
is an instance of the same class or a subclass and has equal children and attributes.
145 146 147 148 149 |
# File 'lib/rattler/util/node.rb', line 145 def ==(other) #:nodoc: self.class === other and other.can_equal?(self) and self.same_contents?(other) end |
#[](index) ⇒ Object #[](start, length) ⇒ Array #[](range) ⇒ Array
Access the node’s children as if the node were an array of its children.
136 137 138 |
# File 'lib/rattler/util/node.rb', line 136 def [](*args) children[*args] end |
#attrs ⇒ Hash
Return a the node’s attributes.
79 80 81 |
# File 'lib/rattler/util/node.rb', line 79 def attrs @attrs ||= {} end |
#can_equal?(other) ⇒ Boolean
171 172 173 |
# File 'lib/rattler/util/node.rb', line 171 def can_equal?(other) #:nodoc: self.class == other.class end |
#child(index = 0) ⇒ Object
Return the node’s child at index
, or the first/only child if no index is given.
72 73 74 |
# File 'lib/rattler/util/node.rb', line 72 def child(index = 0) children[index] end |
#children ⇒ Array
Return an array of the node’s children
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rattler/util/node.rb', line 54 def children @children ||= if @__children__ if @__children__.size == 1 && @__children__.first.respond_to?(:to_ary) @__children__.first else @__children__ end else [] end end |
#each {|child| ... } ⇒ Object
Call block once for each child, passing that child as an argument.
94 95 96 |
# File 'lib/rattler/util/node.rb', line 94 def each # :yield: child block_given? ? children.each { |_| yield _ } : children.each end |
#empty? ⇒ Boolean
Return true
if the node has no children.
115 116 117 |
# File 'lib/rattler/util/node.rb', line 115 def empty? children.empty? end |
#eql?(other) ⇒ Boolean
Return true
if the node has the same value as other
, i.e. other
is an instance of the same class and has equal children and attributes.
155 156 157 158 |
# File 'lib/rattler/util/node.rb', line 155 def eql?(other) self.class == other.class and self.same_contents?(other) end |
#inspect ⇒ Object
182 183 184 185 186 187 |
# File 'lib/rattler/util/node.rb', line 182 def inspect #:nodoc: "#{self.class}[" + (children.map {|_| _.inspect } + attrs.map {|k, v| k.inspect + '=>' + v.inspect}).join(',') + ']' end |
#name ⇒ Object
Return the node’s name, which is the node’s name
attribute if it has one, otherwise the name of the node’s class.
87 88 89 |
# File 'lib/rattler/util/node.rb', line 87 def name attrs.fetch(:name, self.class.name) end |
#respond_to?(symbol) ⇒ Boolean
166 167 168 |
# File 'lib/rattler/util/node.rb', line 166 def respond_to?(symbol) #:nodoc: super || @attrs.has_key?(symbol) end |
#same_contents?(other) ⇒ Boolean
176 177 178 179 |
# File 'lib/rattler/util/node.rb', line 176 def same_contents?(other) #:nodoc: self.children == other.children and self.attrs == other.attrs end |
#to_graphviz ⇒ Object
189 190 191 |
# File 'lib/rattler/util/node.rb', line 189 def to_graphviz Rattler::Util::GraphViz.digraph(self) end |
#with_attrs(new_attrs) ⇒ Object
104 105 106 |
# File 'lib/rattler/util/node.rb', line 104 def with_attrs(new_attrs) self.with_attrs!(attrs.merge new_attrs) end |
#with_attrs!(new_attrs) ⇒ Object
108 109 110 |
# File 'lib/rattler/util/node.rb', line 108 def with_attrs!(new_attrs) self.class.new(children, new_attrs) end |
#with_children(new_children) ⇒ Object Also known as: with_child
98 99 100 |
# File 'lib/rattler/util/node.rb', line 98 def with_children(new_children) self.class.new(new_children, attrs) end |