Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 34 additions & 9 deletions lib/Regexp/Parser/Handlers.pm
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ sub init {
# \g{name} — named backref
if (${&Rx} =~ m{ \G ([a-zA-Z_]\w*) \} }xgc) {
my $name = $1;
$S->error($S->RPe_BGROUP) if !&SIZE_ONLY and !exists $S->{named_captures}{$name};
return $S->object(gref => $name, "\\g{$name}");
}
# \g{N}, \g{-N}, or \g{+N} — numeric (possibly relative)
Expand Down Expand Up @@ -1071,14 +1072,20 @@ sub init {
}

if (${&Rx} =~ m{ \G < ([^>]+) > }xgc) {
return $S->object(named_ref => $1, "\\k<$1>");
my $name = $1;
$S->error($S->RPe_BGROUP) if !&SIZE_ONLY and !exists $S->{named_captures}{$name};
return $S->object(named_ref => $name, "\\k<$name>");
}
elsif (${&Rx} =~ m{ \G ' ([^']+) ' }xgc) {
return $S->object(named_ref => $1, "\\k'$1'");
my $name = $1;
$S->error($S->RPe_BGROUP) if !&SIZE_ONLY and !exists $S->{named_captures}{$name};
return $S->object(named_ref => $name, "\\k'$name'");
}
elsif (${&Rx} =~ m{ \G \{ ([^\}]+) \} }xgc) {
# \k{name} — brace-delimited named backref (Perl 5.32+)
return $S->object(named_ref => $1, "\\k{$1}");
my $name = $1;
$S->error($S->RPe_BGROUP) if !&SIZE_ONLY and !exists $S->{named_captures}{$name};
return $S->object(named_ref => $name, "\\k{$name}");
}

$S->error($S->RPe_BADESC, "k", "");
Expand All @@ -1100,9 +1107,14 @@ sub init {
if (${&Rx} =~ m{ \G ([A-Za-z_]\w*) > }xgc) {
my $name = $1;
push @{ $S->{next} }, qw< c) atom >;
&SIZE_ONLY ? ++$S->{maxpar} : ++$S->{nparen};
if (&SIZE_ONLY) {
++$S->{maxpar};
$S->{named_captures}{$name} = $S->{maxpar};
} else {
++$S->{nparen};
$S->{named_captures}{$name} = $S->{nparen};
}
push @{ $S->{flags} }, &Rf;
$S->{named_captures}{$name} = $S->{nparen} unless &SIZE_ONLY;
return $S->object(named_open => $S->{nparen}, $name);
}

Expand All @@ -1116,9 +1128,14 @@ sub init {
if (${&Rx} =~ m{ \G ([A-Za-z_]\w*) ' }xgc) {
my $name = $1;
push @{ $S->{next} }, qw< c) atom >;
&SIZE_ONLY ? ++$S->{maxpar} : ++$S->{nparen};
if (&SIZE_ONLY) {
++$S->{maxpar};
$S->{named_captures}{$name} = $S->{maxpar};
} else {
++$S->{nparen};
$S->{named_captures}{$name} = $S->{nparen};
}
push @{ $S->{flags} }, &Rf;
$S->{named_captures}{$name} = $S->{nparen} unless &SIZE_ONLY;
return $S->object(named_open => $S->{nparen}, $name);
}

Expand All @@ -1141,6 +1158,7 @@ sub init {

if (${&Rx} =~ m{ \G ([A-Za-z_]\w*) \) }xgc) {
my $name = $1;
$S->error($S->RPe_BGROUP) if !&SIZE_ONLY and !exists $S->{named_captures}{$name};
return $S->object(named_recurse => $name, "(?&$name)");
}

Expand All @@ -1157,21 +1175,28 @@ sub init {
if (${&Rx} =~ m{ \G < ([A-Za-z_]\w*) > }xgc) {
my $name = $1;
push @{ $S->{next} }, qw< c) atom >;
&SIZE_ONLY ? ++$S->{maxpar} : ++$S->{nparen};
if (&SIZE_ONLY) {
++$S->{maxpar};
$S->{named_captures}{$name} = $S->{maxpar};
} else {
++$S->{nparen};
$S->{named_captures}{$name} = $S->{nparen};
}
push @{ $S->{flags} }, &Rf;
$S->{named_captures}{$name} = $S->{nparen} unless &SIZE_ONLY;
return $S->object(named_open => $S->{nparen}, $name);
}

# (?P=name) named backreference
if (${&Rx} =~ m{ \G = ([A-Za-z_]\w*) \) }xgc) {
my $name = $1;
$S->error($S->RPe_BGROUP) if !&SIZE_ONLY and !exists $S->{named_captures}{$name};
return $S->object(named_ref => $name, "(?P=$name)");
}

