module Parslet::Accelerator

Optimizes the parsers by pattern matching on the parser atoms and replacing matches with better versions. See the file qed/accelerators.md for a more in-depth description.

Example:

quote = str('"')
parser = quote >> (quote.absent? >> any).repeat >> quote

A = Accelerator # for making what follows a bit shorter
optimized_parser = A.apply(parser, 
  A.rule( (A.str(:x).absent? >> A.any).repeat ) { GobbleUp.new(x) })

optimized_parser.parse('"Parsing is now fully optimized! (tm)"')

@api private

Public Instance Methods

any() click to toggle source

Returns a match expression that will match `any` parslet atoms.

@return [Parslet::Accelerator::Expression]

# File lib/parslet/accelerator.rb, line 107
def any
  Expression.new(:re, ".")
end
apply(atom, *rules) click to toggle source

Given a parslet atom and a set of rules, tries to match the rules recursively through the parslet atom. Once a rule could be matched, its action block will be called.

Example:

quote = str('"')
parser = quote >> (quote.absent? >> any).repeat >> quote

A = Accelerator # for making what follows a bit shorter
optimized_parser = A.apply(parser, 
  A.rule( (A.str(:x).absent? >> A.any).repeat ) { GobbleUp.new(x) })

optimized_parser.parse('"Parsing is now fully optimized! (tm)"')

@param atom [Parslet::Atoms::Base] a parser to optimize @param *rules [Parslet::Accelerator::Rule] rules produced by .rule @return [Parslet::Atoms::Base] optimized parser

# File lib/parslet/accelerator.rb, line 155
def apply atom, *rules
  Application.new(atom, rules).call
end
match(atom, expr) click to toggle source

Given a parslet atom and an expression, will determine if the expression matches the atom. If successful, returns the bindings into the pattern that were made. If no bindings had to be made to make the match successful, the empty hash is returned.

@param atom [Parslet::Atoms::Base] parslet atom to match against @param expr [Parslet::Accelerator::Expression] expression to match @return [nil, Hash] bindings for the match, nil on failure

# File lib/parslet/accelerator.rb, line 120
def match atom, expr
  engine = Engine.new

  return engine.bindings if engine.match(atom, expr)
end
re(variable, *constraints) click to toggle source

Returns a match expression that will match `match` parslet atoms.

@return [Parslet::Accelerator::Expression]

# File lib/parslet/accelerator.rb, line 99
def re variable, *constraints
  Expression.new(:re, variable, *constraints)
end
rule(expression, &action) click to toggle source

Constructs an accelerator rule. A rule is a matching expression and the code that should be executed once the expression could be bound to a parser.

Example:

Accelerator.rule(Accelerator.any) { Parslet.match('.') }
# File lib/parslet/accelerator.rb, line 133
def rule expression, &action
  [expression, action]
end
str(variable, *constraints) click to toggle source

Returns a match expression that will match `str` parslet atoms.

@return [Parslet::Accelerator::Expression]

# File lib/parslet/accelerator.rb, line 91
def str variable, *constraints
  Expression.new(:str, variable, *constraints)
end