Class: Rley::PTree::NonTerminalNode
- Inherits:
-
ParseTreeNode
- Object
- ParseTreeNode
- Rley::PTree::NonTerminalNode
- Defined in:
- lib/rley/ptree/non_terminal_node.rb
Instance Attribute Summary collapse
-
#subnodes ⇒ Object
readonly
Array of sub-nodes.
Attributes inherited from ParseTreeNode
Instance Method Summary collapse
-
#accept(aVisitor) ⇒ Object
Part of the 'visitee' role in Visitor design pattern.
-
#add_subnode(aSubnode) ⇒ Object
Pre-pend the given subnode in front of the list of subnodes.
-
#initialize(aSymbol, aRange) ⇒ NonTerminalNode
constructor
A new instance of NonTerminalNode.
-
#to_string(indentation) ⇒ Object
Emit a (formatted) string representation of the node.
Methods inherited from ParseTreeNode
Constructor Details
#initialize(aSymbol, aRange) ⇒ NonTerminalNode
Returns a new instance of NonTerminalNode.
11 12 13 14 |
# File 'lib/rley/ptree/non_terminal_node.rb', line 11 def initialize(aSymbol, aRange) super(aSymbol, aRange) @subnodes = [] end |
Instance Attribute Details
#subnodes ⇒ Object (readonly)
Array of sub-nodes.
9 10 11 |
# File 'lib/rley/ptree/non_terminal_node.rb', line 9 def subnodes @subnodes end |
Instance Method Details
#accept(aVisitor) ⇒ Object
Part of the 'visitee' role in Visitor design pattern.
39 40 41 |
# File 'lib/rley/ptree/non_terminal_node.rb', line 39 def accept(aVisitor) aVisitor.visit_nonterminal(self) end |
#add_subnode(aSubnode) ⇒ Object
Pre-pend the given subnode in front of the list of subnodes
18 19 20 |
# File 'lib/rley/ptree/non_terminal_node.rb', line 18 def add_subnode(aSubnode) subnodes.unshift(aSubnode) end |
#to_string(indentation) ⇒ Object
Emit a (formatted) string representation of the node. Mainly used for diagnosis/debugging purposes. rubocop: disable Style/StringConcatenation
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/rley/ptree/non_terminal_node.rb', line 25 def to_string(indentation) connector = '+- ' selfie = super(indentation) prefix = "\n" + (' ' * connector.size * indentation) + connector subnodes_repr = subnodes.reduce(+'') do |sub_result, subnode| sub_result << prefix + subnode.to_string(indentation + 1) end selfie + subnodes_repr end |