Class: BabelBridge::RuleVariant

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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(options = {})
  @pattern = options[:pattern]
  @rule = options[:rule]
  @variant_node_class = options[:variant_node_class]
  raise "variant_node_class required" unless variant_node_class
  @delimiter = options[:delimiter]
end

Instance Attribute Details

#delimiter_patternObject

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_prepostObject

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

#patternObject

Returns the value of attribute pattern.



6
7
8
# File 'lib/rule_variant.rb', line 6

def pattern
  @pattern
end

#ruleObject

Returns the value of attribute rule.



6
7
8
# File 'lib/rule_variant.rb', line 6

def rule
  @rule
end

#variant_node_classObject

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

#inspectObject



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

#parserObject



19
20
21
# File 'lib/rule_variant.rb', line 19

def parser
  @rule.parser
end

#pattern_elementsObject

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

Returns:

  • (Boolean)


23
24
25
# File 'lib/rule_variant.rb', line 23

def root_rule?
  rule.root_rule?
end

#to_sObject



62
# File 'lib/rule_variant.rb', line 62

def to_s; "variant_class: #{variant_node_class}, pattern: #{inspect}"; end