File Coverage

blib/lib/PPI/Structure.pm
Criterion Covered Total %
statement 69 89 77.5
branch 6 32 18.7
condition 5 21 23.8
subroutine 24 29 82.7
pod 11 13 84.6
total 115 184 62.5


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 64     64   465 use strict;
  64         126  
  64         1920  
92 64     64   390 use Scalar::Util ();
  64         117  
  64         1179  
93 64     64   326 use Params::Util qw{_INSTANCE};
  64         127  
  64         2567  
94 64     64   367 use PPI::Node ();
  64         137  
  64         761  
95 64     64   267 use PPI::Exception ();
  64         129  
  64         1052  
96 64     64   328 use PPI::Singletons '%_PARENT';
  64         127  
  64         7827  
97              
98             our $VERSION = '1.277';
99              
100             our @ISA = "PPI::Node";
101              
102 64     64   28427 use PPI::Structure::Block ();
  64         186  
  64         1490  
103 64     64   27426 use PPI::Structure::Condition ();
  64         219  
  64         1615  
104 64     64   26427 use PPI::Structure::Constructor ();
  64         174  
  64         1590  
105 64     64   26413 use PPI::Structure::For ();
  64         184  
  64         1713  
106 64     64   27104 use PPI::Structure::Given ();
  64         179  
  64         1680  
107 64     64   27642 use PPI::Structure::List ();
  64         171  
  64         1819  
108 64     64   27070 use PPI::Structure::Subscript ();
  64         187  
  64         1802  
109 64     64   26304 use PPI::Structure::Unknown ();
  64         181  
  64         1888  
110 64     64   26578 use PPI::Structure::When ();
  64         234  
  64         58550  
