PPI/Structure.pm

filter: Statementstmt 78.4% 73 / 93 Branchbran 25.0% 8 / 32 Conditioncond 23.8% 5 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 83.3% 25 / 30 Podpod 84.6% 11 / 13 totaltotal 60.1% 122 / 203 CC 25 SCAR 22.4
CC
25
Cov
59%
elements
42.0
insert_before
20.0
insert_after
20.0
CRAP 9.4
« PPI/Token/Pod.pm ↑ Summary PPI/Token/Symbol.pm »
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 62 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
92 62 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
93 62 ▶use Params::Util qw{_INSTANCE};
Subroutine BEGIN: called CC=1 SCAR=0.0
94 62 ▶use PPI::Node ();
Subroutine BEGIN: called CC=1 SCAR=0.0
95 62 ▶use PPI::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
96  
97 62 ▶use vars qw{$VERSION @ISA *_PARENT};
Subroutine BEGIN: called CC=1 SCAR=0.0
98 BEGIN {
99 62 ▶ $VERSION = '1.236';
Subroutine BEGIN: called CC=1 SCAR=0.0
100 62 @ISA = 'PPI::Node';
101 62 *_PARENT = *PPI::Element::_PARENT;
102 }
103  
104 62 ▶use PPI::Structure::Block ();
Subroutine BEGIN: called CC=1 SCAR=0.0
105 62 ▶use PPI::Structure::Condition ();
Subroutine BEGIN: called CC=1 SCAR=0.0
106 62 ▶use PPI::Structure::Constructor ();
Subroutine BEGIN: called CC=1 SCAR=0.0
107 62 ▶use PPI::Structure::For ();
Subroutine BEGIN: called CC=1 SCAR=0.0
108 62 ▶use PPI::Structure::Given ();
Subroutine BEGIN: called CC=1 SCAR=0.0
109 62 ▶use PPI::Structure::List ();
Subroutine BEGIN: called CC=1 SCAR=0.0
110 62 ▶use PPI::Structure::Subscript ();
Subroutine BEGIN: called CC=1 SCAR=0.0
111 62 ▶use PPI::Structure::Unknown ();
Subroutine BEGIN: called CC=1 SCAR=0.0
112 62 ▶use PPI::Structure::When ();
Subroutine BEGIN: called CC=1 SCAR=0.0
113  
114  
115  
116  
117  
118 #####################################################################
119 # Constructor
120  
121 sub new {
122 21081 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
123 21081 ▶ my $Token = PPI::Token::__LEXER__opens($_[0]) ? shift : return undef;
Branch: 1 branch
BranchTrueFalseTotal
PPI::Token::__LEXER__opens($_[0]) ? : 21081 0 21081
124  
125 # Create the object
126 21081 my $self = bless {
127 children => [],
128 start => $Token,
129 }, $class;
130  
131 # Set the start braces parent link
132 Scalar::Util::weaken(
133 21081 $_PARENT{Scalar::Util::refaddr $Token} = $self
134 );
135  
136 21081 $self;
137 }
138  
139  
140  
141  
142  
143 #####################################################################
144 # PPI::Structure API methods
145  
146 =pod
147  
148 =head2 start
149  
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).
154  
155 The C<start> method returns the start brace for the structure (i.e. the
156 opening brace).
157  
158 Returns the brace as a L<PPI::Token::Structure> or C<undef> if the
159 structure does not have a starting brace.
160  
161 Under normal parsing circumstances this should never occur, but may happen
162 due to manipulation of the PDOM tree.
163  
164 =cut
165  
166 143657 ▶sub start { $_[0]->{start} }
Subroutine start: called CC=1 SCAR=0.0
Pod documented
167  
168 =pod
169  
170 =head2 finish
171  
172 The C<finish> method returns the finish brace for the structure (i.e. the
173 closing brace).
174  
175 Returns the brace as a L<PPI::Token::Structure> or C<undef> if the
176 structure does not have a finishing 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).
179  
180 =cut
181  
182 124133 ▶sub finish { $_[0]->{finish} }
Subroutine finish: called CC=1 SCAR=0.0
Pod documented
183  
184 =pod
185  
186 =head2 braces
187  
188 The C<braces> method is a utility method which returns the brace type,
189 regardless of whether both or just one of the braces is defined.
190  
191 Returns one of the three strings C<'[]'>, C<'{}'>, or C<'()'>, or C<undef>
192 on error (primarily not having a start brace, as mentioned above).
193  
194 =cut
195  
196 sub braces {
197 819 ▶ my $self = $_[0]->{start} ? shift : return undef;
Subroutine braces: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'start'} ? : 819 0 819
198 return {
199 '[' => '[]',
200 '(' => '()',
201 '{' => '{}',
202 819 }->{ $self->{start}->{content} };
203 }
204  
205 =pod
206  
207 =head1 complete
208  
209 The C<complete> method is a convenience method that returns true if
210 the both braces are defined for the structure, or false if only one
211 brace is defined.
212  
213 Unlike the top level C<complete> method which checks for completeness
214 in depth, the structure complete method ONLY confirms completeness
215 for the braces, and does not recurse downwards.
216  
217 =cut
218  
219 sub complete {
220 0 ▶ !! ($_[0]->{start} and $_[0]->{finish});
Subroutine complete: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/3
$_[0]{'start'} && $_[0]{'finish'}
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0]{'start'} && $_[0]{'finish'}
$_[0]{'start'} $_[0]{'finish'}
Truth table0%0/3
A && B
A= $_[0]{'start'}B= $_[0]{'finish'}
ABresult
0X0
100
111
221 }
222  
223  
224  
225  
226  
227 #####################################################################
228 # PPI::Node overloaded methods
229  
230 # For us, the "elements" concept includes the brace tokens
231 sub elements {
232 0 ▶ my $self = shift;
Subroutine elements: not called CC=6 SCAR=37.4
Pod documented
233  
234 0 ▶ if ( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
235 # Return a list in array context
236 0 ▶ return ( $self->{start} || (), @{$self->{children}}, $self->{finish} || () );
Condition0%0/6
$self->{'start'} || ()
l!l&&r!l&&!r
000
$self->{'finish'} || ()
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'start'} || ()
$self->{'start'} ()
MC/DC0%0/2
$self->{'finish'} || ()
$self->{'finish'} ()
Truth table0%0/3
A || B
A= $self->{'start'}B= ()
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $self->{'finish'}B= ()
ABresult
1X1
011
000
237 } else {
238 # Return the number of elements in scalar context.
239 # This is memory-cheaper than creating another big array
240 0 ▶ return scalar(@{$self->{children}})
Branch: 2 branches
BranchTrueFalseTotal
$self->{'start'} ? : 0 0 0
$self->{'finish'} ? : 0 0 0
241 + ($self->{start} ? 1 : 0)
242 0 + ($self->{finish} ? 1 : 0);
243 }
244 }
245  
246 # For us, the first element is probably the opening brace
247 sub first_element {
248 # Technically, if we have no children and no opening brace,
249 # then the first element is the closing brace.
250 4848 ▶ $_[0]->{start} or $_[0]->{children}->[0] or $_[0]->{finish};
Subroutine first_element: called CC=3 SCAR=14.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"start"} or $_[0]{"children"}[0] or $_[0]{"finish"} 0 4848 4848
Condition33%1/3
$_[0]{"start"} or $_[0]{"children"}[0]
l!l&&r!l&&!r
484800
MC/DC0%0/2
$_[0]{"start"} or $_[0]{"children"}[0]
$_[0]{"start"} $_[0]{"children"}[0]
Truth table33%1/3
A or B
A= $_[0]{"start"}B= $_[0]{"children"}[0]
ABresult
1X1
011
000
251 }
252  
253 # For us, the last element is probably the closing brace
254 sub last_element {
255 # Technically, if we have no children and no closing brace,
256 # then the last element is the opening brace
257 7 ▶ $_[0]->{finish} or $_[0]->{children}->[-1] or $_[0]->{start};
Subroutine last_element: called CC=3 SCAR=14.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"finish"} or $_[0]{"children"}[-1] or $_[0]{"start"} 0 7 7
Condition33%1/3
$_[0]{"finish"} or $_[0]{"children"}[-1]
l!l&&r!l&&!r
700
MC/DC0%0/2
$_[0]{"finish"} or $_[0]{"children"}[-1]
$_[0]{"finish"} $_[0]{"children"}[-1]
Truth table33%1/3
A or B
A= $_[0]{"finish"}B= $_[0]{"children"}[-1]
ABresult
1X1
011
000
258 }
259  
260 # Location is same as the start token, if any
261 sub location {
262 4841 ▶ my $self = shift;
Subroutine location: called CC=2 SCAR=7.1
Pod documented
263 4841 ▶ my $first = $self->first_element or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $first = $self->first_element or return undef 0 4841 4841
264 4841 $first->location;
265 }
266  
267  
268  
269  
270  
271 #####################################################################
272 # PPI::Element overloaded methods
273  
274 # Get the full set of tokens, including start and finish
275 sub tokens {
276 21858 ▶ my $self = shift;
Subroutine tokens: called CC=3 SCAR=12.0
Pod documented
277 my @tokens = (
278 $self->{start} || (),
279 $self->SUPER::tokens(@_),
280 21858 ▶ $self->{finish} || (),
Condition50%3/6
$self->{'start'} || ()
l!l&&r!l&&!r
2185800
$self->{'finish'} || ()
l!l&&r!l&&!r
1972921290
MC/DC0%0/2
$self->{'start'} || ()
$self->{'start'} ()
MC/DC0%0/2
$self->{'finish'} || ()
$self->{'finish'} ()
Truth table33%1/3
A || B
A= $self->{'start'}B= ()
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $self->{'finish'}B= ()
ABresult
1X1
011
000
281 );
282 21858 @tokens;
283 }
284  
285 # Like the token method ->content, get our merged contents.
286 # This will recurse downwards through everything
287 ### Reimplement this using List::Utils stuff
288 sub content {
289 45688 ▶ my $self = shift;
Subroutine content: called CC=4 SCAR=13.9
Pod documented
290 45688 ▶ my $content = $self->{start} ? $self->{start}->content : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'start'} ? : 45688 0 45688
291 45688 foreach my $child ( @{$self->{children}} ) {
292 158259 $content .= $child->content;
293 }
294 45688 ▶ $content .= $self->{finish}->content if $self->{finish};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"finish"} 45687 1 45688
295 45688 $content;
296 }
297  
298 # Is the structure completed
299 sub _complete {
300 0 ▶ !! ( defined $_[0]->{finish} );
Subroutine _complete: not called CC=1 SCAR=6.9
301 }
302  
303 # You can insert either another structure, or a token
304 sub insert_before {
305 0 ▶ my $self = shift;
Subroutine insert_before: not called CC=4 SCAR=30.0
Pod documented
306 0 ▶ my $Element = _INSTANCE(shift, 'PPI::Element') or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $Element = &_INSTANCE(shift(), "PPI::Element") or return undef 0 0 0
307 0 ▶ if ( $Element->isa('PPI::Structure') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Element->isa('PPI::Structure')) { } 0 0 0
elsif ($Element->isa('PPI::Token')) { } 0 0 0
308 0 return $self->__insert_before($Element);
309 } elsif ( $Element->isa('PPI::Token') ) {
310 0 return $self->__insert_before($Element);
311 }
312 0 '';
313 }
314  
315 # As above, you can insert either another structure, or a token
316 sub insert_after {
317 0 ▶ my $self = shift;
Subroutine insert_after: not called CC=4 SCAR=30.0
Pod documented
318 0 ▶ my $Element = _INSTANCE(shift, 'PPI::Element') or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $Element = &_INSTANCE(shift(), "PPI::Element") or return undef 0 0 0
319 0 ▶ if ( $Element->isa('PPI::Structure') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Element->isa('PPI::Structure')) { } 0 0 0
elsif ($Element->isa('PPI::Token')) { } 0 0 0
320 0 return $self->__insert_after($Element);
321 } elsif ( $Element->isa('PPI::Token') ) {
322 0 return $self->__insert_after($Element);
323 }
324 0 '';
325 }
326  
327 62 1;
328  
329 =pod
330  
331 =head1 SUPPORT
332  
333 See the L<support section|PPI/SUPPORT> in the main module.
334  
335 =head1 AUTHOR
336  
337 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
338  
339 =head1 COPYRIGHT
340  
341 Copyright 2001 - 2011 Adam Kennedy.
342  
343 This program is free software; you can redistribute
344 it and/or modify it under the same terms as Perl itself.
345  
346 The full text of the license can be found in the
347 LICENSE file included with this module.
348  
349 =cut
« PPI/Token/Pod.pm ↑ Summary PPI/Token/Symbol.pm »