Class: Packrat::Rule

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

Overview

A grammar Rule is a set of one or more Productions for the same (lhs) nonterminal. It makes an ordered choice between its productions by trying to parse with them in order.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, prods = []) ⇒ Rule

Returns a new instance of Rule.



81
82
83
# File 'lib/packrat/grammar.rb', line 81

def initialize(name, prods = [])
  @name, @prods = name, prods
end

Instance Attribute Details

#grammarObject

Returns the value of attribute grammar.



80
81
82
# File 'lib/packrat/grammar.rb', line 80

def grammar
  @grammar
end

#nameObject (readonly)

Returns the value of attribute name.



80
81
82
# File 'lib/packrat/grammar.rb', line 80

def name
  @name
end

#prodsObject (readonly)

Returns the value of attribute prods.



80
81
82
# File 'lib/packrat/grammar.rb', line 80

def prods
  @prods
end

Instance Method Details

#<<(prod) ⇒ Object



88
89
90
# File 'lib/packrat/grammar.rb', line 88

def <<(prod)
  @prods << prod
end

#inspectObject



91
92
93
94
95
96
# File 'lib/packrat/grammar.rb', line 91

def inspect
  s = "#{name.to_s} ->"
  "\n" + s + " " +
    @prods.map {|p| p.inspect(false)}.join("\n" + 
                                           " " * (s.length - 1) + "| ")
end

#parse(parser) ⇒ Object



555
556
557
558
559
560
561
562
563
# File 'lib/packrat/grammar.rb', line 555

def parse(parser)
  oldpos = parser.pos
  prods.each do |prod|
    res = prod.parse(parser)
    return res unless false == res
    parser.pos = oldpos
  end
  return false
end