Module: Rattler::BackEnd::Optimizer

Defined in:
lib/rattler/back_end/optimizer.rb,
lib/rattler/back_end/optimizer/flattening.rb,
lib/rattler/back_end/optimizer/optimization.rb,
lib/rattler/back_end/optimizer/match_joining.rb,
lib/rattler/back_end/optimizer/flatten_choice.rb,
lib/rattler/back_end/optimizer/flatten_sequence.rb,
lib/rattler/back_end/optimizer/join_match_choice.rb,
lib/rattler/back_end/optimizer/optimize_children.rb,
lib/rattler/back_end/optimizer/specialize_repeat.rb,
lib/rattler/back_end/optimizer/composite_reducing.rb,
lib/rattler/back_end/optimizer/join_match_sequence.rb,
lib/rattler/back_end/optimizer/reduce_repeat_match.rb,
lib/rattler/back_end/optimizer/inline_regular_rules.rb,
lib/rattler/back_end/optimizer/join_predicate_match.rb,
lib/rattler/back_end/optimizer/optimization_context.rb,
lib/rattler/back_end/optimizer/simplify_token_match.rb,
lib/rattler/back_end/optimizer/optimization_sequence.rb,
lib/rattler/back_end/optimizer/join_predicate_or_match.rb,
lib/rattler/back_end/optimizer/join_predicate_bare_match.rb,
lib/rattler/back_end/optimizer/simplify_redundant_repeat.rb,
lib/rattler/back_end/optimizer/remove_meaningless_wrapper.rb,
lib/rattler/back_end/optimizer/join_predicate_nested_match.rb,
lib/rattler/back_end/optimizer/join_match_matching_sequence.rb,
lib/rattler/back_end/optimizer/join_predicate_or_bare_match.rb,
lib/rattler/back_end/optimizer/join_match_capturing_sequence.rb,
lib/rattler/back_end/optimizer/join_predicate_or_nested_match.rb

Overview

The Optimizer transforms parser models into equivalent models that can result in more efficient parsing code. This is primarily achieved by converting regular expressions into equivalent Regexp patterns, thus reducing object instantiation and method dispatch and having StringScanner do as much of the parsing work as possible.

Author:

  • Jason Arhart

Defined Under Namespace

Modules: CompositeReducing, Flattening, MatchJoining Classes: FlattenCapturingSequence, FlattenChoice, FlattenMatchingSequence, InlineRegularRules, JoinMatchCapturingSequence, JoinMatchChoice, JoinMatchMatchingSequence, JoinPredicateBareMatch, JoinPredicateNestedMatch, JoinPredicateOrBareMatch, JoinPredicateOrNestedMatch, Optimization, OptimizationContext, OptimizationSequence, OptimizeChildren, ReduceRepeatMatch, RemoveMeaninglessWrapper, SimplifyRedundantRepeat, SimplifyTokenMatch, SpecializeRepeat

Constant Summary collapse

FlattenSequence =
FlattenMatchingSequence >> FlattenCapturingSequence
JoinMatchSequence =

Sequences of Regexp matches can be joined into a single Regexp match using capturing groups if necessary.

Author:

  • Jason Arhart

JoinMatchMatchingSequence >> JoinMatchCapturingSequence
JoinPredicateMatch =

A predicate and an adjacent Regexp match in a Sequence can be joined into a single Regexp match.

Author:

  • Jason Arhart

JoinPredicateBareMatch >> JoinPredicateNestedMatch
JoinPredicateOrMatch =

A predicate and an adjacent Regexp match in a Choice can be joined into a single Regexp match.

Author:

  • Jason Arhart

JoinPredicateOrBareMatch >> JoinPredicateOrNestedMatch

Class Method Summary collapse

Class Method Details

.optimizationsObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rattler/back_end/optimizer.rb', line 22

def optimizations
  @optimizations ||=
    OptimizeChildren >>
    InlineRegularRules >>
    SimplifyRedundantRepeat >>
    RemoveMeaninglessWrapper >>
    SimplifyTokenMatch >>
    FlattenSequence >>
    FlattenChoice >>
    ReduceRepeatMatch >>
    JoinPredicateMatch >>
    JoinPredicateOrMatch >>
    JoinMatchSequence >>
    JoinMatchChoice
end

.optimize(model, opts = {}) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/rattler/back_end/optimizer.rb', line 38

def optimize(model, opts={})
  case model
  when ::Rattler::Grammar::Grammar then optimize_grammar model, opts
  when ::Rattler::Parsers::RuleSet then optimize_rule_set model, opts
  when ::Rattler::Parsers::Rule then optimize_rule model, default_context(opts)
  else optimize_expr model, default_context(opts)
  end
end