Class: Fabulator::Grammar::Expr::RuleAlternative
- Inherits:
-
Object
- Object
- Fabulator::Grammar::Expr::RuleAlternative
- Defined in:
- lib/fabulator/grammar/expr/rule_alternative.rb
Instance Method Summary collapse
- #add_sequence(s) ⇒ Object
-
#initialize ⇒ RuleAlternative
constructor
A new instance of RuleAlternative.
- #parse(source) ⇒ Object
Constructor Details
#initialize ⇒ RuleAlternative
Returns a new instance of RuleAlternative.
5 6 7 |
# File 'lib/fabulator/grammar/expr/rule_alternative.rb', line 5 def initialize @sequences = [ ] end |
Instance Method Details
#add_sequence(s) ⇒ Object
9 10 11 12 |
# File 'lib/fabulator/grammar/expr/rule_alternative.rb', line 9 def add_sequence(s) s = [ s ] unless s.is_a?(Array) @sequences += s end |
#parse(source) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/fabulator/grammar/expr/rule_alternative.rb', line 14 def parse(source) results = [ ] @sequences.each do |sequence| res = source.attempt{ |s| sequence.parse(s) } raise Fabulator::Grammar::RejectParse if res.nil? res.children.each do |c| if c.name.nil? c.name = sequence.name if c.name.nil? && !sequence.name.nil? res.prune(c) results << c if !c.children.empty? || !c.value.nil? end end res.name = sequence.name unless sequence.name.nil? results << res if !res.children.empty? || !res.value.nil? end # now we want to merge all of the results if results.size > 1 results = results.select{ |r| !r.name.nil? } end if !results.empty? source.set_result(results) end end |