line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package PPI::Token::Symbol; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=pod |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 NAME |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
PPI::Token::Symbol - A token class for variables and other symbols |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 INHERITANCE |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
PPI::Token::Symbol |
12
|
|
|
|
|
|
|
isa PPI::Token |
13
|
|
|
|
|
|
|
isa PPI::Element |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 DESCRIPTION |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
The C class is used to cover all tokens that represent |
18
|
|
|
|
|
|
|
variables and other things that start with a sigil. |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 METHODS |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
This class has several methods beyond what is provided by its |
23
|
|
|
|
|
|
|
L and L parent classes. |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
Most methods are provided to help work out what the object is actually |
26
|
|
|
|
|
|
|
pointing at, rather than what it might appear to be pointing at. |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=cut |
29
|
|
|
|
|
|
|
|
30
|
65
|
|
|
65
|
|
371
|
use strict; |
|
65
|
|
|
|
|
105
|
|
|
65
|
|
|
|
|
1759
|
|
31
|
65
|
|
|
65
|
|
288
|
use Params::Util qw{_INSTANCE}; |
|
65
|
|
|
|
|
113
|
|
|
65
|
|
|
|
|
2631
|
|
32
|
65
|
|
|
65
|
|
292
|
use PPI::Token (); |
|
65
|
|
|
|
|
112
|
|
|
65
|
|
|
|
|
51875
|
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
our $VERSION = '1.276'; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
our @ISA = "PPI::Token"; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
##################################################################### |
43
|
|
|
|
|
|
|
# PPI::Token::Symbol Methods |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=pod |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head2 canonical |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
The C method returns a normalized, canonical version of the |
50
|
|
|
|
|
|
|
symbol. |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
For example, it converts C<$ ::foo'bar::baz> to C<$main::foo::bar::baz>. |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
This does not fully resolve the symbol, but merely removes syntax |
55
|
|
|
|
|
|
|
variations. |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=cut |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub canonical { |
60
|
96
|
|
|
96
|
1
|
204
|
my $symbol = shift->content; |
61
|
96
|
|
|
|
|
234
|
$symbol =~ s/\s+//; |
62
|
96
|
|
|
|
|
169
|
$symbol =~ s/\'/::/g; |
63
|
96
|
|
|
|
|
163
|
$symbol =~ s/(?<=[\$\@\%\&\*])::/main::/; |
64
|
96
|
|
|
|
|
275
|
$symbol; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=pod |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=head2 symbol |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
The C method returns the ACTUAL symbol this token refers to. |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
A token of C<$foo> might actually be referring to C<@foo>, if it is found |
74
|
|
|
|
|
|
|
in the form C<$foo[1]>. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
This method attempts to resolve these issues to determine the actual |
77
|
|
|
|
|
|
|
symbol. |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
Returns the symbol as a string. |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=cut |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
my %cast_which_trumps_braces = map { $_ => 1 } qw{ $ @ % }; |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub symbol { |
86
|
74
|
|
|
74
|
1
|
5271
|
my $self = shift; |
87
|
74
|
|
|
|
|
153
|
my $symbol = $self->canonical; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# Immediately return the cases where it can't be anything else |
90
|
74
|
|
|
|
|
126
|
my $type = substr( $symbol, 0, 1 ); |
91
|
74
|
100
|
|
|
|
161
|
return $symbol if $type eq '&'; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
# Unless the next significant Element is a structure, it's correct. |
94
|
70
|
|
|
|
|
160
|
my $after = $self->snext_sibling; |
95
|
70
|
100
|
|
|
|
440
|
return $symbol unless _INSTANCE($after, 'PPI::Structure'); |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# Process the rest for cases where it might actually be something else |
98
|
30
|
|
|
|
|
96
|
my $braces = $after->braces; |
99
|
30
|
50
|
|
|
|
63
|
return $symbol unless defined $braces; |
100
|
30
|
100
|
|
|
|
61
|
if ( $type eq '$' ) { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# If it is cast to '$' or '@', that trumps any braces |
103
|
18
|
|
|
|
|
38
|
my $before = $self->sprevious_sibling; |
104
|
|
|
|
|
|
|
return $symbol if $before && |
105
|
|
|
|
|
|
|
$before->isa( 'PPI::Token::Cast' ) && |
106
|
18
|
50
|
66
|
|
|
82
|
$cast_which_trumps_braces{ $before->content }; |
|
|
|
66
|
|
|
|
|
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
# Otherwise the braces rule |
109
|
6
|
100
|
|
|
|
20
|
substr( $symbol, 0, 1, '@' ) if $braces eq '[]'; |
110
|
6
|
100
|
|
|
|
15
|
substr( $symbol, 0, 1, '%' ) if $braces eq '{}'; |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
} elsif ( $type eq '@' ) { |
113
|
6
|
100
|
|
|
|
15
|
substr( $symbol, 0, 1, '%' ) if $braces eq '{}'; |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
} elsif ( $type eq '%' ) { |
116
|
6
|
100
|
|
|
|
13
|
substr( $symbol, 0, 1, '@' ) if $braces eq '[]'; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
18
|
|
|
|
|
72
|
$symbol; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=pod |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head2 raw_type |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
The C method returns the B type of the symbol in the |
128
|
|
|
|
|
|
|
form of its sigil. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
Returns the sigil as a string. |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=cut |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub raw_type { |
135
|
28
|
|
|
28
|
1
|
14850
|
substr( $_[0]->content, 0, 1 ); |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=pod |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head2 symbol_type |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
The C method returns the B type of the symbol in the |
143
|
|
|
|
|
|
|
form of its sigil. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
Returns the sigil as a string. |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=cut |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
sub symbol_type { |
150
|
28
|
|
|
28
|
1
|
72
|
substr( $_[0]->symbol, 0, 1 ); |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
##################################################################### |
158
|
|
|
|
|
|
|
# Tokenizer Methods |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub __TOKENIZER__on_char { |
161
|
18955
|
|
|
18955
|
|
23373
|
my $t = $_[1]; |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
# Suck in till the end of the symbol |
164
|
18955
|
|
|
|
|
40974
|
pos $t->{line} = $t->{line_cursor}; |
165
|
18955
|
100
|
|
|
|
67109
|
if ( $t->{line} =~ m/\G([\w:\']+)/gc ) { |
166
|
14153
|
|
|
|
|
30090
|
$t->{token}->{content} .= $1; |
167
|
14153
|
|
|
|
|
22494
|
$t->{line_cursor} += length $1; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
# Handle magic things |
171
|
18955
|
|
|
|
|
28702
|
my $content = $t->{token}->{content}; |
172
|
18955
|
100
|
100
|
|
|
54189
|
if ( $content eq '@_' or $content eq '$_' ) { |
173
|
1228
|
|
|
|
|
3090
|
$t->{class} = $t->{token}->set_class( 'Magic' ); |
174
|
1228
|
|
|
|
|
2741
|
return $t->_finalize_token->__TOKENIZER__on_char( $t ); |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
# Shortcut for most of the X:: symbols |
178
|
17727
|
100
|
|
|
|
28117
|
if ( $content eq '$::' ) { |
179
|
|
|
|
|
|
|
# May well be an alternate form of a Magic |
180
|
20
|
|
|
|
|
43
|
my $nextchar = substr( $t->{line}, $t->{line_cursor}, 1 ); |
181
|
20
|
100
|
|
|
|
43
|
if ( $nextchar eq '|' ) { |
182
|
18
|
|
|
|
|
28
|
$t->{token}->{content} .= $nextchar; |
183
|
18
|
|
|
|
|
28
|
$t->{line_cursor}++; |
184
|
18
|
|
|
|
|
49
|
$t->{class} = $t->{token}->set_class( 'Magic' ); |
185
|
|
|
|
|
|
|
} |
186
|
20
|
|
|
|
|
51
|
return $t->_finalize_token->__TOKENIZER__on_char( $t ); |
187
|
|
|
|
|
|
|
} |
188
|
17707
|
100
|
|
|
|
30160
|
if ( $content =~ /^[\$%*@&]::(?:[^\w]|$)/ ) { |
189
|
52
|
|
|
|
|
139
|
my $current = substr( $content, 0, 3, '' ); |
190
|
52
|
|
|
|
|
94
|
$t->{token}->{content} = $current; |
191
|
52
|
|
|
|
|
70
|
$t->{line_cursor} -= length( $content ); |
192
|
52
|
|
|
|
|
107
|
return $t->_finalize_token->__TOKENIZER__on_char( $t ); |
193
|
|
|
|
|
|
|
} |
194
|
17655
|
100
|
|
|
|
40346
|
if ( $content =~ /^(?:\$|\@)\d+/ ) { |
195
|
59
|
|
|
|
|
238
|
$t->{class} = $t->{token}->set_class( 'Magic' ); |
196
|
59
|
|
|
|
|
201
|
return $t->_finalize_token->__TOKENIZER__on_char( $t ); |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
# Trim off anything we oversucked... |
200
|
17596
|
50
|
|
|
|
57440
|
$content =~ /^( |
201
|
|
|
|
|
|
|
[\$@%&*] |
202
|
|
|
|
|
|
|
(?: : (?!:) | # Allow single-colon non-magic variables |
203
|
|
|
|
|
|
|
(?: \w+ | \' (?!\d) \w+ | \:: \w+ ) |
204
|
|
|
|
|
|
|
(?: |
205
|
|
|
|
|
|
|
# Allow both :: and ' in namespace separators |
206
|
|
|
|
|
|
|
(?: \' (?!\d) \w+ | \:: \w+ ) |
207
|
|
|
|
|
|
|
)* |
208
|
|
|
|
|
|
|
(?: :: )? # Technically a compiler-magic hash, but keep it here |
209
|
|
|
|
|
|
|
) |
210
|
|
|
|
|
|
|
)/x or return undef; |
211
|
17596
|
100
|
|
|
|
43617
|
unless ( length $1 eq length $content ) { |
212
|
68
|
|
|
|
|
200
|
$t->{line_cursor} += length($1) - length($content); |
213
|
68
|
|
|
|
|
157
|
$t->{token}->{content} = $1; |
214
|
|
|
|
|
|
|
} |
215
|
|
|
|
|
|
|
|
216
|
17596
|
|
|
|
|
33994
|
$t->_finalize_token->__TOKENIZER__on_char( $t ); |
217
|
|
|
|
|
|
|
} |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
1; |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=pod |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=head1 SUPPORT |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
See the L in the main module. |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=head1 AUTHOR |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
Adam Kennedy Eadamk@cpan.orgE |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=head1 COPYRIGHT |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
Copyright 2001 - 2011 Adam Kennedy. |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
This program is free software; you can redistribute |
236
|
|
|
|
|
|
|
it and/or modify it under the same terms as Perl itself. |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
The full text of the license can be found in the |
239
|
|
|
|
|
|
|
LICENSE file included with this module. |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
=cut |