Class: BabelBridge::RuleVariant
- Inherits:
-
Object
- Object
- BabelBridge::RuleVariant
- Defined in:
- lib/rule_variant.rb
Overview
Each Rule has one or more RuleVariant Rules attempt to match each of their Variants in order. The first one to succeed returns true and the Rule succeeds.
Instance Attribute Summary collapse
-
#delimiter_pattern ⇒ Object
Returns the value of attribute delimiter_pattern.
-
#match_delimiter_prepost ⇒ Object
Returns the value of attribute match_delimiter_prepost.
-
#pattern ⇒ Object
Returns the value of attribute pattern.
-
#rule ⇒ Object
Returns the value of attribute rule.
-
#variant_node_class ⇒ Object
Returns the value of attribute variant_node_class.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ RuleVariant
constructor
pattern: Array - the pattern to match rule: Rule instance variant_node_class: RuleVariant class.
- #inspect ⇒ Object
-
#parse(parent_node) ⇒ Object
returns a Node object if it matches, nil otherwise.
- #parse_element(element_parser, node) ⇒ Object
- #parser ⇒ Object
-
#pattern_elements ⇒ Object
convert the pattern into a set of lamba functions.
- #root_rule? ⇒ Boolean
- #to_s ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ RuleVariant
pattern: Array - the pattern to match rule: Rule instance variant_node_class: RuleVariant class
11 12 13 14 15 16 17 |
# File 'lib/rule_variant.rb', line 11 def initialize( = {}) @pattern = [:pattern] @rule = [:rule] @variant_node_class = [:variant_node_class] raise "variant_node_class required" unless variant_node_class @delimiter = [:delimiter] end |
Instance Attribute Details
#delimiter_pattern ⇒ Object
Returns the value of attribute delimiter_pattern.
6 7 8 |
# File 'lib/rule_variant.rb', line 6 def delimiter_pattern @delimiter_pattern end |
#match_delimiter_prepost ⇒ Object
Returns the value of attribute match_delimiter_prepost.
6 7 8 |
# File 'lib/rule_variant.rb', line 6 def match_delimiter_prepost @match_delimiter_prepost end |
#pattern ⇒ Object
Returns the value of attribute pattern.
6 7 8 |
# File 'lib/rule_variant.rb', line 6 def pattern @pattern end |
#rule ⇒ Object
Returns the value of attribute rule.
6 7 8 |
# File 'lib/rule_variant.rb', line 6 def rule @rule end |
#variant_node_class ⇒ Object
Returns the value of attribute variant_node_class.
6 7 8 |
# File 'lib/rule_variant.rb', line 6 def variant_node_class @variant_node_class end |
Instance Method Details
#inspect ⇒ Object
61 |
# File 'lib/rule_variant.rb', line 61 def inspect; pattern.collect {|a| a.inspect}.join(', '); end |
#parse(parent_node) ⇒ Object
returns a Node object if it matches, nil otherwise
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/rule_variant.rb', line 45 def parse(parent_node) #return parse_nongreedy_optional(src,offset,parent_node) # nongreedy optionals break standard PEG node = variant_node_class.new(parent_node, delimiter_pattern) node.match parser.delimiter_pattern if root_rule? pattern_elements.each do |pe| return unless node.match(pe) end node.pop_match if node.last_match && node.last_match.delimiter node.match parser.delimiter_pattern if root_rule? node && node.post_match_processing end |
#parse_element(element_parser, node) ⇒ Object
40 41 42 |
# File 'lib/rule_variant.rb', line 40 def parse_element(element_parser, node) node.add_match element_parser.parse(node), element_parser.name end |
#parser ⇒ Object
19 20 21 |
# File 'lib/rule_variant.rb', line 19 def parser @rule.parser end |
#pattern_elements ⇒ Object
convert the pattern into a set of lamba functions
36 37 38 |
# File 'lib/rule_variant.rb', line 36 def pattern_elements @pattern_elements||=pattern.collect { |match| [PatternElement.new(match, :rule_variant => self, :pattern_element => true), delimiter_pattern] }.flatten[0..-2] end |
#root_rule? ⇒ Boolean
23 24 25 |
# File 'lib/rule_variant.rb', line 23 def root_rule? rule.root_rule? end |
#to_s ⇒ Object
62 |
# File 'lib/rule_variant.rb', line 62 def to_s; "variant_class: #{variant_node_class}, pattern: #{inspect}"; end |