pattern-match 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +0 -4
- data/Gemfile +6 -0
- data/LEGAL +4 -0
- data/lib/pattern-match/core.rb +18 -31
- data/lib/pattern-match/deconstructor.rb +4 -2
- data/lib/pattern-match/disable_refinements.rb +8 -6
- data/lib/pattern-match/experimental.rb +2 -4
- data/lib/pattern-match/version.rb +1 -1
- data/pattern-match.gemspec +4 -3
- data/test/helper.rb +6 -0
- data/test/test_experimental.rb +4 -4
- data/test/test_standard.rb +9 -7
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7472cd3f781f3de13a150b3f7451f1b6c7acbebc
|
4
|
+
data.tar.gz: a400bedeed0e8e87ec9a0c56e283184083f34988
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c94fa67c645a45c2e67d3c02a2e86e0612a61749ccbc9098469d784e5afa65891d9dc88311f3ae4b73a27c1fa252d0db31789ad4739f96840100a3633a95f98d
|
7
|
+
data.tar.gz: 3822c0b6c0d94237c589e1f15aa93818c399ffb677bdfce95c6e8ae5082ddd4fb51e5d00b35222e044b2bd9b3041319bbae5ad10f085d36bcf61e09e553932e9
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
@@ -16,10 +16,6 @@ matrix:
|
|
16
16
|
env: TEST="test/test_*"
|
17
17
|
- rvm: 1.9.3
|
18
18
|
env: TEST="test/test_standard.rb"
|
19
|
-
- rvm: 2.0.0
|
20
|
-
env: TEST="test/test_*"
|
21
|
-
- rvm: 2.0.0
|
22
|
-
env: TEST="test/test_standard.rb"
|
23
19
|
- rvm: jruby-19mode
|
24
20
|
env: TEST="test/test_*"
|
25
21
|
- rvm: jruby-19mode
|
data/Gemfile
CHANGED
data/LEGAL
ADDED
data/lib/pattern-match/core.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
require 'pattern-match/deconstructor'
|
2
2
|
|
3
|
+
using PatternMatch if respond_to?(:using, true)
|
4
|
+
|
3
5
|
module PatternMatch
|
4
6
|
module HasOrderedSubPatterns
|
5
7
|
private
|
@@ -16,6 +18,9 @@ module PatternMatch
|
|
16
18
|
class Pattern
|
17
19
|
attr_accessor :parent, :next, :prev
|
18
20
|
|
21
|
+
attr_reader :subpatterns
|
22
|
+
private :subpatterns
|
23
|
+
|
19
24
|
def initialize(*subpatterns)
|
20
25
|
@parent = nil
|
21
26
|
@next = nil
|
@@ -25,7 +30,7 @@ module PatternMatch
|
|
25
30
|
end
|
26
31
|
|
27
32
|
def vars
|
28
|
-
subpatterns.
|
33
|
+
subpatterns.flat_map(&:vars)
|
29
34
|
end
|
30
35
|
|
31
36
|
def ancestors
|
@@ -117,8 +122,6 @@ module PatternMatch
|
|
117
122
|
|
118
123
|
private
|
119
124
|
|
120
|
-
attr_reader :subpatterns
|
121
|
-
|
122
125
|
def repeating_match(vals, is_greedy)
|
123
126
|
quantifier = @next
|
124
127
|
candidates = generate_candidates(vals)
|
@@ -200,8 +203,6 @@ module PatternMatch
|
|
200
203
|
class PatternObjectDeconstructor < PatternDeconstructor
|
201
204
|
include HasOrderedSubPatterns
|
202
205
|
|
203
|
-
using PatternMatch if respond_to?(:using, true)
|
204
|
-
|
205
206
|
def initialize(deconstructor, *subpatterns)
|
206
207
|
super(*subpatterns)
|
207
208
|
@deconstructor = deconstructor
|
@@ -483,14 +484,14 @@ module PatternMatch
|
|
483
484
|
def with(pat_or_val, guard_proc = nil, &block)
|
484
485
|
ctx = @ctx
|
485
486
|
pat = pat_or_val.kind_of?(Pattern) ? pat_or_val : PatternValue.new(pat_or_val)
|
486
|
-
pat.append(PatternCondition.new {
|
487
|
+
pat.append(PatternCondition.new { validate_uniqueness_of_dup_vars(pat.vars) })
|
487
488
|
if guard_proc
|
488
489
|
pat.append(PatternCondition.new { with_quasibinding(ctx, pat.quasibinding, &guard_proc) })
|
489
490
|
end
|
490
491
|
pat.validate
|
491
492
|
if pat.match([@val])
|
492
493
|
ret = with_quasibinding(ctx, pat.quasibinding, &block)
|
493
|
-
::Kernel.throw(
|
494
|
+
::Kernel.throw(self, ret)
|
494
495
|
else
|
495
496
|
nil
|
496
497
|
end
|
@@ -524,8 +525,6 @@ module PatternMatch
|
|
524
525
|
when 0
|
525
526
|
uscore = PatternVariable.new(:_)
|
526
527
|
class << uscore
|
527
|
-
using PatternMatch if respond_to?(:using, true)
|
528
|
-
|
529
528
|
def [](*args)
|
530
529
|
Array.(*args)
|
531
530
|
end
|
@@ -568,17 +567,8 @@ module PatternMatch
|
|
568
567
|
PatternNot.new(*subpatterns)
|
569
568
|
end
|
570
569
|
|
571
|
-
def
|
572
|
-
vars.
|
573
|
-
if h.has_key?(v.name)
|
574
|
-
unless h[v.name] == v.val
|
575
|
-
return false
|
576
|
-
end
|
577
|
-
else
|
578
|
-
h[v.name] = v.val
|
579
|
-
end
|
580
|
-
end
|
581
|
-
true
|
570
|
+
def validate_uniqueness_of_dup_vars(vars)
|
571
|
+
vars.group_by(&:name).all? {|_, dup_vars| dup_vars.map(&:val).uniq.length == 1 }
|
582
572
|
end
|
583
573
|
|
584
574
|
class QuasiBindingModule < ::Module
|
@@ -630,26 +620,23 @@ module PatternMatch
|
|
630
620
|
class NoMatchingPatternError < PatternMatchError; end
|
631
621
|
class MalformedPatternError < PatternMatchError; end
|
632
622
|
|
633
|
-
# Make Pattern and its subclasses
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
private_constant c
|
640
|
-
end
|
623
|
+
# Make Pattern and its subclasses, etc private.
|
624
|
+
constants.each do |c|
|
625
|
+
klass = const_get(c)
|
626
|
+
next unless klass.kind_of?(Class)
|
627
|
+
if klass <= Pattern
|
628
|
+
private_constant c
|
641
629
|
end
|
642
|
-
private_constant :Env
|
643
630
|
end
|
631
|
+
private_constant :Env, :HasOrderedSubPatterns
|
644
632
|
|
645
633
|
refine Object do
|
646
|
-
|
647
634
|
private
|
648
635
|
|
649
636
|
def match(*vals, &block)
|
650
637
|
do_match = Proc.new do |val|
|
651
638
|
env = Env.new(self, val)
|
652
|
-
catch(
|
639
|
+
catch(env) do
|
653
640
|
env.instance_eval(&block)
|
654
641
|
raise NoMatchingPatternError
|
655
642
|
end
|
@@ -8,10 +8,12 @@ module PatternMatch
|
|
8
8
|
PatternObjectDeconstructor.new(self, *subpatterns)
|
9
9
|
end
|
10
10
|
end
|
11
|
+
end
|
11
12
|
|
12
|
-
|
13
|
-
using PatternMatch if respond_to?(:using, true)
|
13
|
+
using PatternMatch if respond_to?(:using, true)
|
14
14
|
|
15
|
+
module PatternMatch
|
16
|
+
module Deconstructable
|
15
17
|
def call(*subpatterns)
|
16
18
|
pattern_matcher(*subpatterns)
|
17
19
|
end
|
@@ -1,10 +1,12 @@
|
|
1
1
|
class Module
|
2
|
-
|
2
|
+
refine_orig = "__refine_orig_#{Time.now.to_i}".to_sym
|
3
|
+
|
4
|
+
methods = instance_methods(false) + private_instance_methods(false)
|
3
5
|
if methods.include?(:refine)
|
4
|
-
if methods.include?(
|
6
|
+
if methods.include?(refine_orig)
|
5
7
|
raise LoadError, "can't re-define Module#refine"
|
6
8
|
end
|
7
|
-
alias_method
|
9
|
+
alias_method refine_orig, :refine
|
8
10
|
remove_method :refine
|
9
11
|
end
|
10
12
|
|
@@ -17,9 +19,9 @@ class Module
|
|
17
19
|
ensure
|
18
20
|
remove_method :refine
|
19
21
|
|
20
|
-
if Kernel.respond_to?(
|
21
|
-
alias_method :refine,
|
22
|
-
remove_method
|
22
|
+
if Kernel.respond_to?(refine_orig, true)
|
23
|
+
alias_method :refine, refine_orig
|
24
|
+
remove_method refine_orig
|
23
25
|
end
|
24
26
|
end
|
25
27
|
end
|
@@ -4,6 +4,8 @@ require 'set'
|
|
4
4
|
|
5
5
|
raise LoadError, 'Module#prepend required' unless Module.respond_to?(:prepend, true)
|
6
6
|
|
7
|
+
using PatternMatch if respond_to?(:using, true)
|
8
|
+
|
7
9
|
module PatternMatch
|
8
10
|
class Pattern
|
9
11
|
module Backtrackable
|
@@ -50,8 +52,6 @@ module PatternMatch
|
|
50
52
|
end
|
51
53
|
|
52
54
|
module Deconstructable
|
53
|
-
using PatternMatch if respond_to?(:using, true)
|
54
|
-
|
55
55
|
remove_method :call
|
56
56
|
def call(*subpatterns)
|
57
57
|
if Object == self
|
@@ -180,8 +180,6 @@ class Hash
|
|
180
180
|
end
|
181
181
|
|
182
182
|
class Object
|
183
|
-
using PatternMatch if respond_to?(:using, true)
|
184
|
-
|
185
183
|
def assert_pattern(pattern)
|
186
184
|
match(self) do
|
187
185
|
Kernel.eval("with(#{pattern}) { self }", Kernel.binding)
|
data/pattern-match.gemspec
CHANGED
@@ -16,10 +16,11 @@ Gem::Specification.new do |s|
|
|
16
16
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
17
|
s.executables = `git ls-files -- bin/*`.split("\n").map{|f| File.basename(f) }
|
18
18
|
s.require_paths = ['lib']
|
19
|
-
s.add_development_dependency 'test-unit'
|
20
|
-
s.add_development_dependency 'rake'
|
21
|
-
s.add_development_dependency 'simplecov'
|
22
19
|
s.extra_rdoc_files = ['README.rdoc']
|
23
20
|
s.rdoc_options = ['--main', 'README.rdoc']
|
24
21
|
s.licenses = ['2-clause BSDL', "Ruby's"]
|
22
|
+
|
23
|
+
s.add_development_dependency 'test-unit'
|
24
|
+
s.add_development_dependency 'rake'
|
25
|
+
s.add_development_dependency 'simplecov'
|
25
26
|
end
|
data/test/helper.rb
CHANGED
data/test/test_experimental.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
require_relative 'helper'
|
2
2
|
require 'test-unit'
|
3
3
|
if ENV['DISABLE_REFINEMENTS']
|
4
|
-
|
5
|
-
|
4
|
+
require 'pattern-match/disable_refinements'
|
5
|
+
require 'pattern-match'
|
6
6
|
else
|
7
|
-
|
7
|
+
require 'pattern-match'
|
8
8
|
using PatternMatch
|
9
9
|
end
|
10
10
|
begin
|
11
|
-
|
11
|
+
require 'pattern-match/experimental'
|
12
12
|
rescue LoadError
|
13
13
|
end
|
14
14
|
|
data/test/test_standard.rb
CHANGED
@@ -1,14 +1,16 @@
|
|
1
1
|
require_relative 'helper'
|
2
2
|
require 'test-unit'
|
3
3
|
if ENV['DISABLE_REFINEMENTS']
|
4
|
-
|
5
|
-
|
4
|
+
require 'pattern-match/disable_refinements'
|
5
|
+
require 'pattern-match'
|
6
6
|
else
|
7
|
-
|
7
|
+
require 'pattern-match'
|
8
8
|
using PatternMatch
|
9
9
|
end
|
10
10
|
|
11
11
|
class TestStandard < Test::Unit::TestCase
|
12
|
+
include TestUtils
|
13
|
+
|
12
14
|
def test_basic
|
13
15
|
this = self
|
14
16
|
ret = match([0, 1, 2, 3]) do
|
@@ -631,14 +633,14 @@ class TestStandard < Test::Unit::TestCase
|
|
631
633
|
|
632
634
|
def test_refinements
|
633
635
|
if ENV['DISABLE_REFINEMENTS']
|
634
|
-
assert_kind_of(PatternMatch.const_get(:Pattern),
|
635
|
-
assert_equal(0,
|
636
|
+
assert_kind_of(PatternMatch.const_get(:Pattern), eval_in_unrefined_scope('Class.()'))
|
637
|
+
assert_equal(0, eval_in_unrefined_scope('match(0) { with(_) { 0 } }'))
|
636
638
|
else
|
637
639
|
assert_raises(NoMethodError) do
|
638
|
-
|
640
|
+
eval_in_unrefined_scope('Class.()')
|
639
641
|
end
|
640
642
|
assert_raises(NoMethodError) do
|
641
|
-
|
643
|
+
eval_in_unrefined_scope('match(0) { with(_) { 0 } }')
|
642
644
|
end
|
643
645
|
end
|
644
646
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pattern-match
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kazuki Tsujimoto
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-09-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: test-unit
|
@@ -65,6 +65,7 @@ files:
|
|
65
65
|
- BSDL
|
66
66
|
- COPYING
|
67
67
|
- Gemfile
|
68
|
+
- LEGAL
|
68
69
|
- README.rdoc
|
69
70
|
- Rakefile
|
70
71
|
- lib/pattern-match.rb
|
@@ -100,7 +101,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
100
101
|
version: '0'
|
101
102
|
requirements: []
|
102
103
|
rubyforge_project:
|
103
|
-
rubygems_version: 2.4.
|
104
|
+
rubygems_version: 2.4.6
|
104
105
|
signing_key:
|
105
106
|
specification_version: 4
|
106
107
|
summary: A pattern matching library
|