class ABNF::Repetition

Public Class Methods

new(spec, what, &blk) click to toggle source

Spec: range (between), integer (exact), [:at_most, N], [:at_least, N], :any (zero or more)

# File lib/abnf.rb, line 152
def initialize(spec, what, &blk)
  @spec = spec
  @what = what
  @blk = blk
end

Public Instance Methods

match(strm) click to toggle source
# File lib/abnf.rb, line 163
def match(strm)
  c_strm = strm
  start = strm.pos

  r = \
    case @spec
    when Array # :at_least, :at_most
      option, i = @spec

      if option == :at_most
        (0..i)
      elsif option == :at_least
        RangeWithInfiniteUpperBound.new(i)
      end
    when Integer # Exact
      @spec..@spec
    when ::Range # Between
      @spec
    when Symbol # Any (zero or more)
      RangeWithInfiniteUpperBound.new(0)
    end

  r.until_end {
    |i|

    tried = @what.match(c_strm)

    if tried.nil?
      if i < r.first
        return nil
      else
        break
      end
    else
      c_strm = tried
    end
  }

  @blk.call(c_strm.clip(start)) unless @blk.nil?

  c_strm
end
set_block(&blk) click to toggle source
# File lib/abnf.rb, line 158
def set_block(&blk)
  @blk = blk
  return self
end