111              
112              
113              
114              
115              
116             #####################################################################
117             # Constructor
118              
119             sub new {
120 21870     21870 0 36734 my $class = shift;
121 21870 50       51352 my $Token = PPI::Token::__LEXER__opens($_[0]) ? shift : return undef;
122              
123             # Create the object
124 21870         99815 my $self = bless {
125             children => [],
126             start => $Token,
127             }, $class;
128              
129             # Set the start braces parent link
130             Scalar::Util::weaken(
131 21870         132705 $_PARENT{Scalar::Util::refaddr $Token} = $self
132             );
133              
134 21870         44443 $self;
135             }
136              
137              
138              
139              
140              
141             #####################################################################
142             # PPI::Structure API methods
143              
144             =pod
145              
146             =head2 start
147              
148             For lack of better terminology (like "open" and "close") that has not
149             already in use for some other more important purpose, the two individual
150             braces for the structure are known within PPI as the "start" and "finish"
151             braces (at least for method purposes).
152              
153             The C method returns the start brace for the structure (i.e. the
154             opening brace).
155              
156             Returns the brace as a L or C if the
157             structure does not have a starting brace.
158              
159             Under normal parsing circumstances this should never occur, but may happen
160             due to manipulation of the PDOM tree.
161              
162             =cut
163              
164 147124     147124 1 406545 sub start { $_[0]->{start} }
165              
166             =pod
167              
168             =head2 finish
169              
170             The C method returns the finish brace for the structure (i.e. the
171             closing brace).
172              
173             Returns the brace as a L or C if the
174             structure does not have a finishing brace. This can be quite common if
175             the document is not complete (for example, from an editor where the user
176             may be halfway through typeing a subroutine).
177              
178             =cut
179              
180 126931     126931 1 371947 sub finish { $_[0]->{finish} }
181              
182             =pod
183              
184             =head2 braces
185              
186             The C method is a utility method which returns the brace type,
187             regardless of whether both or just one of the braces is defined.
188              
189             Returns one of the three strings C<'[]'>, C<'{}'>, or C<'()'>, or C
190             on error (primarily not having a start brace, as mentioned above).
191              
192             =cut
193              
194             sub braces {
195 841 50   841 1 3252 my $self = $_[0]->{start} ? shift : return undef;
196             return {
197             '[' => '[]',
198             '(' => '()',
199             '{' => '{}',
200 841         7706 }->{ $self->{start}->{content} };
201             }
202              
203             =pod
204              
205             =head1 complete
206              
207             The C method is a convenience method that returns true if
208             the both braces are defined for the structure, or false if only one
209             brace is defined.
210              
211             Unlike the top level C method which checks for completeness
212             in depth, the structure complete method ONLY confirms completeness
213             for the braces, and does not recurse downwards.
214              
215             =cut
216              
217             sub complete {
218 0   0 0 0 0 !! ($_[0]->{start} and $_[0]->{finish});
219             }
220              
221              
222              
223              
224              
225             #####################################################################
226             # PPI::Node overloaded methods
227              
228             # For us, the "elements" concept includes the brace tokens
229             sub elements {
230 0     0 1 0 my $self = shift;
231              
232 0 0       0 if ( wantarray ) {
233             # Return a list in array context
234 0   0     0 return ( $self->{start} || (), @{$self->{children}}, $self->{finish} || () );
  0   0     0  
235             } else {
236             # Return the number of elements in scalar context.
237             # This is memory-cheaper than creating another big array
238 0         0 return scalar(@{$self->{children}})
239             + ($self->{start} ? 1 : 0)
240 0 0       0 + ($self->{finish} ? 1 : 0);
    0          
241             }
242             }
243              
244             # For us, the first element is probably the opening brace
245             sub first_element {
246             # Technically, if we have no children and no opening brace,
247             # then the first element is the closing brace.
248 4955 0 33 4955 1 23090 $_[0]->{start} or $_[0]->{children}->[0] or $_[0]->{finish};
249             }
250              
251             # For us, the last element is probably the closing brace
252             sub last_element {
253             # Technically, if we have no children and no closing brace,
254             # then the last element is the opening brace
255 7 0 33 7 1 210 $_[0]->{finish} or $_[0]->{children}->[-1] or $_[0]->{start};
256             }
257              
258             # Location is same as the start token, if any
259             sub location {
260 4948     4948 1 15767 my $self = shift;
261 4948 50       9274 my $first = $self->first_element or return undef;
262 4948         10324 $first->location;
263             }
264              
265              
266              
267              
268              
269             #####################################################################
270             # PPI::Element overloaded methods
271              
272             # Get the full set of tokens, including start and finish
273             sub tokens {
274 22732     22732 1 32199 my $self = shift;
275             my @tokens = (
276             $self->{start} || (),
277             $self->SUPER::tokens(@_),
278 22732   33     85526 $self->{finish} || (),
      66        
279             );
280 22732         110863 @tokens;
281             }
282              
283             # Like the token method ->content, get our merged contents.
284             # This will recurse downwards through everything
285             ### Reimplement this using List::Utils stuff
286             sub content {
287 48368     48368 1 69669 my $self = shift;
288 48368 50       148552 my $content = $self->{start} ? $self->{start}->content : '';
289 48368         63772 foreach my $child ( @{$self->{children}} ) {
  48368         86473  
290 166395         305616 $content .= $child->content;
291             }
292 48368 100       159983 $content .= $self->{finish}->content if $self->{finish};
293 48368         160320 $content;
294             }
295              
296             # Is the structure completed
297             sub _complete {
298 0     0     !! ( defined $_[0]->{finish} );
299             }
300              
301             # You can insert either another structure, or a token
302             sub insert_before {
303 0     0 1   my $self = shift;
304 0 0         my $Element = _INSTANCE(shift, 'PPI::Element') or return undef;
305 0 0         if ( $Element->isa('PPI::Structure') ) {
    0          
306 0           return $self->__insert_before($Element);
307             } elsif ( $Element->isa('PPI::Token') ) {
308 0           return $self->__insert_before($Element);
309             }
310 0           '';
311             }
312              
313             # As above, you can insert either another structure, or a token
314             sub insert_after {
315 0     0 1   my $self = shift;
316 0 0         my $Element = _INSTANCE(shift, 'PPI::Element') or return undef;
317 0 0         if ( $Element->isa('PPI::Structure') ) {
    0          
318 0           return $self->__insert_after($Element);
319             } elsif ( $Element->isa('PPI::Token') ) {
320 0           return $self->__insert_after($Element);
321             }
322 0           '';
323             }
324              
325             1;
326              
327             =pod
328              
329             =head1 SUPPORT
330              
331             See the L in the main module.
332              
333             =head1 AUTHOR
334              
335             Adam Kennedy Eadamk@cpan.orgE
336              
337             =head1 COPYRIGHT
338              
339             Copyright 2001 - 2011 Adam Kennedy.
340              
341             This program is free software; you can redistribute
342             it and/or modify it under the same terms as Perl itself.
343              
344             The full text of the license can be found in the
345             LICENSE file included with this module.
346              
347             =cut