Class: Fabulator::Grammar::Expr::Rule
- Inherits:
-
Object
- Object
- Fabulator::Grammar::Expr::Rule
- Defined in:
- lib/fabulator/grammar/expr/rule.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #add_alternative(a) ⇒ Object
-
#initialize ⇒ Rule
constructor
A new instance of Rule.
- #parse(s) ⇒ Object
Constructor Details
#initialize ⇒ Rule
Returns a new instance of Rule.
7 8 9 |
# File 'lib/fabulator/grammar/expr/rule.rb', line 7 def initialize @alternatives = [ ] end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/fabulator/grammar/expr/rule.rb', line 5 def name @name end |
Instance Method Details
#add_alternative(a) ⇒ Object
11 12 13 |
# File 'lib/fabulator/grammar/expr/rule.rb', line 11 def add_alternative(a) @alternatives << a end |
#parse(s) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/fabulator/grammar/expr/rule.rb', line 15 def parse(s) if s.anchored @alternatives.each do |alternative| ret = s.attempt { |cursor| cursor.anchored = true alternative.parse(cursor) } if !ret.nil? s.set_result(ret) return end end else while !s.eof? start = s.pos @alternatives.each do |alternative| ret = s.attempt { |cursor| cursor.anchored = true alternative.parse(cursor) } if !ret.nil? s.start = start s.set_result(ret) return end end s.advance_position(1) end end raise Fabulator::Grammar::RejectParse end |