Class: Rattler::Parsers::CombinatorParser

Inherits:
Runtime::Parser show all
Defined in:
lib/rattler/parsers/combinator_parser.rb

Overview

CombinatorParser is a runtime parser that parses using the parse rules directly instead of using match methods generated from the parse rules

Instance Attribute Summary

Attributes inherited from Runtime::Parser

#source

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Runtime::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, #method_missing, #name, #pretty_print, #pretty_print_cycle, #respond_to?, #same_contents?, #to_graphviz, #with_attrs, #with_attrs!, #with_children

Constructor Details

#initialize(source, start_rule, rule_set) ⇒ CombinatorParser

Create a new CombinatorParser to parse source using rule_set and starting with start_rule

Parameters:

  • source (String)

    the source to parse

  • start_rule (Symbol)

    the initial rule to use for parsing

  • rule_set (RuleSet)

    the set of rules to use for parsing



28
29
30
31
32
# File 'lib/rattler/parsers/combinator_parser.rb', line 28

def initialize(source, start_rule, rule_set)
  super source
  @start_rule = start_rule
  @rule_set = rule_set
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Rattler::Util::Node

Class Method Details

.as_class(start_rule, rule_set) ⇒ Class

Returns a subclass of CombinatorParser that uses the given start rule and rule set and can be instantiated with only a source.

Parameters:

  • start_rule (Symbol)

    the initial rule to use for parsing

  • rule_set (RuleSet)

    the set of rules to use for parsing

Returns:

  • (Class)

    a subclass of CombinatorParser that uses the given start rule and rule set and can be instantiated with only a source



14
15
16
17
18
19
20
# File 'lib/rattler/parsers/combinator_parser.rb', line 14

def self.as_class(start_rule, rule_set)
  new_class = Class.new(self)
  new_class.send :define_method, :initialize do |source|
    super source, start_rule, rule_set
  end
  new_class
end

Instance Method Details

#__parse__Object

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

Returns:

  • the result of applying the start rule



35
36
37
# File 'lib/rattler/parsers/combinator_parser.rb', line 35

def __parse__
  @start_rule.parse(@scanner, @rule_set)
end