line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package PPI::Element; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=pod |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 NAME |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
PPI::Element - The abstract Element class, a base for all source objects |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 INHERITANCE |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
PPI::Element is the root of the PDOM tree |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 DESCRIPTION |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
The abstract C serves as a base class for all source-related |
16
|
|
|
|
|
|
|
objects, from a single whitespace token to an entire document. It provides |
17
|
|
|
|
|
|
|
a basic set of methods to provide a common interface and basic |
18
|
|
|
|
|
|
|
implementations. |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 METHODS |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=cut |
23
|
|
|
|
|
|
|
|
24
|
65
|
|
|
65
|
|
368
|
use strict; |
|
65
|
|
|
|
|
106
|
|
|
65
|
|
|
|
|
1726
|
|
25
|
65
|
|
|
65
|
|
20983
|
use Clone 0.30 (); |
|
65
|
|
|
|
|
132313
|
|
|
65
|
|
|
|
|
1527
|
|
26
|
65
|
|
|
65
|
|
399
|
use Scalar::Util qw{refaddr}; |
|
65
|
|
|
|
|
144
|
|
|
65
|
|
|
|
|
2624
|
|
27
|
65
|
|
|
65
|
|
309
|
use Params::Util qw{_INSTANCE _ARRAY}; |
|
65
|
|
|
|
|
113
|
|
|
65
|
|
|
|
|
2142
|
|
28
|
65
|
|
|
65
|
|
311
|
use List::Util (); |
|
65
|
|
|
|
|
96
|
|
|
65
|
|
|
|
|
691
|
|
29
|
65
|
|
|
65
|
|
543
|
use PPI::Util (); |
|
65
|
|
|
|
|
102
|
|
|
65
|
|
|
|
|
718
|
|
30
|
65
|
|
|
65
|
|
26394
|
use PPI::Node (); |
|
65
|
|
|
|
|
169
|
|
|
65
|
|
|
|
|
1631
|
|
31
|
65
|
|
|
65
|
|
377
|
use PPI::Singletons '%_PARENT'; |
|
65
|
|
|
|
|
110
|
|
|
65
|
|
|
|
|
6275
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
our $VERSION = '1.276'; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
our $errstr = ""; |
36
|
|
|
|
|
|
|
|
37
|
65
|
|
|
65
|
|
347
|
use overload 'bool' => \&PPI::Util::TRUE; |
|
65
|
|
|
|
|
108
|
|
|
65
|
|
|
|
|
332
|
|
38
|
65
|
|
|
65
|
|
3444
|
use overload '""' => 'content'; |
|
65
|
|
|
|
|
119
|
|
|
65
|
|
|
|
|
180
|
|
39
|
65
|
|
|
65
|
|
2858
|
use overload '==' => '__equals'; |
|
65
|
|
|
|
|
104
|
|
|
65
|
|
|
|
|
205
|
|
40
|
65
|
|
|
65
|
|
2729
|
use overload '!=' => '__nequals'; |
|
65
|
|
|
|
|
103
|
|
|
65
|
|
|
|
|
159
|
|
41
|
65
|
|
|
65
|
|
2668
|
use overload 'eq' => '__eq'; |
|
65
|
|
|
|
|
113
|
|
|
65
|
|
|
|
|
184
|
|
42
|
65
|
|
|
65
|
|
2994
|
use overload 'ne' => '__ne'; |
|
65
|
|
|
|
|
132
|
|
|
65
|
|
|
|
|
231
|
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
##################################################################### |
49
|
|
|
|
|
|
|
# General Properties |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=pod |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=head2 significant |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
Because we treat whitespace and other non-code items as Tokens (in order to |
56
|
|
|
|
|
|
|
be able to "round trip" the L back to a file) the |
57
|
|
|
|
|
|
|
C method allows us to distinguish between tokens that form a |
58
|
|
|
|
|
|
|
part of the code, and tokens that aren't significant, such as whitespace, |
59
|
|
|
|
|
|
|
POD, or the portion of a file after (and including) the C<__END__> token. |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
Returns true if the Element is significant, or false it not. |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=cut |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
### XS -> PPI/XS.xs:_PPI_Element__significant 0.845+ |
66
|
|
|
|
|
|
|
sub significant() { 1 } |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=pod |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head2 class |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
The C method is provided as a convenience, and really does nothing |
73
|
|
|
|
|
|
|
more than returning C[. However, some people have found that ] |
74
|
|
|
|
|
|
|
they appreciate the laziness of C<$Foo-Eclass eq 'whatever'>, so I |
75
|
|
|
|
|
|
|
have caved to popular demand and included it. |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
Returns the class of the Element as a string |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=cut |
80
|
|
|
|
|
|
|
|
81
|
0
|
|
|
0
|
1
|
0
|
sub class { ref($_[0]) } |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=pod |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head2 tokens |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
The C method returns a list of L objects for the |
88
|
|
|
|
|
|
|
Element, essentially getting back that part of the document as if it had |
89
|
|
|
|
|
|
|
not been lexed. |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
This also means there are no Statements and no Structures in the list, |
92
|
|
|
|
|
|
|
just the Token classes. |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=cut |
95
|
|
|
|
|
|
|
|
96
|
289148
|
|
|
289148
|
1
|
432542
|
sub tokens { $_[0] } |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=pod |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head2 content |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
For B C, the C method will reconstitute the |
103
|
|
|
|
|
|
|
base code for it as a single string. This method is also the method used |
104
|
|
|
|
|
|
|
for overloading stringification. When an Element is used in a double-quoted |
105
|
|
|
|
|
|
|
string for example, this is the method that is called. |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
B |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
You should be aware that because of the way that here-docs are handled, any |
110
|
|
|
|
|
|
|
here-doc content is not included in C, and as such you should |
111
|
|
|
|
|
|
|
B eval or execute the result if it contains any L. |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
The L method C should be used to stringify a PDOM |
114
|
|
|
|
|
|
|
document into something that can be executed as expected. |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
Returns the basic code as a string (excluding here-doc content). |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=cut |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
### XS -> PPI/XS.xs:_PPI_Element__content 0.900+ |
121
|
|
|
|
|
|
|
sub content() { '' } |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
##################################################################### |
128
|
|
|
|
|
|
|
# Navigation Methods |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=pod |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head2 parent |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Elements themselves are not intended to contain other Elements, that is |
135
|
|
|
|
|
|
|
left to the L abstract class, a subclass of C. |
136
|
|
|
|
|
|
|
However, all Elements can be contained B a parent Node. |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
If an Element is within a parent Node, the C method returns the |
139
|
|
|
|
|
|
|
Node. |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=cut |
142
|
|
|
|
|
|
|
|
143
|
2483
|
|
|
2483
|
1
|
13053
|
sub parent { $_PARENT{refaddr $_[0]} } |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=pod |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head2 descendant_of $element |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
Answers whether a C is contained within another one. |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
Cs are considered to be descendants of themselves. |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=cut |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
sub descendant_of { |
156
|
6
|
|
|
6
|
1
|
1246
|
my $cursor = shift; |
157
|
6
|
50
|
|
|
|
20
|
my $parent = shift or return undef; |
158
|
6
|
|
|
|
|
20
|
while ( refaddr $cursor != refaddr $parent ) { |
159
|
17
|
100
|
|
|
|
60
|
$cursor = $_PARENT{refaddr $cursor} or return ''; |
160
|
|
|
|
|
|
|
} |
161
|
3
|
|
|
|
|
10
|
return 1; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=pod |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head2 ancestor_of $element |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
Answers whether a C is contains another one. |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Cs are considered to be ancestors of themselves. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=cut |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
sub ancestor_of { |
175
|
6
|
|
|
6
|
1
|
1263
|
my $self = shift; |
176
|
6
|
50
|
|
|
|
19
|
my $cursor = shift or return undef; |
177
|
6
|
|
|
|
|
20
|
while ( refaddr $cursor != refaddr $self ) { |
178
|
17
|
100
|
|
|
|
59
|
$cursor = $_PARENT{refaddr $cursor} or return ''; |
179
|
|
|
|
|
|
|
} |
180
|
3
|
|
|
|
|
10
|
return 1; |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=pod |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=head2 statement |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
For a C that is contained (at some depth) within a |
188
|
|
|
|
|
|
|
L, the C method will return the first parent |
189
|
|
|
|
|
|
|
Statement object lexically 'above' the Element. |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
Returns a L object, which may be the same Element if the |
192
|
|
|
|
|
|
|
Element is itself a L object. |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
Returns false if the Element is not within a Statement and is not itself |
195
|
|
|
|
|
|
|
a Statement. |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=cut |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
sub statement { |
200
|
0
|
|
|
0
|
1
|
0
|
my $cursor = shift; |
201
|
0
|
|
|
|
|
0
|
while ( ! _INSTANCE($cursor, 'PPI::Statement') ) { |
202
|
0
|
0
|
|
|
|
0
|
$cursor = $_PARENT{refaddr $cursor} or return ''; |
203
|
|
|
|
|
|
|
} |
204
|
0
|
|
|
|
|
0
|
$cursor; |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=pod |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=head2 top |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
For a C that is contained within a PDOM tree, the C method |
212
|
|
|
|
|
|
|
will return the top-level Node in the tree. Most of the time this should be |
213
|
|
|
|
|
|
|
a L object, however this will not always be so. For example, |
214
|
|
|
|
|
|
|
if a subroutine has been removed from its Document, to be moved to another |
215
|
|
|
|
|
|
|
Document. |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
Returns the top-most PDOM object, which may be the same Element, if it is |
218
|
|
|
|
|
|
|
not within any parent PDOM object. |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=cut |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
sub top { |
223
|
193
|
|
|
193
|
1
|
325
|
my $cursor = shift; |
224
|
193
|
|
|
|
|
930
|
while ( my $parent = $_PARENT{refaddr $cursor} ) { |
225
|
392
|
|
|
|
|
1100
|
$cursor = $parent; |
226
|
|
|
|
|
|
|
} |
227
|
193
|
|
|
|
|
357
|
$cursor; |
228
|
|
|
|
|
|
|
} |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=pod |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=head2 document |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
For an Element that is contained within a L object, |
235
|
|
|
|
|
|
|
the C method will return the top-level Document for the Element. |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
Returns the L for this Element, or false if the Element is not |
238
|
|
|
|
|
|
|
contained within a Document. |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
=cut |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
sub document { |
243
|
181
|
|
|
181
|
1
|
487
|
my $top = shift->top; |
244
|
181
|
50
|
|
|
|
1662
|
_INSTANCE($top, 'PPI::Document') and $top; |
245
|
|
|
|
|
|
|
} |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=pod |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
=head2 next_sibling |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
All L objects (specifically, our parent Node) contain a number of |
252
|
|
|
|
|
|
|
C objects. The C method returns the C |
253
|
|
|
|
|
|
|
immediately after the current one, or false if there is no next sibling. |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
=cut |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
sub next_sibling { |
258
|
83
|
|
|
83
|
1
|
103
|
my $self = shift; |
259
|
83
|
100
|
|
|
|
238
|
my $parent = $_PARENT{refaddr $self} or return ''; |
260
|
78
|
|
|
|
|
112
|
my $key = refaddr $self; |
261
|
78
|
|
|
|
|
97
|
my $elements = $parent->{children}; |
262
|
|
|
|
|
|
|
my $position = List::Util::first { |
263
|
270
|
|
|
270
|
|
332
|
refaddr $elements->[$_] == $key |
264
|
78
|
|
|
|
|
281
|
} 0..$#$elements; |
265
|
78
|
100
|
|
|
|
292
|
$elements->[$position + 1] || ''; |
266
|
|
|
|
|
|
|
} |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
=pod |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
=head2 snext_sibling |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
As per the other 's' methods, the C method returns the next |
273
|
|
|
|
|
|
|
B sibling of the C object. |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
Returns a C object, or false if there is no 'next' significant |
276
|
|
|
|
|
|
|
sibling. |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
=cut |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
sub snext_sibling { |
281
|
2226
|
|
|
2226
|
1
|
6885
|
my $self = shift; |
282
|
2226
|
100
|
|
|
|
8605
|
my $parent = $_PARENT{refaddr $self} or return ''; |
283
|
2219
|
|
|
|
|
2913
|
my $key = refaddr $self; |
284
|
2219
|
|
|
|
|
2654
|
my $elements = $parent->{children}; |
285
|
|
|
|
|
|
|
my $position = List::Util::first { |
286
|
12010
|
|
|
12010
|
|
14701
|
refaddr $elements->[$_] == $key |
287
|
2219
|
|
|
|
|
8687
|
} 0..$#$elements; |
288
|
2219
|
|
|
|
|
6464
|
while ( defined(my $it = $elements->[++$position]) ) { |
289
|
2202
|
100
|
|
|
|
6349
|
return $it if $it->significant; |
290
|
|
|
|
|
|
|
} |
291
|
25
|
|
|
|
|
58
|
''; |
292
|
|
|
|
|
|
|
} |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
=pod |
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
=head2 previous_sibling |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
All L objects (specifically, our parent Node) contain a number of |
299
|
|
|
|
|
|
|
C objects. The C method returns the Element |
300
|
|
|
|
|
|
|
immediately before the current one, or false if there is no 'previous' |
301
|
|
|
|
|
|
|
C object. |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
=cut |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
sub previous_sibling { |
306
|
76
|
|
|
76
|
1
|
82
|
my $self = shift; |
307
|
76
|
100
|
|
|
|
200
|
my $parent = $_PARENT{refaddr $self} or return ''; |
308
|
72
|
|
|
|
|
94
|
my $key = refaddr $self; |
309
|
72
|
|
|
|
|
78
|
my $elements = $parent->{children}; |
310
|
|
|
|
|
|
|
my $position = List::Util::first { |
311
|
248
|
|
|
248
|
|
334
|
refaddr $elements->[$_] == $key |
312
|
72
|
|
|
|
|
200
|
} 0..$#$elements; |
313
|
72
|
100
|
66
|
|
|
333
|
$position and $elements->[$position - 1] or ''; |
314
|
|
|
|
|
|
|
} |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
=pod |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
=head2 sprevious_sibling |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
As per the other 's' methods, the C method returns |
321
|
|
|
|
|
|
|
the previous B sibling of the C object. |
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
Returns a C object, or false if there is no 'previous' significant |
324
|
|
|
|
|
|
|
sibling. |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
=cut |
327
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
sub sprevious_sibling { |
329
|
1504
|
|
|
1504
|
1
|
2811
|
my $self = shift; |
330
|
1504
|
100
|
|
|
|
4326
|
my $parent = $_PARENT{refaddr $self} or return ''; |
331
|
1503
|
|
|
|
|
2193
|
my $key = refaddr $self; |
332
|
1503
|
|
|
|
|
2284
|
my $elements = $parent->{children}; |
333
|
|
|
|
|
|
|
my $position = List::Util::first { |
334
|
9744
|
|
|
9744
|
|
12023
|
refaddr $elements->[$_] == $key |
335
|
1503
|
|
|
|
|
5951
|
} 0..$#$elements; |
336
|
1503
|
|
66
|
|
|
5877
|
while ( $position-- and defined(my $it = $elements->[$position]) ) { |
337
|
1496
|
100
|
|
|
|
4249
|
return $it if $it->significant; |
338
|
|
|
|
|
|
|
} |
339
|
21
|
|
|
|
|
53
|
''; |
340
|
|
|
|
|
|
|
} |
341
|
|
|
|
|
|
|
|
342
|
|
|
|
|
|
|
=pod |
343
|
|
|
|
|
|
|
|
344
|
|
|
|
|
|
|
=head2 first_token |
345
|
|
|
|
|
|
|
|
346
|
|
|
|
|
|
|
As a support method for higher-order algorithms that deal specifically with |
347
|
|
|
|
|
|
|
tokens and actual Perl content, the C method finds the first |
348
|
|
|
|
|
|
|
PPI::Token object within or equal to this one. |
349
|
|
|
|
|
|
|
|
350
|
|
|
|
|
|
|
That is, if called on a L subclass, it will descend until it |
351
|
|
|
|
|
|
|
finds a L. If called on a L object, it will return |
352
|
|
|
|
|
|
|
the same object. |
353
|
|
|
|
|
|
|
|
354
|
|
|
|
|
|
|
Returns a L object, or dies on error (which should be extremely |
355
|
|
|
|
|
|
|
rare and only occur if an illegal empty L exists below the |
356
|
|
|
|
|
|
|
current Element somewhere.) |
357
|
|
|
|
|
|
|
|
358
|
|
|
|
|
|
|
=cut |
359
|
|
|
|
|
|
|
|
360
|
|
|
|
|
|
|
sub first_token { |
361
|
15
|
|
|
15
|
1
|
386
|
my $cursor = shift; |
362
|
15
|
|
|
|
|
33
|
while ( $cursor->isa('PPI::Node') ) { |
363
|
18
|
50
|
|
|
|
65
|
$cursor = $cursor->first_element |
364
|
|
|
|
|
|
|
or die "Found empty PPI::Node while getting first token"; |
365
|
|
|
|
|
|
|
} |
366
|
15
|
|
|
|
|
32
|
$cursor; |
367
|
|
|
|
|
|
|
} |
368
|
|
|
|
|
|
|
|
369
|
|
|
|
|
|
|
|
370
|
|
|
|
|
|
|
=pod |
371
|
|
|
|
|
|
|
|
372
|
|
|
|
|
|
|
=head2 last_token |
373
|
|
|
|
|
|
|
|
374
|
|
|
|
|
|
|
As a support method for higher-order algorithms that deal specifically with |
375
|
|
|
|
|
|
|
tokens and actual Perl content, the C method finds the last |
376
|
|
|
|
|
|
|
PPI::Token object within or equal to this one. |
377
|
|
|
|
|
|
|
|
378
|
|
|
|
|
|
|
That is, if called on a L subclass, it will descend until it |
379
|
|
|
|
|
|
|
finds a L. If called on a L object, it will return |
380
|
|
|
|
|
|
|
the itself. |
381
|
|
|
|
|
|
|
|
382
|
|
|
|
|
|
|
Returns a L object, or dies on error (which should be extremely |
383
|
|
|
|
|
|
|
rare and only occur if an illegal empty L exists below the |
384
|
|
|
|
|
|
|
current Element somewhere.) |
385
|
|
|
|
|
|
|
|
386
|
|
|
|
|
|
|
=cut |
387
|
|
|
|
|
|
|
|
388
|
|
|
|
|
|
|
sub last_token { |
389
|
14
|
|
|
14
|
1
|
24
|
my $cursor = shift; |
390
|
14
|
|
|
|
|
31
|
while ( $cursor->isa('PPI::Node') ) { |
391
|
18
|
50
|
|
|
|
68
|
$cursor = $cursor->last_element |
392
|
|
|
|
|
|
|
or die "Found empty PPI::Node while getting first token"; |
393
|
|
|
|
|
|
|
} |
394
|
14
|
|
|
|
|
32
|
$cursor; |
395
|
|
|
|
|
|
|
} |
396
|
|
|
|
|
|
|
|
397
|
|
|
|
|
|
|
=pod |
398
|
|
|
|
|
|
|
|
399
|
|
|
|
|
|
|
=head2 next_token |
400
|
|
|
|
|
|
|
|
401
|
|
|
|
|
|
|
As a support method for higher-order algorithms that deal specifically with |
402
|
|
|
|
|
|
|
tokens and actual Perl content, the C method finds the |
403
|
|
|
|
|
|
|
L object that is immediately after the current Element, even if |
404
|
|
|
|
|
|
|
it is not within the same parent L as the one for which the |
405
|
|
|
|
|
|
|
method is being called. |
406
|
|
|
|
|
|
|
|
407
|
|
|
|
|
|
|
Note that this is B defined as a L-specific method, |
408
|
|
|
|
|
|
|
because it can be useful to find the next token that is after, say, a |
409
|
|
|
|
|
|
|
L, although obviously it would be useless to want the |
410
|
|
|
|
|
|
|
next token after a L. |
411
|
|
|
|
|
|
|
|
412
|
|
|
|
|
|
|
Returns a L object, or false if there are no more tokens after |
413
|
|
|
|
|
|
|
the Element. |
414
|
|
|
|
|
|
|
|
415
|
|
|
|
|
|
|
=cut |
416
|
|
|
|
|
|
|
|
417
|
|
|
|
|
|
|
sub next_token { |
418
|
61
|
|
|
61
|
1
|
432
|
my $cursor = shift; |
419
|
|
|
|
|
|
|
|
420
|
|
|
|
|
|
|
# Find the next element, going upwards as needed |
421
|
61
|
|
|
|
|
63
|
while ( 1 ) { |
422
|
74
|
|
|
|
|
144
|
my $element = $cursor->next_sibling; |
423
|
74
|
100
|
|
|
|
142
|
if ( $element ) { |
424
|
52
|
100
|
|
|
|
146
|
return $element if $element->isa('PPI::Token'); |
425
|
10
|
|
|
|
|
49
|
return $element->first_token; |
426
|
|
|
|
|
|
|
} |
427
|
22
|
100
|
|
|
|
55
|
$cursor = $cursor->parent or return ''; |
428
|
18
|
100
|
66
|
|
|
74
|
if ( $cursor->isa('PPI::Structure') and $cursor->finish ) { |
429
|
5
|
|
|
|
|
11
|
return $cursor->finish; |
430
|
|
|
|
|
|
|
} |
431
|
|
|
|
|
|
|
} |
432
|
|
|
|
|
|
|
} |
433
|
|
|
|
|
|
|
|
434
|
|
|
|
|
|
|
=pod |
435
|
|
|
|
|
|
|
|
436
|
|
|
|
|
|
|
=head2 previous_token |
437
|
|
|
|
|
|
|
|
438
|
|
|
|
|
|
|
As a support method for higher-order algorithms that deal specifically with |
439
|
|
|
|
|
|
|
tokens and actual Perl content, the C method finds the |
440
|
|
|
|
|
|
|
L object that is immediately before the current Element, even |
441
|
|
|
|
|
|
|
if it is not within the same parent L as this one. |
442
|
|
|
|
|
|
|
|
443
|
|
|
|
|
|
|
Note that this is not defined as a L-only method, because it can |
444
|
|
|
|
|
|
|
be useful to find the token is before, say, a L, although |
445
|
|
|
|
|
|
|
obviously it would be useless to want the next token before a |
446
|
|
|
|
|
|
|
L. |
447
|
|
|
|
|
|
|
|
448
|
|
|
|
|
|
|
Returns a L object, or false if there are no more tokens before |
449
|
|
|
|
|
|
|
the C. |
450
|
|
|
|
|
|
|
|
451
|
|
|
|
|
|
|
=cut |
452
|
|
|
|
|
|
|
|
453
|
|
|
|
|
|
|
sub previous_token { |
454
|
59
|
|
|
59
|
1
|
425
|
my $cursor = shift; |
455
|
|
|
|
|
|
|
|
456
|
|
|
|
|
|
|
# Find the previous element, going upwards as needed |
457
|
59
|
|
|
|
|
57
|
while ( 1 ) { |
458
|
70
|
|
|
|
|
107
|
my $element = $cursor->previous_sibling; |
459
|
70
|
100
|
|
|
|
120
|
if ( $element ) { |
460
|
51
|
100
|
|
|
|
137
|
return $element if $element->isa('PPI::Token'); |
461
|
9
|
|
|
|
|
26
|
return $element->last_token; |
462
|
|
|
|
|
|
|
} |
463
|
19
|
100
|
|
|
|
32
|
$cursor = $cursor->parent or return ''; |
464
|
16
|
100
|
66
|
|
|
51
|
if ( $cursor->isa('PPI::Structure') and $cursor->start ) { |
465
|
5
|
|
|
|
|
10
|
return $cursor->start; |
466
|
|
|
|
|
|
|
} |
467
|
|
|
|
|
|
|
} |
468
|
|
|
|
|
|
|
} |
469
|
|
|
|
|
|
|
|
470
|
|
|
|
|
|
|
|
471
|
|
|
|
|
|
|
|
472
|
|
|
|
|
|
|
|
473
|
|
|
|
|
|
|
|
474
|
|
|
|
|
|
|
##################################################################### |
475
|
|
|
|
|
|
|
# Manipulation |
476
|
|
|
|
|
|
|
|
477
|
|
|
|
|
|
|
=pod |
478
|
|
|
|
|
|
|
|
479
|
|
|
|
|
|
|
=head2 clone |
480
|
|
|
|
|
|
|
|
481
|
|
|
|
|
|
|
As per the L module, the C method makes a perfect copy of |
482
|
|
|
|
|
|
|
an Element object. In the generic case, the implementation is done using |
483
|
|
|
|
|
|
|
the L module's mechanism itself. In higher-order cases, such as for |
484
|
|
|
|
|
|
|
Nodes, there is more work involved to keep the parent-child links intact. |
485
|
|
|
|
|
|
|
|
486
|
|
|
|
|
|
|
=cut |
487
|
|
|
|
|
|
|
|
488
|
|
|
|
|
|
|
sub clone { |
489
|
6
|
|
|
6
|
1
|
298
|
Clone::clone(shift); |
490
|
|
|
|
|
|
|
} |
491
|
|
|
|
|
|
|
|
492
|
|
|
|
|
|
|
=pod |
493
|
|
|
|
|
|
|
|
494
|
|
|
|
|
|
|
=head2 insert_before @Elements |
495
|
|
|
|
|
|
|
|
496
|
|
|
|
|
|
|
The C method allows you to insert lexical perl content, in |
497
|
|
|
|
|
|
|
the form of C objects, before the calling C. You |
498
|
|
|
|
|
|
|
need to be very careful when modifying perl code, as it's easy to break |
499
|
|
|
|
|
|
|
things. |
500
|
|
|
|
|
|
|
|
501
|
|
|
|
|
|
|
In its initial incarnation, this method allows you to insert a single |
502
|
|
|
|
|
|
|
Element, and will perform some basic checking to prevent you inserting |
503
|
|
|
|
|
|
|
something that would be structurally wrong (in PDOM terms). |
504
|
|
|
|
|
|
|
|
505
|
|
|
|
|
|
|
In future, this method may be enhanced to allow the insertion of multiple |
506
|
|
|
|
|
|
|
Elements, inline-parsed code strings or L objects. |
507
|
|
|
|
|
|
|
|
508
|
|
|
|
|
|
|
Returns true if the Element was inserted, false if it can not be inserted, |
509
|
|
|
|
|
|
|
or C if you do not provide a C object as a parameter. |
510
|
|
|
|
|
|
|
|
511
|
|
|
|
|
|
|
=cut |
512
|
|
|
|
|
|
|
|
513
|
|
|
|
|
|
|
sub __insert_before { |
514
|
2
|
|
|
2
|
|
4
|
my $self = shift; |
515
|
2
|
|
|
|
|
7
|
$self->parent->__insert_before_child( $self, @_ ); |
516
|
|
|
|
|
|
|
} |
517
|
|
|
|
|
|
|
|
518
|
|
|
|
|
|
|
=pod |
519
|
|
|
|
|
|
|
|
520
|
|
|
|
|
|
|
=head2 insert_after @Elements |
521
|
|
|
|
|
|
|
|
522
|
|
|
|
|
|
|
The C method allows you to insert lexical perl content, in |
523
|
|
|
|
|
|
|
the form of C objects, after the calling C. You need |
524
|
|
|
|
|
|
|
to be very careful when modifying perl code, as it's easy to break things. |
525
|
|
|
|
|
|
|
|
526
|
|
|
|
|
|
|
In its initial incarnation, this method allows you to insert a single |
527
|
|
|
|
|
|
|
Element, and will perform some basic checking to prevent you inserting |
528
|
|
|
|
|
|
|
something that would be structurally wrong (in PDOM terms). |
529
|
|
|
|
|
|
|
|
530
|
|
|
|
|
|
|
In future, this method may be enhanced to allow the insertion of multiple |
531
|
|
|
|
|
|
|
Elements, inline-parsed code strings or L objects. |
532
|
|
|
|
|
|
|
|
533
|
|
|
|
|
|
|
Returns true if the Element was inserted, false if it can not be inserted, |
534
|
|
|
|
|
|
|
or C if you do not provide a C object as a parameter. |
535
|
|
|
|
|
|
|
|
536
|
|
|
|
|
|
|
=cut |
537
|
|
|
|
|
|
|
|
538
|
|
|
|
|
|
|
sub __insert_after { |
539
|
2
|
|
|
2
|
|
4
|
my $self = shift; |
540
|
2
|
|
|
|
|
6
|
$self->parent->__insert_after_child( $self, @_ ); |
541
|
|
|
|
|
|
|
} |
542
|
|
|
|
|
|
|
|
543
|
|
|
|
|
|
|
=pod |
544
|
|
|
|
|
|
|
|
545
|
|
|
|
|
|
|
=head2 remove |
546
|
|
|
|
|
|
|
|
547
|
|
|
|
|
|
|
For a given C, the C method will remove it from its |
548
|
|
|
|
|
|
|
parent B, along with all of its children. |
549
|
|
|
|
|
|
|
|
550
|
|
|
|
|
|
|
Returns the C itself as a convenience, or C if an error |
551
|
|
|
|
|
|
|
occurs while trying to remove the C. |
552
|
|
|
|
|
|
|
|
553
|
|
|
|
|
|
|
=cut |
554
|
|
|
|
|
|
|
|
555
|
|
|
|
|
|
|
sub remove { |
556
|
64
|
|
|
64
|
1
|
82
|
my $self = shift; |
557
|
64
|
50
|
|
|
|
111
|
my $parent = $self->parent or return $self; |
558
|
64
|
|
|
|
|
172
|
$parent->remove_child( $self ); |
559
|
|
|
|
|
|
|
} |
560
|
|
|
|
|
|
|
|
561
|
|
|
|
|
|
|
=pod |
562
|
|
|
|
|
|
|
|
563
|
|
|
|
|
|
|
=head2 delete |
564
|
|
|
|
|
|
|
|
565
|
|
|
|
|
|
|
For a given C, the C method will remove it from its |
566
|
|
|
|
|
|
|
parent, immediately deleting the C and all of its children (if it |
567
|
|
|
|
|
|
|
has any). |
568
|
|
|
|
|
|
|
|
569
|
|
|
|
|
|
|
Returns true if the C was successfully deleted, or C if |
570
|
|
|
|
|
|
|
an error occurs while trying to remove the C. |
571
|
|
|
|
|
|
|
|
572
|
|
|
|
|
|
|
=cut |
573
|
|
|
|
|
|
|
|
574
|
|
|
|
|
|
|
sub delete { |
575
|
64
|
50
|
|
64
|
1
|
123
|
$_[0]->remove or return undef; |
576
|
64
|
|
|
|
|
173
|
$_[0]->DESTROY; |
577
|
64
|
|
|
|
|
123
|
1; |
578
|
|
|
|
|
|
|
} |
579
|
|
|
|
|
|
|
|
580
|
|
|
|
|
|
|
=pod |
581
|
|
|
|
|
|
|
|
582
|
|
|
|
|
|
|
=head2 replace $Element |
583
|
|
|
|
|
|
|
|
584
|
|
|
|
|
|
|
Although some higher level class support more exotic forms of replace, |
585
|
|
|
|
|
|
|
at the basic level the C method takes a single C as |
586
|
|
|
|
|
|
|
an argument and replaces the current C with it. |
587
|
|
|
|
|
|
|
|
588
|
|
|
|
|
|
|
To prevent accidental damage to code, in this initial implementation the |
589
|
|
|
|
|
|
|
replacement element B be of the same class (or a subclass) as the |
590
|
|
|
|
|
|
|
one being replaced. |
591
|
|
|
|
|
|
|
|
592
|
|
|
|
|
|
|
If successful, returns the replace element. Otherwise, returns C. |
593
|
|
|
|
|
|
|
|
594
|
|
|
|
|
|
|
=cut |
595
|
|
|
|
|
|
|
|
596
|
|
|
|
|
|
|
sub replace { |
597
|
1
|
50
|
|
1
|
1
|
6
|
my $self = ref $_[0] ? shift : return undef; |
598
|
1
|
50
|
|
|
|
10
|
my $replace = _INSTANCE(shift, ref $self) or return undef; |
599
|
1
|
|
|
|
|
6
|
return $self->parent->replace_child( $self, $replace ); |
600
|
|
|
|
|
|
|
} |
601
|
|
|
|
|
|
|
|
602
|
|
|
|
|
|
|
=pod |
603
|
|
|
|
|
|
|
|
604
|
|
|
|
|
|
|
=head2 location |
605
|
|
|
|
|
|
|
|
606
|
|
|
|
|
|
|
If the Element exists within a L that has |
607
|
|
|
|
|
|
|
indexed the Element locations using C, the |
608
|
|
|
|
|
|
|
C method will return the location of the first character of the |
609
|
|
|
|
|
|
|
Element within the Document. |
610
|
|
|
|
|
|
|
|
611
|
|
|
|
|
|
|
Returns the location as a reference to a five-element array in the form C<[ |
612
|
|
|
|
|
|
|
$line, $rowchar, $col, $logical_line, $logical_file_name ]>. The values are in |
613
|
|
|
|
|
|
|
a human format, with the first character of the file located at C<[ 1, 1, 1, ?, |
614
|
|
|
|
|
|
|
'something' ]>. |
615
|
|
|
|
|
|
|
|
616
|
|
|
|
|
|
|
The second and third numbers are similar, except that the second is the |
617
|
|
|
|
|
|
|
literal horizontal character, and the third is the visual column, taking |
618
|
|
|
|
|
|
|
into account tabbing (see L). |
619
|
|
|
|
|
|
|
|
620
|
|
|
|
|
|
|
The fourth number is the line number, taking into account any C<#line> |
621
|
|
|
|
|
|
|
directives. The fifth element is the name of the file that the element was |
622
|
|
|
|
|
|
|
found in, if available, taking into account any C<#line> directives. |
623
|
|
|
|
|
|
|
|
624
|
|
|
|
|
|
|
Returns C on error, or if the L object has not been |
625
|
|
|
|
|
|
|
indexed. |
626
|
|
|
|
|
|
|
|
627
|
|
|
|
|
|
|
=cut |
628
|
|
|
|
|
|
|
|
629
|
|
|
|
|
|
|
sub location { |
630
|
78751
|
|
|
78751
|
1
|
488897
|
my $self = shift; |
631
|
|
|
|
|
|
|
|
632
|
78751
|
50
|
|
|
|
89273
|
$self->_ensure_location_present or return undef; |
633
|
|
|
|
|
|
|
|
634
|
|
|
|
|
|
|
# Return a copy, not the original |
635
|
78751
|
|
|
|
|
80540
|
return [ @{$self->{_location}} ]; |
|
78751
|
|
|
|
|
202279
|
|
636
|
|
|
|
|
|
|
} |
637
|
|
|
|
|
|
|
|
638
|
|
|
|
|
|
|
=pod |
639
|
|
|
|
|
|
|
|
640
|
|
|
|
|
|
|
=head2 line_number |
641
|
|
|
|
|
|
|
|
642
|
|
|
|
|
|
|
If the Element exists within a L that has indexed the Element |
643
|
|
|
|
|
|
|
locations using C, the C method |
644
|
|
|
|
|
|
|
will return the line number of the first character of the Element within the |
645
|
|
|
|
|
|
|
Document. |
646
|
|
|
|
|
|
|
|
647
|
|
|
|
|
|
|
Returns C on error, or if the L object has not been |
648
|
|
|
|
|
|
|
indexed. |
649
|
|
|
|
|
|
|
|
650
|
|
|
|
|
|
|
=cut |
651
|
|
|
|
|
|
|
|
652
|
|
|
|
|
|
|
sub line_number { |
653
|
94
|
|
|
94
|
1
|
8161
|
my $self = shift; |
654
|
|
|
|
|
|
|
|
655
|
94
|
50
|
|
|
|
152
|
my $location = $self->location() or return undef; |
656
|
94
|
|
|
|
|
199
|
return $location->[0]; |
657
|
|
|
|
|
|
|
} |
658
|
|
|
|
|
|
|
|
659
|
|
|
|
|
|
|
=pod |
660
|
|
|
|
|
|
|
|
661
|
|
|
|
|
|
|
=head2 column_number |
662
|
|
|
|
|
|
|
|
663
|
|
|
|
|
|
|
If the Element exists within a L that has indexed the Element |
664
|
|
|
|
|
|
|
locations using C, the C method |
665
|
|
|
|
|
|
|
will return the column number of the first character of the Element within the |
666
|
|
|
|
|
|
|
Document. |
667
|
|
|
|
|
|
|
|
668
|
|
|
|
|
|
|
Returns C on error, or if the L object has not been |
669
|
|
|
|
|
|
|
indexed. |
670
|
|
|
|
|
|
|
|
671
|
|
|
|
|
|
|
=cut |
672
|
|
|
|
|
|
|
|
673
|
|
|
|
|
|
|
sub column_number { |
674
|
1
|
|
|
1
|
1
|
467
|
my $self = shift; |
675
|
|
|
|
|
|
|
|
676
|
1
|
50
|
|
|
|
14
|
my $location = $self->location() or return undef; |
677
|
1
|
|
|
|
|
3
|
return $location->[1]; |
678
|
|
|
|
|
|
|
} |
679
|
|
|
|
|
|
|
|
680
|
|
|
|
|
|
|
=pod |
681
|
|
|
|
|
|
|
|
682
|
|
|
|
|
|
|
=head2 visual_column_number |
683
|
|
|
|
|
|
|
|
684
|
|
|
|
|
|
|
If the Element exists within a L that has indexed the Element |
685
|
|
|
|
|
|
|
locations using C, the C |
686
|
|
|
|
|
|
|
method will return the visual column number of the first character of the |
687
|
|
|
|
|
|
|
Element within the Document, according to the value of |
688
|
|
|
|
|
|
|
L. |
689
|
|
|
|
|
|
|
|
690
|
|
|
|
|
|
|
Returns C on error, or if the L object has not been |
691
|
|
|
|
|
|
|
indexed. |
692
|
|
|
|
|
|
|
|
693
|
|
|
|
|
|
|
=cut |
694
|
|
|
|
|
|
|
|
695
|
|
|
|
|
|
|
sub visual_column_number { |
696
|
1
|
|
|
1
|
1
|
443
|
my $self = shift; |
697
|
|
|
|
|
|
|
|
698
|
1
|
50
|
|
|
|
3
|
my $location = $self->location() or return undef; |
699
|
1
|
|
|
|
|
5
|
return $location->[2]; |
700
|
|
|
|
|
|
|
} |
701
|
|
|
|
|
|
|
|
702
|
|
|
|
|
|
|
=pod |
703
|
|
|
|
|
|
|
|
704
|
|
|
|
|
|
|
=head2 logical_line_number |
705
|
|
|
|
|
|
|
|
706
|
|
|
|
|
|
|
If the Element exists within a L that has indexed the Element |
707
|
|
|
|
|
|
|
locations using C, the C |
708
|
|
|
|
|
|
|
method will return the line number of the first character of the Element within |
709
|
|
|
|
|
|
|
the Document, taking into account any C<#line> directives. |
710
|
|
|
|
|
|
|
|
711
|
|
|
|
|
|
|
Returns C on error, or if the L object has not been |
712
|
|
|
|
|
|
|
indexed. |
713
|
|
|
|
|
|
|
|
714
|
|
|
|
|
|
|
=cut |
715
|
|
|
|
|
|
|
|
716
|
|
|
|
|
|
|
sub logical_line_number { |
717
|
1
|
|
|
1
|
1
|
463
|
my $self = shift; |
718
|
|
|
|
|
|
|
|
719
|
1
|
|
|
|
|
4
|
return $self->location()->[3]; |
720
|
|
|
|
|
|
|
} |
721
|
|
|
|
|
|
|
|
722
|
|
|
|
|
|
|
=pod |
723
|
|
|
|
|
|
|
|
724
|
|
|
|
|
|
|
=head2 logical_filename |
725
|
|
|
|
|
|
|
|
726
|
|
|
|
|
|
|
If the Element exists within a L that has indexed the Element |
727
|
|
|
|
|
|
|
locations using C, the C |
728
|
|
|
|
|
|
|
method will return the logical file name containing the first character of the |
729
|
|
|
|
|
|
|
Element within the Document, taking into account any C<#line> directives. |
730
|
|
|
|
|
|
|
|
731
|
|
|
|
|
|
|
Returns C on error, or if the L object has not been |
732
|
|
|
|
|
|
|
indexed. |
733
|
|
|
|
|
|
|
|
734
|
|
|
|
|
|
|
=cut |
735
|
|
|
|
|
|
|
|
736
|
|
|
|
|
|
|
sub logical_filename { |
737
|
11
|
|
|
11
|
1
|
2919
|
my $self = shift; |
738
|
|
|
|
|
|
|
|
739
|
11
|
50
|
|
|
|
27
|
my $location = $self->location() or return undef; |
740
|
11
|
|
|
|
|
74
|
return $location->[4]; |
741
|
|
|
|
|
|
|
} |
742
|
|
|
|
|
|
|
|
743
|
|
|
|
|
|
|
sub _ensure_location_present { |
744
|
78751
|
|
|
78751
|
|
79490
|
my $self = shift; |
745
|
|
|
|
|
|
|
|
746
|
78751
|
100
|
|
|
|
118364
|
unless ( exists $self->{_location} ) { |
747
|
|
|
|
|
|
|
# Are we inside a normal document? |
748
|
173
|
50
|
|
|
|
570
|
my $Document = $self->document or return undef; |
749
|
173
|
50
|
|
|
|
1060
|
if ( $Document->isa('PPI::Document::Fragment') ) { |
750
|
|
|
|
|
|
|
# Because they can't be serialized, document fragments |
751
|
|
|
|
|
|
|
# do not support the concept of location. |
752
|
0
|
|
|
|
|
0
|
return undef; |
753
|
|
|
|
|
|
|
} |
754
|
|
|
|
|
|
|
|
755
|
|
|
|
|
|
|
# Generate the locations. If they need one location, then |
756
|
|
|
|
|
|
|
# the chances are they'll want more, and it's better that |
757
|
|
|
|
|
|
|
# everything is already pre-generated. |
758
|
173
|
50
|
|
|
|
593
|
$Document->index_locations or return undef; |
759
|
173
|
50
|
|
|
|
503
|
unless ( exists $self->{_location} ) { |
760
|
|
|
|
|
|
|
# erm... something went very wrong here |
761
|
0
|
|
|
|
|
0
|
return undef; |
762
|
|
|
|
|
|
|
} |
763
|
|
|
|
|
|
|
} |
764
|
|
|
|
|
|
|
|
765
|
78751
|
|
|
|
|
107186
|
return 1; |
766
|
|
|
|
|
|
|
} |
767
|
|
|
|
|
|
|
|
768
|
|
|
|
|
|
|
# Although flush_locations is only publically a Document-level method, |
769
|
|
|
|
|
|
|
# we are able to implement it at an Element level, allowing us to |
770
|
|
|
|
|
|
|
# selectively flush only the part of the document that occurs after the |
771
|
|
|
|
|
|
|
# element for which the flush is called. |
772
|
|
|
|
|
|
|
sub _flush_locations { |
773
|
1
|
|
|
1
|
|
3
|
my $self = shift; |
774
|
1
|
50
|
|
|
|
6
|
unless ( $self == $self->top ) { |
775
|
0
|
|
|
|
|
0
|
return $self->top->_flush_locations( $self ); |
776
|
|
|
|
|
|
|
} |
777
|
|
|
|
|
|
|
|
778
|
|
|
|
|
|
|
# Get the full list of all Tokens |
779
|
1
|
|
|
|
|
11
|
my @Tokens = $self->tokens; |
780
|
|
|
|
|
|
|
|
781
|
|
|
|
|
|
|
# Optionally allow starting from an arbitrary element (or rather, |
782
|
|
|
|
|
|
|
# the first Token equal-to-or-within an arbitrary element) |
783
|
1
|
50
|
|
|
|
8
|
if ( _INSTANCE($_[0], 'PPI::Element') ) { |
784
|
0
|
|
|
|
|
0
|
my $start = shift->first_token; |
785
|
0
|
|
|
|
|
0
|
while ( my $Token = shift @Tokens ) { |
786
|
0
|
0
|
|
|
|
0
|
return 1 unless $Token->{_location}; |
787
|
0
|
0
|
|
|
|
0
|
next unless refaddr($Token) == refaddr($start); |
788
|
|
|
|
|
|
|
|
789
|
|
|
|
|
|
|
# Found the start. Flush its location |
790
|
0
|
|
|
|
|
0
|
delete $$Token->{_location}; |
791
|
0
|
|
|
|
|
0
|
last; |
792
|
|
|
|
|
|
|
} |
793
|
|
|
|
|
|
|
} |
794
|
|
|
|
|
|
|
|
795
|
|
|
|
|
|
|
# Iterate over any remaining Tokens and flush their location |
796
|
1
|
|
|
|
|
4
|
foreach my $Token ( @Tokens ) { |
797
|
169
|
|
|
|
|
228
|
delete $Token->{_location}; |
798
|
|
|
|
|
|
|
} |
799
|
|
|
|
|
|
|
|
800
|
1
|
|
|
|
|
8
|
1; |
801
|
|
|
|
|
|
|
} |
802
|
|
|
|
|
|
|
|
803
|
|
|
|
|
|
|
|
804
|
|
|
|
|
|
|
|
805
|
|
|
|
|
|
|
|
806
|
|
|
|
|
|
|
|
807
|
|
|
|
|
|
|
##################################################################### |
808
|
|
|
|
|
|
|
# XML Compatibility Methods |
809
|
|
|
|
|
|
|
|
810
|
|
|
|
|
|
|
sub _xml_name { |
811
|
5
|
|
33
|
5
|
|
27
|
my $class = ref $_[0] || $_[0]; |
812
|
5
|
|
|
|
|
22
|
my $name = lc join( '_', split /::/, $class ); |
813
|
5
|
|
|
|
|
26
|
substr($name, 4); |
814
|
|
|
|
|
|
|
} |
815
|
|
|
|
|
|
|
|
816
|
|
|
|
|
|
|
sub _xml_attr { |
817
|
5
|
|
|
5
|
|
18
|
return {}; |
818
|
|
|
|
|
|
|
} |
819
|
|
|
|
|
|
|
|
820
|
|
|
|
|
|
|
sub _xml_content { |
821
|
5
|
100
|
|
5
|
|
29
|
defined $_[0]->{content} ? $_[0]->{content} : ''; |
822
|
|
|
|
|
|
|
} |
823
|
|
|
|
|
|
|
|
824
|
|
|
|
|
|
|
|
825
|
|
|
|
|
|
|
|
826
|
|
|
|
|
|
|
|
827
|
|
|
|
|
|
|
|
828
|
|
|
|
|
|
|
##################################################################### |
829
|
|
|
|
|
|
|
# Internals |
830
|
|
|
|
|
|
|
|
831
|
|
|
|
|
|
|
# Set the error string |
832
|
|
|
|
|
|
|
sub _error { |
833
|
0
|
|
|
0
|
|
0
|
$errstr = $_[1]; |
834
|
0
|
|
|
|
|
0
|
undef; |
835
|
|
|
|
|
|
|
} |
836
|
|
|
|
|
|
|
|
837
|
|
|
|
|
|
|
# Clear the error string |
838
|
|
|
|
|
|
|
sub _clear { |
839
|
0
|
|
|
0
|
|
0
|
$errstr = ''; |
840
|
0
|
|
|
|
|
0
|
$_[0]; |
841
|
|
|
|
|
|
|
} |
842
|
|
|
|
|
|
|
|
843
|
|
|
|
|
|
|
# Being DESTROYed in this manner, rather than by an explicit |
844
|
|
|
|
|
|
|
# ->delete means our reference count has probably fallen to zero. |
845
|
|
|
|
|
|
|
# Therefore we don't need to remove ourselves from our parent, |
846
|
|
|
|
|
|
|
# just the index ( just in case ). |
847
|
|
|
|
|
|
|
### XS -> PPI/XS.xs:_PPI_Element__DESTROY 0.900+ |
848
|
364416
|
|
|
364416
|
|
1056295
|
sub DESTROY { delete $_PARENT{refaddr $_[0]} } |
849
|
|
|
|
|
|
|
|
850
|
|
|
|
|
|
|
# Operator overloads |
851
|
734
|
50
|
|
734
|
|
2978
|
sub __equals { ref $_[1] and refaddr($_[0]) == refaddr($_[1]) } |
852
|
1
|
|
|
1
|
|
5
|
sub __nequals { !__equals(@_) } |
853
|
|
|
|
|
|
|
sub __eq { |
854
|
6306
|
50
|
|
6306
|
|
1574724
|
my $self = _INSTANCE($_[0], 'PPI::Element') ? $_[0]->content : $_[0]; |
855
|
6306
|
100
|
|
|
|
15762
|
my $other = _INSTANCE($_[1], 'PPI::Element') ? $_[1]->content : $_[1]; |
856
|
6306
|
|
|
|
|
72228
|
$self eq $other; |
857
|
|
|
|
|
|
|
} |
858
|
1
|
|
|
1
|
|
4
|
sub __ne { !__eq(@_) } |
859
|
|
|
|
|
|
|
|
860
|
|
|
|
|
|
|
1; |
861
|
|
|
|
|
|
|
|
862
|
|
|
|
|
|
|
=pod |
863
|
|
|
|
|
|
|
|
864
|
|
|
|
|
|
|
=head1 TO DO |
865
|
|
|
|
|
|
|
|
866
|
|
|
|
|
|
|
It would be nice if C could be used in an ad-hoc manner. That is, |
867
|
|
|
|
|
|
|
if called on an Element within a Document that has not been indexed, it will |
868
|
|
|
|
|
|
|
do a one-off calculation to find the location. It might be very painful if |
869
|
|
|
|
|
|
|
someone started using it a lot, without remembering to index the document, |
870
|
|
|
|
|
|
|
but it would be handy for things that are only likely to use it once, such |
871
|
|
|
|
|
|
|
as error handlers. |
872
|
|
|
|
|
|
|
|
873
|
|
|
|
|
|
|
=head1 SUPPORT |
874
|
|
|
|
|
|
|
|
875
|
|
|
|
|
|
|
See the L in the main module. |
876
|
|
|
|
|
|
|
|
877
|
|
|
|
|
|
|
=head1 AUTHOR |
878
|
|
|
|
|
|
|
|
879
|
|
|
|
|
|
|
Adam Kennedy Eadamk@cpan.orgE |
880
|
|
|
|
|
|
|
|
881
|
|
|
|
|
|
|
=head1 COPYRIGHT |
882
|
|
|
|
|
|
|
|
883
|
|
|
|
|
|
|
Copyright 2001 - 2011 Adam Kennedy. |
884
|
|
|
|
|
|
|
|
885
|
|
|
|
|
|
|
This program is free software; you can redistribute |
886
|
|
|
|
|
|
|
it and/or modify it under the same terms as Perl itself. |
887
|
|
|
|
|
|
|
|
888
|
|
|
|
|
|
|
The full text of the license can be found in the |
889
|
|
|
|
|
|
|
LICENSE file included with this module. |
890
|
|
|
|
|
|
|
|
891
|
|
|
|
|
|
|
=cut |