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