class Rattler::Compiler::Optimizer::JoinPredicateOrBareMatch
A predicate and an adjacent Regexp match in a Choice can be joined into a single Regexp match.
Protected Instance Methods
_applies_to?(parser, context)
click to toggle source
# File lib/rattler/compiler/optimizer/join_predicate_or_bare_match.rb, line 14 def _applies_to?(parser, context) parser.is_a?(Choice) and parser.children.each_cons(2).any? {|a, b| eligible_pair? a, b } end
_apply(parser, context)
click to toggle source
# File lib/rattler/compiler/optimizer/join_predicate_or_bare_match.rb, line 19 def _apply(parser, context) a = [] parser.each {|_| a << (eligible_pair?(a.last, _) ? reduce(a.pop, _) : _)} finish_reduce parser, a end
eligible_match?(parser)
click to toggle source
# File lib/rattler/compiler/optimizer/join_predicate_or_bare_match.rb, line 43 def eligible_match?(parser) parser.is_a? Match end
eligible_pair?(a, b)
click to toggle source
# File lib/rattler/compiler/optimizer/join_predicate_or_bare_match.rb, line 38 def eligible_pair?(a, b) (eligible_match? a and eligible_predicate? b) or (eligible_predicate? a and eligible_match? b) end
eligible_predicate?(parser)
click to toggle source
# File lib/rattler/compiler/optimizer/join_predicate_or_bare_match.rb, line 47 def eligible_predicate?(parser) case parser when Eof true when Assert, Disallow parser.child.is_a? Match else false end end
prepare_pattern(child)
click to toggle source
# File lib/rattler/compiler/optimizer/join_predicate_or_bare_match.rb, line 29 def prepare_pattern(child) case child when Match then child.re.source when Assert then "(?=#{child.child.re.source})" when Disallow then "(?!#{child.child.re.source})" when Eof then "\\z" end end
reduce(*pair)
click to toggle source
# File lib/rattler/compiler/optimizer/join_predicate_or_bare_match.rb, line 25 def reduce(*pair) Match[Regexp.new(pair.map {|_| prepare_pattern _ }.join '|' )] end