git.s-ol.nu alive / 267ed9a
base.match: fix bug when repeating multi-element patterns s-ol 1 year, 8 months ago
2 changed file(s) with 20 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
128128 new: (@inner, @min, @max) =>
129129
130130 capture: (seq, i) =>
131 take, all = 0, {}
131 total, rep, all = 0, 0, {}
132132 while true
133 num, cap = @inner\capture seq, i+take
133 num, cap = @inner\capture seq, i+total
134134 break unless num
135135
136 take += num
136 total += num
137 rep += 1
137138 table.insert all, cap
138139
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
145146
146147 reset: =>
147148 @inner\reset!
187188 @@ [e for e in *@elements], { ... }
188189
189190 __call: =>
190 @@ [e! for e in *@elements]
191 @@ [e! for e in *@elements], @keys
191192
192193 __add: (other) =>
193194 elements = [e for e in *@elements]
262262 assert.has.error -> rep\match (times 3, str)
263263 assert.has.error -> rep\match (times 2, num)
264264
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
265274 it 'stringifies well', ->
266275 assert.is.equal 'str~{1-3}', tostring val.str*3
267276 assert.is.equal 'str~{1-*}', tostring val.str*0