Sha256: 5c35f9197540983e50baf9d367fdbcfe065726c0933ba79fd096e9251ca13269

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

module BabelBridge

# 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.
class RuleVariant
  attr_accessor :pattern, :rule, :variant_node_class

  def initialize(pattern, rule, variant_node_class=nil)
    @pattern = pattern
    @rule = rule
    @variant_node_class = variant_node_class
  end

  # convert the pattern into a set of lamba functions
  def pattern_elements
    @pattern_elements||=pattern.collect { |match| PatternElement.new match, self }
  end

  # returns a Node object if it matches, nil otherwise
  def parse(parent_node)
    #return parse_nongreedy_optional(src,offset,parent_node) # nongreedy optionals break standard PEG
    node = variant_node_class.new(parent_node)

    pattern_elements.each do |pe|
      match=pe.parse(node)

      # if parse failed
      return if !match

      # parse succeeded, add to node and continue
      node.add_match(match,pe.name)
    end
    node.post_match
  end

  def inspect; pattern.collect {|a| a.inspect}.join(', '); end
  def to_s; "variant_class: #{variant_node_class}, pattern: #{inspect}"; end  
end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
babel_bridge-0.4.0 lib/rule_variant.rb
babel_bridge-0.3.1 lib/rule_variant.rb