Class: PatternMatch::Pattern
- Inherits:
-
Object
- Object
- PatternMatch::Pattern
show all
- Defined in:
- lib/pattern-match.rb
Instance Attribute Summary (collapse)
Instance Method Summary
(collapse)
Constructor Details
- (Pattern) initialize(*subpatterns)
A new instance of Pattern
135
136
137
138
139
140
141
142
|
# File 'lib/pattern-match.rb', line 135
def initialize(*subpatterns)
@parent = nil
@next = nil
@prev = nil
@pattern_match_env = nil
@subpatterns = subpatterns.map {|i| i.is_a?(Pattern) ? i : PatternValue.new(i) }
set_subpatterns_relation
end
|
Instance Attribute Details
- (Object) next
Returns the value of attribute next
132
133
134
|
# File 'lib/pattern-match.rb', line 132
def next
@next
end
|
- (Object) parent
Returns the value of attribute parent
132
133
134
|
# File 'lib/pattern-match.rb', line 132
def parent
@parent
end
|
- (Object) pattern_match_env
185
186
187
|
# File 'lib/pattern-match.rb', line 185
def pattern_match_env
@pattern_match_env || @parent.pattern_match_env
end
|
- (Object) prev
Returns the value of attribute prev
132
133
134
|
# File 'lib/pattern-match.rb', line 132
def prev
@prev
end
|
Instance Method Details
- (Object) !@
160
161
162
|
# File 'lib/pattern-match.rb', line 160
def !@
PatternNot.new(self)
end
|
- (Object) &(pattern)
152
153
154
|
# File 'lib/pattern-match.rb', line 152
def &(pattern)
PatternAnd.new(self, pattern)
end
|
- (Object) binding
148
149
150
|
# File 'lib/pattern-match.rb', line 148
def binding
vars.each_with_object({}) {|v, h| h[v.name] = v.val }
end
|
- (Boolean) quantified?
168
169
170
|
# File 'lib/pattern-match.rb', line 168
def quantified?
@next.is_a?(PatternQuantifier) || (root? ? false : @parent.quantified?)
end
|
- (Boolean) root?
172
173
174
|
# File 'lib/pattern-match.rb', line 172
def root?
@parent == nil
end
|
- (Object) to_a
164
165
166
|
# File 'lib/pattern-match.rb', line 164
def to_a
[self, PatternQuantifier.new(0)]
end
|
- (Object) validate
176
177
178
179
180
181
182
183
|
# File 'lib/pattern-match.rb', line 176
def validate
if root?
dup_vars = vars - vars.uniq {|i| i.name }
raise MalformedPatternError, "duplicate variables: #{dup_vars.map(&:name).join(', ')}" unless dup_vars.empty?
end
raise MalformedPatternError if @subpatterns.count {|i| i.is_a?(PatternQuantifier) } > 1
@subpatterns.each(&:validate)
end
|
- (Object) vars
144
145
146
|
# File 'lib/pattern-match.rb', line 144
def vars
@subpatterns.map(&:vars).inject([], :concat)
end
|
- (Object) |(pattern)
156
157
158
|
# File 'lib/pattern-match.rb', line 156
def |(pattern)
PatternOr.new(self, pattern)
end
|