File Coverage

blib/lib/PPI/Structure.pm
Criterion Covered Total %
statement 72 92 78.2
branch 5 32 15.6
condition 5 21 23.8
subroutine 25 30 83.3
pod 11 13 84.6
total 118 188 62.7


line stmt bran cond sub pod time code
1             package PPI::Structure;
2              
3             =pod
4              
5             =head1 NAME
6              
7             PPI::Structure - The base class for Perl braced structures
8              
9             =head1 INHERITANCE
10              
11             PPI::Structure
12             isa PPI::Node
13             isa PPI::Element
14              
15             =head1 DESCRIPTION
16              
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.
20              
21             The class PPI::Structure itself is full abstract and no objects of that
22             type should actually exist in the tree.
23              
24             =head2 Elements vs Children
25              
26             A B<PPI::Structure> has an unusual existence. 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.
29              
30             That is, the brace tokens are B<not> considered to be "children" of the
31             structure, but are part of it.
32              
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.
36              
37             =head1 STRUCTURE CLASSES
38              
39             Excluding the transient L<PPI::Structure::Unknown> that exists briefly
40             inside the parser, there are eight types of structure.
41              
42             =head2 L<PPI::Structure::List>
43              
44             This covers all round braces used for function arguments, in C<foreach>
45             loops, literal lists, and braces used for precedence-ordering purposes.
46              
47             =head2 L<PPI::Structure::For>
48              
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-separated C<for>
51             loop expression (the traditional C style for loop).
52              
53             =head2 L<PPI::Structure::Given>
54              
55             This is for the expression being matched in switch statements.
56              
57             =head2 L<PPI::Structure::When>
58              
59             This is for the matching expression in "when" statements.
60              
61             =head2 L<PPI::Structure::Condition>
62              
63             This round-brace structure covers boolean conditional braces, such as
64             for C<if> and C<while> blocks.
65              
66             =head2 L<PPI::Structure::Block>
67              
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)
71             scoping blocks.
72              
73             =head2 L<PPI::Structure::Constructor>
74              
75             This class covers brace structures used for the construction of
76             anonymous C<ARRAY> and C<HASH> references.
77              
78             =head2 L<PPI::Structure::Subscript>
79              
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>.
82              
83             =head1 METHODS
84              
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.
88              
89             =cut
90              
91 66     66   375 use strict;
  66         100  
  66         1856  
92 66     66   242 use Scalar::Util ();
  66         89  
  66         1179  
93 66     66   222 use Params::Util qw{_INSTANCE};
  66         95  
  66         2603  
94 66     66   248 use PPI::Node ();
  66         89  
  66         795  
95 66     66   221 use PPI::Exception ();
  66         92  
  66         1233  
96 66     66   199 use PPI::Singletons '%_PARENT';
  66         110  
  66         7796  
97              
98             our $VERSION = '1.284';
99              
100             our @ISA = "PPI::Node";
101              
102 66     66   23972 use PPI::Structure::Block ();
  66         160  
  66         1243  
103 66     66   22781 use PPI::Structure::Condition ();
  66         157  
  66         1316  
104 66     66   22069 use PPI::Structure::Constructor ();
  66         154  
  66         1341  
105 66     66   22475 use PPI::Structure::For ();
  66         177  
  66         1382  
106 66     66   22472 use PPI::Structure::Given ();
  66         152  
  66         1562  
107 66     66   22773 use PPI::Structure::List ();
  66         151  
  66         1583  
108 66     66   23274 use PPI::Structure::Subscript ();
  66         161  
  66         1520  
109 66     66   22521 use PPI::Structure::Unknown ();
  66         164  
  66         1553  
110 66     66   22156 use PPI::Structure::When ();
  66         157  
  66         1678  
111 66     66   21729 use PPI::Structure::Signature ();
  66         200  
  66         46261  
