Class: Peggy::Multiple

Inherits:
Element show all
Defined in:
lib/builder.rb,
lib/Copy of builder.rb

Overview

An element which tries its single child multiple times. It is greedy, meaning it will continue to match as long as possible, unless the range specifies a maximum number of matches.

Direct Known Subclasses

AnyNumber, AtLeastOne, Optional

Constant Summary collapse

MANY =

A big number

32767

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Element

build, #report

Constructor Details

#initialize(range) ⇒ Multiple

Init the range



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

def initialize range
  @range = range
end

Instance Attribute Details

#childObject

The single child



82
83
84
# File 'lib/builder.rb', line 82

def child
  @child
end

#rangeObject

The minimum and maximum number of tries



80
81
82
# File 'lib/builder.rb', line 80

def range
  @range
end

Instance Method Details

#match(parser, index) ⇒ Object

Matches the child multiple times. The range specifies the least and most number of matches. If the number of matches is less than the minimim of the range then NO_MATCH is returned. If equal or more than the minimim then the end index of the last match is returned.



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/builder.rb', line 95

def match parser, index
  raise "multiple element child not set" unless child
  raise "multiple element range not set" unless range
  count = 0
  while count < range.last
    found = child.match parser, index
    break unless found
    index = found
    count += 1
  end
  report range === count ? index : NO_MATCH
end