Class: TreePrintVisitor

Inherits:
Object
  • Object
show all
Defined in:
lib/visitor/tree_print_visitor.rb

Constant Summary collapse

ALL_LEVELS =
-1

Instance Method Summary collapse

Constructor Details

#initialize(dataSource, io, level = ALL_LEVELS) ⇒ TreePrintVisitor

Returns a new instance of TreePrintVisitor.



4
5
6
7
8
9
# File 'lib/visitor/tree_print_visitor.rb', line 4

def initialize(dataSource, io, level=ALL_LEVELS)
  @indentation = 0
  @dataSource = dataSource
  @io = io
  @level = level
end

Instance Method Details

#nodeToStr(node) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/visitor/tree_print_visitor.rb', line 11

def nodeToStr(node)
  if (node.isRoot) then
    "ROOT"
  else
    "#{@dataSource.toString(node.incomingEdgeStartOffset, node.incomingEdgeEndOffset)}"
  end
end

#postVisit(node) ⇒ Object



29
30
31
# File 'lib/visitor/tree_print_visitor.rb', line 29

def postVisit(node)
  @indentation -= 1
end

#preVisit(node) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/visitor/tree_print_visitor.rb', line 19

def preVisit(node)
  @io.print "#{" "*@indentation}#{self.nodeToStr(node)}\n"
  if (@level == ALL_LEVELS) || (@indentation < @level) then
    @indentation += 1
    return true
  else
    return false
  end
end