# (?P>name) named recursion
if (${&Rx} =~ m{ \G > ([A-Za-z_]\w*) \) }xgc) {
my $name = $1;
$S->error($S->RPe_BGROUP) if !&SIZE_ONLY and !exists $S->{named_captures}{$name};
return $S->object(named_recurse => $name, "(?P>$name)");
}

Expand Down
46 changes: 46 additions & 0 deletions t/11errors.t
Original file line number Diff line number Diff line change
Expand Up @@ -342,4 +342,50 @@ parses_ok('(?{1+1})', 'valid code block');
parses_ok('(??{1+1})', 'valid logical code block');
parses_ok('a(?:b|c)d', 'valid alternation in group');

# --- RPe_BGROUP: named backreferences to nonexistent groups ---
# All named backref forms must reject when no matching named group exists.
# Perl: "Reference to nonexistent named group"

{
my $r = Regexp::Parser->new;
my $RPe_BGROUP = ($r->RPe_BGROUP)[0];

# \k forms
fails_visual('\k<foo>', $RPe_BGROUP, '\k<foo> nonexistent group');
fails_visual('(a)\k<foo>', $RPe_BGROUP, '\k<foo> no named capture');
fails_visual('(?<bar>a)\k<foo>', $RPe_BGROUP, '\k<foo> wrong name');
fails_visual("\\k'foo'", $RPe_BGROUP, "\\k'foo' nonexistent group");
fails_visual('\k{foo}', $RPe_BGROUP, '\k{foo} nonexistent group');

# \g{name} form
fails_visual('\g{foo}', $RPe_BGROUP, '\g{foo} nonexistent group');
fails_visual('(?<bar>a)\g{foo}', $RPe_BGROUP, '\g{foo} wrong name');

# (?P=name) Python-style backref
fails_visual('(?P=foo)', $RPe_BGROUP, '(?P=foo) nonexistent group');
fails_visual('(?<bar>a)(?P=foo)', $RPe_BGROUP, '(?P=foo) wrong name');

# (?&name) named recursion
fails_visual('(?&foo)', $RPe_BGROUP, '(?&foo) nonexistent group');
fails_visual('(?<bar>a)(?&foo)', $RPe_BGROUP, '(?&foo) wrong name');

# (?P>name) Python-style named recursion
fails_visual('(?P>foo)', $RPe_BGROUP, '(?P>foo) nonexistent group');
fails_visual('(?<bar>a)(?P>foo)', $RPe_BGROUP, '(?P>foo) wrong name');

# Valid named refs must still parse correctly
parses_ok('(?<foo>a)\k<foo>', 'valid \k<foo> with matching group');
parses_ok("(?<foo>a)\\k'foo'", "valid \\k'foo' with matching group");
parses_ok('(?<foo>a)\k{foo}', 'valid \k{foo} with matching group');
parses_ok('(?<foo>a)\g{foo}', 'valid \g{foo} with matching group');
parses_ok('(?<foo>a)(?P=foo)', 'valid (?P=foo) with matching group');
parses_ok('(?<foo>a)(?&foo)', 'valid (?&foo) with matching group');
parses_ok('(?<foo>a)(?P>foo)', 'valid (?P>foo) with matching group');
parses_ok('(?P<foo>a)\k<foo>', 'valid Python-style capture + \k');

# Forward references must work (group defined after ref)
parses_ok('(?&foo)(?<foo>a)', 'forward named recursion');
parses_ok('(?P>foo)(?<foo>a)', 'forward Python named recursion');
}

done_testing;
Loading