aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2020-05-21 15:00:38 +0000
committers-ol <s-ol@users.noreply.github.com>2020-05-21 15:00:38 +0000
commit267ed9a1a6848e7bf7ef7707b7d71c8ee4181e29 (patch)
treef57bb486c2148032e11e8607f6994f64826abf73
parentsmall internal doc fixes (diff)
downloadalive-267ed9a1a6848e7bf7ef7707b7d71c8ee4181e29.tar.gz
alive-267ed9a1a6848e7bf7ef7707b7d71c8ee4181e29.zip
base.match: fix bug when repeating multi-element patterns
-rw-r--r--alv/base/match.moon17
-rw-r--r--spec/match_spec.moon9
2 files changed, 18 insertions, 8 deletions
diff --git a/alv/base/match.moon b/alv/base/match.moon
index 37d2fad..65f53b1 100644
--- a/alv/base/match.moon
+++ b/alv/base/match.moon
@@ -129,20 +129,21 @@ class Repeat extends Pattern
new: (@inner, @min, @max) =>
capture: (seq, i) =>
- take, all = 0, {}
+ total, rep, all = 0, 0, {}
while true
- num, cap = @inner\capture seq, i+take
+ num, cap = @inner\capture seq, i+total
break unless num
- take += num
+ total += num
+ rep += 1
table.insert all, cap
- break if @max and take >= @max
+ break if @max and rep >= @max
- return if @min and take < @min
- return if @max and take > @max
+ return if @min and rep < @min
+ return if @max and rep > @max
- take, all
+ total, all
reset: =>
@inner\reset!
@@ -188,7 +189,7 @@ class Sequence extends Pattern
@@ [e for e in *@elements], { ... }
__call: =>
- @@ [e! for e in *@elements]
+ @@ [e! for e in *@elements], @keys
__add: (other) =>
elements = [e for e in *@elements]
diff --git a/spec/match_spec.moon b/spec/match_spec.moon
index 9764d5d..0047fd4 100644
--- a/spec/match_spec.moon
+++ b/spec/match_spec.moon
@@ -263,6 +263,15 @@ describe 'repeat', ->
assert.has.error -> rep\match (times 3, str)
assert.has.error -> rep\match (times 2, num)
+ it 'works with complex inner types', ->
+ rep = (val.num + val.str)\rep 2, 2
+ assert.has.error -> rep\match {}
+ assert.has.error -> rep\match {num, str}
+ assert.has.error -> rep\match {num, num}
+ assert.is.same {{num, str}, {num, str}}, rep\match {num, str, num, str}
+ assert.has.error -> rep\match {str, str, str, str}
+ assert.has.error -> rep\match {num, str, num, str, num, str}
+
it 'stringifies well', ->
assert.is.equal 'str~{1-3}', tostring val.str*3
assert.is.equal 'str~{1-*}', tostring val.str*0