Class: Rattler::BackEnd::Optimizer::Optimization
- Inherits:
-
Object
- Object
- Rattler::BackEnd::Optimizer::Optimization
- 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.
Direct Known Subclasses
FlattenCapturingSequence, FlattenChoice, FlattenMatchingSequence, InlineRegularRules, JoinMatchCapturingSequence, JoinMatchChoice, JoinMatchMatchingSequence, JoinPredicateBareMatch, JoinPredicateOrBareMatch, OptimizationSequence, OptimizeChildren, ReduceRepeatMatch, RemoveMeaninglessWrapper, SimplifyRedundantRepeat, SimplifyTokenMatch, SpecializeRepeat
Class Method Summary collapse
Instance Method Summary collapse
-
#>>(other) ⇒ Rattler::BackEnd::Optimizer::Optimization
Return a new optimzation that sequences this optimzation and
other
, i.e. -
#applies_to?(parser, context) ⇒ Boolean
Return
true
if this optimzation applies toparser
incontext
. -
#apply(parser, context) ⇒ Rattler::Parsers::Parser
Apply the optimzation to
parser
incontext
if#applies_to?(parser, context)
istrue
. -
#initialize ⇒ Optimization
constructor
A new instance of Optimization.
Constructor Details
#initialize ⇒ Optimization
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
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
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
32 33 34 |
# File 'lib/rattler/back_end/optimizer/optimization.rb', line 32 def apply(*args) instance.apply(*args) end |
.instance ⇒ Object
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.
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
.
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
.
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 |