PPI/Token.pm

filter: Statementstmt 95.7% 156 / 163 Branchbran 53.8% 14 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 55 / 55 Podpod 75.0% 6 / 8 totaltotal 91.6% 231 / 252 CC 14 SCAR 7.0
CC
14
Cov
90%
set_class
6.7
insert_before
5.2
insert_after
5.2
CRAP 2.0
« PPI/Token/End.pm ↑ Summary PPI/XSAccessor.pm »
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<PPI::Token> is the abstract base class for all Tokens. In PPI terms, a "Token" is
17 a L<PPI::Element> that directly represents bytes of source code.
18  
19 =head1 METHODS
20  
21 =cut
22  
23 62 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 62 ▶use Params::Util qw{_INSTANCE};
Subroutine BEGIN: called CC=1 SCAR=0.0
25 62 ▶use PPI::Element ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 62 ▶use PPI::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 62 ▶use vars qw{$VERSION @ISA};
Subroutine BEGIN: called CC=1 SCAR=0.0
29 BEGIN {
30 62 ▶ $VERSION = '1.236';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 62 @ISA = 'PPI::Element';
32 }
33  
34 # We don't load the abstracts, they are loaded
35 # as part of the inheritance process.
36  
37 # Load the token classes
38 62 ▶use PPI::Token::BOM ();
Subroutine BEGIN: called CC=1 SCAR=0.0
39 62 ▶use PPI::Token::Whitespace ();
Subroutine BEGIN: called CC=1 SCAR=0.0
40 62 ▶use PPI::Token::Comment ();
Subroutine BEGIN: called CC=1 SCAR=0.0
41 62 ▶use PPI::Token::Pod ();
Subroutine BEGIN: called CC=1 SCAR=0.0
42 62 ▶use PPI::Token::Number ();
Subroutine BEGIN: called CC=1 SCAR=0.0
43 62 ▶use PPI::Token::Number::Binary ();
Subroutine BEGIN: called CC=1 SCAR=0.0
44 62 ▶use PPI::Token::Number::Octal ();
Subroutine BEGIN: called CC=1 SCAR=0.0
45 62 ▶use PPI::Token::Number::Hex ();
Subroutine BEGIN: called CC=1 SCAR=0.0
46 62 ▶use PPI::Token::Number::Float ();
Subroutine BEGIN: called CC=1 SCAR=0.0
47 62 ▶use PPI::Token::Number::Exp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
48 62 ▶use PPI::Token::Number::Version ();
Subroutine BEGIN: called CC=1 SCAR=0.0
49 62 ▶use PPI::Token::Word ();
Subroutine BEGIN: called CC=1 SCAR=0.0
50 62 ▶use PPI::Token::DashedWord ();
Subroutine BEGIN: called CC=1 SCAR=0.0
51 62 ▶use PPI::Token::Symbol ();
Subroutine BEGIN: called CC=1 SCAR=0.0
52 62 ▶use PPI::Token::ArrayIndex ();
Subroutine BEGIN: called CC=1 SCAR=0.0
53 62 ▶use PPI::Token::Magic ();
Subroutine BEGIN: called CC=1 SCAR=0.0
54 62 ▶use PPI::Token::Quote::Single ();
Subroutine BEGIN: called CC=1 SCAR=0.0
55 62 ▶use PPI::Token::Quote::Double ();
Subroutine BEGIN: called CC=1 SCAR=0.0
56 62 ▶use PPI::Token::Quote::Literal ();
Subroutine BEGIN: called CC=1 SCAR=0.0
57 62 ▶use PPI::Token::Quote::Interpolate ();
Subroutine BEGIN: called CC=1 SCAR=0.0
58 62 ▶use PPI::Token::QuoteLike::Backtick ();
Subroutine BEGIN: called CC=1 SCAR=0.0
59 62 ▶use PPI::Token::QuoteLike::Command ();
Subroutine BEGIN: called CC=1 SCAR=0.0
60 62 ▶use PPI::Token::QuoteLike::Regexp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
61 62 ▶use PPI::Token::QuoteLike::Words ();
Subroutine BEGIN: called CC=1 SCAR=0.0
62 62 ▶use PPI::Token::QuoteLike::Readline ();
Subroutine BEGIN: called CC=1 SCAR=0.0
63 62 ▶use PPI::Token::Regexp::Match ();
Subroutine BEGIN: called CC=1 SCAR=0.0
64 62 ▶use PPI::Token::Regexp::Substitute ();
Subroutine BEGIN: called CC=1 SCAR=0.0
65 62 ▶use PPI::Token::Regexp::Transliterate ();
Subroutine BEGIN: called CC=1 SCAR=0.0
66 62 ▶use PPI::Token::Operator ();
Subroutine BEGIN: called CC=1 SCAR=0.0
67 62 ▶use PPI::Token::Cast ();
Subroutine BEGIN: called CC=1 SCAR=0.0
68 62 ▶use PPI::Token::Structure ();
Subroutine BEGIN: called CC=1 SCAR=0.0
69 62 ▶use PPI::Token::Label ();
Subroutine BEGIN: called CC=1 SCAR=0.0
70 62 ▶use PPI::Token::HereDoc ();
Subroutine BEGIN: called CC=1 SCAR=0.0
71 62 ▶use PPI::Token::Separator ();
Subroutine BEGIN: called CC=1 SCAR=0.0
72 62 ▶use PPI::Token::Data ();
Subroutine BEGIN: called CC=1 SCAR=0.0
73 62 ▶use PPI::Token::End ();
Subroutine BEGIN: called CC=1 SCAR=0.0
74 62 ▶use PPI::Token::Prototype ();
Subroutine BEGIN: called CC=1 SCAR=0.0
75 62 ▶use PPI::Token::Attribute ();
Subroutine BEGIN: called CC=1 SCAR=0.0
76 62 ▶use PPI::Token::Unknown ();
Subroutine BEGIN: called CC=1 SCAR=0.0
77  
78  
79  
80  
81  
82 #####################################################################
83 # Constructor and Related
84  
85 sub new {
86 337225 ▶ bless { content => (defined $_[1] ? "$_[1]" : '') }, $_[0];
Subroutine new: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 337225 0 337225
87 }
88  
89 sub set_class {
90 29435 ▶ my $self = shift;
Subroutine set_class: called CC=5 SCAR=19.1
Pod undocumented
91 # @_ or throw Exception("No arguments to set_class");
92 29435 ▶ my $class = substr( $_[0], 0, 12 ) eq 'PPI::Token::' ? shift : 'PPI::Token::' . shift;
Branch: 1 branch
BranchTrueFalseTotal
substr($_[0], 0, 12) eq 'PPI::Token::' ? : 0 29435 29435
93  
94 # Find out if the current and new classes are complex
95 29435 ▶ my $old_quote = (ref($self) =~ /\b(?:Quote|Regex)\b/o) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
ref($self) =~ /\b(?:Quote|Regex)\b/o ? : 0 29435 29435
96 29435 ▶ my $new_quote = ($class =~ /\b(?:Quote|Regex)\b/o) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$class =~ /\b(?:Quote|Regex)\b/o ? : 0 29435 29435
97  
98 # No matter what happens, we will have to rebless
99 29435 bless $self, $class;
100  
101 # If we are changing to or from a Quote style token, we
102 # can't just rebless and need to do some extra thing
103 # Otherwise, we have done enough
104 29435 ▶ return $class if ($old_quote - $new_quote) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $old_quote - $new_quote == 0 29435 0 29435
105  
106 # Make a new token from the old content, and overwrite the current
107 # token's attributes with the new token's attributes.
108 0 my $token = $class->new( $self->{content} );
109 0 %$self = %$token;
110  
111 # Return the class as a convenience
112 0 return $class;
113 }
114  
115  
116  
117  
118  
119 #####################################################################
120 # PPI::Token Methods
121  
122 =pod
123  
124 =head2 set_content $string
125  
126 The C<set_content> method allows you to set/change the string that the
127 C<PPI::Token> object represents.
128  
129 Returns the string you set the Token to
130  
131 =cut
132  
133 sub set_content {
134 1 ▶ $_[0]->{content} = $_[1];
Subroutine set_content: called CC=1 SCAR=0.0
Pod documented
135 }
136  
137 =pod
138  
139 =head2 add_content $string
140  
141 The C<add_content> method allows you to add additional bytes of code
142 to the end of the Token.
143  
144 Returns the new full string after the bytes have been added.
145  
146 =cut
147  
148 1 ▶sub add_content { $_[0]->{content} .= $_[1] }
Subroutine add_content: called CC=1 SCAR=0.0
Pod documented
149  
150 =pod
151  
152 =head2 length
153  
154 The C<length> method returns the length of the string in a Token.
155  
156 =cut
157  
158 7 ▶sub length { CORE::length($_[0]->{content}) }
Subroutine length: called CC=1 SCAR=0.0
Pod documented
159  
160  
161  
162  
163  
164 #####################################################################
165 # Overloaded PPI::Element methods
166  
167 sub content {
168 1107546 ▶ $_[0]->{content};
Subroutine content: called CC=1 SCAR=0.0
Pod documented
169 }
170  
171 # You can insert either a statement, or a non-significant token.
172 sub insert_before {
173 1 ▶ my $self = shift;
Subroutine insert_before: called CC=4 SCAR=16.4
Pod documented
174 1 ▶ my $Element = _INSTANCE(shift, 'PPI::Element') or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $Element = &_INSTANCE(shift(), "PPI::Element") or return undef 0 1 1
175 1 ▶ if ( $Element->isa('PPI::Structure') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Element->isa('PPI::Structure')) { } 0 1 1
elsif ($Element->isa('PPI::Token')) { } 1 0 1
176 0 return $self->__insert_before($Element);
177 } elsif ( $Element->isa('PPI::Token') ) {
178 1 return $self->__insert_before($Element);
179 }
180 0 '';
181 }
182  
183 # As above, you can insert a statement, or a non-significant token
184 sub insert_after {
185 1 ▶ my $self = shift;
Subroutine insert_after: called CC=4 SCAR=16.4
Pod documented
186 1 ▶ my $Element = _INSTANCE(shift, 'PPI::Element') or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $Element = &_INSTANCE(shift(), "PPI::Element") or return undef 0 1 1
187 1 ▶ if ( $Element->isa('PPI::Structure') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Element->isa('PPI::Structure')) { } 0 1 1
elsif ($Element->isa('PPI::Token')) { } 1 0 1
188 0 return $self->__insert_after($Element);
189 } elsif ( $Element->isa('PPI::Token') ) {
190 1 return $self->__insert_after($Element);
191 }
192 0 '';
193 }
194  
195  
196  
197  
198  
199 #####################################################################
200 # Tokenizer Methods
201  
202 sub __TOKENIZER__on_line_start() { 1 }
203 sub __TOKENIZER__on_line_end() { 1 }
204 sub __TOKENIZER__on_char() { 'Unknown' }
205  
206  
207  
208  
209  
210 #####################################################################
211 # Lexer Methods
212  
213 sub __LEXER__opens {
214 ref($_[0]) eq 'PPI::Token::Structure'
215 and
216 43681 ▶ $_[0]->{content} =~ /(?:\(|\[|\{)/
Subroutine __LEXER__opens: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq "PPI::Token::Structure" and $_[0]{"content"} =~ /(?:\(|\[|\{)/ 43681 0 43681
217 }
218  
219 sub __LEXER__closes {
220 ref($_[0]) eq 'PPI::Token::Structure'
221 and
222 169924 ▶ $_[0]->{content} =~ /(?:\)|\]|\})/
Subroutine __LEXER__closes: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq "PPI::Token::Structure" and $_[0]{"content"} =~ /(?:\)|\]|\})/ 80390 89534 169924
223 }
224  
225 62 1;
226  
227 =pod
228  
229 =head1 SUPPORT
230  
231 See the L<support section|PPI/SUPPORT> in the main module.
232  
233 =head1 AUTHOR
234  
235 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
236  
237 =head1 COPYRIGHT
238  
239 Copyright 2001 - 2011 Adam Kennedy.
240  
241 This program is free software; you can redistribute
242 it and/or modify it under the same terms as Perl itself.
243  
244 The full text of the license can be found in the
245 LICENSE file included with this module.
246  
247 =cut
« PPI/Token/End.pm ↑ Summary PPI/XSAccessor.pm »