1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
package Method::Signatures::Parser;
use strict;
use warnings;
use Carp;
use base qw(Exporter);
our @EXPORT = qw(split_proto);
sub split_proto {
my $proto = shift;
return unless $proto =~ /\S/;
local $@ = undef;
require PPI;
my $ppi = PPI::Document->new(\$proto);
$ppi->prune('PPI::Token::Comment');
my $statement = $ppi->find_first("PPI::Statement");
confess("PPI failed to find statement for '$proto'") unless $statement;
my $token = $statement->first_token;
my @proto = ('');
do {
if( $token->class eq "PPI::Token::Operator" and $token->content eq ',' ) {
push @proto, '';
}
else {
$proto[-1] .= $token->content;
}
$token = $token->class eq 'PPI::Token::Label' ? $token->next_token : $token->next_sibling;
} while( $token );
strip_ws($_) for @proto;
return @proto;
}
sub strip_ws {
$_[0] =~ s{^\s+}{};
$_[0] =~ s{\s+$}{};
}
1;
|