PPI/Structure.pm

filter: Statementstmt 78.2% 72 / 92 Branchbran 25.0% 8 / 32 Conditioncond 23.8% 5 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 82.7% 24 / 29 Podpod 84.6% 11 / 13 totaltotal 59.7% 120 / 201 CC 25 SCAR 22.6
CC
25
Cov
59%
elements
42.0
insert_before
20.0
insert_after
20.0
CRAP 9.5
« PPI/Token/Symbol.pm ↑ Summary PPI/Token/_QuoteEngine/Full.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 63 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
92 63 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
93 63 ▶use Params::Util qw{_INSTANCE};
Subroutine BEGIN: called CC=1 SCAR=0.0
94 63 ▶use PPI::Node ();
Subroutine BEGIN: called CC=1 SCAR=0.0
95 63 ▶use PPI::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
96 63 ▶use PPI::Singletons '%_PARENT';
Subroutine BEGIN: called CC=1 SCAR=0.0
97  
98 63 our $VERSION = '1.260'; # VERSION
99  
100 63 our @ISA = "PPI::Node";
101  
102 63 ▶use PPI::Structure::Block ();
Subroutine BEGIN: called CC=1 SCAR=0.0
103 63 ▶use PPI::Structure::Condition ();
Subroutine BEGIN: called CC=1 SCAR=0.0
104 63 ▶use PPI::Structure::Constructor ();
Subroutine BEGIN: called CC=1 SCAR=0.0
105 63 ▶use PPI::Structure::For ();
Subroutine BEGIN: called CC=1 SCAR=0.0
106 63 ▶use PPI::Structure::Given ();
Subroutine BEGIN: called CC=1 SCAR=0.0
107 63 ▶use PPI::Structure::List ();
Subroutine BEGIN: called CC=1 SCAR=0.0
108 63 ▶use PPI::Structure::Subscript ();
Subroutine BEGIN: called CC=1 SCAR=0.0
109 63 ▶use PPI::Structure::Unknown ();
Subroutine BEGIN: called CC=1 SCAR=0.0
110 63 ▶use PPI::Structure::When ();
Subroutine BEGIN: called CC=1 SCAR=0.0
111  
112  
113  
114  
115  
116 #####################################################################
117 # Constructor
118  
119 sub new {
120 21496 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
121 21496 ▶ my $Token = PPI::Token::__LEXER__opens($_[0]) ? shift : return undef;
Branch: 1 branch
BranchTrueFalseTotal
PPI::Token::__LEXER__opens($_[0]) ? : 21496 0 21496
122  
123 # Create the object
124 21496 my $self = bless {
125 children => [],
126 start => $Token,
127 }, $class;
128  
129 # Set the start braces parent link
130 Scalar::Util::weaken(
131 21496 $_PARENT{Scalar::Util::refaddr $Token} = $self
132 );
133  
134 21496 $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<start> method returns the start brace for the structure (i.e. the
154 opening brace).
155  
156 Returns the brace as a L<PPI::Token::Structure> or C<undef> 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 147118 ▶sub start { $_[0]->{start} }
Subroutine start: called CC=1 SCAR=0.0
Pod documented
165  
166 =pod
167  
168 =head2 finish
169  
170 The C<finish> method returns the finish brace for the structure (i.e. the
171 closing brace).
172  
173 Returns the brace as a L<PPI::Token::Structure> or C<undef> 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 127071 ▶sub finish { $_[0]->{finish} }
Subroutine finish: called CC=1 SCAR=0.0
Pod documented
181  
182 =pod
183  
184 =head2 braces
185  
186 The C<braces> 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<undef>
190 on error (primarily not having a start brace, as mentioned above).
191  
192 =cut
193  
194 sub braces {
195 838 ▶ my $self = $_[0]->{start} ? shift : return undef;
Subroutine braces: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'start'} ? : 838 0 838
196 return {
197 '[' => '[]',
198 '(' => '()',
199 '{' => '{}',
200 838 }->{ $self->{start}->{content} };
201 }
202  
203 =pod
204  
205 =head1 complete
206  
207 The C<complete> 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<complete> 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]->{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
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 ▶ my $self = shift;
Subroutine elements: not called CC=6 SCAR=37.4
Pod documented
231  
232 0 ▶ if ( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
233 # Return a list in array context
234 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
235 } else {
236 # Return the number of elements in scalar context.
237 # This is memory-cheaper than creating another big array
238 0 ▶ return scalar(@{$self->{children}})
Branch: 2 branches
BranchTrueFalseTotal
$self->{'start'} ? : 0 0 0
$self->{'finish'} ? : 0 0 0
239 + ($self->{start} ? 1 : 0)
240 0 + ($self->{finish} ? 1 : 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 4916 ▶ $_[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 4916 4916
Condition33%1/3
$_[0]{"start"} or $_[0]{"children"}[0]
l!l&&r!l&&!r
491600
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
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]->{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
256 }
257  
258 # Location is same as the start token, if any
259 sub location {
260 4909 ▶ my $self = shift;
Subroutine location: called CC=2 SCAR=7.1
Pod documented
261 4909 ▶ my $first = $self->first_element or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $first = $self->first_element or return undef 0 4909 4909
262 4909 $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 22285 ▶ my $self = shift;
Subroutine tokens: called CC=3 SCAR=12.0
Pod documented
275 my @tokens = (
276 $self->{start} || (),
277 $self->SUPER::tokens(@_),
278 22285 ▶ $self->{finish} || (),
Condition50%3/6
$self->{'start'} || ()
l!l&&r!l&&!r
2228500
$self->{'finish'} || ()
l!l&&r!l&&!r
2021020750
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
279 );
280 22285 @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 48404 ▶ my $self = shift;
Subroutine content: called CC=4 SCAR=13.9
Pod documented
288 48404 ▶ my $content = $self->{start} ? $self->{start}->content : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'start'} ? : 48404 0 48404
289 48404 foreach my $child ( @{$self->{children}} ) {
290 166608 $content .= $child->content;
291 }
292 48404 ▶ $content .= $self->{finish}->content if $self->{finish};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"finish"} 48400 4 48404
293 48404 $content;
294 }
295  
296 # Is the structure completed
297 sub _complete {
298 0 ▶ !! ( defined $_[0]->{finish} );
Subroutine _complete: not called CC=1 SCAR=6.9
299 }
300  
301 # You can insert either another structure, or a token
302 sub insert_before {
303 0 ▶ my $self = shift;
Subroutine insert_before: not called CC=4 SCAR=30.0
Pod documented
304 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
305 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
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 ▶ my $self = shift;
Subroutine insert_after: not called CC=4 SCAR=30.0
Pod documented
316 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
317 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
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 63 1;
326  
327 =pod
328  
329 =head1 SUPPORT
330  
331 See the L<support section|PPI/SUPPORT> in the main module.
332  
333 =head1 AUTHOR
334  
335 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
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
« PPI/Token/Symbol.pm ↑ Summary PPI/Token/_QuoteEngine/Full.pm »