Class: Packrat::Production

Inherits:
Object
  • Object
show all
Defined in:
lib/packrat/grammar.rb,
lib/packrat/grammar.rb

Overview

A grammar Production is sequence of rhs elements describing how the lhs symbol should be parsed.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, rhs) ⇒ Production

Returns a new instance of Production.



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/packrat/grammar.rb', line 104

def initialize(name, rhs)
  @name, @rhs = name, rhs
  if Packrat::ResultProducer === rhs.last
    @result_producer = @rhs.pop
  else
    # Default producer is to create a Sexpr with the production name
    # as the head of the returned array.
    @result_producer = Packrat::SexprProducer.new(@name)
  end
  @rhs.map! {|e| e.to_packrat_grammar_element}
end

Instance Attribute Details

#grammarObject

Returns the value of attribute grammar.



102
103
104
# File 'lib/packrat/grammar.rb', line 102

def grammar
  @grammar
end

#nameObject (readonly)

Returns the value of attribute name.



103
104
105
# File 'lib/packrat/grammar.rb', line 103

def name
  @name
end

#result_producerObject (readonly)

Returns the value of attribute result_producer.



103
104
105
# File 'lib/packrat/grammar.rb', line 103

def result_producer
  @result_producer
end

#rhsObject (readonly)

Returns the value of attribute rhs.



103
104
105
# File 'lib/packrat/grammar.rb', line 103

def rhs
  @rhs
end

Instance Method Details

#finalize!Object



115
116
117
# File 'lib/packrat/grammar.rb', line 115

def finalize!
  @result_producer.production = self
end

#inspect(withLhs = true) ⇒ Object



118
119
120
121
# File 'lib/packrat/grammar.rb', line 118

def inspect(withLhs = true)
  rhs = @rhs.map {|e| e.hidden ? nil : e.inspect}.compact.join(' ')
  withLhs ? "#{name.to_s} -> " + rhs : rhs
end

#parse(parser) ⇒ Object



538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'lib/packrat/grammar.rb', line 538

def parse(parser)
  res = @result_producer.new_result
  nonhidden_index = 0
  @rhs.each_with_index do |e, i|
    subres = e.parse(parser)
    return false if false == subres
    unless e.hidden
      res = @result_producer.update_result(res, subres, e, 
                                           i, nonhidden_index)
      nonhidden_index += 1
    end
  end
  return @result_producer.finalize_result(res)
end