base.match: fix bug when repeating multi-element patterns
s-ol
2 years ago
128 | 128 |
new: (@inner, @min, @max) =>
|
129 | 129 |
|
130 | 130 |
capture: (seq, i) =>
|
131 | |
take, all = 0, {}
|
|
131 |
total, rep, all = 0, 0, {}
|
132 | 132 |
while true
|
133 | |
num, cap = @inner\capture seq, i+take
|
|
133 |
num, cap = @inner\capture seq, i+total
|
134 | 134 |
break unless num
|
135 | 135 |
|
136 | |
take += num
|
|
136 |
total += num
|
|
137 |
rep += 1
|
137 | 138 |
table.insert all, cap
|
138 | 139 |
|
139 | |
break if @max and take >= @max
|
140 | |
|
141 | |
return if @min and take < @min
|
142 | |
return if @max and take > @max
|
143 | |
|
144 | |
take, all
|
|
140 |
break if @max and rep >= @max
|
|
141 |
|
|
142 |
return if @min and rep < @min
|
|
143 |
return if @max and rep > @max
|
|
144 |
|
|
145 |
total, all
|
145 | 146 |
|
146 | 147 |
reset: =>
|
147 | 148 |
@inner\reset!
|
|
187 | 188 |
@@ [e for e in *@elements], { ... }
|
188 | 189 |
|
189 | 190 |
__call: =>
|
190 | |
@@ [e! for e in *@elements]
|
|
191 |
@@ [e! for e in *@elements], @keys
|
191 | 192 |
|
192 | 193 |
__add: (other) =>
|
193 | 194 |
elements = [e for e in *@elements]
|
262 | 262 |
assert.has.error -> rep\match (times 3, str)
|
263 | 263 |
assert.has.error -> rep\match (times 2, num)
|
264 | 264 |
|
|
265 |
it 'works with complex inner types', ->
|
|
266 |
rep = (val.num + val.str)\rep 2, 2
|
|
267 |
assert.has.error -> rep\match {}
|
|
268 |
assert.has.error -> rep\match {num, str}
|
|
269 |
assert.has.error -> rep\match {num, num}
|
|
270 |
assert.is.same {{num, str}, {num, str}}, rep\match {num, str, num, str}
|
|
271 |
assert.has.error -> rep\match {str, str, str, str}
|
|
272 |
assert.has.error -> rep\match {num, str, num, str, num, str}
|
|
273 |
|
265 | 274 |
it 'stringifies well', ->
|
266 | 275 |
assert.is.equal 'str~{1-3}', tostring val.str*3
|
267 | 276 |
assert.is.equal 'str~{1-*}', tostring val.str*0
|