Class: Dhaka::ProductionBuilder
- Inherits:
-
Object
- Object
- Dhaka::ProductionBuilder
- Defined in:
- lib/dhaka/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 = {}, &blk) ⇒ 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.
10 11 12 13 |
# File 'lib/dhaka/grammar/grammar.rb', line 10 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 = {}, &blk) ⇒ 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.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/dhaka/grammar/grammar.rb', line 20 def method_missing(production_name, expansion, = {}, &blk) expansion_symbols = (expansion.respond_to?('lines') ? expansion.lines : expansion).collect {|name| @grammar.symbols[name]} production_args = [@symbol, expansion_symbols, production_name.to_s, blk, @grammar.production_index] if precedence_symbol_name = [:prec] production_args << @grammar.symbol_for_name(precedence_symbol_name).precedence end production = Production.new(*production_args) @grammar.production_index += 1 @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 |