class Rattler::Runtime::RecursiveDescentParser

RecursiveDescentParser is the base class for any recursive descent parsers. It supports unlimited backtracking, which may result in rules being applied to the same source many times. It is usually preferable to use {PackratParser}, which memoizes parse results.

Public Class Methods

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

Create a new recursive descent parser to parse source.

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

Calls superclass method Rattler::Runtime::Parser::new
# File lib/rattler/runtime/recursive_descent_parser.rb, line 24
def initialize(source, options={})
  super
  @rule_method_names = Hash.new {|h, name| h[name] = :"match_#{name}" }
end

Private Class Methods

grammar(source) click to toggle source
# File lib/rattler/runtime/recursive_descent_parser.rb, line 14
def grammar(source)
  Rattler.compile(self, source)
end

Public Instance Methods

match(rule_name) click to toggle source

Attempt to match the source by applying the named parse rule and return the result. If the rule is successfully matched the result is “thruthy”. If the rules captures parse results, the captured results are returned, otherwise the result is true. If the rule fails to match, the result may be false or nil.

@param [Symbol] rule_name the name of the parse rule. @return the result of applying the parse rule.

# File lib/rattler/runtime/recursive_descent_parser.rb, line 37
def match(rule_name)
  send @rule_method_names[rule_name]
end

Protected Instance Methods

__parse__() click to toggle source

Parse by matching the rule returned by #start_rule or :start if #start_rule is not defined.

@return the result of applying the start rule

# File lib/rattler/runtime/recursive_descent_parser.rb, line 57
def __parse__
  match start_rule
end
apply(match_method_name) click to toggle source

Apply a rule by directly dispatching to the given match method.

@param [Symbol] match_method_name the name of the match method. @return the result of applying the parse rule.

# File lib/rattler/runtime/recursive_descent_parser.rb, line 65
def apply(match_method_name)
  send match_method_name
end