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

#initialize, #render

Constructor Details

This class inherits a constructor from Rley::Formatter::BaseFormatter

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

Parameters:

  • _nonterm (NonTerminalNode)


47
48
49
# File 'lib/rley/formatter/bracket_notation.rb', line 47

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.

Parameters:

  • aTerm (TerminalNode)


36
37
38
39
40
41
# File 'lib/rley/formatter/bracket_notation.rb', line 36

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

Parameters:

  • aNonTerm (NonTerminalNode)


20
21
22
# File 'lib/rley/formatter/bracket_notation.rb', line 20

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

Parameters:

  • aTerm (TerminalNode)


28
29
30
# File 'lib/rley/formatter/bracket_notation.rb', line 28

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