Class: Rattler::Parsers::RuleSet

Inherits:
Util::Node show all
Defined in:
lib/rattler/parsers/rule_set.rb

Overview

RuleSet encapsulates a set of parse rules that define a grammar and provides accessors to the rules by name.

Class Method Summary collapse

Instance Method Summary collapse

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_children

Constructor Details

#initializeRuleSet #initialize(rule...) ⇒ RuleSet #initialize(attribute...) ⇒ RuleSet #initialize(rule..., attribute...) ⇒ RuleSet

Create a RuleSet instance.

Overloads:

  • #initializeRuleSet
  • #initialize(rule...) ⇒ RuleSet
  • #initialize(attribute...) ⇒ RuleSet
  • #initialize(rule..., attribute...) ⇒ RuleSet


24
25
26
27
28
# File 'lib/rattler/parsers/rule_set.rb', line 24

def initialize(*args)
  super
  @by_name = {}
  children.each {|rule| @by_name[rule.name] = rule }
end

Dynamic Method Handling

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

Class Method Details

.parsed(results, *_) ⇒ Object



10
11
12
# File 'lib/rattler/parsers/rule_set.rb', line 10

def self.parsed(results, *_) #:nodoc:
  self.new(results[0])
end

Instance Method Details

#[](rule_name) ⇒ Rule #[](index) ⇒ Object #[](start, length) ⇒ Array #[](range) ⇒ Array

Access the rule set’s rules

Overloads:

  • #[](rule_name) ⇒ Rule

    Returns the rule with the given name in the rule set.

    Parameters:

    • name (Symbol)

      the name of a rule in the rule set

    Returns:

    • (Rule)

      the rule with the given name in the rule set

  • #[](index) ⇒ Object

    Returns the rule at index.

    Parameters:

    • index (Integer)

      index of the rule

    Returns:

    • the rule at index

  • #[](start, length) ⇒ Array

    Returns the rules starting at start and continuing for length rules.

    Parameters:

    • start (Integer)

      the index of the first rule

    • length (Integer)

      the number of rules to return

    Returns:

    • (Array)

      the rules starting at start and continuing for length rules

  • #[](range) ⇒ Array

    Returns the rules specified by range.

    Parameters:

    • range (Range)

      the range of rules

    Returns:

    • (Array)

      the rules specified by range



70
71
72
73
74
75
76
# File 'lib/rattler/parsers/rule_set.rb', line 70

def [](*args)
  if args.length == 1 and args[0].is_a?(Symbol)
    rule(args[0])
  else
    super
  end
end

#analysisAnalysis

Returns a static analysis of the rules.

Returns:

  • (Analysis)

    a static analysis of the rules



113
114
115
# File 'lib/rattler/parsers/rule_set.rb', line 113

def analysis
  @analysis ||= Analysis.new(self)
end

#includesArray<String>

Returns a list of modules to be included in the parser.

Returns:

  • (Array<String>)

    a list of modules to be included in the parser



50
51
52
# File 'lib/rattler/parsers/rule_set.rb', line 50

def includes
  attrs[:includes] || []
end

#inherited_rule(name) ⇒ Rule

Returns the rule with the given name in the inherited rule set.

Parameters:

  • name (Symbol)

    the name of a rule in an inherited rule set

Returns:

  • (Rule)

    the rule with the given name in the inherited rule set



45
46
47
# File 'lib/rattler/parsers/rule_set.rb', line 45

def inherited_rule(name)
  includes.inject(nil) {|r,s| r || s.rule(name) }
end

#map_rules {|rule| ... } ⇒ RuleSet

Returns a new rule set with the result of running the block once for each rule.

Yields:

  • Run the block once for each rule

Yield Parameters:

  • rule (Rule)

    one rule in the rule set

Yield Returns:

  • (Rule)

    a rule to replace the one yielded to the block

Returns:

  • (RuleSet)

    a new rule set with the result of running the block once for each rule



99
100
101
# File 'lib/rattler/parsers/rule_set.rb', line 99

def map_rules
  self.with_rules rules.map {|rule| yield rule }
end

#rule(name) ⇒ Rule

Returns the rule with the given name in the rule set.

Parameters:

  • name (Symbol)

    the name of a rule in the rule set

Returns:

  • (Rule)

    the rule with the given name in the rule set



39
40
41
# File 'lib/rattler/parsers/rule_set.rb', line 39

def rule(name)
  @by_name[name] || inherited_rule(name)
end

#select_rules {|rule| ... } ⇒ RuleSet

A new rule set with the rules for which the block returns true

Yields:

  • Run the block as a predicate once for each rule

Yield Parameters:

  • rule (Rule)

    one rule in the rule set

Yield Returns:

  • (Boolean)

    true to include the rule in the new rule set

Returns:

  • (RuleSet)

    a new rule set with the rules for which the block returns true



108
109
110
# File 'lib/rattler/parsers/rule_set.rb', line 108

def select_rules
  self.with_rules rules.select {|rule| yield rule }
end

#start_ruleSymbol

Returns the name of the rule to start parsing with.

Returns:

  • (Symbol)

    the name of the rule to start parsing with



33
34
35
# File 'lib/rattler/parsers/rule_set.rb', line 33

def start_rule
  attrs[:start_rule]
end

#with_attrs(new_attrs) ⇒ RuleSet

Returns a new rule set with the same rules and the new attributes merged.

Parameters:

  • new_attrs (Hash)

    a hash of attributes to merge with the existing attributes

Returns:

  • (RuleSet)

    a new rule set with the same rules and the new attributes merged



82
83
84
# File 'lib/rattler/parsers/rule_set.rb', line 82

def with_attrs(new_attrs)
  self.class.new(children, attrs.merge(new_attrs))
end

#with_rules(new_rules) ⇒ RuleSet

Returns a new rule set with a new list of rules and the same attributes.

Parameters:

  • new_rules (Array<Rule>)

    the new list of rules to replace the existing rules

Returns:

  • (RuleSet)

    a new rule set with a new list of rules and the same attributes



90
91
92
# File 'lib/rattler/parsers/rule_set.rb', line 90

def with_rules(new_rules)
  self.class.new(new_rules, attrs)
end