Class: TreeStack

Inherits:
Object
  • Object
show all
Defined in:
lib/pseudohiki/treestack.rb

Defined Under Namespace

Modules: LeafType, Mergeable, NodeType, TreeElement Classes: Leaf, Node, NodeEnd, NotLeafError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_node = Node.new) ⇒ TreeStack

Returns a new instance of TreeStack.



64
65
66
67
68
69
# File 'lib/pseudohiki/treestack.rb', line 64

def initialize(root_node=Node.new)
  @stack = [root_node]
  @current_node = root_node # @stack[-1]
  @node_end = NodeEnd.new
  root_node.depth = 0
end

Instance Attribute Details

#current_nodeObject (readonly)

Returns the value of attribute current_node.



62
63
64
# File 'lib/pseudohiki/treestack.rb', line 62

def current_node
  @current_node
end

#last_leafObject (readonly)

Returns the value of attribute last_leaf.



62
63
64
# File 'lib/pseudohiki/treestack.rb', line 62

def last_leaf
  @last_leaf
end

#node_endObject (readonly)

Returns the value of attribute node_end.



62
63
64
# File 'lib/pseudohiki/treestack.rb', line 62

def node_end
  @node_end
end

Instance Method Details

#accept(visitor, memo = nil) ⇒ Object



110
111
112
# File 'lib/pseudohiki/treestack.rb', line 110

def accept(visitor, memo=nil)
  visitor.visit(tree, memo)
end

#popObject Also known as: return_to_previous_node



80
81
82
83
84
# File 'lib/pseudohiki/treestack.rb', line 80

def pop
  return unless @stack.length > 1
  @current_node = @stack[-2]
  @stack.pop
end

#push(node = Node.new) ⇒ Object



75
76
77
78
# File 'lib/pseudohiki/treestack.rb', line 75

def push(node=Node.new)
  @last_leaf = node.push_self(self)
  node
end

#push_as_child_node(node) ⇒ Object



87
88
89
90
91
# File 'lib/pseudohiki/treestack.rb', line 87

def push_as_child_node(node)
  @current_node.push node
  @current_node = node
  @stack.push node
end

#push_as_leaf(node) ⇒ Object



93
94
95
# File 'lib/pseudohiki/treestack.rb', line 93

def push_as_leaf(node)
  @current_node.push node
end

#push_as_sibling(sibling_node = nil) ⇒ Object



97
98
99
100
101
102
# File 'lib/pseudohiki/treestack.rb', line 97

def push_as_sibling(sibling_node=nil)
  sibling_node ||= @current_node.class.new
  pop if sibling_node.kind_of? NodeType
  push(sibling_node)
  sibling_node
end

#remove_current_nodeObject



104
105
106
107
108
# File 'lib/pseudohiki/treestack.rb', line 104

def remove_current_node
  removed_node = pop
  @current_node.pop
  removed_node
end

#treeObject



71
72
73
# File 'lib/pseudohiki/treestack.rb', line 71

def tree
  @stack[0]
end