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.
24
25
26
27
28
29
30
|
# 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
|
Instance Attribute Details
Returns the value of attribute next.
19
20
21
|
# File 'lib/pattern-match/core.rb', line 19
def next
@next
end
|
Returns the value of attribute parent.
19
20
21
|
# File 'lib/pattern-match/core.rb', line 19
def parent
@parent
end
|
Returns the value of attribute prev.
19
20
21
|
# File 'lib/pattern-match/core.rb', line 19
def prev
@prev
end
|
Instance Method Details
52
53
54
|
# File 'lib/pattern-match/core.rb', line 52
def !@
PatternNot.new(self)
end
|
#&(pattern) ⇒ Object
44
45
46
|
# File 'lib/pattern-match/core.rb', line 44
def &(pattern)
PatternAnd.new(self, pattern)
end
|
#ancestors ⇒ Object
36
37
38
|
# File 'lib/pattern-match/core.rb', line 36
def ancestors
root? ? [self] : parent.ancestors.unshift(self)
end
|
#append(pattern) ⇒ Object
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# 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? ⇒ Boolean
68
69
70
|
# File 'lib/pattern-match/core.rb', line 68
def directly_quantified?
@next and @next.quantifier?
end
|
119
120
121
|
# File 'lib/pattern-match/core.rb', line 119
def inspect
"#<#{self.class.name}: subpatterns=#{subpatterns.inspect}>"
end
|
#match(vals) ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# 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? ⇒ Boolean
64
65
66
|
# File 'lib/pattern-match/core.rb', line 64
def quantified?
directly_quantified? or (root? ? false : @parent.quantified?)
end
|
#quantifier? ⇒ Boolean
60
61
62
|
# File 'lib/pattern-match/core.rb', line 60
def quantifier?
raise NotImplementedError
end
|
#quasibinding ⇒ Object
40
41
42
|
# File 'lib/pattern-match/core.rb', line 40
def quasibinding
vars.each_with_object({}) {|v, h| h[v.name] = v.val }
end
|
72
73
74
|
# File 'lib/pattern-match/core.rb', line 72
def root
root? ? self : @parent.root
end
|
#root? ⇒ Boolean
76
77
78
|
# File 'lib/pattern-match/core.rb', line 76
def root?
@parent == nil
end
|
56
57
58
|
# File 'lib/pattern-match/core.rb', line 56
def to_a
[self, PatternQuantifier.new(0, true)]
end
|
80
81
82
|
# File 'lib/pattern-match/core.rb', line 80
def validate
subpatterns.each(&:validate)
end
|
32
33
34
|
# File 'lib/pattern-match/core.rb', line 32
def vars
subpatterns.flat_map(&:vars)
end
|
#|(pattern) ⇒ Object
48
49
50
|
# File 'lib/pattern-match/core.rb', line 48
def |(pattern)
PatternOr.new(self, pattern)
end
|