Class: PatternMatch::PatternWithMatcher

Inherits:
PatternElement show all
Defined in:
lib/egison/core.rb

Instance Attribute Summary collapse

Attributes inherited from Pattern

#quantified

Instance Method Summary collapse

Methods inherited from Pattern

#to_a

Constructor Details

#initialize(matcher, *subpatterns) ⇒ PatternWithMatcher

Returns a new instance of PatternWithMatcher.



135
136
137
138
139
# File 'lib/egison/core.rb', line 135

def initialize(matcher, *subpatterns)
  super()
  @matcher = matcher
  @subpatterns = subpatterns
end

Instance Attribute Details

#matcherObject (readonly)

Returns the value of attribute matcher.



133
134
135
# File 'lib/egison/core.rb', line 133

def matcher
  @matcher
end

#subpatternsObject (readonly)

Returns the value of attribute subpatterns.



133
134
135
# File 'lib/egison/core.rb', line 133

def subpatterns
  @subpatterns
end

Instance Method Details

#match(tgt, bindings) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/egison/core.rb', line 141

def match(tgt, bindings)
  tgt = tgt.to_a
  if subpatterns.empty?
    if tgt.empty?
      return [[[], []]]
    else
      return []
    end
  else
    subpatterns = @subpatterns.clone
    px = subpatterns.shift
    if px.kind_of?(Pattern)
      if px.quantified
        if subpatterns.empty?
          [[[[px.pattern, tgt]], []]]
        else
          unjoineds = @matcher.unjoin(tgt)
          unjoineds.map do |xs, ys|
            [[[px.pattern, xs], [PatternWithMatcher.new(@matcher, *subpatterns), ys]], []]
          end
        end
      else
        if tgt.empty?
          []
        else
          unconseds = @matcher.uncons(tgt)
          unconseds.map do |x, xs|
            [[[px, x], [PatternWithMatcher.new(@matcher, *subpatterns), xs]], []]
          end
        end
      end
    else
      if tgt.empty?
        []
      else
        unconseds = @matcher.uncons(tgt).select { |x, xs| px == x }
        unconseds.map do |x, xs|
          [[[PatternWithMatcher.new(@matcher, *subpatterns), xs]], []]
        end
      end
    end
  end
end

#match_stream(tgt, bindings, &block) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/egison/core.rb', line 185

def match_stream(tgt, bindings, &block)
  if subpatterns.empty?
    if tgt.empty?
      return block.([[], []])
    end
  else
    subpatterns = @subpatterns.clone
    px = subpatterns.shift
    if px.kind_of?(Pattern)
      if px.quantified
        if subpatterns.empty?
          block.([[[px.pattern, tgt]], []])
        else
          @matcher.unjoin_stream(tgt) do |xs, ys|
            block.([[px.pattern, xs], [PatternWithMatcher.new(@matcher, *subpatterns), ys]], [])
          end
        end
      else
        unless tgt.empty?
          @matcher.uncons_stream(tgt) do |x, xs|
            block.([[px, x], [PatternWithMatcher.new(@matcher, *subpatterns), xs]], [])
          end
        end
      end
    else
      unless tgt.empty?
        @matcher.uncons_stream(tgt) do |x, xs|
          if px == x
            block.([[PatternWithMatcher.new(@matcher, *subpatterns), xs]], [])
          end
        end
      end
    end
  end
end