class Rattler::Parsers::Repeat

Repeat decorates a parser with repeat counts to match repeatedly (up to the upper bound, if given) and succeed if the decorated parser succeeds at least as many times as specified by the lower bound.

Public Class Methods

[](parser, lower_bound, upper_bound = nil) click to toggle source

@param [Parser] parser the parser to wrap and match with repeatedly @param [Integer] lower_bound the minimum number of times to match @param [Integer] upper_bound the maximum number of times to match @return [Repeat] a parser that decorates parser and matches repeatedly

# File lib/rattler/parsers/repeat.rb, line 15
def self.[](parser, lower_bound, upper_bound = nil)
  self.new(parser, :lower_bound => lower_bound, :upper_bound => upper_bound)
end
parsed(results, *_) click to toggle source

@private

# File lib/rattler/parsers/repeat.rb, line 20
def self.parsed(results, *_) #:nodoc
  parser, bounds = results
  self[parser, *bounds]
end

Public Instance Methods

lower_bound?() click to toggle source

@return true if there is a lower bound

# File lib/rattler/parsers/repeat.rb, line 68
def lower_bound?
  lower_bound > 0
end
one_or_more?() click to toggle source

@return true if the bounds define a one-or-more parser

# File lib/rattler/parsers/repeat.rb, line 58
def one_or_more?
  lower_bound == 1 and not upper_bound?
end
optional?() click to toggle source

@return true if the bounds define an optional (zero-or-one) parser

# File lib/rattler/parsers/repeat.rb, line 63
def optional?
  lower_bound == 0 and upper_bound == 1
end
parse(scanner, rules, scope = ParserScope.empty) click to toggle source

Parse using the wrapped parser repeatedly until it fails or the upper bound is reached. If the wrapped parser succeeds at least as many times as specified by the lower bound, return the results in an array, or true if the wrapped parser is not capturing?. Return false if the lower bound is not reached.

@param (see Match#parse)

@return [Array, Boolean] an array containing the decorated parser’s parse

results, or +true+ if the decorated parser is not <tt>capturing?</tt>,
or +false+ if the decorated parser does not succeed up to the lower
bound.
# File lib/rattler/parsers/repeat.rb, line 37
def parse(scanner, rules, scope = ParserScope.empty)
  a = []
  start_pos = scanner.pos
  while result = child.parse(scanner, rules, scope)
    a << result
    break if upper_bound? and a.size >= upper_bound
  end
  if a.size >= lower_bound
    capturing? ? select_captures(a) : true
  else
    scanner.pos = start_pos
    false
  end
end
upper_bound?() click to toggle source

@return true if there is an upper bound

# File lib/rattler/parsers/repeat.rb, line 73
def upper_bound?
  not upper_bound.nil?
end
variable_capture_count?() click to toggle source

(see Parser#variable_capture_count?)

# File lib/rattler/parsers/repeat.rb, line 78
def variable_capture_count?
  true
end
zero_or_more?() click to toggle source

@return true if the bounds define a zero-or-more

# File lib/rattler/parsers/repeat.rb, line 53
def zero_or_more?
  lower_bound == 0 and not upper_bound?
end