Skip to content

allow variables with spaces after the sigil #307

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Revision history for Perl extension PPI

{{$NEXT}}
- Allow zero byte documents to have a location
- Allow variable names to have whitespace after the sigil (GH#158)

1.281 2024-12-27 14:44:47Z
Summary:
Expand Down
3 changes: 2 additions & 1 deletion lib/PPI/Token/Symbol.pm
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ sub __TOKENIZER__on_char {

# Suck in till the end of the symbol
pos $t->{line} = $t->{line_cursor};
if ( $t->{line} =~ m/\G([\w:\']+)/gc ) {
if ( $t->{line} =~ m/\G(\s*[\w:\']+)/gc ) {
$t->{token}->{content} .= $1;
$t->{line_cursor} += length $1;
}
Expand Down Expand Up @@ -201,6 +201,7 @@ sub __TOKENIZER__on_char {
my $pattern = qr/
^(
[\$@%&*]
\s*
(?:
: (?! : ) # allow single-colon non-magic variables
|
Expand Down
8 changes: 8 additions & 0 deletions lib/PPI/Token/Unknown.pm
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ sub __TOKENIZER__on_char {
return 1;
}

if ( $char eq ' ' ) {
pos $t->{line} = $t->{line_cursor} + 1;
if ( $t->{line} =~ m/\G\s*[a-z_]/gci ) {
$t->{class} = $t->{token}->set_class('Symbol');
return 1;
}
}

# Is it a nameless arg in a signature?
if ( $char eq ')' or $char eq '=' or $char eq ',' ) {
my ($has_sig) = $t->_current_token_has_signatures_active;
Expand Down
113 changes: 113 additions & 0 deletions t/foreach_whitespace.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#!/usr/bin/perl

BEGIN { chdir ".." if -d "../t" and -d "../lib" }
use lib 't/lib';
use PPI::Test::pragmas;
use Test::More tests => 8 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 );

use B 'perlstring';

use PPI ();
use PPI::Dumper;

sub test_document;

BASE_SPACE_SYMBOL: {
test_document #
'$ s', #
[
'PPI::Statement', '$ s', #
'PPI::Token::Symbol', '$ s',
],
"base space symbol example";
}

FOR_LOOP: {
test_document
'for my $ s ( qw( a b ) ) { say $s }',
[
'PPI::Statement::Compound', 'for my $ s ( qw( a b ) ) { say $s }',
'PPI::Token::Word', 'for',
'PPI::Token::Word', 'my',
'PPI::Token::Symbol', '$ s',
'PPI::Structure::List', '( qw( a b ) )',
'PPI::Token::Structure', '(',
'PPI::Statement', 'qw( a b )',
'PPI::Token::QuoteLike::Words', 'qw( a b )',
'PPI::Token::Structure', ')',
'PPI::Structure::Block', '{ say $s }',
'PPI::Token::Structure', '{',
'PPI::Statement', 'say $s',
'PPI::Token::Word', 'say',
'PPI::Token::Symbol', '$s',
'PPI::Token::Structure', '}',
],
"space symboln in for loop";
}

SIGIL_WITH_TRASH: {
test_document
'$ \"8;b',
[
'PPI::Statement', '$ \\"8;b',
'PPI::Token::Cast', '$',
'PPI::Token::Cast', '\\',
'PPI::Token::Quote::Double', '"8;b',
],
"sigil with a space and trash that is NOT a symbol";
}

SIGIL_WITH_TABS_AND_TRAIL: {
test_document #
'$ b ', #
[ #
'PPI::Statement', "\$ \t b",
'PPI::Token::Symbol', "\$ \t b",
],
"sigil with tabs and trailing space";
}

sub one_line_explain {
my ($data) = @_;
my @explain = explain $data;
s/\n//g for @explain;
return join "", @explain;
}

sub main_level_line {
return "" if not $TODO;
my @outer_final;
my $level = 0;
while ( my @outer = caller( $level++ ) ) {
@outer_final = @outer;
}
return "l $outer_final[2] - ";
}

sub test_document {
local $Test::Builder::Level = $Test::Builder::Level + 1;
my $args = ref $_[0] eq "ARRAY" ? shift : [];
my ( $code, $expected, $msg ) = @_;
$msg = perlstring $code if !defined $msg;

my $d = PPI::Document->new( \$code, @{$args} ) or do {
diag explain $@;
fail "PPI::Document->new failed";
fail "code round trips";
return;
};
my $tokens = $d->find( sub { $_[1]->significant } );
$tokens = [ map { ref($_), $_->content } @$tokens ];

is $d->serialize, $code, "code round trips";

return if #
is_deeply( $tokens, $expected, main_level_line . $msg );

diag ">>> $code -- $msg\n";
diag( PPI::Dumper->new($d)->string );
diag one_line_explain $tokens;
diag one_line_explain $expected;

return;
}
Loading