Class: Sequitur::Formatter::BaseText

Inherits:
BaseFormatter show all
Defined in:
lib/sequitur/formatter/base_text.rb

Overview

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

Examples:

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)

Instance Attribute Summary collapse

Attributes inherited from BaseFormatter

#output

Instance Method Summary collapse

Methods inherited from BaseFormatter

#render

Constructor Details

#initialize(anIO) ⇒ BaseText

Constructor. is written.

Parameters:

  • anIO (IO)

    The output stream to which the rendered grammar


21
22
23
24
# File 'lib/sequitur/formatter/base_text.rb', line 21

def initialize(anIO)
  super(anIO)
  @prod_lookup = {}
end

Instance Attribute Details

#prod_lookupHash{Production => Integer} (readonly)

Returns:


16
17
18
# File 'lib/sequitur/formatter/base_text.rb', line 16

def prod_lookup
  @prod_lookup
end

Instance Method Details

#after_production(_) ⇒ Object

Method called by a GrammarVisitor to which the formatter subscribed. Notification of a visit event: the visitor complete the visit of a production

Parameters:


74
75
76
# File 'lib/sequitur/formatter/base_text.rb', line 74

def after_production(_)
  output.print ".\n"
end

#before_grammar(aGrammar) ⇒ Object

Method called by a GrammarVisitor to which the formatter subscribed. Notification of a visit event: the visitor is about to visit a grammar

Parameters:


29
30
31
32
33
# 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) ⇒ Object

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

Parameters:

  • aProduction (Production)

    a production occurring in the rhs


65
66
67
68
# 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) ⇒ Object

Method called by a GrammarVisitor to which the formatter subscribed. Notification of a visit event: the visitor is about to visit a production

Parameters:


39
40
41
42
# 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(_) ⇒ Object

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

Parameters:

  • _ (Array)

48
49
50
# File 'lib/sequitur/formatter/base_text.rb', line 48

def before_rhs(_)
  output.print ' :'
end

#before_terminal(aSymbol) ⇒ Object

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

Parameters:

  • aSymbol (Object)

56
57
58
# File 'lib/sequitur/formatter/base_text.rb', line 56

def before_terminal(aSymbol)
  output.print " #{aSymbol}"
end