class PatternMatch::Pattern
Attributes
next[RW]
parent[RW]
prev[RW]
subpatterns[R]
Public Class Methods
new(*subpatterns)
click to toggle source
# File lib/pattern-match/core.rb, line 24 def initialize(*subpatterns) @parent = nil @next = nil @prev = nil @subpatterns = subpatterns.map {|i| i.kind_of?(Pattern) ? i : PatternValue.new(i) } set_subpatterns_relation end
Public Instance Methods
&(pattern)
click to toggle source
# File lib/pattern-match/core.rb, line 44 def &(pattern) PatternAnd.new(self, pattern) end
ancestors()
click to toggle source
# File lib/pattern-match/core.rb, line 36 def ancestors root? ? [self] : parent.ancestors.unshift(self) end
append(pattern)
click to toggle source
# File lib/pattern-match/core.rb, line 102 def append(pattern) if @next @next.append(pattern) else if subpatterns.empty? if root? new_root = PatternAnd.new(self) self.parent = new_root end pattern.parent = @parent @next = pattern else subpatterns[-1].append(pattern) end end end
directly_quantified?()
click to toggle source
# File lib/pattern-match/core.rb, line 68 def directly_quantified? @next and @next.quantifier? end
inspect()
click to toggle source
# File lib/pattern-match/core.rb, line 119 def inspect "#<#{self.class.name}: subpatterns=#{subpatterns.inspect}>" end
match(vals) { |v| ... }
click to toggle source
# File lib/pattern-match/core.rb, line 84 def match(vals) if directly_quantified? q = @next repeating_match(vals, q.greedy?) do |vs, rest| if vs.length < q.min_k next false end vs.all? {|v| yield(v) } and q.match(rest) end else if vals.empty? return false end val, *rest = vals yield(val) and (@next ? @next.match(rest) : rest.empty?) end end
quantified?()
click to toggle source
# File lib/pattern-match/core.rb, line 64 def quantified? directly_quantified? or (root? ? false : @parent.quantified?) end
quantifier?()
click to toggle source
# File lib/pattern-match/core.rb, line 60 def quantifier? raise NotImplementedError end
quasibinding()
click to toggle source
# File lib/pattern-match/core.rb, line 40 def quasibinding vars.each_with_object({}) {|v, h| h[v.name] = v.val } end
root()
click to toggle source
# File lib/pattern-match/core.rb, line 72 def root root? ? self : @parent.root end
root?()
click to toggle source
# File lib/pattern-match/core.rb, line 76 def root? @parent == nil end
to_a()
click to toggle source
# File lib/pattern-match/core.rb, line 56 def to_a [self, PatternQuantifier.new(0, true)] end
validate()
click to toggle source
# File lib/pattern-match/core.rb, line 80 def validate subpatterns.each(&:validate) end
vars()
click to toggle source
# File lib/pattern-match/core.rb, line 32 def vars subpatterns.flat_map(&:vars) end
|(pattern)
click to toggle source
# File lib/pattern-match/core.rb, line 48 def |(pattern) PatternOr.new(self, pattern) end
Private Instance Methods
generate_candidates(vals)
click to toggle source
# File lib/pattern-match/core.rb, line 141 def generate_candidates(vals) vals.length.downto(0).map do |n| [vals.take(n), vals.drop(n)] end end
repeating_match(vals, is_greedy) { |vs, rest| ... }
click to toggle source
# File lib/pattern-match/core.rb, line 125 def repeating_match(vals, is_greedy) quantifier = @next candidates = generate_candidates(vals) (is_greedy ? candidates : candidates.reverse).each do |(vs, rest)| vars.each {|i| i.set_bind_to(quantifier) } begin if yield vs, rest return true end rescue PatternNotMatch end vars.each {|i| i.unset_bind_to(quantifier) } end false end
set_subpatterns_relation()
click to toggle source
# File lib/pattern-match/core.rb, line 147 def set_subpatterns_relation subpatterns.each do |i| i.parent = self end end