Class: Peggy::Sequence

Inherits:
Element show all
Defined in:
lib/builder.rb,
lib/Copy of 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.



38
39
40
# File 'lib/builder.rb', line 38

def [] index
  @list[index]
end

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

Add a child element.



29
30
31
32
# File 'lib/builder.rb', line 29

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

#each(&blk) ⇒ Object

Child iterator.



43
44
45
# File 'lib/builder.rb', line 43

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.



49
50
51
52
53
54
55
56
# File 'lib/builder.rb', line 49

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