Class: BabelBridge::Rule
- Inherits:
-
Object
- Object
- BabelBridge::Rule
- Defined in:
- lib/rule.rb
Overview
Rules define one or more patterns (RuleVariants) to match for a given non-terminal
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#node_class ⇒ Object
Returns the value of attribute node_class.
-
#parser ⇒ Object
Returns the value of attribute parser.
-
#variants ⇒ Object
Returns the value of attribute variants.
Instance Method Summary collapse
- #add_variant(options = {}, &block) ⇒ Object
-
#initialize(name, parser) ⇒ Rule
constructor
A new instance of Rule.
-
#inspect ⇒ Object
inspect returns a string which approximates the syntax for generating the rule and all its variants.
- #parse(node) ⇒ Object
- #root_rule? ⇒ Boolean
-
#to_s ⇒ Object
returns a more human-readable explanation of the rule.
Constructor Details
#initialize(name, parser) ⇒ Rule
Returns a new instance of Rule.
20 21 22 23 24 25 |
# File 'lib/rule.rb', line 20 def initialize(name,parser) @name = name @variants = [] @parser = parser @node_class = create_node_class end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/rule.rb', line 4 def name @name end |
#node_class ⇒ Object
Returns the value of attribute node_class.
4 5 6 |
# File 'lib/rule.rb', line 4 def node_class @node_class end |
#parser ⇒ Object
Returns the value of attribute parser.
4 5 6 |
# File 'lib/rule.rb', line 4 def parser @parser end |
#variants ⇒ Object
Returns the value of attribute variants.
4 5 6 |
# File 'lib/rule.rb', line 4 def variants @variants end |
Instance Method Details
#add_variant(options = {}, &block) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/rule.rb', line 31 def add_variant(={}, &block) new_variant_node_class.tap do |variant_node_class| [:variant_node_class] = variant_node_class [:rule] = self variants << RuleVariant.new() variant_node_class.class_eval &block if block end end |
#inspect ⇒ Object
inspect returns a string which approximates the syntax for generating the rule and all its variants
56 57 58 59 60 |
# File 'lib/rule.rb', line 56 def inspect variants.collect do |v| "rule #{name.inspect}, #{v.inspect}" end.join("\n") end |
#parse(node) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/rule.rb', line 40 def parse(node) if cached = node.parser.cached(name,node.next) return cached == :no_match ? nil : cached # return nil if cached==:no_matched end variants.each do |v| if match = v.parse(node) node.parser.cache_match(name,match) return match end end node.parser.cache_no_match(name,node.next) nil end |
#root_rule? ⇒ Boolean
27 28 29 |
# File 'lib/rule.rb', line 27 def root_rule? parser.root_rule == name end |
#to_s ⇒ Object
returns a more human-readable explanation of the rule
63 64 65 66 |
# File 'lib/rule.rb', line 63 def to_s "rule #{name.inspect}, node_class: #{node_class}\n\t"+ "#{variants.collect {|v|v.to_s}.join("\n\t")}" end |