In perl.git, the branch blead has been updated <http://perl5.git.perl.org/perl.git/commitdiff/1cb2b5d4ff78efbd407ee199ed0ed9ff0227cecb?hp=13cedc2a3e4c518d94e81e9837febef77948e0e5>
- Log ----------------------------------------------------------------- commit 1cb2b5d4ff78efbd407ee199ed0ed9ff0227cecb Author: Father Chrysostomos <[email protected]> Date: Fri Jun 7 18:14:43 2013 -0700 Allow qr/(?[ [a] ])/ interpolation in (?[...]) Interpolation fails if the interpolated extended character class con- tains any bracketed character classes itself. The sizing pass looks for [ and passes control to the regular charac- ter class parser. When the charclass is finished, it begins scanning for [ again. If it finds ], it assumes it is the end. That fails with (?[ (?a:(?[ [a] ])) ]). The sizing pass hands [ [a] ]... off to the charclass parser, which parses [ [a] and hands control back to the sizing pass. It then sees â ])) ])â, assumes that the first ]) is the end of the entire construct, so the main regexp parser sees the parenthesis following and dies. If we change the sizing pass to look for ?[ we can simply record the depth (depth++) and then when we see ] decrement the depth or exist the loop if it is zero. M regcomp.c M t/re/regex_sets.t commit 8b6fbf55492d62755d8dabe8593bae6befddd121 Author: Father Chrysostomos <[email protected]> Date: Fri Jun 7 13:13:46 2013 -0700 perldiag: Correct descr of âExpecting '(?flags:(?[...'â I did not realise at first that (?:(?[...])) is forbidden. M pod/perldiag.pod ----------------------------------------------------------------------- Summary of changes: pod/perldiag.pod | 8 ++++---- regcomp.c | 5 +++++ t/re/regex_sets.t | 3 +++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/pod/perldiag.pod b/pod/perldiag.pod index 21c0e91..86e1b46 100644 --- a/pod/perldiag.pod +++ b/pod/perldiag.pod @@ -1917,10 +1917,10 @@ but omitted the C<")">. =item Expecting '(?flags:(?[...' in regex; marked by <-- HERE in m/%s/ -(F) The C<(?[...])> extended character class regular expression -construct only allows character classes (including character class -escapes like C<\d>), operators, and parentheses. The one exception is -C<(?:...)> or C<(?flags:...)> containing exactly one C<(?[...])> construct. +(F) The C<(?[...])> extended character class regular expression construct +only allows character classes (including character class escapes like +C<\d>), operators, and parentheses. The one exception is C<(?flags:...)> +containing at least one flag and exactly one C<(?[...])> construct. This allows a regular expression containing just C<(?[...])> to be interpolated. If you see this error message, then you probably have some other C<(?...)> construct inside your character class. See diff --git a/regcomp.c b/regcomp.c index 173fd6c..eb91465 100644 --- a/regcomp.c +++ b/regcomp.c @@ -11664,6 +11664,7 @@ S_handle_regex_sets(pTHX_ RExC_state_t *pRExC_state, SV** return_invlist, I32 *f * these things, we need to realize that something preceded by a backslash * is escaped, so we have to keep track of backslashes */ if (SIZE_ONLY) { + UV depth = 0; /* how many nested (?[...]) constructs */ Perl_ck_warner_d(aTHX_ packWARN(WARN_EXPERIMENTAL__REGEX_SETS), @@ -11675,6 +11676,9 @@ S_handle_regex_sets(pTHX_ RExC_state_t *pRExC_state, SV** return_invlist, I32 *f RExC_parse = regpatws(pRExC_state, RExC_parse, TRUE); /* means recognize comments */ switch (*RExC_parse) { + case '?': + if (RExC_parse[1] == '[') depth++, RExC_parse++; + /* FALL THROUGH */ default: break; case '\\': @@ -11722,6 +11726,7 @@ S_handle_regex_sets(pTHX_ RExC_state_t *pRExC_state, SV** return_invlist, I32 *f } case ']': + if (depth--) break; RExC_parse++; if (RExC_parse < RExC_end && *RExC_parse == ')') diff --git a/t/re/regex_sets.t b/t/re/regex_sets.t index b70e7ec..9ddceae 100644 --- a/t/re/regex_sets.t +++ b/t/re/regex_sets.t @@ -85,6 +85,9 @@ my $still_fold = qr/(?[ $kelvin_fold ])/; like("K", $still_fold, "/i on interpolated (?[ ]) is retained in outer without /i"); like("k", $still_fold, "/i on interpolated (?[ ]) is retained in outer without /i"); +eval 'my $x = qr/(?[ [a] ])/; qr/(?[ $x ])/'; +is($@, "", 'qr/(?[ [a] ])/ can be interpolated'); + done_testing(); 1; -- Perl5 Master Repository
