/usr/share/perl5/PPIx/Utilities/Node.pm is in libppix-utilities-perl 1.001000-1.
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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | package PPIx::Utilities::Node;
use 5.006001;
use strict;
use warnings;
our $VERSION = '1.001000';
use Readonly;
use PPI::Document::Fragment 1.208 qw< >;
use Scalar::Util qw< refaddr >;
use PPIx::Utilities::Exception::Bug qw< >;
use base 'Exporter';
Readonly::Array our @EXPORT_OK => qw<
split_ppi_node_by_namespace
>;
sub split_ppi_node_by_namespace {
my ($node) = @_;
# Ensure we don't screw up the original.
$node = $node->clone();
# We want to make sure that we have locations prior to things being split
# up, if we can, but don't worry about it if we don't.
eval { $node->location(); }; ## no critic (RequireCheckingReturnValueOfEval)
if ( my $single_namespace = _split_ppi_node_by_namespace_single($node) ) {
return $single_namespace;
} # end if
my %nodes_by_namespace;
_split_ppi_node_by_namespace_in_lexical_scope(
$node, 'main', undef, \%nodes_by_namespace,
);
return \%nodes_by_namespace;
} # end split_ppi_node_by_namespace()
# Handle the case where there's only one.
sub _split_ppi_node_by_namespace_single {
my ($node) = @_;
my $package_statements = $node->find('PPI::Statement::Package');
if ( not $package_statements or not @{$package_statements} ) {
return { main => [$node] };
} # end if
if (@{$package_statements} == 1) {
my $package_statement = $package_statements->[0];
my $package_address = refaddr $package_statement;
# Yes, child and not schild.
my $first_child = $node->child(0);
if (
$package_address == refaddr $node
or $first_child and $package_address == refaddr $first_child
) {
return { $package_statement->namespace() => [$node] };
} # end if
} # end if
return;
} # end _split_ppi_node_by_namespace_single()
sub _split_ppi_node_by_namespace_in_lexical_scope {
my ($node, $initial_namespace, $initial_fragment, $nodes_by_namespace)
= @_;
my %scope_fragments_by_namespace;
# I certainly hope a value isn't going to exist at address 0.
my $initial_fragment_address = refaddr $initial_fragment || 0;
my ($namespace, $fragment) = ($initial_namespace, $initial_fragment);
if ($initial_fragment) {
$scope_fragments_by_namespace{$namespace} = $initial_fragment;
} # end if
foreach my $child ( $node->children() ) {
if ( $child->isa('PPI::Statement::Package') ) {
if ($fragment) {
_push_fragment($nodes_by_namespace, $namespace, $fragment);
undef $fragment;
} # end if
$namespace = $child->namespace();
} elsif (
$child->isa('PPI::Statement::Compound')
or $child->isa('PPI::Statement::Given')
or $child->isa('PPI::Statement::When')
) {
my $block;
my @components = $child->children();
while (not $block and my $component = shift @components) {
if ( $component->isa('PPI::Structure::Block') ) {
$block = $component;
} # end if
} # end while
if ($block) {
if (not $fragment) {
$fragment = _get_fragment_for_split_ppi_node(
$nodes_by_namespace,
\%scope_fragments_by_namespace,
$namespace,
);
} # end if
_split_ppi_node_by_namespace_in_lexical_scope(
$block, $namespace, $fragment, $nodes_by_namespace,
);
} # end if
} # end if
$fragment = _get_fragment_for_split_ppi_node(
$nodes_by_namespace, \%scope_fragments_by_namespace, $namespace,
);
if ($initial_fragment_address != refaddr $fragment) {
# Need to fix these to use exceptions. Thankfully the P::C tests
# will insist that this happens.
$child->remove()
or PPIx::Utilities::Exception::Bug->throw(
'Could not remove child from parent.'
);
$fragment->add_element($child)
or PPIx::Utilities::Exception::Bug->throw(
'Could not add child to fragment.'
);
} # end if
} # end foreach
return;
} # end _split_ppi_node_by_namespace_in_lexical_scope()
sub _get_fragment_for_split_ppi_node {
my ($nodes_by_namespace, $scope_fragments_by_namespace, $namespace) = @_;
my $fragment;
if ( not $fragment = $scope_fragments_by_namespace->{$namespace} ) {
$fragment = PPI::Document::Fragment->new();
$scope_fragments_by_namespace->{$namespace} = $fragment;
_push_fragment($nodes_by_namespace, $namespace, $fragment);
} # end if
return $fragment;
} # end _get_fragment_for_split_ppi_node()
# Due to $fragment being passed into recursive calls to
# _split_ppi_node_by_namespace_in_lexical_scope(), we can end up attempting to
# put the same fragment into a namespace's nodes multiple times.
sub _push_fragment {
my ($nodes_by_namespace, $namespace, $fragment) = @_;
my $nodes = $nodes_by_namespace->{$namespace} ||= [];
if (not @{$nodes} or refaddr $nodes->[-1] != refaddr $fragment) {
push @{$nodes}, $fragment;
} # end if
return;
} # end _push_fragment()
1;
__END__
=head1 NAME
PPIx::Utilities::Node - Extensions to L<PPI::Node|PPI::Node>.
=head1 VERSION
This document describes PPIx::Utilities::Node version 1.1.0.
=head1 SYNOPSIS
use PPIx::Utilities::Node qw< split_ppi_node_by_namespace >;
my $dom = PPI::Document->new("...");
while (
my ($namespace, $sub_doms) = each split_ppi_node_by_namespace($dom)
) {
foreach my $sub_dom ( @{$sub_doms} ) {
...
}
}
=head1 DESCRIPTION
This is a collection of functions for dealing with L<PPI::Node|PPI::Node>s.
=head1 INTERFACE
Nothing is exported by default.
=head2 split_ppi_node_by_namespace($node)
Returns the sub-trees for each namespace in the node as a reference to a hash
of references to arrays of L<PPI::Node|PPI::Node>s. Say we've got the
following code:
#!perl
my $x = blah();
package Foo;
my $y = blah_blah();
{
say 'Whee!';
package Bar;
something();
}
thingy();
package Baz;
da_da_da();
package Foo;
foreach ( blrfl() ) {
...
}
Calling this function on a L<PPI::Document|PPI::Document> for the above
returns a value that looks like this, using multi-line string literals for the
actual code parts instead of PPI trees to make this easier to read:
{
main => [
q<
#!perl
my $x = blah();
>,
],
Foo => [
q<
package Foo;
my $y = blah_blah();
{
say 'Whee!';
}
thingy();
>,
q<
package Foo;
foreach ( blrfl() ) {
...
}
>,
],
Bar => [
q<
package Bar;
something();
>,
],
Baz => [
q<
package Baz;
da_da_da();
>,
],
}
Note that the return value contains copies of the original nodes, and not the
original nodes themselves due to the need to handle namespaces that are not
file-scoped. (Notice how the first element for "Foo" above differs from the
original code.)
=head1 BUGS AND LIMITATIONS
Please report any bugs or feature requests to
C<bug-ppix-utilities@rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org>.
=head1 AUTHOR
Elliot Shank C<< <perl@galumph.com> >>
=head1 COPYRIGHT
Copyright (c)2009-2010, Elliot Shank C<< <perl@galumph.com> >>.
This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself. The full text of this license
can be found in the LICENSE file included with this module.
=cut
##############################################################################
# $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/distributions/PPIx-Utilities/lib/PPIx/Utilities/Node.pm $
# $Date: 2010-12-01 20:31:47 -0600 (Wed, 01 Dec 2010) $
# $Author: clonezone $
# $Revision: 4001 $
##############################################################################
# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# fill-column: 70
# indent-tabs-mode: nil
# c-indentation-style: bsd
# End:
# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround:
|