Class: Rattler::Compiler::Optimizer::Optimization

Inherits:
Object
  • Object
show all
Defined in:
lib/rattler/compiler/optimizer/optimization.rb

Overview

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.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOptimization

Returns a new instance of Optimization.



33
34
35
# File 'lib/rattler/compiler/optimizer/optimization.rb', line 33

def initialize
  @applies_to_cache = Hash.new {|h, k| h[k] = {} }
end

Class Method Details

.>>(other) ⇒ Rattler::Compiler::Optimizer::Optimization

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

Parameters:

Returns:



18
19
20
# File 'lib/rattler/compiler/optimizer/optimization.rb', line 18

def >>(other)
  instance >> (other.respond_to?(:instance) ? other.instance : other)
end

.applies_to?(*args) ⇒ Object

Returns true if this optimzation applies to parser in context.

Parameters:

Returns:

  • true if this optimzation applies to parser in context



28
29
30
# File 'lib/rattler/compiler/optimizer/optimization.rb', line 28

def applies_to?(*args)
  instance.applies_to?(*args)
end

.apply(*args) ⇒ Rattler::Parsers::Parser

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

Parameters:

Returns:



23
24
25
# File 'lib/rattler/compiler/optimizer/optimization.rb', line 23

def apply(*args)
  instance.apply(*args)
end

.instanceObject

Returns a lazy singleton instance.

Returns:

  • a lazy singleton instance



13
14
15
# File 'lib/rattler/compiler/optimizer/optimization.rb', line 13

def instance
  @instance ||= self.new
end

Instance Method Details

#>>(other) ⇒ Rattler::Compiler::Optimizer::Optimization

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

Parameters:

Returns:



46
47
48
# File 'lib/rattler/compiler/optimizer/optimization.rb', line 46

def >>(other)
  OptimizationSequence.new(self, other)
end

#applies_to?(parser, context) ⇒ Boolean

Returns true if this optimzation applies to parser in context.

Parameters:

Returns:

  • (Boolean)

    true if this optimzation applies to parser in context



65
66
67
68
69
# 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) ⇒ Rattler::Parsers::Parser

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

Parameters:

Returns:



57
58
59
# File 'lib/rattler/compiler/optimizer/optimization.rb', line 57

def apply(parser, context)
  applies_to?(parser, context) ? _apply(parser, context) : parser
end