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.



235
236
237
238
239
240
241
# File 'lib/kpeg/grammar.rb', line 235

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.



243
244
245
# File 'lib/kpeg/grammar.rb', line 243

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



243
244
245
# File 'lib/kpeg/grammar.rb', line 243

def min
  @min
end

#opObject (readonly)

Returns the value of attribute op.



243
244
245
# File 'lib/kpeg/grammar.rb', line 243

def op
  @op
end

#save_valuesObject (readonly)

Returns the value of attribute save_values.



243
244
245
# File 'lib/kpeg/grammar.rb', line 243

def save_values
  @save_values
end

Instance Method Details

#==(obj) ⇒ Object



278
279
280
281
282
283
284
285
# File 'lib/kpeg/grammar.rb', line 278

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

#inspectObject



287
288
289
# File 'lib/kpeg/grammar.rb', line 287

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

#match(x) ⇒ Object



249
250
251
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
# File 'lib/kpeg/grammar.rb', line 249

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



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

def save_values!
  @save_values = true
end