Peg captures get stack-like behavior (#18369)

* Implements reverse capture indexing.
* Now works for modified backrefs too.
* Changed reverse indexing syntax prefix for back-references to '$^'.
This commit is contained in:
gemath
2021-06-28 12:33:20 +02:00
committed by GitHub
parent 908b2cc2e4
commit e720bbdd76
3 changed files with 94 additions and 34 deletions

View File

@@ -293,6 +293,34 @@ block:
doAssert "test1".match(peg"""{@}$""")
doAssert "test2".match(peg"""{(!$ .)*} $""")
doAssert "abbb".match(peg"{a} {b} $2 $^1")
doAssert "abBA".match(peg"{a} {b} i$2 i$^2")
doAssert "abba".match(peg"{a} {b} $^1 {} $^1")
block:
let grammar = peg"""
program <- {''} stmt* $
stmt <- call / block
call <- 'call()' EOL
EOL <- \n / $
block <- 'block:' \n indBody
indBody <- {$^1 ' '+} stmt ($^1 stmt)* {}
"""
let program = """
call()
block:
block:
call()
call()
call()
call()
"""
var c: Captures
doAssert program.len == program.rawMatch(grammar, 0, c)
doAssert c.ml == 1
pegsTest()
static:
pegsTest()