Class: PatternMatch::Pattern
Defined Under Namespace
Modules: Backtrackable
Instance Attribute Summary collapse
Instance Method Summary
collapse
#choice_points
Constructor Details
#initialize(*subpatterns) ⇒ Pattern
Returns a new instance of Pattern.
19
20
21
22
23
24
25
|
# File 'lib/pattern-match/core.rb', line 19
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
|
Instance Attribute Details
Returns the value of attribute next.
17
18
19
|
# File 'lib/pattern-match/core.rb', line 17
def next
@next
end
|
Returns the value of attribute parent.
17
18
19
|
# File 'lib/pattern-match/core.rb', line 17
def parent
@parent
end
|
Returns the value of attribute prev.
17
18
19
|
# File 'lib/pattern-match/core.rb', line 17
def prev
@prev
end
|
Instance Method Details
47
48
49
|
# File 'lib/pattern-match/core.rb', line 47
def !@
PatternNot.new(self)
end
|
#&(pattern) ⇒ Object
39
40
41
|
# File 'lib/pattern-match/core.rb', line 39
def &(pattern)
PatternAnd.new(self, pattern)
end
|
#ancestors ⇒ Object
31
32
33
|
# File 'lib/pattern-match/core.rb', line 31
def ancestors
root? ? [self] : parent.ancestors.unshift(self)
end
|
#append(pattern) ⇒ Object
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/pattern-match/core.rb', line 97
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? ⇒ Boolean
63
64
65
|
# File 'lib/pattern-match/core.rb', line 63
def directly_quantified?
@next and @next.quantifier?
end
|
114
115
116
|
# File 'lib/pattern-match/core.rb', line 114
def inspect
"#<#{self.class.name}: subpatterns=#{subpatterns.inspect}>"
end
|
#match(vals) ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/pattern-match/core.rb', line 79
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? ⇒ Boolean
59
60
61
|
# File 'lib/pattern-match/core.rb', line 59
def quantified?
directly_quantified? or (root? ? false : @parent.quantified?)
end
|
#quantifier? ⇒ Boolean
55
56
57
|
# File 'lib/pattern-match/core.rb', line 55
def quantifier?
raise NotImplementedError
end
|
#quasibinding ⇒ Object
35
36
37
|
# File 'lib/pattern-match/core.rb', line 35
def quasibinding
vars.each_with_object({}) {|v, h| h[v.name] = v.val }
end
|
67
68
69
|
# File 'lib/pattern-match/core.rb', line 67
def root
root? ? self : @parent.root
end
|
#root? ⇒ Boolean
71
72
73
|
# File 'lib/pattern-match/core.rb', line 71
def root?
@parent == nil
end
|
51
52
53
|
# File 'lib/pattern-match/core.rb', line 51
def to_a
[self, PatternQuantifier.new(0, true)]
end
|
75
76
77
|
# File 'lib/pattern-match/core.rb', line 75
def validate
subpatterns.each(&:validate)
end
|
27
28
29
|
# File 'lib/pattern-match/core.rb', line 27
def vars
subpatterns.map(&:vars).flatten
end
|
#|(pattern) ⇒ Object
43
44
45
|
# File 'lib/pattern-match/core.rb', line 43
def |(pattern)
PatternOr.new(self, pattern)
end
|