Module: Parslet::Accelerator Private

Defined in:
lib/parslet/accelerator.rb,
lib/parslet/accelerator/engine.rb,
lib/parslet/accelerator/application.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Defined Under Namespace

Classes: Application, Apply, Engine, Expression

Class Method Summary collapse

Class Method Details

.anyParslet::Accelerator::Expression

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



108
109
110
# File 'lib/parslet/accelerator.rb', line 108

def any
  Expression.new(:re, ".")
end

.apply(atom, *rules) ⇒ Parslet::Atoms::Base

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)"')

Parameters:

  • atom (Parslet::Atoms::Base)

    a parser to optimize

  • *rules (Parslet::Accelerator::Rule)

    rules produced by .rule

Returns:



156
157
158
# File 'lib/parslet/accelerator.rb', line 156

def apply atom, *rules
  Application.new(atom, rules).call
end

.match(atom, expr) ⇒ nil, Hash

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.

Parameters:

Returns:

  • (nil, Hash)

    bindings for the match, nil on failure



121
122
123
124
125
# File 'lib/parslet/accelerator.rb', line 121

def match atom, expr
  engine = Engine.new

  return engine.bindings if engine.match(atom, expr)
end

.re(variable, *constraints) ⇒ Parslet::Accelerator::Expression

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



100
101
102
# File 'lib/parslet/accelerator.rb', line 100

def re variable, *constraints
  Expression.new(:re, variable, *constraints)
end

.rule(expression, &action) ⇒ Object

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('.') }


134
135
136
# File 'lib/parslet/accelerator.rb', line 134

def rule expression, &action
  [expression, action]
end

.str(variable, *constraints) ⇒ Parslet::Accelerator::Expression

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



92
93
94
# File 'lib/parslet/accelerator.rb', line 92

def str variable, *constraints
  Expression.new(:str, variable, *constraints)
end