File Coverage

blib/lib/PPI/Statement.pm
Criterion Covered Total %
statement 76 92 82.6
branch 3 20 15.0
condition 2 9 22.2
subroutine 24 28 85.7
pod 5 6 83.3
total 110 155 70.9


line stmt bran cond sub pod time code
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) "Declarations", "Simple Statements" and "Compound Statements".
19              
20             The class PPI::Statement itself represents a "Simple Statement" as defined
21             in the L 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
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, C, C, C and C blocks.
33              
34             =head2 L
35              
36             A package declaration, as defined in L.
37              
38             =head2 L
39              
40             A statement that loads or unloads another module.
41              
42             This includes 'use', 'no', and 'require' statements.
43              
44             =head2 L
45              
46             A named subroutine declaration, or forward declaration
47              
48             =head2 L
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
56              
57             This covers the whole family of 'compound' statements, as described in
58             L.
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
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
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
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
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
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
95              
96             L 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
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
121              
122             Because L 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 in the PDOM is generally indicative of a
131             misparse.
132              
133             =head2 L
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 itself has very few methods. Most of the time, you will be
144             working with the more generic L or L methods, or one
145             of the methods that are subclass-specific.
146              
147             =cut
148              
149 66     66   548 use strict;
  66         165  
  66         2649  
150 66     66   382 use Scalar::Util ();
  66         139  
  66         1754  
151 66     66   356 use Params::Util qw{_INSTANCE};
  66         118  
  66         4404  
152 66     66   400 use PPI::Node ();
  66         150  
  66         1333  
153 66     66   333 use PPI::Exception ();
  66         155  
  66         1956  
154 66     66   335 use PPI::Singletons '%_PARENT';
  66         133  
  66         12777  
155              
156             our $VERSION = '1.28401'; # TRIAL
157              
158             our @ISA = "PPI::Node";
159              
160 66     66   34269 use PPI::Statement::Break ();
  66         268  
  66         3300  
161 66     66   33528 use PPI::Statement::Compound ();
  66         242  
  66         2377  
162 66     66   33618 use PPI::Statement::Data ();
  66         230  
  66         1958  
163 66     66   31825 use PPI::Statement::End ();
  66         226  
  66         2111  
164 66     66   32220 use PPI::Statement::Expression ();
  66         221  
  66         2148  
165 66     66   37378 use PPI::Statement::Include ();
  66         258  
  66         2810  
166 66     66   34937 use PPI::Statement::Null ();
  66         225  
  66         2625  
167 66     66   35889 use PPI::Statement::Package ();
  66         233  
  66         2755  
168 66     66   35282 use PPI::Statement::Scheduled ();
  66         285  
  66         2883  
169 66     66   481 use PPI::Statement::Sub ();
  66         145  
  66         1600  
170 66     66   33502 use PPI::Statement::Given ();
  66         258  
  66         3101  
171 66     66   34281 use PPI::Statement::UnmatchedBrace ();
  66         2743  
  66         2960  
172 66     66   33860 use PPI::Statement::Unknown ();
  66         999  
  66         6128  
173 66     66   34906 use PPI::Statement::Variable ();
  66         245  
  66         3363  
174 66     66   35186 use PPI::Statement::When ();
  66         239  
  66         48216  
