Class: Peggy::Sequence

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

Overview

An element that matches a sequence of elements. All must match for the sequence to match.

Direct Known Subclasses

Alternatives

Instance Method Summary collapse

Methods inherited from Element

build, #report

Instance Method Details

#[](index) ⇒ Object

Reference a child by index.



65
66
67
# File 'lib/parse/builder.rb', line 65

def [] index
  @list[index]
end

#add(element) ⇒ Object Also known as: <<

Add a child element.



56
57
58
59
# File 'lib/parse/builder.rb', line 56

def add element
  @list = [] unless @list
  @list << element
end

#each(&blk) ⇒ Object

Child iterator.



70
71
72
# File 'lib/parse/builder.rb', line 70

def each &blk
  @list.each &blk
end

#match(parser, index) ⇒ Object

Match each child in sequence. If any fail this returns NO_MATCH. If all succeed this returns the end index of the last.



76
77
78
79
80
81
82
83
# File 'lib/parse/builder.rb', line 76

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

#to_sObject

Convert element to String.



86
87
88
# File 'lib/parse/builder.rb', line 86

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