112              
113              
114              
115              
116              
117             #####################################################################
118             # Constructor
119              
120             sub new {
121 22425     22425 0 33433 my $class = shift;
122 22425 50       53133 my $Token = PPI::Token::__LEXER__opens($_[0]) ? shift : return undef;
123              
124             # Create the object
125 22425         81451 my $self = bless {
126             children => [],
127             start => $Token,
128             }, $class;
129              
130             # Set the start braces parent link
131             Scalar::Util::weaken(
132 22425         122089 $_PARENT{Scalar::Util::refaddr $Token} = $self
133             );
134              
135 22425         40408 $self;
136             }
137              
138              
139              
140              
141              
142             #####################################################################
143             # PPI::Structure API methods
144              
145             =pod
146              
147             =head2 start
148              
149             For lack of better terminology (like "open" and "close") that has not
150             already in use for some other more important purpose, the two individual
151             braces for the structure are known within PPI as the "start" and "finish"
152             braces (at least for method purposes).
153              
154             The C<start> method returns the start brace for the structure (i.e. the
155             opening brace).
156              
157             Returns the brace as a L<PPI::Token::Structure> or C<undef> if the
158             structure does not have a starting brace.
159              
160             Under normal parsing circumstances this should never occur, but may happen
161             due to manipulation of the PDOM tree.
162              
163             =cut
164              
165 151539     151539 1 351871 sub start { $_[0]->{start} }
166              
167             =pod
168              
169             =head2 finish
170              
171             The C<finish> method returns the finish brace for the structure (i.e. the
172             closing brace).
173              
174             Returns the brace as a L<PPI::Token::Structure> or C<undef> if the
175             structure does not have a finishing brace. This can be quite common if
176             the document is not complete (for example, from an editor where the user
177             may be halfway through typeing a subroutine).
178              
179             =cut
180              
181 130895     130895 1 301627 sub finish { $_[0]->{finish} }
182              
183             =pod
184              
185             =head2 braces
186              
187             The C<braces> method is a utility method which returns the brace type,
188             regardless of whether both or just one of the braces is defined.
189              
190             Returns one of the three strings C<'[]'>, C<'{}'>, or C<'()'>, or C<undef>
191             on error (primarily not having a start brace, as mentioned above).
192              
193             =cut
194              
195             sub braces {
196 895 50   895 1 3086 my $self = $_[0]->{start} ? shift : return undef;
197             return {
198             '[' => '[]',
199             '(' => '()',
200             '{' => '{}',
201 895         7496 }->{ $self->{start}->{content} };
202             }
203              
204             =pod
205              
206             =head1 complete
207              
208             The C<complete> method is a convenience method that returns true if
209             the both braces are defined for the structure, or false if only one
210             brace is defined.
211              
212             Unlike the top level C<complete> method which checks for completeness
213             in depth, the structure complete method ONLY confirms completeness
214             for the braces, and does not recurse downwards.
215              
216             =cut
217              
218             sub complete {
219 0   0 0 0 0 !! ($_[0]->{start} and $_[0]->{finish});
220             }
221              
222              
223              
224              
225              
226             #####################################################################
227             # PPI::Node overloaded methods
228              
229             # For us, the "elements" concept includes the brace tokens
230             sub elements {
231 0     0 1 0 my $self = shift;
232              
233 0 0       0 if ( wantarray ) {
234             # Return a list in array context
235 0   0     0 return ( $self->{start} || (), @{$self->{children}}, $self->{finish} || () );
  0   0     0  
236             } else {
237             # Return the number of elements in scalar context.
238             # This is memory-cheaper than creating another big array
239 0         0 return scalar(@{$self->{children}})
240             + ($self->{start} ? 1 : 0)
241 0 0       0 + ($self->{finish} ? 1 : 0);
    0          
242             }
243             }
244              
245             # For us, the first element is probably the opening brace
246             sub first_element {
247             # Technically, if we have no children and no opening brace,
248             # then the first element is the closing brace.
249 5097 0 33 5097 1 17933 $_[0]->{start} or $_[0]->{children}->[0] or $_[0]->{finish};
250             }
251              
252             # For us, the last element is probably the closing brace
253             sub last_element {
254             # Technically, if we have no children and no closing brace,
255             # then the last element is the opening brace
256 7 0 33 7 1 40 $_[0]->{finish} or $_[0]->{children}->[-1] or $_[0]->{start};
257             }
258              
259             # Location is same as the start token, if any
260             sub location {
261 5090     5090 1 13095 my $self = shift;
262 5090 50       7370 my $first = $self->first_element or return undef;
263 5090         7930 $first->location;
264             }
265              
266              
267              
268              
269              
270             #####################################################################
271             # PPI::Element overloaded methods
272              
273             # Get the full set of tokens, including start and finish
274             sub tokens {
275 23384     23384 1 26177 my $self = shift;
276             my @tokens = (
277             $self->{start} || (),
278             $self->SUPER::tokens(@_),
279 23384   33     80001 $self->{finish} || (),
      66        
280             );
281 23384         105150 @tokens;
282             }
283              
284             # Like the token method ->content, get our merged contents.
285             # This will recurse downwards through everything
286             ### Reimplement this using List::Utils stuff
287             sub content {
288 50522     50522 1 59578 my $self = shift;
289 50522 50       143437 my $content = $self->{start} ? $self->{start}->content : '';
290 50522         55251 foreach my $child ( @{$self->{children}} ) {
  50522         85040  
291 173518         263815 $content .= $child->content;
292             }
293 50522 50       137925 $content .= $self->{finish}->content if $self->{finish};
294 50522         144669 $content;
295             }
296              
297             # Is the structure completed
298             sub _complete {
299 0     0     !! ( defined $_[0]->{finish} );
300             }
301              
302             # You can insert either another structure, or a token
303             sub insert_before {
304 0     0 1   my $self = shift;
305 0 0         my $Element = _INSTANCE(shift, 'PPI::Element') or return undef;
306 0 0         if ( $Element->isa('PPI::Structure') ) {
    0          
307 0           return $self->__insert_before($Element);
308             } elsif ( $Element->isa('PPI::Token') ) {
309 0           return $self->__insert_before($Element);
310             }
311 0           '';
312             }
313              
314             # As above, you can insert either another structure, or a token
315             sub insert_after {
316 0     0 1   my $self = shift;
317 0 0         my $Element = _INSTANCE(shift, 'PPI::Element') or return undef;
318 0 0         if ( $Element->isa('PPI::Structure') ) {
    0          
319 0           return $self->__insert_after($Element);
320             } elsif ( $Element->isa('PPI::Token') ) {
321 0           return $self->__insert_after($Element);
322             }
323 0           '';
324             }
325              
326             1;
327              
328             =pod
329              
330             =head1 SUPPORT
331              
332             See the L<support section|PPI/SUPPORT> in the main module.
333              
334             =head1 AUTHOR
335              
336             Adam Kennedy E<lt>adamk@cpan.orgE<gt>
337              
338             =head1 COPYRIGHT
339              
340             Copyright 2001 - 2011 Adam Kennedy.
341              
342             This program is free software; you can redistribute
343             it and/or modify it under the same terms as Perl itself.
344              
345             The full text of the license can be found in the
346             LICENSE file included with this module.
347              
348             =cut