class Rattler::Compiler::Optimizer::JoinMatchCapturingSequence

Sequences of Regexp matches can be joined into a single Regexp match using capturing groups if necessary.

Protected Instance Methods

_applies_to?(parser, context) click to toggle source
# File lib/rattler/compiler/optimizer/join_match_capturing_sequence.rb, line 15
def _applies_to?(parser, context)
  context.capturing? and
  parser.is_a?(Sequence) and
  not disqualifying_captures?(parser) and
  any_neighbors?(parser) {|_| eligible_child? _ }
end
capture_incompatible?(child) click to toggle source
# File lib/rattler/compiler/optimizer/join_match_capturing_sequence.rb, line 33
def capture_incompatible?(child)
  (child.capturing? and not eligible_child? child) or
  child.semantic?
end
create_match(info) click to toggle source
Calls superclass method
# File lib/rattler/compiler/optimizer/join_match_capturing_sequence.rb, line 63
def create_match(info)
  match = super(info[:pattern])
  if info[:num_groups] > 0
    GroupMatch[match, {:num_groups => info[:num_groups]}]
  else
    Skip[match]
  end
end
create_patterns(parsers) click to toggle source
# File lib/rattler/compiler/optimizer/join_match_capturing_sequence.rb, line 38
def create_patterns(parsers)
  num_groups = 0
  patterns = parsers.map do |parser|
    case parser

    when Match
      num_groups += 1
      "(#{parser.re.source})"

    when GroupMatch
      num_groups += parser.num_groups
      "(?>#{parser.re.source})"

    when Skip
      "(?>#{parser.child.re.source})"

    end
  end
  return {:patterns => patterns, :num_groups => num_groups }
end
disqualifying_captures?(parser) click to toggle source
# File lib/rattler/compiler/optimizer/join_match_capturing_sequence.rb, line 28
def disqualifying_captures?(parser)
  parser.any? {|_| _.capturing? and eligible_child? _ } and
  parser.any? {|_| capture_incompatible? _ }
end
eligible_child?(child) click to toggle source
# File lib/rattler/compiler/optimizer/join_match_capturing_sequence.rb, line 22
def eligible_child?(child)
  child.is_a? Match or
  (child.is_a? GroupMatch and child.num_groups == 1) or
  (child.is_a? Skip and child.child.is_a? Match)
end
join_patterns(info) click to toggle source
# File lib/rattler/compiler/optimizer/join_match_capturing_sequence.rb, line 59
def join_patterns(info)
  return info.merge(:pattern => info[:patterns].join)
end