1 package PPI::Structure;
7 PPI::Structure - The base class for Perl braced structures
17 PPI::Structure is the root class for all Perl bracing structures. This
18 covers all forms of C< [ ... ] >, C< { ... } >, and C< ( ... ) > brace
19 types, and includes cases where only one half of the pair exist.
21 The class PPI::Structure itself is full abstract and no objects of that
22 type should actually exist in the tree.
24 =head2 Elements vs Children
26 A B<PPI::Structure> has an unusual existance. Unlike a L<PPI::Document>
27 or L<PPI::Statement>, which both simply contain other elements, a
28 structure B<both> contains and consists of content.
30 That is, the brace tokens are B<not> considered to be "children" of the
31 structure, but are part of it.
33 In practice, this will mean that while the -E<gt>elements and -E<gt>tokens
34 methods (and related) B<will> return a list with the brace tokens at either
35 end, the -E<gt>children method explicitly will B<not> return the brace.
37 =head1 STRUCTURE CLASSES
39 Excluding the transient L<PPI::Structure::Unknown> that exists briefly
40 inside the parser, there are eight types of structure.
42 =head2 L<PPI::Structure::List>
44 This covers all round braces used for function arguments, in C<foreach>
45 loops, literal lists, and braces used for precedence-ordering purposes.
47 =head2 L<PPI::Structure::For>
49 Although B<not> used for the C<foreach> loop list, this B<is> used for
50 the special case of the round-brace three-part semicolon-seperated C<for>
51 loop expression (the traditional C style for loop).
53 =head2 L<PPI::Structure::Given>
55 This is for the expression being matched in switch statements.
57 =head2 L<PPI::Structure::When>
59 This is for the matching expression in "when" statements.
61 =head2 L<PPI::Structure::Condition>
63 This round-brace structure covers boolean conditional braces, such as
64 for C<if> and C<while> blocks.
66 =head2 L<PPI::Structure::Block>
68 This curly-brace and common structure is used for all form of code
69 blocks. This includes those for C<if>, C<do> and similar, as well
70 as C<grep>, C<map>, C<sort>, C<sub> and (labelled or anonymous)
73 =head2 L<PPI::Structure::Constructor>
75 This class covers brace structures used for the construction of
76 anonymous C<ARRAY> and C<HASH> references.
78 =head2 L<PPI::Structure::Subscript>
80 This class covers square-braces and curly-braces used after a
81 -E<gt> pointer to access the subscript of an C<ARRAY> or C<HASH>.
85 C<PPI::Structure> itself has very few methods. Most of the time, you will be
86 working with the more generic L<PPI::Element> or L<PPI::Node> methods, or one
87 of the methods that are subclass-specific.
93 use Params::Util qw{_INSTANCE};
95 use PPI::Exception ();
97 use vars qw{$VERSION @ISA *_PARENT};
101 *_PARENT = *PPI::Element::_PARENT;
104 use PPI::Structure::Block ();
105 use PPI::Structure::Condition ();
106 use PPI::Structure::Constructor ();
107 use PPI::Structure::For ();
108 use PPI::Structure::Given ();
109 use PPI::Structure::List ();
110 use PPI::Structure::Subscript ();
111 use PPI::Structure::Unknown ();
112 use PPI::Structure::When ();
118 #####################################################################
123 my $Token = PPI::Token::__LEXER__opens($_[0]) ? shift : return undef;
131 # Set the start braces parent link
132 Scalar::Util::weaken(
133 $_PARENT{Scalar::Util::refaddr $Token} = $self
143 #####################################################################
144 # PPI::Structure API methods
150 For lack of better terminology (like "open" and "close") that has not
151 already in use for some other more important purpose, the two individual
152 braces for the structure are known within PPI as the "start" and "finish"
153 braces (at least for method purposes).
155 The C<start> method returns the start brace for the structure (i.e. the
158 Returns the brace as a L<PPI::Token::Structure> or C<undef> if the
159 structure does not have a starting brace.
161 Under normal parsing circumstances this should never occur, but may happen
162 due to manipulation of the PDOM tree.
166 sub start { $_[0]->{start} }
172 The C<finish> method returns the finish brace for the structure (i.e. the
175 Returns the brace as a L<PPI::Token::Structure> or C<undef> if the
176 structure does not have a starting brace. This can be quite common if
177 the document is not complete (for example, from an editor where the user
178 may be halfway through typeing a subroutine).
182 sub finish { $_[0]->{finish} }
188 The C<braces> method is a utility method which returns the brace type,
189 regardless of whether has both braces defined, or just the starting
190 brace, or just the ending brace.
192 Returns on of the three strings C<'[]'>, C<'{}'>, or C<'()'>, or C<undef>
193 on error (primarily not having a start brace, as mentioned above).
198 my $self = $_[0]->{start} ? shift : return undef;
203 }->{ $self->{start}->{content} };
210 The C<complete> method is a convenience method that returns true if
211 the both braces are defined for the structure, or false if only one
214 Unlike the top level C<complete> method which checks for completeness
215 in depth, the structure complete method ONLY confirms completeness
216 for the braces, and does not recurse downwards.
221 !! ($_[0]->{start} and $_[0]->{finish});
228 #####################################################################
229 # PPI::Node overloaded methods
231 # For us, the "elements" concept includes the brace tokens
236 # Return a list in array context
237 return ( $self->{start} || (), @{$self->{children}}, $self->{finish} || () );
239 # Return the number of elements in scalar context.
240 # This is memory-cheaper than creating another big array
241 return scalar(@{$self->{children}})
242 + ($self->{start} ? 1 : 0)
243 + ($self->{finish} ? 1 : 0);
247 # For us, the first element is probably the opening brace
249 # Technically, if we have no children and no opening brace,
250 # then the first element is the closing brace.
251 $_[0]->{start} or $_[0]->{children}->[0] or $_[0]->{finish};
254 # For us, the last element is probably the closing brace
256 # Technically, if we have no children and no closing brace,
257 # then the last element is the opening brace
258 $_[0]->{finish} or $_[0]->{children}->[-1] or $_[0]->{start};
261 # Location is same as the start token, if any
264 my $first = $self->first_element or return undef;
272 #####################################################################
273 # PPI::Element overloaded methods
275 # Get the full set of tokens, including start and finish
279 $self->{start} || (),
280 $self->SUPER::tokens(@_),
281 $self->{finish} || (),
286 # Like the token method ->content, get our merged contents.
287 # This will recurse downwards through everything
288 ### Reimplement this using List::Utils stuff
291 my $content = $self->{start} ? $self->{start}->content : '';
292 foreach my $child ( @{$self->{children}} ) {
293 $content .= $child->content;
295 $content .= $self->{finish}->content if $self->{finish};
299 # Is the structure completed
301 !! ( defined $_[0]->{finish} );
304 # You can insert either another structure, or a token
307 my $Element = _INSTANCE(shift, 'PPI::Element') or return undef;
308 if ( $Element->isa('PPI::Structure') ) {
309 return $self->__insert_before($Element);
310 } elsif ( $Element->isa('PPI::Token') ) {
311 return $self->__insert_before($Element);
316 # As above, you can insert either another structure, or a token
319 my $Element = _INSTANCE(shift, 'PPI::Element') or return undef;
320 if ( $Element->isa('PPI::Structure') ) {
321 return $self->__insert_after($Element);
322 } elsif ( $Element->isa('PPI::Token') ) {
323 return $self->__insert_after($Element);
334 See the L<support section|PPI/SUPPORT> in the main module.
338 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
342 Copyright 2001 - 2009 Adam Kennedy.
344 This program is free software; you can redistribute
345 it and/or modify it under the same terms as Perl itself.
347 The full text of the license can be found in the
348 LICENSE file included with this module.