/usr/share/perl5/PPI/Structure.pm is in libppi-perl 1.220-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 346 347 348 349 350 | package PPI::Structure;
=pod
=head1 NAME
PPI::Structure - The base class for Perl braced structures
=head1 INHERITANCE
PPI::Structure
isa PPI::Node
isa PPI::Element
=head1 DESCRIPTION
PPI::Structure is the root class for all Perl bracing structures. This
covers all forms of C< [ ... ] >, C< { ... } >, and C< ( ... ) > brace
types, and includes cases where only one half of the pair exist.
The class PPI::Structure itself is full abstract and no objects of that
type should actually exist in the tree.
=head2 Elements vs Children
A B<PPI::Structure> has an unusual existence. Unlike a L<PPI::Document>
or L<PPI::Statement>, which both simply contain other elements, a
structure B<both> contains and consists of content.
That is, the brace tokens are B<not> considered to be "children" of the
structure, but are part of it.
In practice, this will mean that while the -E<gt>elements and -E<gt>tokens
methods (and related) B<will> return a list with the brace tokens at either
end, the -E<gt>children method explicitly will B<not> return the brace.
=head1 STRUCTURE CLASSES
Excluding the transient L<PPI::Structure::Unknown> that exists briefly
inside the parser, there are eight types of structure.
=head2 L<PPI::Structure::List>
This covers all round braces used for function arguments, in C<foreach>
loops, literal lists, and braces used for precedence-ordering purposes.
=head2 L<PPI::Structure::For>
Although B<not> used for the C<foreach> loop list, this B<is> used for
the special case of the round-brace three-part semicolon-separated C<for>
loop expression (the traditional C style for loop).
=head2 L<PPI::Structure::Given>
This is for the expression being matched in switch statements.
=head2 L<PPI::Structure::When>
This is for the matching expression in "when" statements.
=head2 L<PPI::Structure::Condition>
This round-brace structure covers boolean conditional braces, such as
for C<if> and C<while> blocks.
=head2 L<PPI::Structure::Block>
This curly-brace and common structure is used for all form of code
blocks. This includes those for C<if>, C<do> and similar, as well
as C<grep>, C<map>, C<sort>, C<sub> and (labelled or anonymous)
scoping blocks.
=head2 L<PPI::Structure::Constructor>
This class covers brace structures used for the construction of
anonymous C<ARRAY> and C<HASH> references.
=head2 L<PPI::Structure::Subscript>
This class covers square-braces and curly-braces used after a
-E<gt> pointer to access the subscript of an C<ARRAY> or C<HASH>.
=head1 METHODS
C<PPI::Structure> itself has very few methods. Most of the time, you will be
working with the more generic L<PPI::Element> or L<PPI::Node> methods, or one
of the methods that are subclass-specific.
=cut
use strict;
use Scalar::Util ();
use Params::Util qw{_INSTANCE};
use PPI::Node ();
use PPI::Exception ();
use vars qw{$VERSION @ISA *_PARENT};
BEGIN {
$VERSION = '1.220';
@ISA = 'PPI::Node';
*_PARENT = *PPI::Element::_PARENT;
}
use PPI::Structure::Block ();
use PPI::Structure::Condition ();
use PPI::Structure::Constructor ();
use PPI::Structure::For ();
use PPI::Structure::Given ();
use PPI::Structure::List ();
use PPI::Structure::Subscript ();
use PPI::Structure::Unknown ();
use PPI::Structure::When ();
#####################################################################
# Constructor
sub new {
my $class = shift;
my $Token = PPI::Token::__LEXER__opens($_[0]) ? shift : return undef;
# Create the object
my $self = bless {
children => [],
start => $Token,
}, $class;
# Set the start braces parent link
Scalar::Util::weaken(
$_PARENT{Scalar::Util::refaddr $Token} = $self
);
$self;
}
#####################################################################
# PPI::Structure API methods
=pod
=head2 start
For lack of better terminology (like "open" and "close") that has not
already in use for some other more important purpose, the two individual
braces for the structure are known within PPI as the "start" and "finish"
braces (at least for method purposes).
The C<start> method returns the start brace for the structure (i.e. the
opening brace).
Returns the brace as a L<PPI::Token::Structure> or C<undef> if the
structure does not have a starting brace.
Under normal parsing circumstances this should never occur, but may happen
due to manipulation of the PDOM tree.
=cut
sub start { $_[0]->{start} }
=pod
=head2 finish
The C<finish> method returns the finish brace for the structure (i.e. the
closing brace).
Returns the brace as a L<PPI::Token::Structure> or C<undef> if the
structure does not have a finishing brace. This can be quite common if
the document is not complete (for example, from an editor where the user
may be halfway through typeing a subroutine).
=cut
sub finish { $_[0]->{finish} }
=pod
=head2 braces
The C<braces> method is a utility method which returns the brace type,
regardless of whether has both braces defined, or just the starting
brace, or just the ending brace.
Returns on of the three strings C<'[]'>, C<'{}'>, or C<'()'>, or C<undef>
on error (primarily not having a start brace, as mentioned above).
=cut
sub braces {
my $self = $_[0]->{start} ? shift : return undef;
return {
'[' => '[]',
'(' => '()',
'{' => '{}',
}->{ $self->{start}->{content} };
}
=pod
=head1 complete
The C<complete> method is a convenience method that returns true if
the both braces are defined for the structure, or false if only one
brace is defined.
Unlike the top level C<complete> method which checks for completeness
in depth, the structure complete method ONLY confirms completeness
for the braces, and does not recurse downwards.
=cut
sub complete {
!! ($_[0]->{start} and $_[0]->{finish});
}
#####################################################################
# PPI::Node overloaded methods
# For us, the "elements" concept includes the brace tokens
sub elements {
my $self = shift;
if ( wantarray ) {
# Return a list in array context
return ( $self->{start} || (), @{$self->{children}}, $self->{finish} || () );
} else {
# Return the number of elements in scalar context.
# This is memory-cheaper than creating another big array
return scalar(@{$self->{children}})
+ ($self->{start} ? 1 : 0)
+ ($self->{finish} ? 1 : 0);
}
}
# For us, the first element is probably the opening brace
sub first_element {
# Technically, if we have no children and no opening brace,
# then the first element is the closing brace.
$_[0]->{start} or $_[0]->{children}->[0] or $_[0]->{finish};
}
# For us, the last element is probably the closing brace
sub last_element {
# Technically, if we have no children and no closing brace,
# then the last element is the opening brace
$_[0]->{finish} or $_[0]->{children}->[-1] or $_[0]->{start};
}
# Location is same as the start token, if any
sub location {
my $self = shift;
my $first = $self->first_element or return undef;
$first->location;
}
#####################################################################
# PPI::Element overloaded methods
# Get the full set of tokens, including start and finish
sub tokens {
my $self = shift;
my @tokens = (
$self->{start} || (),
$self->SUPER::tokens(@_),
$self->{finish} || (),
);
@tokens;
}
# Like the token method ->content, get our merged contents.
# This will recurse downwards through everything
### Reimplement this using List::Utils stuff
sub content {
my $self = shift;
my $content = $self->{start} ? $self->{start}->content : '';
foreach my $child ( @{$self->{children}} ) {
$content .= $child->content;
}
$content .= $self->{finish}->content if $self->{finish};
$content;
}
# Is the structure completed
sub _complete {
!! ( defined $_[0]->{finish} );
}
# You can insert either another structure, or a token
sub insert_before {
my $self = shift;
my $Element = _INSTANCE(shift, 'PPI::Element') or return undef;
if ( $Element->isa('PPI::Structure') ) {
return $self->__insert_before($Element);
} elsif ( $Element->isa('PPI::Token') ) {
return $self->__insert_before($Element);
}
'';
}
# As above, you can insert either another structure, or a token
sub insert_after {
my $self = shift;
my $Element = _INSTANCE(shift, 'PPI::Element') or return undef;
if ( $Element->isa('PPI::Structure') ) {
return $self->__insert_after($Element);
} elsif ( $Element->isa('PPI::Token') ) {
return $self->__insert_after($Element);
}
'';
}
1;
=pod
=head1 SUPPORT
See the L<support section|PPI/SUPPORT> in the main module.
=head1 AUTHOR
Adam Kennedy E<lt>adamk@cpan.orgE<gt>
=head1 COPYRIGHT
Copyright 2001 - 2011 Adam Kennedy.
This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the
LICENSE file included with this module.
=cut
|