Perl-Critic / PPI

53 stars 44 forks source link

lexer confused by 1<<bar() #183

Open jhi opened 8 years ago

jhi commented 8 years ago

Using this simple round-tripper:

use PPI; print PPI::Document->new($ARGV[0]), "\n";

the following trivial code fails:

sub foo { 1<<bar(); }

I am guessing it thinks the << is a heredoc, when it should be a bit shift.

The original line was

$num |= (1<<index($Dflags, $)) for split //, $on;

which comes from this in the standard Devel/Peek.pm:

sub debug_flags (;$) { my $out = ""; for my $i (0 .. length($D_flags)-1) { $out .= substr $D_flags, $i, 1 if $^D & (1<<$i); } my $arg = shift; my $num = $arg; if (defined $arg and $arg =~ /\D/) { die "unknown flags in debug_flags()" if $arg =~ /[^-$D_flags]/; my ($on,$off) = split /-/, "$arg-"; $num = $^D; $num |= (1<<index($Dflags, $)) for split //, $on; $num &= ~(1<<index($Dflags, $)) for split //, $off; } $^D = $num if defined $arg; $out }

h3xx commented 1 year ago

I've been investigating a related issue, whereby stringifying the PPI::Document object (same as calling ->content() on it) strips out the content of heredocs, giving back an un-parseable document.

See #288 for a full description of the bug I found.

A workaround I've found in the mean time is to call ->serialize() instead of using the automatic stringification.

To fix your code, replace:

 print PPI::Document->new($ARGV[0]), "\n";

with:

print PPI::Document->new($ARGV[0])->serialize;