Class: Dhaka::ProductionBuilder
- Inherits:
-
Object
- Object
- Dhaka::ProductionBuilder
- Defined in:
- lib/grammar/grammar.rb
Overview
Productions for specific grammar symbols are defined in the context of this class.
Instance Method Summary collapse
-
#initialize(grammar, symbol) ⇒ ProductionBuilder
constructor
symbol
is the grammar symbol that productions are being defined for. -
#method_missing(production_name, expansion, options = {}) ⇒ Object
Creates a new production for
symbol
with an expansion ofexpansion
.
Constructor Details
#initialize(grammar, symbol) ⇒ ProductionBuilder
symbol
is the grammar symbol that productions are being defined for.
13 14 15 16 |
# File 'lib/grammar/grammar.rb', line 13 def initialize(grammar, symbol) @grammar = grammar @symbol = symbol end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(production_name, expansion, options = {}) ⇒ Object
Creates a new production for symbol
with an expansion of expansion
. The options hash can include a directive :prec
, the value of which is a grammar symbol name. The precedence of the production is then set to the precedence of the grammar symbol corresponding to that name.
See the arithmetic precedence grammar in the test suites for an example.
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/grammar/grammar.rb', line 23 def method_missing(production_name, expansion, = {}) expansion_symbols = expansion.collect {|name| @grammar.symbols[name]} if precedence_symbol_name = [:prec] production = Production.new(@symbol, expansion_symbols, production_name.to_s, @grammar.symbol_for_name(precedence_symbol_name).precedence) else production = Production.new(@symbol, expansion_symbols, production_name.to_s) end @symbol.nullable = true if expansion_symbols.empty? @grammar.productions_by_symbol[production.symbol] << production raise "Duplicate production named #{production.name}" if @grammar.productions_by_name[production.name] @grammar.productions_by_name[production.name] = production end |