Class: Language::Atom::Repeat
- Inherits:
-
Language::Atom
- Object
- Language::Atom
- Language::Atom::Repeat
- Defined in:
- lib/language/atom/repeat.rb
Instance Method Summary collapse
-
#initialize(parent: nil, minimum: 0, maximum: nil) ⇒ Repeat
constructor
A new instance of Repeat.
- #parse(parser) ⇒ Object
- #to_s ⇒ Object
Methods inherited from Language::Atom
#<<, #>>, #absent, #aka, #any, #ignore, #inspect, #maybe, #repeat, #str, #then, #|
Constructor Details
#initialize(parent: nil, minimum: 0, maximum: nil) ⇒ Repeat
Returns a new instance of Repeat.
6 7 8 9 10 |
# File 'lib/language/atom/repeat.rb', line 6 def initialize(parent: nil, minimum: 0, maximum: nil) @parent = parent @minimum = minimum @maximum = maximum end |
Instance Method Details
#parse(parser) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/language/atom/repeat.rb', line 12 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_s ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/language/atom/repeat.rb', line 29 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 |