Skip to content

Commit 5962591

Browse files
committed
factor out _cast_or_op in Token::Unknown
1 parent 5362d39 commit 5962591

File tree

1 file changed

+25
-31
lines changed

1 file changed

+25
-31
lines changed

lib/PPI/Token/Unknown.pm

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ BEGIN {
4646
# Tokenizer Methods
4747

4848
sub __TOKENIZER__on_char {
49-
my $t = $_[1]; # Tokenizer object
49+
my ( $self, $t ) = @_; # Self and Tokenizer
5050
my $c = $t->{token}->{content}; # Current token
5151
my $char = substr( $t->{line}, $t->{line_cursor}, 1 ); # Current character
5252

@@ -77,35 +77,7 @@ sub __TOKENIZER__on_char {
7777
}
7878

7979
if ( $char eq '$' ) {
80-
# Operator/operand-sensitive, multiple or GLOB cast
81-
my $_class;
82-
my $tokens = $t->_previous_significant_tokens(1);
83-
my $p0 = $tokens->[0];
84-
if ( $p0 ) {
85-
# Is it a token or a number
86-
if ( $p0->isa('PPI::Token::Symbol') ) {
87-
$_class = 'Operator';
88-
} elsif ( $p0->isa('PPI::Token::Number') ) {
89-
$_class = 'Operator';
90-
} elsif (
91-
$p0->isa('PPI::Token::Structure')
92-
and
93-
$p0->content =~ /^(?:\)|\])$/
94-
) {
95-
$_class = 'Operator';
96-
} else {
97-
### This is pretty weak, there's
98-
### room for a dozen more tests
99-
### before going with a default.
100-
### Or even better, a proper
101-
### operator/operand method :(
102-
$_class = 'Cast';
103-
}
104-
} else {
105-
# Nothing before it, must be glob cast
106-
$_class = 'Cast';
107-
}
108-
80+
my $_class = $self->_cast_or_op( $t );
10981
# Set class and rerun
11082
$t->{class} = $t->{token}->set_class( $_class );
11183
return $t->_finalize_token->__TOKENIZER__on_char( $t );
@@ -283,7 +255,7 @@ sub __TOKENIZER__on_char {
283255
# Now, : acts very very differently in different contexts.
284256
# Mainly, we need to find out if this is a subroutine attribute.
285257
# We'll leave a hint in the token to indicate that, if it is.
286-
if ( $_[0]->__TOKENIZER__is_an_attribute( $t ) ) {
258+
if ( $self->__TOKENIZER__is_an_attribute( $t ) ) {
287259
# This : is an attribute indicator
288260
$t->{class} = $t->{token}->set_class( 'Operator' );
289261
$t->{token}->{_attribute} = 1;
@@ -299,6 +271,28 @@ sub __TOKENIZER__on_char {
299271
PPI::Exception->throw('Unknown value in PPI::Token::Unknown token');
300272
}
301273

274+
# Operator/operand-sensitive, multiple or GLOB cast
275+
sub _cast_or_op {
276+
my ( undef, $t ) = @_;
277+
my ( $prev ) = @{ $t->_previous_significant_tokens(1) };
278+
return 'Cast' if !$prev;
279+
280+
return 'Operator' if
281+
$prev->isa('PPI::Token::Symbol')
282+
or
283+
$prev->isa('PPI::Token::Number')
284+
or
285+
(
286+
$prev->isa('PPI::Token::Structure')
287+
and
288+
$prev->content =~ /^(?:\)|\])$/
289+
);
290+
291+
# This is pretty weak, there's room for a dozen more tests before going with
292+
# a default. Or even better, a proper operator/operand method :(
293+
return 'Cast';
294+
}
295+
302296
# Are we at a location where a ':' would indicate a subroutine attribute
303297
sub __TOKENIZER__is_an_attribute {
304298
my $t = $_[1]; # Tokenizer object

0 commit comments

Comments
 (0)