Class: Packrat::Rule
- Inherits:
-
Object
- Object
- Packrat::Rule
- 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
-
#grammar ⇒ Object
Returns the value of attribute grammar.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#prods ⇒ Object
readonly
Returns the value of attribute prods.
Instance Method Summary collapse
- #<<(prod) ⇒ Object
-
#initialize(name, prods = []) ⇒ Rule
constructor
A new instance of Rule.
- #inspect ⇒ Object
- #parse(parser) ⇒ Object
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
#grammar ⇒ Object
Returns the value of attribute grammar.
80 81 82 |
# File 'lib/packrat/grammar.rb', line 80 def grammar @grammar end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
80 81 82 |
# File 'lib/packrat/grammar.rb', line 80 def name @name end |
#prods ⇒ Object (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 |
#inspect ⇒ Object
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 |