Skip to content

Commit 23f7ae5

Browse files
committed
ParseXS: refactor: add OVERLOAD node
Add ExtUtils::ParseXS::Node::OVERLOAD class, and add a basic test. Note that currently this code doesn't warn about duplicate op names (it just silently skips duplicates), nor warn about unknown op names (it happily accepts them). This commit preserves the current behaviour for now.
1 parent 313e026 commit 23f7ae5

File tree

3 files changed

+65
-18
lines changed

3 files changed

+65
-18
lines changed

dist/ExtUtils-ParseXS/lib/ExtUtils/ParseXS.pm

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2227,24 +2227,6 @@ sub INIT_handler {
22272227
}
22282228

22292229

2230-
# Add all overload method names, like 'cmp', '<=>', etc, (possibly
2231-
# multiple ones per line) until the next keyword line, as 'seen' keys to
2232-
# the $self->{xsub_map_overload_name_to_seen} hash.
2233-
2234-
sub OVERLOAD_handler {
2235-
my ExtUtils::ParseXS $self = shift;
2236-
$_ = shift;
2237-
2238-
for (; !/^$BLOCK_regexp/o; $_ = shift(@{ $self->{line} })) {
2239-
next unless /\S/;
2240-
trim_whitespace($_);
2241-
while ( s/^\s*([\w:"\\)\+\-\*\/\%\<\>\.\&\|\^\!\~\{\}\=]+)\s*//) {
2242-
$self->{xsub_map_overload_name_to_seen}->{$1} = 1;
2243-
}
2244-
}
2245-
}
2246-
2247-
22482230
sub FALLBACK_handler {
22492231
my ExtUtils::ParseXS $self = shift;
22502232
my ($setting) = @_;

dist/ExtUtils-ParseXS/lib/ExtUtils/ParseXS/Node.pm

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,6 +1836,34 @@ sub parse {
18361836
}
18371837

18381838

1839+
# ======================================================================
1840+
1841+
package ExtUtils::ParseXS::Node::OVERLOAD;
1842+
1843+
# Handle OVERLOAD keyword
1844+
1845+
BEGIN { $build_subclass->('multiline_merged', # parent
1846+
'ops', # has ref of overloaded op names
1847+
)};
1848+
1849+
# Add all overload method names, like 'cmp', '<=>', etc, (possibly
1850+
# multiple ones per line) until the next keyword line, as 'seen' keys to
1851+
# the $self->{xsub_map_overload_name_to_seen} hash.
1852+
1853+
sub parse {
1854+
my __PACKAGE__ $self = shift;
1855+
my ExtUtils::ParseXS $pxs = shift;
1856+
1857+
$self->SUPER::parse($pxs); # set file/line_no, get lines, set text
1858+
1859+
my $s = $self->{text};
1860+
while ($s =~ s/^\s*([\w:"\\)\+\-\*\/\%\<\>\.\&\|\^\!\~\{\}\=]+)\s*//) {
1861+
$self->{ops}{$1} = 1;
1862+
$pxs->{xsub_map_overload_name_to_seen}->{$1} = 1;
1863+
}
1864+
}
1865+
1866+
18391867
# ======================================================================
18401868

18411869
package ExtUtils::ParseXS::Node::ATTRS;

dist/ExtUtils-ParseXS/t/001-basic.t

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3973,4 +3973,41 @@ EOF
39733973
}
39743974
39753975
3976+
{
3977+
# Test OVERLOAD keyword
3978+
3979+
my $preamble = Q(<<'EOF');
3980+
|MODULE = Foo PACKAGE = Foo
3981+
|
3982+
|PROTOTYPES: DISABLE
3983+
|
3984+
EOF
3985+
3986+
my @test_fns = (
3987+
[
3988+
"OVERLOAD basic",
3989+
[ Q(<<'EOF') ],
3990+
|void
3991+
|foo()
3992+
| OVERLOAD: cmp <=>
3993+
| + - * /
3994+
| OVERLOAD: > < >=
3995+
EOF
3996+
[ 0, 0, qr{\Q"Foo::(*"}, "has Foo::(* method" ],
3997+
[ 0, 0, qr{\Q"Foo::(+"}, "has Foo::(+ method" ],
3998+
[ 0, 0, qr{\Q"Foo::(-"}, "has Foo::(- method" ],
3999+
[ 0, 0, qr{\Q"Foo::(/"}, "has Foo::(/ method" ],
4000+
[ 0, 0, qr{\Q"Foo::(<"}, "has Foo::(< method" ],
4001+
[ 0, 0, qr{\Q"Foo::(<=>"}, "has Foo::(<=> method" ],
4002+
[ 0, 0, qr{\Q"Foo::(>"}, "has Foo::(> method" ],
4003+
[ 0, 0, qr{\Q"Foo::(>="}, "has Foo::(>= method" ],
4004+
[ 0, 0, qr{\Q"Foo::(cmp"}, "has Foo::(cmp method" ],
4005+
],
4006+
4007+
);
4008+
4009+
test_many($preamble, 'boot_Foo', \@test_fns);
4010+
}
4011+
4012+
39764013
done_testing;

0 commit comments

Comments
 (0)