Class: Rattler::Runtime::RecursiveDescentParser

Inherits:
Parser show all
Includes:
Grammar::GrammarDSL, ParserHelper
Defined in:
lib/rattler/runtime/recursive_descent_parser.rb

Overview

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.

Author:

  • Jason Arhart

Direct Known Subclasses

PackratParser

Instance Attribute Summary

Attributes inherited from Parser

#source

Instance Method Summary collapse

Methods included from Grammar::GrammarDSL

included

Methods included from ParserHelper

#select_captures

Methods inherited from Parser

#fail, #fail!, #fail_parse, #failure, #failure?, #parse, #parse!, parse!, #parse_fully, #parse_fully!, parse_fully!, #pos, #pos=

Methods inherited from Util::Node

#==, #[], [], #attrs, #can_equal?, #child, #children, #each, #empty?, #eql?, #inspect, #name, #same_contents?, #to_graphviz, #with_attrs, #with_attrs!, #with_children

Constructor Details

#initialize(source, options = {}) ⇒ RecursiveDescentParser

Create a new recursive descent parser to parse source.

Parameters:

  • source (String)

    the source to parse

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :tab_size (Int) — default: 8

    tab size to use to calculate column numbers



28
29
30
31
# File 'lib/rattler/runtime/recursive_descent_parser.rb', line 28

def initialize(source, options={})
  super
  @rule_method_names = Hash.new {|h, name| h[name] = :"match_#{name}" }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object



44
45
46
# File 'lib/rattler/runtime/recursive_descent_parser.rb', line 44

def method_missing(symbol, *args)
  (symbol == :start_rule) ? :start : super
end

Instance Method Details

#match(rule_name) ⇒ Object

Apply a rule by dispatching to the method associated with rule_name which is named by <tt>“match_#rule_name”<tt>, and if the match fails register a parse failure.



40
41
42
# File 'lib/rattler/runtime/recursive_descent_parser.rb', line 40

def match(rule_name)
  send @rule_method_names[rule_name] or fail { rule_name }
end

#respond_to?(symbol) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/rattler/runtime/recursive_descent_parser.rb', line 48

def respond_to?(symbol)
  super or (symbol == :start_rule)
end