Class: PatternMatch::Pattern

Inherits:
Object
  • Object
show all
Defined in:
lib/pattern-match.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*subpatterns) ⇒ Pattern

Returns 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

#nextObject

Returns the value of attribute next



132
133
134
# File 'lib/pattern-match.rb', line 132

def next
  @next
end

#parentObject

Returns the value of attribute parent



132
133
134
# File 'lib/pattern-match.rb', line 132

def parent
  @parent
end

#pattern_match_envObject



185
186
187
# File 'lib/pattern-match.rb', line 185

def pattern_match_env
  @pattern_match_env || @parent.pattern_match_env
end

#prevObject

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

#&(pattern) ⇒ Object



152
153
154
# File 'lib/pattern-match.rb', line 152

def &(pattern)
  PatternAnd.new(self, pattern)
end

#bindingObject



148
149
150
# File 'lib/pattern-match.rb', line 148

def binding
  vars.each_with_object({}) {|v, h| h[v.name] = v.val }
end

#quantified?Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/pattern-match.rb', line 168

def quantified?
  @next.is_a?(PatternQuantifier) || (root? ? false : @parent.quantified?)
end

#root?Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/pattern-match.rb', line 172

def root?
  @parent == nil
end

#to_aObject



164
165
166
# File 'lib/pattern-match.rb', line 164

def to_a
  [self, PatternQuantifier.new(0)]
end

#validateObject



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

#varsObject



144
145
146
# File 'lib/pattern-match.rb', line 144

def vars
  @subpatterns.map(&:vars).inject([], :concat)
end

#|(pattern) ⇒ Object



156
157
158
# File 'lib/pattern-match.rb', line 156

def |(pattern)
  PatternOr.new(self, pattern)
end