Skip to content

Commit c511660

Browse files
moreganwchristian
authored andcommitted
handle "1*{2}"; make bitwise handling more consistent
1 parent bb03bcd commit c511660

File tree

2 files changed

+25
-32
lines changed

2 files changed

+25
-32
lines changed

lib/PPI/Token/Unknown.pm

+24-16
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,17 @@ sub __TOKENIZER__on_char {
5252

5353
# Now, we split on the different values of the current content
5454
if ( $c eq '*' ) {
55-
if ( $char =~ /(?:(?!\d)\w|\:)/ ) {
55+
# Is it a number?
56+
if ( $char =~ /\d/ ) {
57+
# bitwise operator
58+
$t->{class} = $t->{token}->set_class( 'Operator' );
59+
return $t->_finalize_token->__TOKENIZER__on_char( $t );
60+
}
61+
62+
if ( $char =~ /[\w:]/ ) {
5663
# Symbol (unless the thing before it is a number
57-
my $tokens = $t->_previous_significant_tokens(1);
58-
my $p0 = $tokens->[0];
59-
if ( $p0 and ! $p0->isa('PPI::Token::Number') ) {
64+
my ( $prev ) = @{ $t->_previous_significant_tokens(1) };
65+
if ( $prev and ! $prev->isa('PPI::Token::Number') ) {
6066
$t->{class} = $t->{token}->set_class( 'Symbol' );
6167
return 1;
6268
}
@@ -69,10 +75,6 @@ sub __TOKENIZER__on_char {
6975
# control-character symbol (e.g. *{^_Foo})
7076
$t->{class} = $t->{token}->set_class( 'Magic' );
7177
return 1;
72-
} else {
73-
# Obvious GLOB cast
74-
$t->{class} = $t->{token}->set_class( 'Cast' );
75-
return $t->_finalize_token->__TOKENIZER__on_char( $t );
7678
}
7779
}
7880

@@ -150,7 +152,7 @@ sub __TOKENIZER__on_char {
150152
} elsif ( $c eq '%' ) {
151153
# Is it a number?
152154
if ( $char =~ /\d/ ) {
153-
# This is %2 (modulus number)
155+
# bitwise operator
154156
$t->{class} = $t->{token}->set_class( 'Operator' );
155157
return $t->_finalize_token->__TOKENIZER__on_char( $t );
156158
}
@@ -161,10 +163,13 @@ sub __TOKENIZER__on_char {
161163
return 1;
162164
}
163165

164-
# Is it a symbol?
165166
if ( $char =~ /[\w:]/ ) {
166-
$t->{class} = $t->{token}->set_class( 'Symbol' );
167-
return 1;
167+
# Symbol (unless the thing before it is a number
168+
my ( $prev ) = @{ $t->_previous_significant_tokens(1) };
169+
if ( $prev and ! $prev->isa('PPI::Token::Number') ) {
170+
$t->{class} = $t->{token}->set_class( 'Symbol' );
171+
return 1;
172+
}
168173
}
169174

170175
if ( $char eq '{' ) {
@@ -188,15 +193,18 @@ sub __TOKENIZER__on_char {
188193
} elsif ( $c eq '&' ) {
189194
# Is it a number?
190195
if ( $char =~ /\d/ ) {
191-
# This is &2 (bitwise-and number)
196+
# bitwise operator
192197
$t->{class} = $t->{token}->set_class( 'Operator' );
193198
return $t->_finalize_token->__TOKENIZER__on_char( $t );
194199
}
195200

196-
# Is it a symbol
197201
if ( $char =~ /[\w:]/ ) {
198-
$t->{class} = $t->{token}->set_class( 'Symbol' );
199-
return 1;
202+
# Symbol (unless the thing before it is a number
203+
my ( $prev ) = @{ $t->_previous_significant_tokens(1) };
204+
if ( $prev and ! $prev->isa('PPI::Token::Number') ) {
205+
$t->{class} = $t->{token}->set_class( 'Symbol' );
206+
return 1;
207+
}
200208
}
201209

202210
return $self->_as_cast_or_op($t) if $self->_is_cast_or_op($char);

t/ppi_token_unknown.t

+1-16
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,9 @@ OPERATOR_CAST: {
8585
test_varying_whitespace( @number, @asterisk_op, @scalar );
8686
test_varying_whitespace( @number, @asterisk_op, @list );
8787
test_varying_whitespace( @number, @asterisk_op, @hash );
88-
{
89-
local %known_bad_seps = map { $_ => 1 } qw( null );
9088
test_varying_whitespace( @number, @asterisk_op, @hashctor1 );
9189
test_varying_whitespace( @number, @asterisk_op, @hashctor2 );
9290
test_varying_whitespace( @number, @asterisk_op, @hashctor3 );
93-
}
9491
test_varying_whitespace( @number, @exp_op, @bareword );
9592
test_varying_whitespace( @number, @exp_op, @hashctor3 ); # doesn't compile, but make sure ** is operator
9693
test_varying_whitespace( @number, @asteriskeq_op, @bareword );
@@ -149,21 +146,15 @@ OPERATOR_CAST: {
149146

150147
my @single = ( "'3'", [ 'PPI::Token::Quote::Single' => "'3'", ] );
151148
test_varying_whitespace( @single, @asterisk_op, @scalar );
152-
{
153-
local %known_bad_seps = map { $_ => 1 } qw( null );
154149
test_varying_whitespace( @single, @asterisk_op, @hashctor3 );
155-
}
156150
test_varying_whitespace( @single, @percent_op, @scalar );
157151
test_varying_whitespace( @single, @percent_op, @hashctor3 );
158152
test_varying_whitespace( @single, @ampersand_op, @scalar );
159153
test_varying_whitespace( @single, @ampersand_op, @hashctor3 );
160154

161155
my @double = ( '"3"', [ 'PPI::Token::Quote::Double' => '"3"', ] );
162156
test_varying_whitespace( @double, @asterisk_op, @scalar );
163-
{
164-
local %known_bad_seps = map { $_ => 1 } qw( null );
165157
test_varying_whitespace( @double, @asterisk_op, @hashctor3 );
166-
}
167158
test_varying_whitespace( @double, @percent_op, @scalar );
168159
test_varying_whitespace( @double, @percent_op, @hashctor3 );
169160
test_varying_whitespace( @double, @ampersand_op, @scalar );
@@ -293,10 +284,7 @@ OPERATOR_CAST: {
293284
]
294285
);
295286
test_varying_whitespace( @evalblock, @asterisk_op, @scalar );
296-
{
297-
local %known_bad_seps = map { $_ => 1 } qw( null );
298-
test_varying_whitespace( @evalblock, @asterisk_op, @hashctor3 );
299-
}
287+
test_varying_whitespace( @double, @asterisk_op, @hashctor3 );
300288
test_varying_whitespace( @evalblock, @percent_op, @scalar );
301289
test_varying_whitespace( @evalblock, @percent_op, @hashctor3 );
302290
test_varying_whitespace( @evalblock, @ampersand_op, @scalar );
@@ -310,10 +298,7 @@ OPERATOR_CAST: {
310298
]
311299
);
312300
test_varying_whitespace( @evalstring, @asterisk_op, @scalar );
313-
{
314-
local %known_bad_seps = map { $_ => 1 } qw( null );
315301
test_varying_whitespace( @evalstring, @asterisk_op, @hashctor3 );
316-
}
317302
test_varying_whitespace( @evalstring, @percent_op, @scalar );
318303
test_varying_whitespace( @evalstring, @percent_op, @hashctor3 );
319304
test_varying_whitespace( @evalstring, @ampersand_op, @scalar );

0 commit comments

Comments
 (0)