lib/sequitur/formatter/base_text.rb in sequitur-0.1.24 vs lib/sequitur/formatter/base_text.rb in sequitur-0.1.25
- old
+ new
@@ -10,10 +10,11 @@
# # 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)
class BaseText < BaseFormatter
+ # @return [Hash{Production => Integer}]
attr_reader :prod_lookup
# Constructor.
# @param anIO [IO] The output stream to which the rendered grammar
# is written.
@@ -22,21 +23,21 @@
@prod_lookup = {}
end
# 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-like object]
+ # @param aGrammar [DynamicGrammar]
def before_grammar(aGrammar)
aGrammar.productions.each_with_index do |a_prod, index|
prod_lookup[a_prod] = index
end
end
# 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 [aProduction]
+ # @param aProduction [Production]
def before_production(aProduction)
p_name = prod_name(aProduction)
output.print p_name
end
@@ -67,17 +68,19 @@
end
# 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]
def after_production(_)
output.print ".\n"
end
private
# Generate a name of a given production.
# @param aProduction [Production]
+ # @return [String]
def prod_name(aProduction)
prod_index = prod_lookup[aProduction]
prod_index.zero? ? 'start' : "P#{prod_index}"
end
end # class