PPI/Statement.pm

filter: Statementstmt 83.3% 80 / 96 Branchbran 15.0% 3 / 20 Conditioncond 33.3% 4 / 12 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 86.2% 25 / 29 Podpod 83.3% 5 / 6 totaltotal 69.4% 118 / 170 CC 15 SCAR 21.8
CC
15
Cov
68%
insert_before
30.0
insert_after
30.0
label
12.0
CRAP 8.8
« PPI/Token/_QuoteEngine/Full.pm ↑ Summary PPI/Tokenizer.pm »
1 package PPI::Statement;
2  
3 =pod
4  
5 =head1 NAME
6  
7 PPI::Statement - The base class for Perl statements
8  
9 =head1 INHERITANCE
10  
11 PPI::Statement
12 isa PPI::Node
13 isa PPI::Element
14  
15 =head1 DESCRIPTION
16  
17 PPI::Statement is the root class for all Perl statements. This includes (from
18 L<perlsyn>) "Declarations", "Simple Statements" and "Compound Statements".
19  
20 The class PPI::Statement itself represents a "Simple Statement" as defined
21 in the L<perlsyn> manpage.
22  
23 =head1 STATEMENT CLASSES
24  
25 Please note that unless documented themselves, these classes are yet to be
26 frozen/finalised. Names may change slightly or be added or removed.
27  
28 =head2 L<PPI::Statement::Scheduled>
29  
30 This covers all "scheduled" blocks, chunks of code that are executed separately
31 from the main body of the code, at a particular time. This includes all
32 C<BEGIN>, C<CHECK>, C<UNITCHECK>, C<INIT> and C<END> blocks.
33  
34 =head2 L<PPI::Statement::Package>
35  
36 A package declaration, as defined in L<perlfunc|perlfunc/package>.
37  
38 =head2 L<PPI::Statement::Include>
39  
40 A statement that loads or unloads another module.
41  
42 This includes 'use', 'no', and 'require' statements.
43  
44 =head2 L<PPI::Statement::Sub>
45  
46 A named subroutine declaration, or forward declaration
47  
48 =head2 L<PPI::Statement::Variable>
49  
50 A variable declaration statement. This could be either a straight
51 declaration or also be an expression.
52  
53 This includes all 'my', 'state', 'local' and 'our' statements.
54  
55 =head2 L<PPI::Statement::Compound>
56  
57 This covers the whole family of 'compound' statements, as described in
58 L<perlsyn|perlsyn>.
59  
60 This includes all statements starting with 'if', 'unless', 'for', 'foreach'
61 and 'while'. Note that this does NOT include 'do', as it is treated
62 differently.
63  
64 All compound statements have implicit ends. That is, they do not end with
65 a ';' statement terminator.
66  
67 =head2 L<PPI::Statement::Break>
68  
69 A statement that breaks out of a structure.
70  
71 This includes all of 'redo', 'goto', 'next', 'last' and 'return' statements.
72  
73 =head2 L<PPI::Statement::Given>
74  
75 The kind of statement introduced in Perl 5.10 that starts with 'given'. This
76 has an implicit end.
77  
78 =head2 L<PPI::Statement::When>
79  
80 The kind of statement introduced in Perl 5.10 that starts with 'when' or
81 'default'. This also has an implicit end.
82  
83 =head2 L<PPI::Statement::Data>
84  
85 A special statement which encompasses an entire C<__DATA__> block, including
86 the initial C<'__DATA__'> token itself and the entire contents.
87  
88 =head2 L<PPI::Statement::End>
89  
90 A special statement which encompasses an entire __END__ block, including
91 the initial '__END__' token itself and the entire contents, including any
92 parsed PPI::Token::POD that may occur in it.
93  
94 =head2 L<PPI::Statement::Expression>
95  
96 L<PPI::Statement::Expression> is a little more speculative, and is intended
97 to help represent the special rules relating to "expressions" such as in:
98  
99 # Several examples of expression statements
100
101 # Boolean conditions
102 if ( expression ) { ... }
103
104 # Lists, such as for arguments
105 Foo->bar( expression )
106  
107 =head2 L<PPI::Statement::Null>
108  
109 A null statement is a special case for where we encounter two consecutive
110 statement terminators. ( ;; )
111  
112 The second terminator is given an entire statement of its own, but one
113 that serves no purpose. Hence a 'null' statement.
114  
115 Theoretically, assuming a correct parsing of a perl file, all null statements
116 are superfluous and should be able to be removed without damage to the file.
117  
118 But don't do that, in case PPI has parsed something wrong.
119  
120 =head2 L<PPI::Statement::UnmatchedBrace>
121  
122 Because L<PPI> is intended for use when parsing incorrect or incomplete code,
123 the problem arises of what to do with a stray closing brace.
124  
125 Rather than die, it is allocated its own "unmatched brace" statement,
126 which really means "unmatched closing brace". An unmatched open brace at the
127 end of a file would become a structure with no contents and no closing brace.
128  
129 If the document loaded is intended to be correct and valid, finding a
130 L<PPI::Statement::UnmatchedBrace> in the PDOM is generally indicative of a
131 misparse.
132  
133 =head2 L<PPI::Statement::Unknown>
134  
135 This is used temporarily mid-parsing to hold statements for which the lexer
136 cannot yet determine what class it should be, usually because there are
137 insufficient clues, or it might be more than one thing.
138  
139 You should never encounter these in a fully parsed PDOM tree.
140  
141 =head1 METHODS
142  
143 C<PPI::Statement> itself has very few methods. Most of the time, you will be
144 working with the more generic L<PPI::Element> or L<PPI::Node> methods, or one
145 of the methods that are subclass-specific.
146  
147 =cut
148  
149 62 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
150 62 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
151 62 ▶use Params::Util qw{_INSTANCE};
Subroutine BEGIN: called CC=1 SCAR=0.0
152 62 ▶use PPI::Node ();
Subroutine BEGIN: called CC=1 SCAR=0.0
153 62 ▶use PPI::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
154  
155 62 ▶use vars qw{$VERSION @ISA *_PARENT};
Subroutine BEGIN: called CC=1 SCAR=0.0
156 BEGIN {
157 62 ▶ $VERSION = '1.236';
Subroutine BEGIN: called CC=1 SCAR=0.0
158 62 @ISA = 'PPI::Node';
159 62 *_PARENT = *PPI::Element::_PARENT;
160 }
161  
162 62 ▶use PPI::Statement::Break ();
Subroutine BEGIN: called CC=1 SCAR=0.0
163 62 ▶use PPI::Statement::Compound ();
Subroutine BEGIN: called CC=1 SCAR=0.0
164 62 ▶use PPI::Statement::Data ();
Subroutine BEGIN: called CC=1 SCAR=0.0
165 62 ▶use PPI::Statement::End ();
Subroutine BEGIN: called CC=1 SCAR=0.0
166 62 ▶use PPI::Statement::Expression ();
Subroutine BEGIN: called CC=1 SCAR=0.0
167 62 ▶use PPI::Statement::Include ();
Subroutine BEGIN: called CC=1 SCAR=0.0
168 62 ▶use PPI::Statement::Null ();
Subroutine BEGIN: called CC=1 SCAR=0.0
169 62 ▶use PPI::Statement::Package ();
Subroutine BEGIN: called CC=1 SCAR=0.0
170 62 ▶use PPI::Statement::Scheduled ();
Subroutine BEGIN: called CC=1 SCAR=0.0
171 62 ▶use PPI::Statement::Sub ();
Subroutine BEGIN: called CC=1 SCAR=0.0
172 62 ▶use PPI::Statement::Given ();
Subroutine BEGIN: called CC=1 SCAR=0.0
173 62 ▶use PPI::Statement::UnmatchedBrace ();
Subroutine BEGIN: called CC=1 SCAR=0.0
174 62 ▶use PPI::Statement::Unknown ();
Subroutine BEGIN: called CC=1 SCAR=0.0
175 62 ▶use PPI::Statement::Variable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
176 62 ▶use PPI::Statement::When ();
Subroutine BEGIN: called CC=1 SCAR=0.0
177  
178 # "Normal" statements end at a statement terminator ;
179 # Some are not, and need the more rigorous _continues to see
180 # if we are at an implicit statement boundary.
181 sub __LEXER__normal() { 1 }
182  
183  
184  
185  
186  
187 #####################################################################
188 # Constructor
189  
190 sub new {
191 54510 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=11.1
Pod undocumented
192 54510 ▶ if ( ref $class ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) 0 54510 54510
193 0 PPI::Exception->throw;
194 }
195  
196 # Create the object
197 54510 my $self = bless {
198 children => [],
199 }, $class;
200  
201 # If we have been passed what should be an initial token, add it
202 54510 my $token = shift;
203 54510 ▶ if ( _INSTANCE($token, 'PPI::Token') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_INSTANCE($token, "PPI::Token")) 53111 1399 54510
204 # Inlined $self->__add_element(shift);
205 Scalar::Util::weaken(
206 53111 $_PARENT{Scalar::Util::refaddr $token} = $self
207 );
208 53111 push @{$self->{children}}, $token;
209 }
210  
211 54510 $self;
212 }
213  
214 =pod
215  
216 =head2 label
217  
218 One factor common to most statements is their ability to be labeled.
219  
220 The C<label> method returns the label for a statement, if one has been
221 defined, but without the trailing colon. Take the following example
222  
223 MYLABEL: while ( 1 .. 10 ) { last MYLABEL if $_ > 5 }
224  
225 For the above statement, the C<label> method would return 'MYLABEL'.
226  
227 Returns false if the statement does not have a label.
228  
229 =cut
230  
231 sub label {
232 0 ▶ my $first = shift->schild(1) or return '';
Subroutine label: not called CC=3 SCAR=24.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $first = (shift())->schild(1) or return "" 0 0 0
233 0 ▶ $first->isa('PPI::Token::Label')
Branch: 1 branch
BranchTrueFalseTotal
$first->isa('PPI::Token::Label') ? : 0 0 0
234 ? substr($first, 0, length($first) - 1)
235 : '';
236 }
237  
238 =pod
239  
240 =head2 specialized
241  
242 Answer whether this is a plain statement or one that has more
243 significance.
244  
245 Returns true if the statement is a subclass of this one, false
246 otherwise.
247  
248 =cut
249  
250 # Yes, this is doing precisely what it's intending to prevent
251 # client code from doing. However, since it's here, if the
252 # implementation changes, code outside PPI doesn't care.
253 sub specialized {
254 10 ▶ __PACKAGE__ ne ref $_[0];
Subroutine specialized: called CC=1 SCAR=0.0
Pod documented
255 }
256  
257 =pod
258  
259 =head2 stable
260  
261 Much like the L<PPI::Document> method of the same name, the ->stable
262 method converts a statement to source and back again, to determine if
263 a modified statement is still legal, and won't be interpreted in a
264 different way.
265  
266 Returns true if the statement is stable, false if not, or C<undef> on
267 error.
268  
269 =cut
270  
271 sub stable {
272 0 ▶ die "The ->stable method has not yet been implemented";
Subroutine stable: not called CC=1 SCAR=6.9
Pod documented
273 }
274  
275  
276  
277  
278  
279 #####################################################################
280 # PPI::Element Methods
281  
282 # Is the statement complete.
283 # By default for a statement, we need a semi-colon at the end.
284 sub _complete {
285 2 ▶ my $self = shift;
Subroutine _complete: called CC=3 SCAR=11.3
286 2 my $semi = $self->schild(-1);
287 return !! (
288 2 ▶ defined $semi
Condition66%4/6
defined $semi && $semi->isa('PPI::Token::Structure') && $semi->content eq ';'
!ll&&!rl&&r
101
defined $semi and $semi->isa("PPI::Token::Structure")
!ll&&!rl&&r
011
MC/DC33%1/3
defined $semi && $semi->isa('PPI::Token::Structure') && $semi->content eq ';'
defined $semi $semi->isa("PPI::Token::Structure") $semi->content eq ';'
Truth table50%2/4
A and B && C
A= defined $semiB= $semi->isa("PPI::Token::Structure")C= $semi->content eq ';'
ABCresult
0XX0
10X0
1100
1111
289 and
290 $semi->isa('PPI::Token::Structure')
291 and
292 $semi->content eq ';'
293 );
294 }
295  
296 # You can insert either a statement or a non-significant token.
297 sub insert_before {
298 0 ▶ my $self = shift;
Subroutine insert_before: not called CC=5 SCAR=34.0
Pod documented
299 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
300 0 ▶ if ( $Element->isa('PPI::Statement') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Element->isa('PPI::Statement')) { } 0 0 0
elsif ($Element->isa('PPI::Token') and not $Element->significant) { } 0 0 0
301 0 return $self->__insert_before($Element);
302 ▶ } elsif ( $Element->isa('PPI::Token') and ! $Element->significant ) {
Condition0%0/3
$Element->isa("PPI::Token") and not $Element->significant
!ll&&!rl&&r
000
MC/DC0%0/2
$Element->isa("PPI::Token") and not $Element->significant
$Element->isa("PPI::Token") not $Element->significant
Truth table0%0/3
A and not(B)
A= $Element->isa("PPI::Token")B= not $Element->significant
ABresult
0X0
100
111
303 0 return $self->__insert_before($Element);
304 }
305 0 '';
306 }
307  
308 # As above, you can insert a statement, or a non-significant token
309 sub insert_after {
310 0 ▶ my $self = shift;
Subroutine insert_after: not called CC=5 SCAR=34.0
Pod documented
311 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
312 0 ▶ if ( $Element->isa('PPI::Statement') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Element->isa('PPI::Statement')) { } 0 0 0
elsif ($Element->isa('PPI::Token') and not $Element->significant) { } 0 0 0
313 0 return $self->__insert_after($Element);
314 ▶ } elsif ( $Element->isa('PPI::Token') and ! $Element->significant ) {
Condition0%0/3
$Element->isa("PPI::Token") and not $Element->significant
!ll&&!rl&&r
000
MC/DC0%0/2
$Element->isa("PPI::Token") and not $Element->significant
$Element->isa("PPI::Token") not $Element->significant
Truth table0%0/3
A and not(B)
A= $Element->isa("PPI::Token")B= not $Element->significant
ABresult
0X0
100
111
315 0 return $self->__insert_after($Element);
316 }
317 0 '';
318 }
319  
320 62 1;
321  
322 =pod
323  
324 =head1 TO DO
325  
326 - Complete, freeze and document the remaining classes
327  
328 =head1 SUPPORT
329  
330 See the L<support section|PPI/SUPPORT> in the main module.
331  
332 =head1 AUTHOR
333  
334 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
335  
336 =head1 COPYRIGHT
337  
338 Copyright 2001 - 2011 Adam Kennedy.
339  
340 This program is free software; you can redistribute
341 it and/or modify it under the same terms as Perl itself.
342  
343 The full text of the license can be found in the
344 LICENSE file included with this module.
345  
346 =cut
« PPI/Token/_QuoteEngine/Full.pm ↑ Summary PPI/Tokenizer.pm »