class Rattler::Parsers::RuleSet

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

Public Class Methods

new(*args) click to toggle source

Create a RuleSet instance.

@overload initialize()

@return [RuleSet]

@overload initialize(rule…)

@return [RuleSet]

@overload initialize(attribute…)

@return [RuleSet]

@overload initialize(rule…, attribute…)

@return [RuleSet]
Calls superclass method Rattler::Util::Node::new
# File lib/rattler/parsers/rule_set.rb, line 24
def initialize(*args)
  super
  @by_name = {}
  children.each {|rule| @by_name[rule.name] = rule }
end

Public Instance Methods

[](*args) click to toggle source

Access the rule set’s rules

@overload [](rule_name)

@param [Symbol] name the name of a rule in the rule set
@return [Rule] the rule with the given name in the rule set

@overload [](index)

@param [Integer] index index of the rule
@return the rule at +index+

@overload [](start, length)

@param [Integer] start the index of the first rule
@param [Integer] length the number of rules to return
@return [Array] the rules starting at +start+ and continuing for
  +length+ rules

@overload [](range)

@param [Range] range the range of rules
@return [Array] the rules specified by +range+
Calls superclass method Rattler::Util::Node::[]
# 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
analysis() click to toggle source

@return [Analysis] a static analysis of the rules

# File lib/rattler/parsers/rule_set.rb, line 113
def analysis
  @analysis ||= Analysis.new(self)
end
includes() click to toggle source

@return [Array<String>] a list of modules to be included in the parser

# File lib/rattler/parsers/rule_set.rb, line 50
def includes
  attrs[:includes] || []
end
inherited_rule(name) click to toggle source

@param [Symbol] name the name of a rule in an inherited rule set @return [Rule] the rule with the given name in the inherited rule set

# 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| ... } click to toggle source

@yield Run the block once for each rule @yieldparam [Rule] rule one rule in the rule set @yieldreturn [Rule] a rule to replace the one yielded to the block @return [RuleSet] a new rule set with the result of running the block

once for each rule
# File lib/rattler/parsers/rule_set.rb, line 99
def map_rules
  self.with_rules rules.map {|rule| yield rule }
end
rule(name) click to toggle source

@param [Symbol] name the name of a rule in the rule set @return [Rule] the rule with the given name in the rule set

# File lib/rattler/parsers/rule_set.rb, line 39
def rule(name)
  @by_name[name] || inherited_rule(name)
end
select_rules() { |rule| ... } click to toggle source

@yield Run the block as a predicate once for each rule @yieldparam [Rule] rule one rule in the rule set @yieldreturn [Boolean] true to include the rule in the new rule set @return [RuleSet] a new rule set with the rules for which the block

returns +true+
# File lib/rattler/parsers/rule_set.rb, line 108
def select_rules
  self.with_rules rules.select {|rule| yield rule }
end
start_rule() click to toggle source

@return [Symbol] the name of the rule to start parsing with

# File lib/rattler/parsers/rule_set.rb, line 33
def start_rule
  attrs[:start_rule]
end
with_attrs(new_attrs) click to toggle source

@param [Hash] new_attrs a hash of attributes to merge with the existing

attributes

@return [RuleSet] a new rule set with the same rules and the new

attributes merged
# 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) click to toggle source

@param [Array<Rule>] new_rules the new list of rules to replace the

existing rules

@return [RuleSet] a new rule set with a new list of rules and the same

attributes
# File lib/rattler/parsers/rule_set.rb, line 90
def with_rules(new_rules)
  self.class.new(new_rules, attrs)
end