From 267ed9a1a6848e7bf7ef7707b7d71c8ee4181e29 Mon Sep 17 00:00:00 2001 From: s-ol Date: Thu, 21 May 2020 17:00:38 +0200 Subject: base.match: fix bug when repeating multi-element patterns --- alv/base/match.moon | 17 +++++++++-------- spec/match_spec.moon | 9 +++++++++ 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 -- cgit v1.2.3