class PatternMatch::Env
Public Class Methods
new(ctx, val)
click to toggle source
# File lib/pattern-match/core.rb, line 477 def initialize(ctx, val) @ctx = ctx @val = val end
Private Instance Methods
And(*subpatterns)
click to toggle source
# File lib/pattern-match/core.rb, line 558 def And(*subpatterns) PatternAnd.new(*subpatterns) end
Not(*subpatterns)
click to toggle source
# File lib/pattern-match/core.rb, line 566 def Not(*subpatterns) PatternNot.new(*subpatterns) end
Or(*subpatterns)
click to toggle source
# File lib/pattern-match/core.rb, line 562 def Or(*subpatterns) PatternOr.new(*subpatterns) end
Seq(*subpatterns)
click to toggle source
# File lib/pattern-match/core.rb, line 554 def Seq(*subpatterns) PatternSequence.new(*subpatterns) end
_(*vals)
click to toggle source
# File lib/pattern-match/core.rb, line 523 def _(*vals) case vals.length when 0 uscore = PatternVariable.new(:_) class << uscore def [](*args) Array.(*args) end def vars [] end private def bind(val) end end uscore when 1 PatternValue.new(vals[0]) when 2 PatternValue.new(vals[0], vals[1]) else ::Kernel.raise MalformedPatternError end end
___()
click to toggle source
# File lib/pattern-match/core.rb, line 505 def ___ PatternQuantifier.new(0, true) end
___?()
click to toggle source
# File lib/pattern-match/core.rb, line 509 def ___? PatternQuantifier.new(0, false) end
guard(&block)
click to toggle source
# File lib/pattern-match/core.rb, line 501 def guard(&block) block end
method_missing(name, *args)
click to toggle source
# File lib/pattern-match/core.rb, line 513 def method_missing(name, *args) ::Kernel.raise ::ArgumentError, "wrong number of arguments (#{args.length} for 0)" unless args.empty? case name.to_s when /\A__(\d+)(\??)\z/ PatternQuantifier.new($1.to_i, $2.empty?) else PatternVariable.new(name) end end
quasibinding_module(obj)
click to toggle source
# File lib/pattern-match/core.rb, line 600 def quasibinding_module(obj) m = obj.singleton_class.ancestors.find {|i| i.kind_of?(QuasiBindingModule) } unless m m = QuasiBindingModule.new do @stacks = ::Hash.new {|h, k| h[k] = [] } end obj.singleton_class.class_eval do if respond_to?(:prepend, true) prepend m else include m end end end m end
validate_uniqueness_of_dup_vars(vars)
click to toggle source
# File lib/pattern-match/core.rb, line 570 def validate_uniqueness_of_dup_vars(vars) vars.group_by(&:name).all? {|_, dup_vars| dup_vars.map(&:val).uniq.length == 1 } end
with(pat_or_val, guard_proc = nil, &block)
click to toggle source
# File lib/pattern-match/core.rb, line 484 def with(pat_or_val, guard_proc = nil, &block) ctx = @ctx pat = pat_or_val.kind_of?(Pattern) ? pat_or_val : PatternValue.new(pat_or_val) pat.append(PatternCondition.new { validate_uniqueness_of_dup_vars(pat.vars) }) if guard_proc pat.append(PatternCondition.new { with_quasibinding(ctx, pat.quasibinding, &guard_proc) }) end pat.validate if pat.match([@val]) ret = with_quasibinding(ctx, pat.quasibinding, &block) ::Kernel.throw(self, ret) else nil end rescue PatternNotMatch end
with_quasibinding(obj, quasibinding, &block)
click to toggle source
# File lib/pattern-match/core.rb, line 577 def with_quasibinding(obj, quasibinding, &block) quasibinding_module(obj).module_eval do begin quasibinding.each do |name, val| stack = @stacks[name] if stack.empty? define_method(name) { stack[-1] } private name end stack.push(val) end obj.instance_eval(&block) ensure quasibinding.each do |name, _| @stacks[name].pop if @stacks[name].empty? remove_method(name) end end end end end