Class: ABNF::Repetition

Inherits:
Object
  • Object
show all
Defined in:
lib/abnf.rb

Direct Known Subclasses

LWSP, Optional

Instance Method Summary collapse

Constructor Details

#initialize(spec, what, &blk) ⇒ Repetition

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



152
153
154
155
156
# File 'lib/abnf.rb', line 152

def initialize(spec, what, &blk)
  @spec = spec
  @what = what
  @blk = blk
end

Instance Method Details

#match(strm) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# 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) ⇒ Object



158
159
160
161
# File 'lib/abnf.rb', line 158

def set_block(&blk)
  @blk = blk
  return self
end