Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for new octal number syntax #295

Merged
merged 1 commit into from Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/PPI/Token/Number.pm
Expand Up @@ -94,6 +94,9 @@ sub __TOKENIZER__on_char {
} elsif ( $char eq 'b' || $char eq 'B' ) {
$t->{class} = $t->{token}->set_class( 'Number::Binary' );
return 1;
} elsif ( $char eq 'o' || $char eq 'O' ) {
$t->{class} = $t->{token}->set_class( 'Number::Octal' );
return 1;
} elsif ( $char =~ /\d/ ) {
# You cannot have 8s and 9s on octals
if ( $char eq '8' or $char eq '9' ) {
Expand Down
2 changes: 2 additions & 0 deletions lib/PPI/Token/Number/Octal.pm
Expand Up @@ -55,6 +55,8 @@ sub literal {
my $self = shift;
return if $self->{_error};
my $str = $self->_literal;
# oct supports '0o' notation only since 5.34
$str =~ s (^0[oO]) (0);
my $neg = $str =~ s/^\-//;
my $val = oct $str;
return $neg ? -$val : $val;
Expand Down
19 changes: 18 additions & 1 deletion t/07_token.t
Expand Up @@ -8,7 +8,7 @@ sub dies_on_incomplete_bx { $] >= 5.031002 }
use if !(-e 'META.yml'), "Test::InDistDir";
use lib 't/lib';
use PPI::Test::pragmas;
use Test::More tests => 588 + (warns_on_misplaced_underscore() ? 2 : 0 ) + ($ENV{AUTHOR_TESTING} ? 1 : 0);
use Test::More tests => 594 + (warns_on_misplaced_underscore() ? 2 : 0 ) + ($ENV{AUTHOR_TESTING} ? 1 : 0);

use File::Spec::Functions qw( catdir );
use PPI ();
Expand Down Expand Up @@ -212,3 +212,20 @@ HEX: {
is($token->literal, $test->{value}, "literal('$code') is $test->{value}");
}
}

OCTAL: {
my @tests = (
{ code => '0o10', parsed => '0o10', value => 8 },
{ code => '0O10', parsed => '0O10', value => 8 },
);

foreach my $test ( @tests ) {
my $code = $test->{code};
my $T = PPI::Tokenizer->new( \$code );
my $token = $T->get_token;

isa_ok($token, 'PPI::Token::Number::Octal');
is($token->content, $test->{parsed}, "correctly parsed everything expected");
is($token->literal, $test->{value}, "literal('$code') is $test->{value}");
}
}