Class: Peggy::Alternatives

Inherits:
Sequence show all
Defined in:
lib/parse/builder.rb

Overview

An element which matches any one of its children. The children are tested in order. The first to match wins.

Instance Method Summary collapse

Methods inherited from Sequence

#[], #add, #each

Methods inherited from Element

build, #report

Instance Method Details

#match(parser, index) ⇒ Object

Match any one of the children. The children are tried in order. The first to match wins. The result is the end index of the first matching child. If none match this returns NO_MATCH.



96
97
98
99
100
101
102
103
# File 'lib/parse/builder.rb', line 96

def match parser, index
  raise "no children added to alternate" unless @list
  each do |element|
    found = element.match parser, index
    return report(found) if found
  end
  report NO_MATCH
end

#to_sObject

Convert element to String.



106
107
108
# File 'lib/parse/builder.rb', line 106

def to_s
  @list.map{|el| el.to_s}.join ' | '
end