class Rattler::Compiler::Optimizer::ReduceRepeatMatch

A repeat of a Regexp match can be reduced to a single Regexp match.

Protected Instance Methods

_applies_to?(parser, context) click to toggle source
# File lib/rattler/compiler/optimizer/reduce_repeat_match.rb, line 12
def _applies_to?(parser, context)
  context.matching? and
  parser.is_a? Repeat and
  parser.child.is_a? Match
end
_apply(parser, context) click to toggle source
# File lib/rattler/compiler/optimizer/reduce_repeat_match.rb, line 18
def _apply(parser, context)
  Match[Regexp.new("(?>#{parser.child.re.source})#{suffix parser}")]
end

Private Instance Methods

general_suffix(lower_bound, upper_bound) click to toggle source
# File lib/rattler/compiler/optimizer/reduce_repeat_match.rb, line 36
def general_suffix(lower_bound, upper_bound)
  if lower_bound == upper_bound
    "{#{lower_bound}}"
  elsif !upper_bound
    "{#{lower_bound},}"
  else
    "{#{lower_bound},#{upper_bound}}"
  end
end
suffix(parser) click to toggle source
# File lib/rattler/compiler/optimizer/reduce_repeat_match.rb, line 24
def suffix(parser)
  if parser.zero_or_more?
    '*'
  elsif parser.one_or_more?
    '+'
  elsif parser.optional?
    '?'
  else
    general_suffix parser.lower_bound, parser.upper_bound
  end
end