Class: Language::Atom::Repeat

Inherits:
Language::Atom show all
Defined in:
lib/language/atom.rb,
lib/language/atom/repeat.rb

Instance Method Summary collapse

Methods inherited from Language::Atom

#<<, #>>, #absent, #aka, #any, #ignore, #inspect, #maybe, #repeat, #str, #then, #|

Constructor Details

#initialize(parent:) ⇒ Repeat

Returns a new instance of Repeat.



32
33
34
35
36
# File 'lib/language/atom.rb', line 32

def initialize(parent: nil, minimum: 0, maximum: nil)
  @parent = parent
  @minimum = minimum
  @maximum = maximum
end

Instance Method Details

#parse(parser) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/language/atom.rb', line 38

def parse(parser)
  return unless @parent

  @minimum.times { match(parser) }
  if @maximum.nil?
    begin
      loop { match(parser) }
    rescue Parser::Interuption
    end
  else
    begin
      (@maximum - @minimum).times { match(parser) }
    rescue Parser::Interuption
    end
  end
end

#to_sObject



55
56
57
58
59
60
61
62
# File 'lib/language/atom.rb', line 55

def to_s
  minimum = @minimum.zero? ? "" : @minimum.to_s
  maximum = @maximum.nil? ? "" : ", #{@maximum}"
  parenthesis =
    minimum.empty? && maximum.empty? ? "" : "(#{minimum}#{maximum})"

  @parent ? "(#{@parent}).repeat#{parenthesis}" : "repeat#{parenthesis}"
end