Class: Rattler::Runtime::RecursiveDescentParser

Inherits:
Parser show all
Includes:
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.

Direct Known Subclasses

PackratParser

Instance Attribute Summary

Attributes inherited from Parser

#source

Instance Method Summary collapse

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, #pretty_print, #pretty_print_cycle, #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 (Integer) — default: 8

    tab size to use to calculate column numbers



24
25
26
27
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object



42
43
44
# File 'lib/rattler/runtime/recursive_descent_parser.rb', line 42

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

Instance Method Details

#match(rule_name) ⇒ Object

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.

Parameters:

  • rule_name (Symbol)

    the name of the parse rule.

Returns:

  • the result of applying the parse rule.



37
38
39
# File 'lib/rattler/runtime/recursive_descent_parser.rb', line 37

def match(rule_name)
  send @rule_method_names[rule_name]
end

#respond_to?(symbol) ⇒ Boolean

Returns:

  • (Boolean)


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

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