Class: Packrat::Repeat

Inherits:
GrammarElement show all
Defined in:
lib/packrat/grammar.rb,
lib/packrat/grammar.rb

Instance Attribute Summary

Attributes inherited from GrammarElement

#hidden

Instance Method Summary collapse

Methods inherited from GrammarElement

#to_packrat_grammar_element

Constructor Details

#initialize(subElement, minimumReps = 0, maximumReps = false) ⇒ Repeat

Returns a new instance of Repeat.



205
206
207
208
# File 'lib/packrat/grammar.rb', line 205

def initialize(subElement, minimumReps = 0, maximumReps = false)
  @min, @max = minimumReps, maximumReps
  @sub = subElement.to_packrat_grammar_element
end

Instance Method Details

#inspectObject



209
210
211
212
213
214
# File 'lib/packrat/grammar.rb', line 209

def inspect
  subi = @sub.inspect
  return "mult(#{subi})" if @min == 0 && @max == false
  return "plus(#{subi})" if @min == 1 && @max == false
  return "rep(@min, @max, #{subi})"
end

#parse(parser) ⇒ Object



573
574
575
576
577
578
579
580
581
582
583
584
585
586
# File 'lib/packrat/grammar.rb', line 573

def parse(parser)
  result_list = []
  oldpos = parser.pos
  # XXX: Should we take only amx number of results here if max != false?
  while (res = @sub.parse(parser))
    result_list << res
  end
  if valid_result?(result_list)
    return result_list
  else
    parser.pos = oldpos
    return false
  end
end

#valid_result?(list) ⇒ Boolean

Returns:

  • (Boolean)


587
588
589
590
591
# File 'lib/packrat/grammar.rb', line 587

def valid_result?(list)
  return false if @min && list.length < @min
  return false if @max && list.length > @max
  true
end