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.

Parameters:

  • anIO (IO)

    The output stream to which the rendered grammar



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

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

Parameters:

  • _nonterm (NonTerminalNode)


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

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)


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

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)


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

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)


35
36
37
# File 'lib/rley/formatter/bracket_notation.rb', line 35

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