class Sequitur::Formatter::BaseText

A formatter class that can render a dynamic grammar in plain text. @example

some_grammar = ... # Points to a DynamicGrammar-like object
# Output the result to the standard console output
formatter = Sequitur::Formatter::BaseText.new(STDOUT)
# Render the grammar (through a visitor)
formatter.run(some_grammar.visitor)

Attributes

prod_lookup[R]

@return [Hash{Production => Integer}]

Public Class Methods

new(anIO) click to toggle source

Constructor. @param anIO [IO] The output stream to which the rendered grammar is written.

Calls superclass method Sequitur::Formatter::BaseFormatter::new
# File lib/sequitur/formatter/base_text.rb, line 21
def initialize(anIO)
  super(anIO)
  @prod_lookup = {}
end

Public Instance Methods

after_production(_) click to toggle source

Method called by a GrammarVisitor to which the formatter subscribed. Notification of a visit event: the visitor complete the visit of a production @param _ [Production]

# File lib/sequitur/formatter/base_text.rb, line 74
def after_production(_)
  output.print ".\n"
end
before_grammar(aGrammar) click to toggle source

Method called by a GrammarVisitor to which the formatter subscribed. Notification of a visit event: the visitor is about to visit a grammar @param aGrammar [DynamicGrammar]

# File lib/sequitur/formatter/base_text.rb, line 29
def before_grammar(aGrammar)
  aGrammar.productions.each_with_index do |a_prod, index|
    prod_lookup[a_prod] = index
  end
end
before_non_terminal(aProduction) click to toggle source

Method called by a GrammarVisitor to which the formatter subscribed. Notification of a visit event: the visitor is about to visit a non-terminal (= an allusion to a production) in the rhs of a production @param aProduction [Production] a production occurring in the rhs

# File lib/sequitur/formatter/base_text.rb, line 65
def before_non_terminal(aProduction)
  p_name = prod_name(aProduction)
  output.print " #{p_name}"
end
before_production(aProduction) click to toggle source

Method called by a GrammarVisitor to which the formatter subscribed. Notification of a visit event: the visitor is about to visit a production @param aProduction [Production]

# File lib/sequitur/formatter/base_text.rb, line 39
def before_production(aProduction)
  p_name = prod_name(aProduction)
  output.print p_name
end
before_rhs(_) click to toggle source

Method called by a GrammarVisitor to which the formatter subscribed. Notification of a visit event: the visitor is about to visit the rhs of a production @param _ [Array]

# File lib/sequitur/formatter/base_text.rb, line 48
def before_rhs(_)
  output.print ' :'
end
before_terminal(aSymbol) click to toggle source

Method called by a GrammarVisitor to which the formatter subscribed. Notification of a visit event: the visitor is about to visit a terminal symbol from the rhs of a production @param aSymbol [Object]

# File lib/sequitur/formatter/base_text.rb, line 56
def before_terminal(aSymbol)
  output.print " #{aSymbol}"
end

Private Instance Methods

prod_name(aProduction) click to toggle source

Generate a name of a given production. @param aProduction [Production] @return [String]

# File lib/sequitur/formatter/base_text.rb, line 83
def prod_name(aProduction)
  prod_index = prod_lookup[aProduction]
  prod_index.zero? ? 'start' : "P#{prod_index}"
end