Class: KPeg::Multiple

Inherits:
Operator show all
Defined in:
lib/kpeg/grammar.rb

Instance Attribute Summary (collapse)

Attributes inherited from Operator

#action

Instance Method Summary (collapse)

Methods inherited from Operator

#detect_tags, #inspect_type, #prune_values, #set_action, #|

Constructor Details

- (Multiple) initialize(op, min, max)

A new instance of Multiple



238
239
240
241
242
243
244
# File 'lib/kpeg/grammar.rb', line 238

def initialize(op, min, max)
  super()
  @op = op
  @min = min
  @max = max
  @save_values = nil
end

Instance Attribute Details

- (Object) max (readonly)

Returns the value of attribute max



246
247
248
# File 'lib/kpeg/grammar.rb', line 246

def max
  @max
end

- (Object) min (readonly)

Returns the value of attribute min



246
247
248
# File 'lib/kpeg/grammar.rb', line 246

def min
  @min
end

- (Object) op (readonly)

Returns the value of attribute op



246
247
248
# File 'lib/kpeg/grammar.rb', line 246

def op
  @op
end

- (Object) save_values (readonly)

Returns the value of attribute save_values



246
247
248
# File 'lib/kpeg/grammar.rb', line 246

def save_values
  @save_values
end

Instance Method Details

- (Object) ==(obj)



281
282
283
284
285
286
287
288
# File 'lib/kpeg/grammar.rb', line 281

def ==(obj)
  case obj
  when Multiple
    @op == obj.op and @min == obj.min and @max == obj.max
  else
    super
  end
end

- (Object) inspect



290
291
292
# File 'lib/kpeg/grammar.rb', line 290

def inspect
  inspect_type "multi", "#{@min} #{@max ? @max : "*"} #{@op.inspect}"
end

- (Object) match(x)



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/kpeg/grammar.rb', line 252

def match(x)
  n = 0
  matches = []

  start = x.pos

  while true
    if m = @op.match(x)
      matches << m
    else
      break
    end

    n += 1

    if @max and n > @max
      x.pos = start
      return nil
    end
  end

  if n >= @min
    return MatchComposition.new(self, matches)
  end

  x.pos = start
  return nil
end

- (Object) save_values!



248
249
250
# File 'lib/kpeg/grammar.rb', line 248

def save_values!
  @save_values = true
end