class Rattler::Runtime::PackratParser

A PackratParser is a recursive descent parser that memoizes the results of applying nonterminal rules so that each rule method is invoked at most once at a given parse position.

Public Class Methods

new(source, options={}) click to toggle source

Create a new packrat parser to parse source.

@param (see RecursiveDescentParser#initialize) @option (see RecursiveDescentParser#initialize)

Calls superclass method
# File lib/rattler/runtime/packrat_parser.rb, line 15
def initialize(source, options={})
  super
  @memo = Hash.new {|h, match_method_name| h[match_method_name] = {} }
end

Protected Instance Methods

apply(match_method_name) click to toggle source

Apply a rule by dispatching to the given match method. The result of applying the rule is memoized so that the match method is invoked at most once at a given parse position.

@param (see RecursiveDescentParser#apply) @return (see RecursiveDescentParser#apply)

# File lib/rattler/runtime/packrat_parser.rb, line 29
def apply(match_method_name)
  start_pos = @scanner.pos
  if m = memo(match_method_name, start_pos)
    recall m, match_method_name
  else
    m = inject_fail match_method_name, start_pos
    save m, eval_rule(match_method_name)
  end
end