class Rattler::Compiler::Optimizer::Optimization

An Optimization represents a simple transformation of a parser model into an equivalent model that can result in more efficient parsing code. Subclasses override #_applies_to? and #_apply to define an optimization.

Public Class Methods

>>(other) click to toggle source

(see Rattler::Compiler::Optimizer::Optimization#>>)

# File lib/rattler/compiler/optimizer/optimization.rb, line 18
def >>(other)
  instance >> (other.respond_to?(:instance) ? other.instance : other)
end
applies_to?(*args) click to toggle source

(see Rattler::Compiler::Optimizer::Optimization#applies_to?)

# File lib/rattler/compiler/optimizer/optimization.rb, line 28
def applies_to?(*args)
  instance.applies_to?(*args)
end
apply(*args) click to toggle source

(see Rattler::Compiler::Optimizer::Optimization#apply)

# File lib/rattler/compiler/optimizer/optimization.rb, line 23
def apply(*args)
  instance.apply(*args)
end
instance() click to toggle source

@return a lazy singleton instance

# File lib/rattler/compiler/optimizer/optimization.rb, line 13
def instance
  @instance ||= self.new
end
new() click to toggle source
# File lib/rattler/compiler/optimizer/optimization.rb, line 33
def initialize
  @applies_to_cache = Hash.new {|h, k| h[k] = {} }
end

Public Instance Methods

>>(other) click to toggle source

Return a new optimzation that sequences this optimzation and other, i.e. the new optimzation applies this optimzation, then applies other to the result.

@param [Rattler::Compiler::Optimizer::Optimization] other the optimization

to apply after this one

@return [Rattler::Compiler::Optimizer::Optimization] a new optimzation

that sequences this optimzation and +other+
# File lib/rattler/compiler/optimizer/optimization.rb, line 46
def >>(other)
  OptimizationSequence.new(self, other)
end
applies_to?(parser, context) click to toggle source

@param [Rattler::Parsers::Parser] parser a parser model @param [Rattler::Compiler::Optimizer::OptimizationContext] context

@return true if this optimzation applies to parser in context

# File lib/rattler/compiler/optimizer/optimization.rb, line 65
def applies_to?(parser, context)
  @applies_to_cache[context].fetch(parser) do
    @applies_to_cache[context][parser] = _applies_to?(parser, context)
  end
end
apply(parser, context) click to toggle source

Apply the optimzation to parser in context if #applies_to?(parser, context) is true.

@param [Rattler::Parsers::Parser] parser the parser to be optimized @param [Rattler::Compiler::Optimizer::OptimizationContext] context

@return [Rattler::Parsers::Parser] the optimized parser

# File lib/rattler/compiler/optimizer/optimization.rb, line 57
def apply(parser, context)
  applies_to?(parser, context) ? _apply(parser, context) : parser
end