class MetaParse::RepetitionMatcher

Matcher subclass matching sub_match repeatedly.

Attributes

max[RW]

Match at most max times, if max is present.

min[RW]

Match at least min times, if min is present.

reducer[RW]

A proc used to combine match results as by inject, if present.

Public Class Methods

new(sub_match, min=0, max=nil, reducer=nil, initial_value=[]) click to toggle source
# File lib/meta_parse.rb, line 265
def initialize(sub_match, min=0, max=nil, reducer=nil, initial_value=[])
  @spec, @min, @max, @reducer, @initial_value = sub_match, min, max, reducer, initial_value
end

Public Instance Methods

match?(scanner, context=nil) click to toggle source
unless min && (matches.count < min)
  case finalizer
  when Proc
    finalizer.call(matches, *finalizer_args)
  when Symbol
    send(finalizer, matches, *finalizer_args)
  when nil 
    matches
  end
end

end

# File lib/meta_parse.rb, line 295
def match?(scanner, context=nil)
  # Need to copy the initial value since it is potentially destructively modified (if an array, for example).
  acc = begin @initial_value.dup
        rescue TypeError
          @initial_value
        end
  match_count = 0

  while (!max || (match_count < max)) && (one_match = spec.match(scanner))
    match_count += 1
    if reducer
      acc = reducer.call(acc, one_match)
    else
      acc << one_match
    end
  end
  unless min && (match_count < min)
    acc
  end
end
show() click to toggle source
# File lib/meta_parse.rb, line 316
def show
  "[#{min}, #{max}] #{ spec.show }"
end
stateful() click to toggle source

RepetitionMatcher is stateful.

# File lib/meta_parse.rb, line 272
def stateful
  true
end