File Coverage

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


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 has an unusual existence. Unlike a L
27             or L, which both simply contain other elements, a
28             structure B contains and consists of content.
29              
30             That is, the brace tokens are B considered to be "children" of the
31             structure, but are part of it.
32              
33             In practice, this will mean that while the -Eelements and -Etokens
34             methods (and related) B return a list with the brace tokens at either
35             end, the -Echildren method explicitly will B return the brace.
36              
37             =head1 STRUCTURE CLASSES
38              
39             Excluding the transient L that exists briefly
40             inside the parser, there are eight types of structure.
41              
42             =head2 L
43              
44             This covers all round braces used for function arguments, in C
45             loops, literal lists, and braces used for precedence-ordering purposes.
46              
47             =head2 L
48              
49             Although B used for the C loop list, this B used for
50             the special case of the round-brace three-part semicolon-separated C
51             loop expression (the traditional C style for loop).
52              
53             =head2 L
54              
55             This is for the expression being matched in switch statements.
56              
57             =head2 L
58              
59             This is for the matching expression in "when" statements.
60              
61             =head2 L
62              
63             This round-brace structure covers boolean conditional braces, such as
64             for C and C blocks.
65              
66             =head2 L
67              
68             This curly-brace and common structure is used for all form of code
69             blocks. This includes those for C, C and similar, as well
70             as C, C, C, C and (labelled or anonymous)
71             scoping blocks.
72              
73             =head2 L
74              
75             This class covers brace structures used for the construction of
76             anonymous C and C references.
77              
78             =head2 L
79              
80             This class covers square-braces and curly-braces used after a
81             -E pointer to access the subscript of an C or C.
82              
83             =head1 METHODS
84              
85             C itself has very few methods. Most of the time, you will be
86             working with the more generic L or L methods, or one
87             of the methods that are subclass-specific.
88              
89             =cut
90              
91 66     66   491 use strict;
  66         152  
  66         2733  
92 66     66   399 use Scalar::Util ();
  66         143  
  66         1757  
93 66     66   368 use Params::Util qw{_INSTANCE};
  66         153  
  66         4072  
94 66     66   434 use PPI::Node ();
  66         139  
  66         1464  
95 66     66   346 use PPI::Exception ();
  66         169  
  66         1965  
96 66     66   360 use PPI::Singletons '%_PARENT';
  66         123  
  66         12013  
97              
98             our $VERSION = '1.28401'; # TRIAL
99              
100             our @ISA = "PPI::Node";
101              
102 66     66   37502 use PPI::Structure::Block ();
  66         230  
  66         1831  
103 66     66   33728 use PPI::Structure::Condition ();
  66         235  
  66         2136  
104 66     66   34071 use PPI::Structure::Constructor ();
  66         321  
  66         2014  
105 66     66   34137 use PPI::Structure::For ();
  66         226  
  66         2195  
106 66     66   35571 use PPI::Structure::Given ();
  66         229  
  66         2270  
107 66     66   33098 use PPI::Structure::List ();
  66         243  
  66         2480  
108 66     66   34730 use PPI::Structure::Subscript ();
  66         302  
  66         2356  
109 66     66   32885 use PPI::Structure::Unknown ();
  66         236  
  66         2576  
110 66     66   33098 use PPI::Structure::When ();
  66         310  
  66         2531  
111 66     66   33088 use PPI::Structure::Signature ();
  66         265  
  66         75669  
112              
113              
114              
115              
116              
117             #####################################################################
118             # Constructor
119              
120             sub new {
121 22473     22473 0 43286 my $class = shift;
122 22473 50       85619 my $Token = PPI::Token::__LEXER__opens($_[0]) ? shift : return undef;
123              
124             # Create the object
125 22473         125593 my $self = bless {
126             children => [],
127             start => $Token,
128             }, $class;
129              
130             # Set the start braces parent link
131             Scalar::Util::weaken(
132 22473         99940 $_PARENT{Scalar::Util::refaddr $Token} = $self
133             );
134              
135 22473         59168 $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 method returns the start brace for the structure (i.e. the
155             opening brace).
156              
157             Returns the brace as a L or C 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 152124     152124 1 566682 sub start { $_[0]->{start} }
166              
167             =pod
168              
169             =head2 finish
170              
171             The C method returns the finish brace for the structure (i.e. the
172             closing brace).
173              
174             Returns the brace as a L or C 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 131435     131435 1 456887 sub finish { $_[0]->{finish} }
182              
183             =pod
184              
185             =head2 braces
186              
187             The C 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
191             on error (primarily not having a start brace, as mentioned above).
192              
193             =cut
194              
195             sub braces {
196 891 50   891 1 4032 my $self = $_[0]->{start} ? shift : return undef;
197             return {
198             '[' => '[]',
199             '(' => '()',
200             '{' => '{}',
201 891         10016 }->{ $self->{start}->{content} };
202             }
203              
204             =pod
205              
206             =head1 complete
207              
208             The C 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 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 5124 0 33 5124 1 30424 $_[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 49 $_[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 5117     5117 1 24039 my $self = shift;
262 5117 50       12555 my $first = $self->first_element or return undef;
263 5117         13065 $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 23459     23459 1 39196 my $self = shift;
276             my @tokens = (
277             $self->{start} || (),
278             $self->SUPER::tokens(@_),
279 23459   33     125320 $self->{finish} || (),
      66        
280             );
281 23459         163609 @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 50528     50528 1 98412 my $self = shift;
289 50528 50       218232 my $content = $self->{start} ? $self->{start}->content : '';
290 50528         81014 foreach my $child ( @{$self->{children}} ) {
  50528         116721  
291 173682         389441 $content .= $child->content;
292             }
293 50528 100       207975 $content .= $self->{finish}->content if $self->{finish};
294 50528         208046 $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 in the main module.
333              
334             =head1 AUTHOR
335              
336             Adam Kennedy Eadamk@cpan.orgE
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