Class: Rley::Formatter::Asciitree

Inherits:
BaseFormatter show all
Defined in:
lib/rley/formatter/asciitree.rb

Overview

A formatter class that draws parse trees by using characters

Instance Attribute Summary collapse

Attributes inherited from BaseFormatter

#output

Instance Method Summary collapse

Methods inherited from BaseFormatter

#render

Constructor Details

#initialize(anIO) ⇒ Asciitree

Constructor. is written.

Parameters:

  • anIO (IO)

    The output stream to which the parse tree



30
31
32
33
34
35
36
37
38
# File 'lib/rley/formatter/asciitree.rb', line 30

def initialize(anIO)
  super(anIO)
  @curr_path = []
  @ranks = []

  @nesting_prefix = '+-- '
  @blank_indent = '    '
  @continuation_indent = '|   '
end

Instance Attribute Details

#blank_indentString (readonly)

Returns The character pattern used for a blank indentation.

Returns:

  • (String)

    The character pattern used for a blank indentation



21
22
23
# File 'lib/rley/formatter/asciitree.rb', line 21

def blank_indent
  @blank_indent
end

#continuation_indentString (readonly)

continuation.

Returns:

  • (String)

    The character pattern for indentation and nesting



25
26
27
# File 'lib/rley/formatter/asciitree.rb', line 25

def continuation_indent
  @continuation_indent
end

#curr_pathObject (readonly)

TODO



10
11
12
# File 'lib/rley/formatter/asciitree.rb', line 10

def curr_path
  @curr_path
end

#nesting_prefixString (readonly)

a parent - child nesting

Returns:

  • (String)

    The character pattern used for rendering



18
19
20
# File 'lib/rley/formatter/asciitree.rb', line 18

def nesting_prefix
  @nesting_prefix
end

#ranksObject (readonly)

For each node in curr_path, there is a corresponding string value. Allowed string values are: 'first', 'last', 'first_and_last', 'other'



14
15
16
# File 'lib/rley/formatter/asciitree.rb', line 14

def ranks
  @ranks
end

Instance Method Details

#after_subnodes(_parent, _children) ⇒ Object

Method called by a ParseTreeVisitor to which the formatter subscribed. Notification of a visit event: the visitor completed the visit of the children of a non-terminal node.

Parameters:

  • _parent (NonTerminalNode)
  • _children (Array)

    array of children nodes



71
72
73
74
# File 'lib/rley/formatter/asciitree.rb', line 71

def after_subnodes(_parent, _children)
  curr_path.pop
  ranks.pop
end

#before_non_terminal(aNonTerm) ⇒ Object

Method called by a ParseTreeVisitor to which the formatter subscribed. Notification of a visit event: the visitor is about to visit a non-terminal node

Parameters:

  • aNonTerm (NonTerminalNode)


54
55
56
# File 'lib/rley/formatter/asciitree.rb', line 54

def before_non_terminal(aNonTerm)
  emit(aNonTerm)
end

#before_subnodes(parent, _children) ⇒ Object

Method called by a ParseTreeVisitor to which the formatter subscribed. Notification of a visit event: the visitor is about to visit the children of a non-terminal node

Parameters:

  • parent (NonTerminalNode)
  • _children (Array<ParseTreeNode>)

    array of children nodes



45
46
47
48
# File 'lib/rley/formatter/asciitree.rb', line 45

def before_subnodes(parent, _children)
  rank_of(parent)
  curr_path << parent
end

#before_terminal(aTerm) ⇒ Object

Method called by a ParseTreeVisitor to which the formatter subscribed. Notification of a visit event: the visitor is about to visit a terminal node

Parameters:

  • aTerm (TerminalNode)


62
63
64
# File 'lib/rley/formatter/asciitree.rb', line 62

def before_terminal(aTerm)
  emit(aTerm, ": '#{aTerm.token.lexeme}'")
end