/usr/share/perl5/Padre/PPI.pm is in padre 1.00+dfsg-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | package Padre::PPI;
use 5.008;
use strict;
use warnings;
use PPI ();
our $VERSION = '1.00';
#####################################################################
# Assorted Search Functions
sub find_unmatched_brace {
$_[1]->isa('PPI::Statement::UnmatchedBrace') and return 1;
$_[1]->isa('PPI::Structure') or return '';
$_[1]->start and $_[1]->finish and return '';
return 1;
}
# scans a document for variable declarations and
# sorts them into three categories:
# lexical (my)
# our (our, doh)
# dynamic (local)
# package (use vars)
# Returns a hash reference containing the three category names
# each pointing at a hash which contains '$variablename' => locations.
# locations is an array reference containing one or more PPI-style
# locations. Example:
# {
# lexical => {
# '$foo' => [ [ 2, 3, 3], [ 6, 7, 7 ] ],
# },
# ...
# }
# Thus, there are two places where a "my $foo" was declared. On line 2 col 3
# and line 6 col 7.
sub get_all_variable_declarations {
my $document = shift;
my %vars;
my $declarations = $document->find(
sub {
return 0
unless $_[1]->isa('PPI::Statement::Variable')
or $_[1]->isa('PPI::Statement::Include');
return 1;
},
);
my %our;
my %lexical;
my %dynamic;
my %package;
foreach my $decl (@$declarations) {
if ( $decl->isa('PPI::Statement::Variable') ) {
my $type = $decl->type;
my @vars = $decl->variables;
my $location = $decl->location;
my $target_type;
if ( $type eq 'my' ) {
$target_type = \%lexical;
} elsif ( $type eq 'our' ) {
$target_type = \%our;
} elsif ( $type eq 'local' ) {
$target_type = \%dynamic;
}
foreach my $var (@vars) {
$target_type->{$var} ||= [];
push @{ $target_type->{$var} }, $location;
}
}
# find use vars...
elsif ( $decl->isa('PPI::Statement::Include')
and $decl->module eq 'vars'
and $decl->type eq 'use' )
{
# do it the low-tech way
my $string = $decl->content;
my $location = $decl->location;
my @vars = $string =~ /([\%\@\$][\w_:]+)/g;
foreach my $var (@vars) {
$package{$var} ||= [];
push @{ $package{$var} }, $location;
}
}
} # end foreach declaration
return ( { our => \%our, lexical => \%lexical, dynamic => \%dynamic, package => \%package } );
}
#####################################################################
# Stuff that should be in PPI itself
sub element_depth {
my $cursor = shift;
my $depth = 0;
while ( $cursor = $cursor->parent ) {
$depth += 1;
}
return $depth;
}
# TO DO: PPIx::IndexOffsets or something similar might help.
# TO DO: See the 71... tests. If we don#t flush locations there, this breaks.
sub find_token_at_location {
my $document = shift;
my $location = shift;
if ( not defined $document
or not $document->isa('PPI::Document')
or not defined $location
or not ref($location) eq 'ARRAY' )
{
require Carp;
Carp::croak("find_token_at_location() requires a PPI::Document and a PPI-style location as arguments");
}
$document->index_locations;
foreach my $token ( $document->tokens ) {
my $tloc = $token->location;
return $token->previous_token
if $tloc->[0] > $location->[0]
or ( $tloc->[0] == $location->[0]
and $tloc->[1] > $location->[1] );
}
return ();
# old way that would only handle beginning of tokens; Should probably simply go away.; Should probably simply go away.
#my $variable_token = $document->find_first(
# sub {
# my $elem = $_[1];
# return 0 if not $elem->isa('PPI::Token');
# my $loc = $elem->location;
# return 0 if $loc->[0] != $location->[0] or $loc->[1] != $location->[1];
# return 1;
# },
#);
#
#return $variable_token;
}
# given either a PPI::Token::Symbol (i.e. a variable)
# or a PPI::Token which contains something that looks like
# a variable (quoted vars, interpolated vars in regexes...)
# find where that variable has been declared lexically.
# Doesn't find stuff like "use vars...".
sub find_variable_declaration {
my $cursor = shift;
return ()
if not $cursor
or not $cursor->isa("PPI::Token");
my ( $varname, $token_str );
if ( $cursor->isa("PPI::Token::Symbol") ) {
$varname = $cursor->symbol;
$token_str = $cursor->content;
} else {
my $content = $cursor->content;
if ( $content =~ /((?:\$#?|[@%*])[\w:']+)/ ) {
$varname = $1;
$token_str = $1;
}
}
return ()
if not defined $varname;
$varname =~ s/^\$\#/@/;
my $document = $cursor->top;
my $declaration;
my $prev_cursor;
while (1) {
$prev_cursor = $cursor;
$cursor = $cursor->parent;
if ( $cursor->isa("PPI::Structure::Block") or $cursor == $document ) {
my @elems = $cursor->elements;
foreach my $elem (@elems) {
# Stop scanning this scope if we're at the branch we're coming
# from. This is to ignore declarations later in the block.
last if $elem == $prev_cursor;
if ( $elem->isa("PPI::Statement::Variable")
and grep { $_ eq $varname } $elem->variables )
{
$declaration = $elem;
last;
}
# find use vars ...
elsif ( $elem->isa("PPI::Statement::Include")
and $elem->module eq 'vars'
and $elem->type eq 'use' )
{
# do it the low-tech way
my $string = $elem->content;
my @vars = $string =~ /([\%\@\$][\w_:]+)/g;
if ( grep { $varname eq $_ } @vars ) {
$declaration = $elem;
last;
}
}
}
last if $declaration or $cursor == $document;
}
# this is for "foreach my $i ..."
elsif ( $cursor->isa("PPI::Statement::Compound") and $cursor->type =~ /^for/ ) {
my @elems = $cursor->elements;
foreach my $elem (@elems) {
# Stop scanning this scope if we're at the branch we're coming
# from. This is to ignore declarations later in the block.
last if $elem == $prev_cursor;
if ( $elem->isa("PPI::Token::Word") and $elem->content =~ /^(?:my|our)$/ ) {
my $nelem = $elem->snext_sibling;
if ( defined $nelem
and $nelem->isa("PPI::Token::Symbol")
and $nelem->symbol eq $varname || $nelem->content eq $token_str )
{
$declaration = $nelem;
last;
}
}
}
last if $declaration or $cursor == $document;
}
} # end while not top level
return $declaration;
}
1;
# Copyright 2008-2013 The Padre development team as listed in Padre.pm.
# LICENSE
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl 5 itself.
|