Class: Rattler::Parsers::RuleSet
- Inherits:
-
Util::Node
- Object
- Util::Node
- Rattler::Parsers::RuleSet
- 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
-
#[](*args) ⇒ Object
Access the rule set’s rules.
-
#analysis ⇒ Analysis
A static analysis of the rules.
-
#includes ⇒ Array<String>
A list of modules to be included in the parser.
-
#inherited_rule(name) ⇒ Rule
The rule with the given name in the inherited rule set.
-
#initialize(*args) ⇒ RuleSet
constructor
Create a
RuleSet
instance. -
#map_rules {|rule| ... } ⇒ RuleSet
A new rule set with the result of running the block once for each rule.
-
#rule(name) ⇒ Rule
The rule with the given name in the rule set.
-
#select_rules {|rule| ... } ⇒ RuleSet
A new rule set with the rules for which the block returns
true
. -
#start_rule ⇒ Symbol
The name of the rule to start parsing with.
-
#with_attrs(new_attrs) ⇒ RuleSet
A new rule set with the same rules and the new attributes merged.
-
#with_rules(new_rules) ⇒ RuleSet
A new rule set with a new list of rules and the same attributes.
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
#initialize ⇒ RuleSet #initialize(rule...) ⇒ RuleSet #initialize(attribute...) ⇒ RuleSet #initialize(rule..., attribute...) ⇒ RuleSet
Create a RuleSet
instance.
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
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 |
#analysis ⇒ Analysis
Returns 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 |
#includes ⇒ Array<String>
Returns 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.
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.
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.
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
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_rule ⇒ Symbol
Returns 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.
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.
90 91 92 |
# File 'lib/rattler/parsers/rule_set.rb', line 90 def with_rules(new_rules) self.class.new(new_rules, attrs) end |