Class: Rley::Formatter::BracketNotation

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

Overview

A formatter class that generates the labelled bracket notation (LBN) representation of a parse tree. The output can be then fed to an application or library that is capable of displaying parse tree diagrams. For Ruby developers, there is RSyntaxTree by Yoichiro Hasebe. (accessible via: http://yohasebe.com/rsyntaxtree/)

Instance Attribute Summary

Attributes inherited from BaseFormatter

#output

Instance Method Summary collapse

Methods inherited from BaseFormatter

#render

Constructor Details

#initialize(anIO) ⇒ BracketNotation

Constructor. is written.



17
18
19
# File 'lib/rley/formatter/bracket_notation.rb', line 17

def initialize(anIO)
  super(anIO)
end

Instance Method Details

#after_non_terminal(_nonterm) ⇒ Object

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



52
53
54
# File 'lib/rley/formatter/bracket_notation.rb', line 52

def after_non_terminal(_nonterm)
  write(']')
end

#after_terminal(aTerm) ⇒ Object

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



41
42
43
44
45
46
# File 'lib/rley/formatter/bracket_notation.rb', line 41

def after_terminal(aTerm)
  # Escape all opening and closing square brackets
  escape_lbrackets = aTerm.token.lexeme.gsub(/\[/, '\[')
  escaped = escape_lbrackets.gsub(/\]/, '\]')
  write(escaped + ']')
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



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

def before_non_terminal(aNonTerm)
  write("[#{aNonTerm.symbol.name} ")
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



33
34
35
# File 'lib/rley/formatter/bracket_notation.rb', line 33

def before_terminal(aTerm)
  write("[#{aTerm.symbol.name} ")
end