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 |
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
|
|
352
|
use strict; |
|
64
|
|
|
|
|
122
|
|
|
64
|
|
|
|
|
1547
|
|
92
|
64
|
|
|
64
|
|
320
|
use Scalar::Util (); |
|
64
|
|
|
|
|
93
|
|
|
64
|
|
|
|
|
954
|
|
93
|
64
|
|
|
64
|
|
247
|
use Params::Util qw{_INSTANCE}; |
|
64
|
|
|
|
|
108
|
|
|
64
|
|
|
|
|
2044
|
|
94
|
64
|
|
|
64
|
|
311
|
use PPI::Node (); |
|
64
|
|
|
|
|
110
|
|
|
64
|
|
|
|
|
654
|
|
95
|
64
|
|
|
64
|
|
231
|
use PPI::Exception (); |
|
64
|
|
|
|
|
93
|
|
|
64
|
|
|
|
|
903
|
|
96
|
64
|
|
|
64
|
|
270
|
use PPI::Singletons '%_PARENT'; |
|
64
|
|
|
|
|
92
|
|
|
64
|
|
|
|
|
6712
|
|
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
our $VERSION = '1.276'; |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
our @ISA = "PPI::Node"; |
101
|
|
|
|
|
|
|
|
102
|
64
|
|
|
64
|
|
22177
|
use PPI::Structure::Block (); |
|
64
|
|
|
|
|
155
|
|
|
64
|
|
|
|
|
1233
|
|
103
|
64
|
|
|
64
|
|
20802
|
use PPI::Structure::Condition (); |
|
64
|
|
|
|
|
155
|
|
|
64
|
|
|
|
|
1289
|
|
104
|
64
|
|
|
64
|
|
20839
|
use PPI::Structure::Constructor (); |
|
64
|
|
|
|
|
146
|
|
|
64
|
|
|
|
|
1285
|
|
105
|
64
|
|
|
64
|
|
20551
|
use PPI::Structure::For (); |
|
64
|
|
|
|
|
137
|
|
|
64
|
|
|
|
|
1359
|
|
106
|
64
|
|
|
64
|
|
21359
|
use PPI::Structure::Given (); |
|
64
|
|
|
|
|
141
|
|
|
64
|
|
|
|
|
1363
|
|
107
|
64
|
|
|
64
|
|
21644
|
use PPI::Structure::List (); |
|
64
|
|
|
|
|
142
|
|
|
64
|
|
|
|
|
1511
|
|
108
|
64
|
|
|
64
|
|
20961
|
use PPI::Structure::Subscript (); |
|
64
|
|
|
|
|
149
|
|
|
64
|
|
|
|
|
1398
|
|
109
|
64
|
|
|
64
|
|
20658
|
use PPI::Structure::Unknown (); |
|
64
|
|
|
|
|
189
|
|
|
64
|
|
|
|
|
1509
|
|
110
|
64
|
|
|
64
|
|
20904
|
use PPI::Structure::When (); |
|
64
|
|
|
|
|
161
|
|
|
64
|
|
|
|
|
46757
|
|
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
##################################################################### |
117
|
|
|
|
|
|
|
# Constructor |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub new { |
120
|
22199
|
|
|
22199
|
0
|
29664
|
my $class = shift; |
121
|
22199
|
50
|
|
|
|
43491
|
my $Token = PPI::Token::__LEXER__opens($_[0]) ? shift : return undef; |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
# Create the object |
124
|
22199
|
|
|
|
|
76850
|
my $self = bless { |
125
|
|
|
|
|
|
|
children => [], |
126
|
|
|
|
|
|
|
start => $Token, |
127
|
|
|
|
|
|
|
}, $class; |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
# Set the start braces parent link |
130
|
|
|
|
|
|
|
Scalar::Util::weaken( |
131
|
22199
|
|
|
|
|
100545
|
$_PARENT{Scalar::Util::refaddr $Token} = $self |
132
|
|
|
|
|
|
|
); |
133
|
|
|
|
|
|
|
|
134
|
22199
|
|
|
|
|
36772
|
$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
|
148970
|
|
|
148970
|
1
|
329171
|
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
|
128551
|
|
|
128551
|
1
|
293997
|
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
|
2384
|
my $self = $_[0]->{start} ? shift : return undef; |
196
|
|
|
|
|
|
|
return { |
197
|
|
|
|
|
|
|
'[' => '[]', |
198
|
|
|
|
|
|
|
'(' => '()', |
199
|
|
|
|
|
|
|
'{' => '{}', |
200
|
841
|
|
|
|
|
5885
|
}->{ $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
|
4951
|
0
|
33
|
4951
|
1
|
16851
|
$_[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
|
39
|
$_[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
|
4944
|
|
|
4944
|
1
|
12946
|
my $self = shift; |
261
|
4944
|
50
|
|
|
|
6642
|
my $first = $self->first_element or return undef; |
262
|
4944
|
|
|
|
|
7771
|
$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
|
23009
|
|
|
23009
|
1
|
25423
|
my $self = shift; |
275
|
|
|
|
|
|
|
my @tokens = ( |
276
|
|
|
|
|
|
|
$self->{start} || (), |
277
|
|
|
|
|
|
|
$self->SUPER::tokens(@_), |
278
|
23009
|
|
33
|
|
|
68107
|
$self->{finish} || (), |
|
|
|
66
|
|
|
|
|
279
|
|
|
|
|
|
|
); |
280
|
23009
|
|
|
|
|
89463
|
@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
|
48576
|
|
|
48576
|
1
|
57859
|
my $self = shift; |
288
|
48576
|
50
|
|
|
|
125445
|
my $content = $self->{start} ? $self->{start}->content : ''; |
289
|
48576
|
|
|
|
|
52237
|
foreach my $child ( @{$self->{children}} ) { |
|
48576
|
|
|
|
|
71259
|
|
290
|
167399
|
|
|
|
|
247695
|
$content .= $child->content; |
291
|
|
|
|
|
|
|
} |
292
|
48576
|
100
|
|
|
|
131506
|
$content .= $self->{finish}->content if $self->{finish}; |
293
|
48576
|
|
|
|
|
134417
|
$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 |