summaryrefslogtreecommitdiff
path: root/regexec.c
diff options
context:
space:
mode:
authorYves Orton <demerphq@gmail.com>2022-12-29 12:07:22 +0100
committerYves Orton <demerphq@gmail.com>2023-01-12 09:11:51 +0100
commitfe5492d916201ce31a107839a36bcb1435fe7bf0 (patch)
tree91da4178b61e1e788e5979b534396dc923760d35 /regexec.c
parent17419a88100044035ee6dd9f8947f7d411d94863 (diff)
downloadperl-fe5492d916201ce31a107839a36bcb1435fe7bf0.tar.gz
regcomp.c etc - rework branch reset so it works properly
Branch reset was hacked in without much thought about how it might interact with other features. Over time we added named capture and recursive patterns with GOSUB, but I guess because branch reset is somewhat esoteric we didnt notice the accumulating issues related to it. The main problem was my original hack used a fairly simple device to give multiple OPEN/CLOSE opcodes the same target buffer id. When it was introduced this was fine. When GOSUB was added later however, we overlooked at that this broke a key part of the book-keeping for GOSUB. A GOSUB regop needs to know where to jump to, and which close paren to stop at. However the structure of the regexp program can change from the time the regop is created. This means we keep track of every OPEN/CLOSE regop we encounter during parsing, and when something is inserted into the middle of the program we make sure to move the offsets we store for the OPEN/CLOSE data. This is essentially keyed and scaled to the number of parens we have seen. When branch reset is used however the number of OPEN/CLOSE regops is more than the number of logical buffers we have seen, and we only move one of the OPEN/CLOSE buffers that is in the branch reset. Which of course breaks things. Another issues with branch reset is that it creates weird artifacts like this: /(?|(?<a>a)|(?<b>b))(?&a)(?&b)/ where the (?&b) actually maps to the (?<a>a) capture buffer because they both have the same id. Another case is that you cannot check if $+{b} matched and $+{a} did not, because conceptually they were the same buffer under the hood. These bugs are now fixed. The "aliasing" of capture buffers to each other is now done virtually, and under the hood each capture buffer is distinct. We introduce the concept of a "logical parno" which is the user visible capture buffer id, and keep it distinct from the true capture buffer id. Most of the internal logic uses the "true parno" for its business, so a bunch of problems go away, and we keep maps from logical to physical parnos, and vice versa, along with a map that gives use the "next physical parno with the same logical parno". Thus we can quickly skip through the physical capture buffers to find the one that matched. This means we also have to introduce a logical_total_parens as well, to complement the already existing total_parens. The latter refers to the true number of capture buffers. The former represents the logical number visible to the user. It is helpful to consider the following table: Logical: $1 $2 $3 $2 $3 $4 $2 $5 Physical: 1 2 3 4 5 6 7 8 Next: 0 4 5 7 0 0 0 0 Pattern: /(pre)(?|(?<a>a)(?<b>b)|(?<c>c)(?<d>d)(?<e>e)|(?<f>))(post)/ The names are mapped to physical buffers. So $+{b} will show what is in physical buffer 3. But $3 will show whichever of buffer 3 or 5 matched. Similarly @{^CAPTURE} will contain 5 elements, not 8. But %+ will contain all 6 named buffers. Since the need to map these values is rare, we only store these maps when they are needed and branch reset has been used, when they are NULL it is assumed that physical and logical buffers are identical. Currently the way this change is implemented will likely break plug in regexp engines because they will be missing the new logical_total_parens field at the very least. Given that the perl internals code is somewhat poorly abstracted from the regexp engine, with parts of the abstraction leaking out, I think this is acceptable. If we want to make plug in regexp engines work properly IMO we need to add some more hooks that they need to implement than we currently do. For instance mg.c does more work than it should. Given there are only a few plug in regexp engines and that it is specialized work, I think this is acceptable. We can work with the authors to refine the API properly later.
Diffstat (limited to 'regexec.c')
-rw-r--r--regexec.c101
1 files changed, 72 insertions, 29 deletions
diff --git a/regexec.c b/regexec.c
index 333a459c88..a521ae3013 100644
--- a/regexec.c
+++ b/regexec.c
@@ -7920,6 +7920,18 @@ S_regmatch(pTHX_ regmatch_info *reginfo, char *startpos, regnode *prog)
do_ref:
type = OP(scan);
n = ARG(scan); /* which paren pair */
+ if (rex->logical_to_parno) {
+ n = rex->logical_to_parno[n];
+ do {
+ if (rex->lastparen < n || rex->offs[n].start == -1 || rex->offs[n].end == -1) {
+ n = rex->parno_to_logical_next[n];
+ }
+ else {
+ break;
+ }
+ } while(n);
+ if (!n) sayNO;
+ }
do_nref_ref_common:
ln = rex->offs[n].start;
@@ -11818,7 +11830,7 @@ Perl_reg_named_buff_fetch(pTHX_ REGEXP * const r, SV * const namesv,
&& RXp_OFFS_VALID(rx,nums[i]))
{
ret = newSVpvs("");
- CALLREG_NUMBUF_FETCH(r, nums[i], ret);
+ Perl_reg_numbered_buff_fetch_flags(aTHX_ r, nums[i], ret, REG_FETCH_ABSOLUTE);
if (!retarray)
return ret;
} else {
@@ -11971,16 +11983,24 @@ Perl_reg_named_buff_all(pTHX_ REGEXP * const r, const U32 flags)
}
void
-Perl_reg_numbered_buff_fetch(pTHX_ REGEXP * const r, const I32 paren,
+Perl_reg_numbered_buff_fetch(pTHX_ REGEXP * const re, const I32 paren,
SV * const sv)
{
- struct regexp *const rx = ReANY(r);
+ PERL_ARGS_ASSERT_REG_NUMBERED_BUFF_FETCH;
+ Perl_reg_numbered_buff_fetch_flags(aTHX_ re, paren, sv, 0);
+}
+
+void
+Perl_reg_numbered_buff_fetch_flags(pTHX_ REGEXP * const re, const I32 paren,
+ SV * const sv, U32 flags)
+{
+ struct regexp *const rx = ReANY(re);
char *s = NULL;
SSize_t i,t = 0;
SSize_t s1, t1;
I32 n = paren;
- PERL_ARGS_ASSERT_REG_NUMBERED_BUFF_FETCH;
+ PERL_ARGS_ASSERT_REG_NUMBERED_BUFF_FETCH_FLAGS;
if ( n == RX_BUFF_IDX_CARET_PREMATCH
|| n == RX_BUFF_IDX_CARET_FULLMATCH
@@ -11993,7 +12013,7 @@ Perl_reg_numbered_buff_fetch(pTHX_ REGEXP * const r, const I32 paren,
* $r = qr/.../;
* /$qr/p;
* the KEEPCOPY is set on the PMOP rather than the regex */
- if (PL_curpm && r == PM_GETRE(PL_curpm))
+ if (PL_curpm && re == PM_GETRE(PL_curpm))
keepcopy = cBOOL(PL_curpm->op_pmflags & PMf_KEEPCOPY);
}
if (!keepcopy)
@@ -12021,18 +12041,32 @@ Perl_reg_numbered_buff_fetch(pTHX_ REGEXP * const r, const I32 paren,
s = rx->subbeg - rx->suboffset + t;
i = rx->sublen + rx->suboffset - t;
}
- else
- if (inRANGE(n, 0, (I32)rx->nparens) &&
- ((s1 = RXp_OFFS_START(rx,n)) != -1 &&
- (t1 = RXp_OFFS_END(rx,n)) != -1))
- {
- /* $&, ${^MATCH}, $1 ... */
- i = t1 - s1;
- s = rx->subbeg + s1 - rx->suboffset;
+ else /* when flags is true we do an absolute lookup, and compare against rx->nparens */
+ if (inRANGE(n, 0, flags ? (I32)rx->nparens : (I32)rx->logical_nparens)) {
+ I32 *map = (!flags && n) ? rx->logical_to_parno : NULL;
+ I32 true_parno = map ? map[n] : n;
+ do {
+ if (((s1 = RXp_OFFS_START(rx,true_parno)) != -1) &&
+ ((t1 = RXp_OFFS_END(rx,true_parno)) != -1))
+ {
+ /* $&, ${^MATCH}, $1 ... */
+ i = t1 - s1;
+ s = rx->subbeg + s1 - rx->suboffset;
+ goto found_it;
+ }
+ else if (map) {
+ true_parno = rx->parno_to_logical_next[true_parno];
+ }
+ else {
+ break;
+ }
+ } while (true_parno);
+ goto ret_undef;
} else {
goto ret_undef;
}
+ found_it:
assert(s >= rx->subbeg);
assert((STRLEN)rx->sublen >= (STRLEN)((s - rx->subbeg) + i) );
if (i >= 0) {
@@ -12120,7 +12154,7 @@ Perl_reg_numbered_buff_length(pTHX_ REGEXP * const r, const SV * const sv,
switch (paren) {
case RX_BUFF_IDX_CARET_PREMATCH: /* ${^PREMATCH} */
case RX_BUFF_IDX_PREMATCH: /* $` */
- if ( (i= RXp_OFFS_START(rx,0)) != -1) {
+ if ( (i = RXp_OFFS_START(rx,0)) != -1) {
if (i > 0) {
s1 = 0;
t1 = i;
@@ -12131,29 +12165,38 @@ Perl_reg_numbered_buff_length(pTHX_ REGEXP * const r, const SV * const sv,
case RX_BUFF_IDX_CARET_POSTMATCH: /* ${^POSTMATCH} */
case RX_BUFF_IDX_POSTMATCH: /* $' */
- if ( (i = RXp_OFFS_END(rx,0)) != -1) {
+ if ( (i = RXp_OFFS_END(rx,0)) != -1 ) {
i = rx->sublen - i;
if (i > 0) {
- s1 = RXp_OFFS_END(rx,0);
- t1 = rx->sublen;
- goto getlen;
+ s1 = rx->offs[0].end;
+ t1 = rx->sublen;
+ goto getlen;
}
}
return 0;
default: /* $& / ${^MATCH}, $1, $2, ... */
- if (paren <= (I32)rx->nparens &&
- (s1 = RXp_OFFS_START(rx,paren)) != -1 &&
- (t1 = RXp_OFFS_END(rx,paren)) != -1)
- {
- i = t1 - s1;
- goto getlen;
- } else {
- warn_undef:
- if (ckWARN(WARN_UNINITIALIZED))
- report_uninit((const SV *)sv);
- return 0;
+ if (paren <= (I32)rx->logical_nparens) {
+ I32 true_paren = rx->logical_to_parno
+ ? rx->logical_to_parno[paren]
+ : paren;
+ do {
+ if (((s1 = RXp_OFFS_START(rx,true_paren)) != -1) &&
+ ((t1 = RXp_OFFS_END(rx,true_paren)) != -1))
+ {
+ i = t1 - s1;
+ goto getlen;
+ } else if (rx->parno_to_logical_next) {
+ true_paren = rx->parno_to_logical_next[true_paren];
+ } else {
+ break;
+ }
+ } while(true_paren);
}
+ warn_undef:
+ if (ckWARN(WARN_UNINITIALIZED))
+ report_uninit((const SV *)sv);
+ return 0;
}
getlen:
if (i > 0 && RXp_MATCH_UTF8(rx)) {