Class: Rattler::Runtime::RecursiveDescentParser
- Inherits:
-
Parser
- Object
- Util::Node
- Parser
- Rattler::Runtime::RecursiveDescentParser
- 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
Instance Attribute Summary
Attributes inherited from Parser
Instance Method Summary collapse
-
#initialize(source, options = {}) ⇒ RecursiveDescentParser
constructor
Create a new recursive descent parser to parse
source
. -
#match(rule_name) ⇒ Object
Attempt to match the source by applying the named parse rule and return the result.
- #method_missing(symbol, *args) ⇒ Object
- #respond_to?(symbol) ⇒ Boolean
Methods included from ParserHelper
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
.
24 25 26 27 |
# File 'lib/rattler/runtime/recursive_descent_parser.rb', line 24 def initialize(source, ={}) 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
.
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
47 48 49 |
# File 'lib/rattler/runtime/recursive_descent_parser.rb', line 47 def respond_to?(symbol) #:nodoc: super or (symbol == :start_rule) end |