Skip to content

Commit 60f43fb

Browse files
ringaboutkhchen
authored andcommitted
fixes #19104; peg Incorrect captures [backport:1.6] (#20352)
* fixes #19104; peg Incorrect captures [backport:1.6] * add tests Co-authored-by: khchen <[email protected]> (cherry picked from commit 2b80ff2)
1 parent e32de02 commit 60f43fb

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

lib/pure/pegs.nim

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,10 +836,13 @@ template matchOrParse(mopProc: untyped) =
836836
var idx = c.ml # reserve a slot for the subpattern
837837
result = mopProc(s, p.sons[0], start, c)
838838
if result >= 0:
839-
inc(c.ml)
840839
if idx < MaxSubpatterns:
840+
if idx != c.ml:
841+
for i in countdown(c.ml, idx):
842+
c.matches[i+1] = c.matches[i]
841843
c.matches[idx] = (start, start+result-1)
842844
#else: silently ignore the capture
845+
inc(c.ml)
843846
leave(pkCapture, s, p, start, result)
844847
of pkBackRef:
845848
enter(pkBackRef, s, p, start)

tests/stdlib/tpegs.nim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ block:
158158
privateAccess(NonTerminal)
159159
privateAccess(Captures)
160160

161+
if "test" =~ peg"s <- {{\ident}}": # bug #19104
162+
doAssert matches[0] == "test"
163+
doAssert matches[1] == "test", $matches[1]
164+
161165
doAssert escapePeg("abc''def'") == r"'abc'\x27\x27'def'\x27"
162166
doAssert match("(a b c)", peg"'(' @ ')'")
163167
doAssert match("W_HI_Le", peg"\y 'while'")

0 commit comments

Comments
 (0)