175              
176             # "Normal" statements end at a statement terminator ;
177             # Some are not, and need the more rigorous _continues to see
178             # if we are at an implicit statement boundary.
179             sub __LEXER__normal() { 1 }
180              
181              
182              
183              
184              
185             #####################################################################
186             # Constructor
187              
188             sub new {
189 59349     59349 0 128347 my $class = shift;
190 59349 50       150686 if ( ref $class ) {
191 0         0 PPI::Exception->throw;
192             }
193              
194             # Create the object
195 59349         229650 my $self = bless {
196             children => [],
197             }, $class;
198              
199             # If we have been passed what should be an initial token, add it
200 59349         110953 my $token = shift;
201 59349 100       424378 if ( _INSTANCE($token, 'PPI::Token') ) {
202             # Inlined $self->__add_element(shift);
203             Scalar::Util::weaken(
204 57956         251217 $_PARENT{Scalar::Util::refaddr $token} = $self
205             );
206 57956         93433 push @{$self->{children}}, $token;
  57956         188835  
207             }
208              
209 59349         161533 $self;
210             }
211              
212             =pod
213              
214             =head2 label
215              
216             One factor common to most statements is their ability to be labeled.
217              
218             The C
219             defined, but without the trailing colon. Take the following example
220              
221             MYLABEL: while ( 1 .. 10 ) { last MYLABEL if $_ > 5 }
222              
223             For the above statement, the C
224              
225             Returns false if the statement does not have a label.
226              
227             =cut
228              
229             sub label {
230 0 0   0 1 0 my $first = shift->schild(1) or return '';
231 0 0       0 $first->isa('PPI::Token::Label')
232             ? substr($first, 0, length($first) - 1)
233             : '';
234             }
235              
236             =pod
237              
238             =head2 specialized
239              
240             Answer whether this is a plain statement or one that has more
241             significance.
242              
243             Returns true if the statement is a subclass of this one, false
244             otherwise.
245              
246             =cut
247              
248             # Yes, this is doing precisely what it's intending to prevent
249             # client code from doing. However, since it's here, if the
250             # implementation changes, code outside PPI doesn't care.
251             sub specialized {
252 10     10 1 1037 __PACKAGE__ ne ref $_[0];
253             }
254              
255             =pod
256              
257             =head2 stable
258              
259             Much like the L method of the same name, the ->stable
260             method converts a statement to source and back again, to determine if
261             a modified statement is still legal, and won't be interpreted in a
262             different way.
263              
264             Returns true if the statement is stable, false if not, or C on
265             error.
266              
267             =cut
268              
269             sub stable {
270 0     0 1 0 die "The ->stable method has not yet been implemented";
271             }
272              
273              
274              
275              
276              
277             #####################################################################
278             # PPI::Element Methods
279              
280             # Is the statement complete.
281             # By default for a statement, we need a semi-colon at the end.
282             sub _complete {
283 2     2   4 my $self = shift;
284 2         8 my $semi = $self->schild(-1);
285             return !! (
286 2   66     23 defined $semi
287             and
288             $semi->isa('PPI::Token::Structure')
289             and
290             $semi->content eq ';'
291             );
292             }
293              
294             # You can insert either a statement or a non-significant token.
295             sub insert_before {
296 0     0 1   my $self = shift;
297 0 0         my $Element = _INSTANCE(shift, 'PPI::Element') or return undef;
298 0 0 0       if ( $Element->isa('PPI::Statement') ) {
    0          
299 0           return $self->__insert_before($Element);
300             } elsif ( $Element->isa('PPI::Token') and ! $Element->significant ) {
301 0           return $self->__insert_before($Element);
302             }
303 0           '';
304             }
305              
306             # As above, you can insert a statement, or a non-significant token
307             sub insert_after {
308 0     0 1   my $self = shift;
309 0 0         my $Element = _INSTANCE(shift, 'PPI::Element') or return undef;
310 0 0 0       if ( $Element->isa('PPI::Statement') ) {
    0          
311 0           return $self->__insert_after($Element);
312             } elsif ( $Element->isa('PPI::Token') and ! $Element->significant ) {
313 0           return $self->__insert_after($Element);
314             }
315 0           '';
316             }
317              
318             1;
319              
320             =pod
321              
322             =head1 TO DO
323              
324             - Complete, freeze and document the remaining classes
325              
326             =head1 SUPPORT
327              
328             See the L in the main module.
329              
330             =head1 AUTHOR
331              
332             Adam Kennedy Eadamk@cpan.orgE
333              
334             =head1 COPYRIGHT
335              
336             Copyright 2001 - 2011 Adam Kennedy.
337              
338             This program is free software; you can redistribute
339             it and/or modify it under the same terms as Perl itself.
340              
341             The full text of the license can be found in the
342             LICENSE file included with this module.
343              
344             =cut