class PatternMatch::PatternSequence

Public Instance Methods

match(vals) click to toggle source
# File lib/pattern-match/core.rb, line 341
def match(vals)
  if directly_quantified?
    repeating_match(vals, @next.greedy?) do |rewind|
      if rewind.ntimes < @next.min_k
        next false
      end
      rewind.match(vals)
    end
  else
    with_rewind(make_rewind(1)) do |rewind|
      rewind.match(vals)
    end
  end
end
validate() click to toggle source
Calls superclass method PatternMatch::Pattern#validate
# File lib/pattern-match/core.rb, line 356
def validate
  super
  raise MalformedPatternError if subpatterns.empty?
  raise MalformedPatternError unless @parent.kind_of?(HasOrderedSubPatterns)
end

Private Instance Methods

generate_candidates(vals) click to toggle source
# File lib/pattern-match/core.rb, line 386
def generate_candidates(vals)
  vals.length.downto(0).map do |n|
    make_rewind(n)
  end
end
make_rewind(n) click to toggle source
# File lib/pattern-match/core.rb, line 364
def make_rewind(n)
  PatternRewind.new(n, subpatterns[0], directly_quantified? ? @next.next : @next)
end
repeating_match(vals, is_greedy) { |rewind| ... } click to toggle source
# File lib/pattern-match/core.rb, line 368
def repeating_match(vals, is_greedy)
  quantifier = @next
  candidates = generate_candidates(vals)
  (is_greedy ? candidates : candidates.reverse).each do |rewind|
    vars.each {|i| i.set_bind_to(quantifier) }
    begin
      with_rewind(rewind) do
        if yield rewind
          return true
        end
      end
    rescue PatternNotMatch
    end
    vars.each {|i| i.unset_bind_to(quantifier) }
  end
  false
end
with_rewind(rewind) { |rewind| ... } click to toggle source
# File lib/pattern-match/core.rb, line 392
def with_rewind(rewind)
  subpatterns[-1].next = rewind
  yield rewind
ensure
  subpatterns[-1].next = nil
end