line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package PPI::Token; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=pod |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 NAME |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
PPI::Token - A single token of Perl source code |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 INHERITANCE |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
PPI::Token |
12
|
|
|
|
|
|
|
isa PPI::Element |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 DESCRIPTION |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
C is the abstract base class for all Tokens. In PPI terms, a "Token" is |
17
|
|
|
|
|
|
|
a L that directly represents bytes of source code. |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 METHODS |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=cut |
22
|
|
|
|
|
|
|
|
23
|
64
|
|
|
64
|
|
366
|
use strict; |
|
64
|
|
|
|
|
113
|
|
|
64
|
|
|
|
|
1725
|
|
24
|
64
|
|
|
64
|
|
639
|
use Params::Util qw{_INSTANCE}; |
|
64
|
|
|
|
|
5157
|
|
|
64
|
|
|
|
|
2178
|
|
25
|
64
|
|
|
64
|
|
690
|
use PPI::Element (); |
|
64
|
|
|
|
|
104
|
|
|
64
|
|
|
|
|
653
|
|
26
|
64
|
|
|
64
|
|
577
|
use PPI::Exception (); |
|
64
|
|
|
|
|
98
|
|
|
64
|
|
|
|
|
1993
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
our $VERSION = '1.275'; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
our @ISA = 'PPI::Element'; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# We don't load the abstracts, they are loaded |
33
|
|
|
|
|
|
|
# as part of the inheritance process. |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# Load the token classes |
36
|
64
|
|
|
64
|
|
22971
|
use PPI::Token::BOM (); |
|
64
|
|
|
|
|
124
|
|
|
64
|
|
|
|
|
1313
|
|
37
|
64
|
|
|
64
|
|
24509
|
use PPI::Token::Whitespace (); |
|
64
|
|
|
|
|
135
|
|
|
64
|
|
|
|
|
1529
|
|
38
|
64
|
|
|
64
|
|
22408
|
use PPI::Token::Comment (); |
|
64
|
|
|
|
|
139
|
|
|
64
|
|
|
|
|
1711
|
|
39
|
64
|
|
|
64
|
|
22249
|
use PPI::Token::Pod (); |
|
64
|
|
|
|
|
139
|
|
|
64
|
|
|
|
|
1449
|
|
40
|
64
|
|
|
64
|
|
21363
|
use PPI::Token::Number (); |
|
64
|
|
|
|
|
161
|
|
|
64
|
|
|
|
|
1475
|
|
41
|
64
|
|
|
64
|
|
22158
|
use PPI::Token::Number::Binary (); |
|
64
|
|
|
|
|
125
|
|
|
64
|
|
|
|
|
1456
|
|
42
|
64
|
|
|
64
|
|
20737
|
use PPI::Token::Number::Octal (); |
|
64
|
|
|
|
|
158
|
|
|
64
|
|
|
|
|
1470
|
|
43
|
64
|
|
|
64
|
|
22227
|
use PPI::Token::Number::Hex (); |
|
64
|
|
|
|
|
136
|
|
|
64
|
|
|
|
|
1515
|
|
44
|
64
|
|
|
64
|
|
21690
|
use PPI::Token::Number::Float (); |
|
64
|
|
|
|
|
144
|
|
|
64
|
|
|
|
|
1610
|
|
45
|
64
|
|
|
64
|
|
21908
|
use PPI::Token::Number::Exp (); |
|
64
|
|
|
|
|
142
|
|
|
64
|
|
|
|
|
1635
|
|
46
|
64
|
|
|
64
|
|
22513
|
use PPI::Token::Number::Version (); |
|
64
|
|
|
|
|
133
|
|
|
64
|
|
|
|
|
1720
|
|
47
|
64
|
|
|
64
|
|
23061
|
use PPI::Token::Word (); |
|
64
|
|
|
|
|
148
|
|
|
64
|
|
|
|
|
2033
|
|
48
|
64
|
|
|
64
|
|
20807
|
use PPI::Token::DashedWord (); |
|
64
|
|
|
|
|
139
|
|
|
64
|
|
|
|
|
1828
|
|
49
|
64
|
|
|
64
|
|
21232
|
use PPI::Token::Symbol (); |
|
64
|
|
|
|
|
144
|
|
|
64
|
|
|
|
|
1978
|
|
50
|
64
|
|
|
64
|
|
21292
|
use PPI::Token::ArrayIndex (); |
|
64
|
|
|
|
|
186
|
|
|
64
|
|
|
|
|
1896
|
|
51
|
64
|
|
|
64
|
|
21703
|
use PPI::Token::Magic (); |
|
64
|
|
|
|
|
152
|
|
|
64
|
|
|
|
|
2220
|
|
52
|
64
|
|
|
64
|
|
23087
|
use PPI::Token::Quote::Single (); |
|
64
|
|
|
|
|
134
|
|
|
64
|
|
|
|
|
3309
|
|
53
|
64
|
|
|
64
|
|
22837
|
use PPI::Token::Quote::Double (); |
|
64
|
|
|
|
|
133
|
|
|
64
|
|
|
|
|
2181
|
|
54
|
64
|
|
|
64
|
|
22637
|
use PPI::Token::Quote::Literal (); |
|
64
|
|
|
|
|
138
|
|
|
64
|
|
|
|
|
2189
|
|
55
|
64
|
|
|
64
|
|
22405
|
use PPI::Token::Quote::Interpolate (); |
|
64
|
|
|
|
|
160
|
|
|
64
|
|
|
|
|
2273
|
|
56
|
64
|
|
|
64
|
|
22379
|
use PPI::Token::QuoteLike::Backtick (); |
|
64
|
|
|
|
|
148
|
|
|
64
|
|
|
|
|
2189
|
|
57
|
64
|
|
|
64
|
|
21213
|
use PPI::Token::QuoteLike::Command (); |
|
64
|
|
|
|
|
131
|
|
|
64
|
|
|
|
|
2239
|
|
58
|
64
|
|
|
64
|
|
21941
|
use PPI::Token::QuoteLike::Regexp (); |
|
64
|
|
|
|
|
141
|
|
|
64
|
|
|
|
|
2384
|
|
59
|
64
|
|
|
64
|
|
21836
|
use PPI::Token::QuoteLike::Words (); |
|
64
|
|
|
|
|
143
|
|
|
64
|
|
|
|
|
2401
|
|
60
|
64
|
|
|
64
|
|
21785
|
use PPI::Token::QuoteLike::Readline (); |
|
64
|
|
|
|
|
145
|
|
|
64
|
|
|
|
|
2382
|
|
61
|
64
|
|
|
64
|
|
21842
|
use PPI::Token::Regexp::Match (); |
|
64
|
|
|
|
|
137
|
|
|
64
|
|
|
|
|
2430
|
|
62
|
64
|
|
|
64
|
|
21589
|
use PPI::Token::Regexp::Substitute (); |
|
64
|
|
|
|
|
147
|
|
|
64
|
|
|
|
|
2463
|
|
63
|
64
|
|
|
64
|
|
21480
|
use PPI::Token::Regexp::Transliterate (); |
|
64
|
|
|
|
|
145
|
|
|
64
|
|
|
|
|
2598
|
|
64
|
64
|
|
|
64
|
|
21409
|
use PPI::Token::Operator (); |
|
64
|
|
|
|
|
141
|
|
|
64
|
|
|
|
|
2860
|
|
65
|
64
|
|
|
64
|
|
19704
|
use PPI::Token::Cast (); |
|
64
|
|
|
|
|
131
|
|
|
64
|
|
|
|
|
2783
|
|
66
|
64
|
|
|
64
|
|
22352
|
use PPI::Token::Structure (); |
|
64
|
|
|
|
|
147
|
|
|
64
|
|
|
|
|
2870
|
|
67
|
64
|
|
|
64
|
|
21109
|
use PPI::Token::Label (); |
|
64
|
|
|
|
|
182
|
|
|
64
|
|
|
|
|
3056
|
|
68
|
64
|
|
|
64
|
|
24081
|
use PPI::Token::HereDoc (); |
|
64
|
|
|
|
|
142
|
|
|
64
|
|
|
|
|
3131
|
|
69
|
64
|
|
|
64
|
|
20475
|
use PPI::Token::Separator (); |
|
64
|
|
|
|
|
142
|
|
|
64
|
|
|
|
|
2883
|
|
70
|
64
|
|
|
64
|
|
20593
|
use PPI::Token::Data (); |
|
64
|
|
|
|
|
151
|
|
|
64
|
|
|
|
|
3480
|
|
71
|
64
|
|
|
64
|
|
22873
|
use PPI::Token::End (); |
|
64
|
|
|
|
|
149
|
|
|
64
|
|
|
|
|
3181
|
|
72
|
64
|
|
|
64
|
|
22176
|
use PPI::Token::Prototype (); |
|
64
|
|
|
|
|
137
|
|
|
64
|
|
|
|
|
3229
|
|
73
|
64
|
|
|
64
|
|
22013
|
use PPI::Token::Attribute (); |
|
64
|
|
|
|
|
165
|
|
|
64
|
|
|
|
|
3265
|
|
74
|
64
|
|
|
64
|
|
390
|
use PPI::Token::Unknown (); |
|
64
|
|
|
|
|
107
|
|
|
64
|
|
|
|
|
47551
|
|
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
##################################################################### |
81
|
|
|
|
|
|
|
# Constructor and Related |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub new { |
84
|
362879
|
50
|
|
362879
|
0
|
1339522
|
bless { content => (defined $_[1] ? "$_[1]" : '') }, $_[0]; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub set_class { |
88
|
33889
|
|
|
33889
|
0
|
42827
|
my $self = shift; |
89
|
|
|
|
|
|
|
# @_ or throw Exception("No arguments to set_class"); |
90
|
33889
|
50
|
|
|
|
68146
|
my $class = substr( $_[0], 0, 12 ) eq 'PPI::Token::' ? shift : 'PPI::Token::' . shift; |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
# Find out if the current and new classes are complex |
93
|
33889
|
50
|
|
|
|
98977
|
my $old_quote = (ref($self) =~ /\b(?:Quote|Regex)\b/o) ? 1 : 0; |
94
|
33889
|
50
|
|
|
|
68221
|
my $new_quote = ($class =~ /\b(?:Quote|Regex)\b/o) ? 1 : 0; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# No matter what happens, we will have to rebless |
97
|
33889
|
|
|
|
|
47118
|
bless $self, $class; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
# If we are changing to or from a Quote style token, we |
100
|
|
|
|
|
|
|
# can't just rebless and need to do some extra thing |
101
|
|
|
|
|
|
|
# Otherwise, we have done enough |
102
|
33889
|
50
|
|
|
|
94527
|
return $class if ($old_quote - $new_quote) == 0; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
# Make a new token from the old content, and overwrite the current |
105
|
|
|
|
|
|
|
# token's attributes with the new token's attributes. |
106
|
0
|
|
|
|
|
0
|
my $token = $class->new( $self->{content} ); |
107
|
0
|
|
|
|
|
0
|
%$self = %$token; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# Return the class as a convenience |
110
|
0
|
|
|
|
|
0
|
return $class; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
##################################################################### |
118
|
|
|
|
|
|
|
# PPI::Token Methods |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=pod |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head2 set_content $string |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
The C method allows you to set/change the string that the |
125
|
|
|
|
|
|
|
C object represents. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
Returns the string you set the Token to |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=cut |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub set_content { |
132
|
1
|
|
|
1
|
1
|
5
|
$_[0]->{content} = $_[1]; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=pod |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head2 add_content $string |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
The C method allows you to add additional bytes of code |
140
|
|
|
|
|
|
|
to the end of the Token. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
Returns the new full string after the bytes have been added. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=cut |
145
|
|
|
|
|
|
|
|
146
|
1
|
|
|
1
|
1
|
6
|
sub add_content { $_[0]->{content} .= $_[1] } |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=pod |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head2 length |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
The C method returns the length of the string in a Token. |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=cut |
155
|
|
|
|
|
|
|
|
156
|
7
|
|
|
7
|
1
|
123
|
sub length { CORE::length($_[0]->{content}) } |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
##################################################################### |
163
|
|
|
|
|
|
|
# Overloaded PPI::Element methods |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
sub content { |
166
|
1190306
|
|
|
1190306
|
1
|
2769423
|
$_[0]->{content}; |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
# You can insert either a statement, or a non-significant token. |
170
|
|
|
|
|
|
|
sub insert_before { |
171
|
1
|
|
|
1
|
1
|
2
|
my $self = shift; |
172
|
1
|
50
|
|
|
|
9
|
my $Element = _INSTANCE(shift, 'PPI::Element') or return undef; |
173
|
1
|
50
|
|
|
|
8
|
if ( $Element->isa('PPI::Structure') ) { |
|
|
50
|
|
|
|
|
|
174
|
0
|
|
|
|
|
0
|
return $self->__insert_before($Element); |
175
|
|
|
|
|
|
|
} elsif ( $Element->isa('PPI::Token') ) { |
176
|
1
|
|
|
|
|
4
|
return $self->__insert_before($Element); |
177
|
|
|
|
|
|
|
} |
178
|
0
|
|
|
|
|
0
|
''; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
# As above, you can insert a statement, or a non-significant token |
182
|
|
|
|
|
|
|
sub insert_after { |
183
|
1
|
|
|
1
|
1
|
3
|
my $self = shift; |
184
|
1
|
50
|
|
|
|
9
|
my $Element = _INSTANCE(shift, 'PPI::Element') or return undef; |
185
|
1
|
50
|
|
|
|
8
|
if ( $Element->isa('PPI::Structure') ) { |
|
|
50
|
|
|
|
|
|
186
|
0
|
|
|
|
|
0
|
return $self->__insert_after($Element); |
187
|
|
|
|
|
|
|
} elsif ( $Element->isa('PPI::Token') ) { |
188
|
1
|
|
|
|
|
3
|
return $self->__insert_after($Element); |
189
|
|
|
|
|
|
|
} |
190
|
0
|
|
|
|
|
0
|
''; |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
##################################################################### |
198
|
|
|
|
|
|
|
# Tokenizer Methods |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
sub __TOKENIZER__on_line_start() { 1 } |
201
|
|
|
|
|
|
|
sub __TOKENIZER__on_line_end() { 1 } |
202
|
|
|
|
|
|
|
sub __TOKENIZER__on_char() { 'Unknown' } |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
##################################################################### |
209
|
|
|
|
|
|
|
# Lexer Methods |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
sub __LEXER__opens { |
212
|
|
|
|
|
|
|
ref($_[0]) eq 'PPI::Token::Structure' |
213
|
|
|
|
|
|
|
and |
214
|
45980
|
50
|
|
45980
|
|
234247
|
$_[0]->{content} =~ /(?:\(|\[|\{)/ |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
sub __LEXER__closes { |
218
|
|
|
|
|
|
|
ref($_[0]) eq 'PPI::Token::Structure' |
219
|
|
|
|
|
|
|
and |
220
|
186424
|
100
|
|
186424
|
|
972248
|
$_[0]->{content} =~ /(?:\)|\]|\})/ |
221
|
|
|
|
|
|
|
} |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
1; |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=pod |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=head1 SUPPORT |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
See the L in the main module. |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=head1 AUTHOR |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
Adam Kennedy Eadamk@cpan.orgE |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
=head1 COPYRIGHT |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
Copyright 2001 - 2011 Adam Kennedy. |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
This program is free software; you can redistribute |
240
|
|
|
|
|
|
|
it and/or modify it under the same terms as Perl itself. |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
The full text of the license can be found in the |
243
|
|
|
|
|
|
|
LICENSE file included with this module. |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=cut |