class PatternMatch::PatternVariable

Attributes

name[R]
val[R]

Public Class Methods

new(name) click to toggle source
Calls superclass method PatternMatch::Pattern::new
# File lib/pattern-match/core.rb, line 229
def initialize(name)
  super()
  @name = name
  @val = nil
  @bind_to = nil
end

Public Instance Methods

<<(converter) click to toggle source
# File lib/pattern-match/experimental.rb, line 158
def <<(converter)
  @converter = converter.respond_to?(:call) ? converter : converter.to_proc
  self
end
inspect() click to toggle source
# File lib/pattern-match/core.rb, line 275
def inspect
  "#<#{self.class.name}: name=#{name.inspect}, val=#{@val.inspect}>"
end
match(vals) click to toggle source
Calls superclass method PatternMatch::Pattern#match
# File lib/pattern-match/core.rb, line 236
def match(vals)
  super do |val|
    bind(val)
    true
  end
end
set_bind_to(quantifier) click to toggle source
# File lib/pattern-match/core.rb, line 247
def set_bind_to(quantifier)
  n = nest_level(quantifier)
  if n == 0
    @val = @bind_to = []
  else
    outer = @val
    (n - 1).times do
      outer = outer[-1]
    end
    @bind_to = []
    outer << @bind_to
  end
end
unset_bind_to(quantifier) click to toggle source
# File lib/pattern-match/core.rb, line 261
def unset_bind_to(quantifier)
  n = nest_level(quantifier)
  @bind_to = nil
  if n == 0
    # do nothing
  else
    outer = @val
    (n - 1).times do
      outer = outer[-1]
    end
    outer.pop
  end
end
vars() click to toggle source
# File lib/pattern-match/core.rb, line 243
def vars
  [self]
end

Private Instance Methods

bind(val) click to toggle source
# File lib/pattern-match/core.rb, line 281
def bind(val)
  if quantified?
    @bind_to << val
  else
    @val = val
  end
end
nest_level(quantifier) click to toggle source
# File lib/pattern-match/core.rb, line 289
def nest_level(quantifier)
  raise PatternMatchError unless quantifier.kind_of?(PatternQuantifier)
  qs = ancestors.map {|i| (i.next and i.next.quantifier?) ? i.next : nil }.compact.reverse
  qs.index(quantifier) || (raise PatternMatchError)
end