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

@@ -27,7 +27,10 @@ notation meaning
``{E}`` Capture: Apply expression `E` and store the substring
that matched `E` into a *capture* that can be accessed
after the matching process.
``$i`` Back reference to the ``i``th capture. ``i`` counts from 1.
``{}`` Empty capture: Delete the last capture. No character
is consumed.
``$i`` Back reference to the ``i``th capture. ``i`` counts forwards
from 1 or backwards (last capture to first) from ^1.
``$`` Anchor: Matches at the end of the input. No character
is consumed. Same as ``!.``.
``^`` Anchor: Matches at the start of the input. No character
@@ -149,14 +152,15 @@ The PEG parser implements this grammar (written in PEG syntax)::
rule <- identifier \s* "<-" expr ig
identNoArrow <- identifier !(\s* "<-")
prefixOpr <- ig '&' / ig '!' / ig '@' / ig '{@}' / ig '@@'
literal <- ig identifier? '$' [0-9]+ / '$' / '^' /
literal <- ig identifier? '$' '^'? [0-9]+ / '$' / '^' /
ig identNoArrow /
ig charset /
ig stringlit /
ig builtin /
ig '.' /
ig '_' /
(ig "(" expr ig ")")
(ig "(" expr ig ")") /
(ig "{" expr? ig "}")
postfixOpr <- ig '?' / ig '*' / ig '+'
primary <- prefixOpr* (literal postfixOpr*)