PPI/Token/Symbol.pm

filter: Statementstmt 100% 69 / 69 Branchbran 88.8% 32 / 36 Conditioncond 55.5% 5 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 10 / 10 Podpod 100% 4 / 4 totaltotal 91.7% 122 / 133 CC 22 SCAR 22.3
CC
22
Cov
93%
symbol
13.8
__TOKENIZER__on_char
10.0
BEGIN
1.0
CRAP 9.3
« PPI/Structure.pm ↑ Summary PPI/Token/HereDoc.pm »
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<PPI::Token::Symbol> 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<PPI::Token> and L<PPI::Element> 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 62 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 62 ▶use Params::Util qw{_INSTANCE};
Subroutine BEGIN: called CC=1 SCAR=0.0
32 62 ▶use PPI::Token ();
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 62 ▶use vars qw{$VERSION @ISA};
Subroutine BEGIN: called CC=1 SCAR=0.0
35 BEGIN {
36 62 ▶ $VERSION = '1.236';
Subroutine BEGIN: called CC=1 SCAR=0.0
37 62 @ISA = 'PPI::Token';
38 }
39  
40  
41  
42  
43  
44 #####################################################################
45 # PPI::Token::Symbol Methods
46  
47 =pod
48  
49 =head2 canonical
50  
51 The C<canonical> method returns a normalized, canonical version of the
52 symbol.
53  
54 For example, it converts C<$ ::foo'bar::baz> to C<$main::foo::bar::baz>.
55  
56 This does not fully resolve the symbol, but merely removes syntax
57 variations.
58  
59 =cut
60  
61 sub canonical {
62 69 ▶ my $symbol = shift->content;
Subroutine canonical: called CC=1 SCAR=0.0
Pod documented
63 69 $symbol =~ s/\s+//;
64 69 $symbol =~ s/\'/::/g;
65 69 $symbol =~ s/(?<=[\$\@\%\&\*])::/main::/;
66 69 $symbol;
67 }
68  
69 =pod
70  
71 =head2 symbol
72  
73 The C<symbol> method returns the ACTUAL symbol this token refers to.
74  
75 A token of C<$foo> might actually be referring to C<@foo>, if it is found
76 in the form C<$foo[1]>.
77  
78 This method attempts to resolve these issues to determine the actual
79 symbol.
80  
81 Returns the symbol as a string.
82  
83 =cut
84  
85 62 my %cast_which_trumps_braces = map { $_ => 1 } qw{ $ @ };
86  
87 sub symbol {
88 56 ▶ my $self = shift;
Subroutine symbol: called CC=13 SCAR=26.2
Pod documented
89 56 my $symbol = $self->canonical;
90  
91 # Immediately return the cases where it can't be anything else
92 56 my $type = substr( $symbol, 0, 1 );
93 56 ▶ return $symbol if $type eq '%';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "%" 6 50 56
94 50 ▶ return $symbol if $type eq '&';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "&" 4 46 50
95  
96 # Unless the next significant Element is a structure, it's correct.
97 46 my $after = $self->snext_sibling;
98 46 ▶ return $symbol unless _INSTANCE($after, 'PPI::Structure');
Branch: 1 branch
BranchTrueFalseTotal
unless &_INSTANCE($after, "PPI::Structure") 34 12 46
99  
100 # Process the rest for cases where it might actually be something else
101 12 my $braces = $after->braces;
102 12 ▶ return $symbol unless defined $braces;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $braces 0 12 12
103 12 ▶ if ( $type eq '$' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq '$') { } 6 6 12
elsif ($type eq '@') { } 6 0 6
104  
105 # If it is cast to '$' or '@', that trumps any braces
106 6 my $before = $self->sprevious_sibling;
107 return $symbol if $before &&
108 $before->isa( 'PPI::Token::Cast' ) &&
109 6 ▶ $cast_which_trumps_braces{ $before->content };
Branch: 1 branch
BranchTrueFalseTotal
if $before and $before->isa("PPI::Token::Cast") and $cast_which_trumps_braces{$before->content} 0 6 6
Condition33%2/6
$before and $before->isa("PPI::Token::Cast") and $cast_which_trumps_braces{$before->content}
!ll&&!rl&&r
600
$before and $before->isa("PPI::Token::Cast")
!ll&&!rl&&r
600
MC/DC0%0/3
$before and $before->isa("PPI::Token::Cast") and $cast_which_trumps_braces{$before->content}
$before $before->isa("PPI::Token::Cast") $cast_which_trumps_braces{$before->content}
Truth table25%1/4
A and B and C
A= $beforeB= $before->isa("PPI::Token::Cast")C= $cast_which_trumps_braces{$before->content}
ABCresult
0XX0
10X0
1100
1111
110  
111 # Otherwise the braces rule
112 6 ▶ substr( $symbol, 0, 1, '@' ) if $braces eq '[]';
Branch: 1 branch
BranchTrueFalseTotal
if $braces eq "[]" 3 3 6
113 6 ▶ substr( $symbol, 0, 1, '%' ) if $braces eq '{}';
Branch: 1 branch
BranchTrueFalseTotal
if $braces eq "{}" 3 3 6
114  
115 } elsif ( $type eq '@' ) {
116 6 ▶ substr( $symbol, 0, 1, '%' ) if $braces eq '{}';
Branch: 1 branch
BranchTrueFalseTotal
if $braces eq "{}" 2 4 6
117  
118 }
119  
120 12 $symbol;
121 }
122  
123 =pod
124  
125 =head2 raw_type
126  
127 The C<raw_type> method returns the B<apparent> 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 19 ▶ substr( $_[0]->content, 0, 1 );
Subroutine raw_type: called CC=1 SCAR=0.0
Pod documented
136 }
137  
138 =pod
139  
140 =head2 symbol_type
141  
142 The C<symbol_type> method returns the B<actual> 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 19 ▶ substr( $_[0]->symbol, 0, 1 );
Subroutine symbol_type: called CC=1 SCAR=0.0
Pod documented
151 }
152  
153  
154  
155  
156  
157 #####################################################################
158 # Tokenizer Methods
159  
160 sub __TOKENIZER__on_char {
161 17342 ▶ my $t = $_[1];
Subroutine __TOKENIZER__on_char: called CC=10 SCAR=23.0
162  
163 # Suck in till the end of the symbol
164 17342 pos $t->{line} = $t->{line_cursor};
165 17342 ▶ if ( $t->{line} =~ m/\G([\w:\']+)/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{"line"} =~ /\G([\w:\']+)/cg) 13053 4289 17342
166 13053 $t->{token}->{content} .= $1;
167 13053 $t->{line_cursor} += length $1;
168 }
169  
170 # Handle magic things
171 17342 my $content = $t->{token}->{content};
172 17342 ▶ if ( $content eq '@_' or $content eq '$_' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content eq "\@_" or $content eq "\$_") 1194 16148 17342
Condition100%3/3
$content eq "\@_" or $content eq "\$_"
l!l&&r!l&&!r
20998516148
MC/DC100%2/2
$content eq "\@_" or $content eq "\$_"
$content eq "\@_" $content eq "\$_"
Truth table100%3/3
A or B
A= $content eq "\@_"B= $content eq "\$_"
ABresult
1X1
011
000
173 1194 $t->{class} = $t->{token}->set_class( 'Magic' );
174 1194 return $t->_finalize_token->__TOKENIZER__on_char( $t );
175 }
176  
177 # Shortcut for most of the X:: symbols
178 16148 ▶ if ( $content eq '$::' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content eq "\$::") 20 16128 16148
179 # May well be an alternate form of a Magic
180 20 my $nextchar = substr( $t->{line}, $t->{line_cursor}, 1 );
181 20 ▶ if ( $nextchar eq '|' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nextchar eq "|") 17 3 20
182 17 $t->{token}->{content} .= $nextchar;
183 17 $t->{line_cursor}++;
184 17 $t->{class} = $t->{token}->set_class( 'Magic' );
185 }
186 20 return $t->_finalize_token->__TOKENIZER__on_char( $t );
187 }
188 16128 ▶ if ( $content =~ /^[\$%*@&]::(?:[^\w]|$)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /^[\$%*\@&]::(?:[^\w]|$)/) 53 16075 16128
189 53 my $current = substr( $content, 0, 3, '' );
190 53 $t->{token}->{content} = $current;
191 53 $t->{line_cursor} -= length( $content );
192 53 return $t->_finalize_token->__TOKENIZER__on_char( $t );
193 }
194 16075 ▶ if ( $content =~ /^(?:\$|\@)\d+/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /^(?:\$|\@)\d+/) 48 16027 16075
195 48 $t->{class} = $t->{token}->set_class( 'Magic' );
196 48 return $t->_finalize_token->__TOKENIZER__on_char( $t );
197 }
198  
199 # Trim off anything we oversucked...
200 16027 ▶ $content =~ /^(
Branch: 1 branch
BranchTrueFalseTotal
$content =~ /^( [\$\@%&*] (?: : (?!:) | # Allow single-colon non-magic vars (?: \w+ | \' (?!\d) \w+ | \:: \w+ ) (?: # Allow both :: and ' in namespace separators (?: \' (?!\d) \w+ | \:: \w+ ) )* (?: :: )? # Technically a compiler-magic hash, but keep it here ) )/x or return undef 0 16027 16027
201 [\$@%&*]
202 (?: : (?!:) | # Allow single-colon non-magic vars
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 16027 ▶ unless ( length $1 eq length $content ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $1 eq length $content) 60 15967 16027
212 60 $t->{line_cursor} += length($1) - length($content);
213 60 $t->{token}->{content} = $1;
214 }
215  
216 16027 $t->_finalize_token->__TOKENIZER__on_char( $t );
217 }
218  
219 62 1;
220  
221 =pod
222  
223 =head1 SUPPORT
224  
225 See the L<support section|PPI/SUPPORT> in the main module.
226  
227 =head1 AUTHOR
228  
229 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
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
« PPI/Structure.pm ↑ Summary PPI/Token/HereDoc.pm »