Class: Rattler::BackEnd::Optimizer::Optimization

Inherits:
Object
  • Object
show all
Defined in:
lib/rattler/back_end/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.

Author:

  • Jason Arhart

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOptimization

Returns a new instance of Optimization.



42
43
44
# File 'lib/rattler/back_end/optimizer/optimization.rb', line 42

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

Class Method Details

.>>(other) ⇒ Object

See Also:



27
28
29
# File 'lib/rattler/back_end/optimizer/optimization.rb', line 27

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

.applies_to?(*args) ⇒ Boolean

Returns:

  • (Boolean)

See Also:



37
38
39
# File 'lib/rattler/back_end/optimizer/optimization.rb', line 37

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

.apply(*args) ⇒ Object

See Also:

  • #match


32
33
34
# File 'lib/rattler/back_end/optimizer/optimization.rb', line 32

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

.instanceObject



22
23
24
# File 'lib/rattler/back_end/optimizer/optimization.rb', line 22

def instance
  @instance ||= self.new
end

Instance Method Details

#>>(other) ⇒ Rattler::BackEnd::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:



55
56
57
# File 'lib/rattler/back_end/optimizer/optimization.rb', line 55

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

#applies_to?(parser, context) ⇒ Boolean

Return true if this optimzation applies to parser in context.

Parameters:

Returns:

  • (Boolean)

    true if this optimzation applies to parser in context



77
78
79
80
81
# File 'lib/rattler/back_end/optimizer/optimization.rb', line 77

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:



66
67
68
# File 'lib/rattler/back_end/optimizer/optimization.rb', line 66

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