Class: Rattler::Runtime::RecursiveDescentParser
- Inherits:
-
Parser
- Object
- Util::Node
- Parser
- Rattler::Runtime::RecursiveDescentParser
- 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.
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
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. - #method_missing(symbol, *args) ⇒ Object
- #respond_to?(symbol) ⇒ Boolean
Methods included from Grammar::GrammarDSL
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, #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
.
28 29 30 31 |
# File 'lib/rattler/runtime/recursive_descent_parser.rb', line 28 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
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
48 49 50 |
# File 'lib/rattler/runtime/recursive_descent_parser.rb', line 48 def respond_to?(symbol) super or (symbol == :start_rule) end |