Skip to content

Commit

Permalink
fix GitHub #122: "x64" being parsed as x operator plus number
Browse files Browse the repository at this point in the history
fixes #122
fixes #123
  • Loading branch information
moregan authored and wchristian committed Nov 21, 2014
1 parent 136b2cd commit 4e72b4f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Revision history for Perl extension PPI
- Fix 1.218 regression where packages, subs, and words after
labels like /^x\d+/ would parse as x operator (GitHub #122)
(MOREGAN)

1.221_01
Summary:
Expand Down
32 changes: 31 additions & 1 deletion lib/PPI/Tokenizer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,35 @@ my %X_CAN_FOLLOW_OPERATOR = map { $_ => 1 } qw( -- ++ );
# These are the exceptions.
my %X_CAN_FOLLOW_STRUCTURE = map { $_ => 1 } qw( } ] \) );


# Something that looks like the x operator but follows a word
# is usually that word's argument.
# These are the exceptions.
# chop, chomp, dump are ambiguous because they can have either parms
# or no parms.
my %X_CAN_FOLLOW_WORD = map { $_ => 1 } qw(
endgrent
endhostent
endnetent
endprotoent
endpwent
endservent
fork
getgrent
gethostent
getlogin
getnetent
getppid
getprotoent
getpwent
getservent
setgrent
setpwent
time
times
wait
wantarray
__SUB__
);



Expand Down Expand Up @@ -771,6 +799,8 @@ sub _current_x_is_operator {
$prev
&& (!$prev->isa('PPI::Token::Operator') || $X_CAN_FOLLOW_OPERATOR{$prev})
&& (!$prev->isa('PPI::Token::Structure') || $X_CAN_FOLLOW_STRUCTURE{$prev})
&& (!$prev->isa('PPI::Token::Word') || $X_CAN_FOLLOW_WORD{$prev})
&& !$prev->isa('PPI::Token::Label')
;
}

Expand Down
4 changes: 3 additions & 1 deletion t/ppi_statement_package.t
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ BEGIN {
$PPI::XS_DISABLE = 1;
$PPI::Lexer::X_TOKENIZER ||= $ENV{X_TOKENIZER};
}
use Test::More tests => 14889;
use Test::More tests => 14949;
use Test::NoWarnings;
use PPI;

Expand Down Expand Up @@ -69,6 +69,8 @@ PERL_5_12_SYNTAX: {
keys %PPI::Token::Word::KEYWORDS,
# regression: misparsed as version string
'v10',
# regression GitHub #122: 'x' parsed as x operator
'x64',
# Other weird and/or special words, just in case
'__PACKAGE__',
'__FILE__',
Expand Down
3 changes: 2 additions & 1 deletion t/ppi_statement_sub.t
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ BEGIN {
$PPI::Lexer::X_TOKENIZER ||= $ENV{X_TOKENIZER};
}

use Test::More tests => 6204;
use Test::More tests => 6208;
use Test::NoWarnings;
use PPI;

Expand All @@ -28,6 +28,7 @@ NAME: {
{ code => 'sub baz : method lvalue{}', name => 'baz' },
{ code => 'sub baz : method:lvalue{}', name => 'baz' },
{ code => 'sub baz (*) : method : lvalue{}', name => 'baz' },
{ code => 'sub x64 {}', name => 'x64' }, # should not be parsed as x operator
) {
my $code = $test->{code};
my $name = $test->{name};
Expand Down
13 changes: 12 additions & 1 deletion t/ppi_token_operator.t
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ BEGIN {
$PPI::XS_DISABLE = 1;
$PPI::Lexer::X_TOKENIZER ||= $ENV{X_TOKENIZER};
}
use Test::More tests => 1141;
use Test::More tests => 1142;
use Test::NoWarnings;
use PPI;

Expand Down Expand Up @@ -409,6 +409,17 @@ OPERATOR_X: {
'PPI::Token::Structure' => '}',
]
},
{
desc => 'label plus x',
code => 'LABEL: x64',
expected => [
'PPI::Statement::Compound' => 'LABEL:',
'PPI::Token::Label' => 'LABEL:',
'PPI::Token::Whitespace' => ' ',
'PPI::Statement' => 'x64',
'PPI::Token::Word' => 'x64',
]
},
);

# Exhaustively test when a preceding operator implies following
Expand Down

0 comments on commit 4e72b4f

Please sign in to comment.