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

#initialize(op, min, max) ⇒ Multiple

Returns a new instance of Multiple.



245
246
247
248
249
250
251
# File 'lib/kpeg/grammar.rb', line 245

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

Instance Attribute Details

#maxObject (readonly)

Returns the value of attribute max.



253
254
255
# File 'lib/kpeg/grammar.rb', line 253

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



253
254
255
# File 'lib/kpeg/grammar.rb', line 253

def min
  @min
end

#opObject (readonly)

Returns the value of attribute op.



253
254
255
# File 'lib/kpeg/grammar.rb', line 253

def op
  @op
end

#save_valuesObject (readonly)

Returns the value of attribute save_values.



253
254
255
# File 'lib/kpeg/grammar.rb', line 253

def save_values
  @save_values
end

Instance Method Details

#==(obj) ⇒ Object



288
289
290
291
292
293
294
295
# File 'lib/kpeg/grammar.rb', line 288

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

#inspectObject



297
298
299
# File 'lib/kpeg/grammar.rb', line 297

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

#match(x) ⇒ Object



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/kpeg/grammar.rb', line 259

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

#save_values!Object



255
256
257
# File 'lib/kpeg/grammar.rb', line 255

def save_values!
  @save_values = true
end