class Peggy::Multiple

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.

Constants

MANY

A big number

Attributes

range[RW]

The minimum and maximum number of tries

Public Class Methods

new(range) click to toggle source

Init the range

# File lib/parse/builder.rb, line 122
def initialize range
  @range = range
end

Public Instance Methods

match(parser, index) click to toggle source

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.

# File lib/parse/builder.rb, line 129
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
to_s() click to toggle source

Convert element to String.

# File lib/parse/builder.rb, line 143
def to_s
  "#{wrap}{#{range.min}..#{range.max}}"
end