Class: Rley::PTree::NonTerminalNode

Inherits:
ParseTreeNode show all
Defined in:
lib/rley/ptree/non_terminal_node.rb

Instance Attribute Summary collapse

Attributes inherited from ParseTreeNode

#range, #symbol

Instance Method Summary collapse

Methods inherited from ParseTreeNode

#done!, #to_s

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

#subnodesObject (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.

Parameters:



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

Parameters:



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