summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorringabout <43030857+ringabout@users.noreply.github.com>2022-09-15 13:54:53 +0800
committerGitHub <noreply@github.com>2022-09-15 07:54:53 +0200
commit2b80ff2374fce1610d366654d2afd570381ab4c0 (patch)
treef37fd477f16f13bcf4bac33a1d2ee3942ecb6875 /lib
parent79afee868d784eb90972deb3ea89c96702585968 (diff)
downloadNim-2b80ff2374fce1610d366654d2afd570381ab4c0.tar.gz
fixes #19104; peg Incorrect captures [backport:1.6] (#20352)
* fixes #19104; peg Incorrect captures [backport:1.6]

* add tests

Co-authored-by: khchen <khchen@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/pegs.nim5
1 files changed, 4 insertions, 1 deletions
diff --git a/lib/pure/pegs.nim b/lib/pure/pegs.nim
index 6319be551..a95700825 100644
--- a/lib/pure/pegs.nim
+++ b/lib/pure/pegs.nim
@@ -838,10 +838,13 @@ template matchOrParse(mopProc: untyped) =
         var idx = c.ml # reserve a slot for the subpattern
         result = mopProc(s, p.sons[0], start, c)
         if result >= 0:
-          inc(c.ml)
           if idx < MaxSubpatterns:
+            if idx != c.ml:
+              for i in countdown(c.ml, idx):
+                c.matches[i+1] = c.matches[i]
             c.matches[idx] = (start, start+result-1)
           #else: silently ignore the capture
+          inc(c.ml)
       leave(pkCapture, s, p, start, result)
     of pkBackRef:
       enter(pkBackRef, s, p, start)