class Rattler::Runtime::ExtendedPackratParser

ExtendedPackratParser implements the algorithm described by Alessandro Warth, James R. Douglass, and Todd Millstein for extending packrat parsing to support left-recursive grammars.

Public Class Methods

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

Create a new extended packrat parser to parse source.

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

Calls superclass method
# File lib/rattler/runtime/extended_packrat_parser.rb, line 15
def initialize(source, options={})
  super
  @heads = {}
  @lr_stack = []
end

Public 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 PackratParser#apply) @return (see PackratParser#apply)

# File lib/rattler/runtime/extended_packrat_parser.rb, line 28
def apply(match_method_name)
  start_pos = @scanner.pos
  if m = memo_lr(match_method_name, start_pos)
    recall m, match_method_name
  else
    lr = LR.new(false, match_method_name, nil)
    @lr_stack.push lr
    m = inject_memo match_method_name, start_pos, lr, start_pos
    result = eval_rule match_method_name
    @lr_stack.pop
    if lr.head
      m.end_pos = @scanner.pos
      lr.seed = result
      lr_answer match_method_name, start_pos, m
    else
      save m, result
    end
  end
end