PPI/Token.pm

filter: Statementstmt 95.6% 153 / 160 Branchbran 53.8% 14 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 53 / 53 Podpod 75.0% 6 / 8 totaltotal 91.5% 226 / 247 CC 14 SCAR 7.1
CC
14
Cov
90%
set_class
6.7
insert_before
5.2
insert_after
5.2
CRAP 2.0
« PPI/Token/Number/Hex.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 63 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 63 ▶use Params::Util qw{_INSTANCE};
Subroutine BEGIN: called CC=1 SCAR=0.0
25 63 ▶use PPI::Element ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 63 ▶use PPI::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 63 our $VERSION = '1.260'; # VERSION
29  
30 63 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 63 ▶use PPI::Token::BOM ();
Subroutine BEGIN: called CC=1 SCAR=0.0
37 63 ▶use PPI::Token::Whitespace ();
Subroutine BEGIN: called CC=1 SCAR=0.0
38 63 ▶use PPI::Token::Comment ();
Subroutine BEGIN: called CC=1 SCAR=0.0
39 63 ▶use PPI::Token::Pod ();
Subroutine BEGIN: called CC=1 SCAR=0.0
40 63 ▶use PPI::Token::Number ();
Subroutine BEGIN: called CC=1 SCAR=0.0
41 63 ▶use PPI::Token::Number::Binary ();
Subroutine BEGIN: called CC=1 SCAR=0.0
42 63 ▶use PPI::Token::Number::Octal ();
Subroutine BEGIN: called CC=1 SCAR=0.0
43 63 ▶use PPI::Token::Number::Hex ();
Subroutine BEGIN: called CC=1 SCAR=0.0
44 63 ▶use PPI::Token::Number::Float ();
Subroutine BEGIN: called CC=1 SCAR=0.0
45 63 ▶use PPI::Token::Number::Exp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
46 63 ▶use PPI::Token::Number::Version ();
Subroutine BEGIN: called CC=1 SCAR=0.0
47 63 ▶use PPI::Token::Word ();
Subroutine BEGIN: called CC=1 SCAR=0.0
48 63 ▶use PPI::Token::DashedWord ();
Subroutine BEGIN: called CC=1 SCAR=0.0
49 63 ▶use PPI::Token::Symbol ();
Subroutine BEGIN: called CC=1 SCAR=0.0
50 63 ▶use PPI::Token::ArrayIndex ();
Subroutine BEGIN: called CC=1 SCAR=0.0
51 63 ▶use PPI::Token::Magic ();
Subroutine BEGIN: called CC=1 SCAR=0.0
52 63 ▶use PPI::Token::Quote::Single ();
Subroutine BEGIN: called CC=1 SCAR=0.0
53 63 ▶use PPI::Token::Quote::Double ();
Subroutine BEGIN: called CC=1 SCAR=0.0
54 63 ▶use PPI::Token::Quote::Literal ();
Subroutine BEGIN: called CC=1 SCAR=0.0
55 63 ▶use PPI::Token::Quote::Interpolate ();
Subroutine BEGIN: called CC=1 SCAR=0.0
56 63 ▶use PPI::Token::QuoteLike::Backtick ();
Subroutine BEGIN: called CC=1 SCAR=0.0
57 63 ▶use PPI::Token::QuoteLike::Command ();
Subroutine BEGIN: called CC=1 SCAR=0.0
58 63 ▶use PPI::Token::QuoteLike::Regexp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
59 63 ▶use PPI::Token::QuoteLike::Words ();
Subroutine BEGIN: called CC=1 SCAR=0.0
60 63 ▶use PPI::Token::QuoteLike::Readline ();
Subroutine BEGIN: called CC=1 SCAR=0.0
61 63 ▶use PPI::Token::Regexp::Match ();
Subroutine BEGIN: called CC=1 SCAR=0.0
62 63 ▶use PPI::Token::Regexp::Substitute ();
Subroutine BEGIN: called CC=1 SCAR=0.0
63 63 ▶use PPI::Token::Regexp::Transliterate ();
Subroutine BEGIN: called CC=1 SCAR=0.0
64 63 ▶use PPI::Token::Operator ();
Subroutine BEGIN: called CC=1 SCAR=0.0
65 63 ▶use PPI::Token::Cast ();
Subroutine BEGIN: called CC=1 SCAR=0.0
66 63 ▶use PPI::Token::Structure ();
Subroutine BEGIN: called CC=1 SCAR=0.0
67 63 ▶use PPI::Token::Label ();
Subroutine BEGIN: called CC=1 SCAR=0.0
68 63 ▶use PPI::Token::HereDoc ();
Subroutine BEGIN: called CC=1 SCAR=0.0
69 63 ▶use PPI::Token::Separator ();
Subroutine BEGIN: called CC=1 SCAR=0.0
70 63 ▶use PPI::Token::Data ();
Subroutine BEGIN: called CC=1 SCAR=0.0
71 63 ▶use PPI::Token::End ();
Subroutine BEGIN: called CC=1 SCAR=0.0
72 63 ▶use PPI::Token::Prototype ();
Subroutine BEGIN: called CC=1 SCAR=0.0
73 63 ▶use PPI::Token::Attribute ();
Subroutine BEGIN: called CC=1 SCAR=0.0
74 63 ▶use PPI::Token::Unknown ();
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76  
77  
78  
79  
80 #####################################################################
81 # Constructor and Related
82  
83 sub new {
84 349144 ▶ bless { content => (defined $_[1] ? "$_[1]" : '') }, $_[0];
Subroutine new: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 349144 0 349144
85 }
86  
87 sub set_class {
88 31685 ▶ my $self = shift;
Subroutine set_class: called CC=5 SCAR=19.1
Pod undocumented
89 # @_ or throw Exception("No arguments to set_class");
90 31685 ▶ 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 31685 31685
91  
92 # Find out if the current and new classes are complex
93 31685 ▶ my $old_quote = (ref($self) =~ /\b(?:Quote|Regex)\b/o) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
ref($self) =~ /\b(?:Quote|Regex)\b/o ? : 0 31685 31685
94 31685 ▶ my $new_quote = ($class =~ /\b(?:Quote|Regex)\b/o) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$class =~ /\b(?:Quote|Regex)\b/o ? : 0 31685 31685
95  
96 # No matter what happens, we will have to rebless
97 31685 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 31685 ▶ return $class if ($old_quote - $new_quote) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $old_quote - $new_quote == 0 31685 0 31685
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 my $token = $class->new( $self->{content} );
107 0 %$self = %$token;
108  
109 # Return the class as a convenience
110 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<set_content> method allows you to set/change the string that the
125 C<PPI::Token> object represents.
126  
127 Returns the string you set the Token to
128  
129 =cut
130  
131 sub set_content {
132 1 ▶ $_[0]->{content} = $_[1];
Subroutine set_content: called CC=1 SCAR=0.0
Pod documented
133 }
134  
135 =pod
136  
137 =head2 add_content $string
138  
139 The C<add_content> 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 ▶sub add_content { $_[0]->{content} .= $_[1] }
Subroutine add_content: called CC=1 SCAR=0.0
Pod documented
147  
148 =pod
149  
150 =head2 length
151  
152 The C<length> method returns the length of the string in a Token.
153  
154 =cut
155  
156 7 ▶sub length { CORE::length($_[0]->{content}) }
Subroutine length: called CC=1 SCAR=0.0
Pod documented
157  
158  
159  
160  
161  
162 #####################################################################
163 # Overloaded PPI::Element methods
164  
165 sub content {
166 1164320 ▶ $_[0]->{content};
Subroutine content: called CC=1 SCAR=0.0
Pod documented
167 }
168  
169 # You can insert either a statement, or a non-significant token.
170 sub insert_before {
171 1 ▶ my $self = shift;
Subroutine insert_before: called CC=4 SCAR=16.4
Pod documented
172 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
173 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
174 0 return $self->__insert_before($Element);
175 } elsif ( $Element->isa('PPI::Token') ) {
176 1 return $self->__insert_before($Element);
177 }
178 0 '';
179 }
180  
181 # As above, you can insert a statement, or a non-significant token
182 sub insert_after {
183 1 ▶ my $self = shift;
Subroutine insert_after: called CC=4 SCAR=16.4
Pod documented
184 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
185 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
186 0 return $self->__insert_after($Element);
187 } elsif ( $Element->isa('PPI::Token') ) {
188 1 return $self->__insert_after($Element);
189 }
190 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 44630 ▶ $_[0]->{content} =~ /(?:\(|\[|\{)/
Subroutine __LEXER__opens: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq "PPI::Token::Structure" and $_[0]{"content"} =~ /(?:\(|\[|\{)/ 44630 0 44630
215 }
216  
217 sub __LEXER__closes {
218 ref($_[0]) eq 'PPI::Token::Structure'
219 and
220 176129 ▶ $_[0]->{content} =~ /(?:\)|\]|\})/
Subroutine __LEXER__closes: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq "PPI::Token::Structure" and $_[0]{"content"} =~ /(?:\)|\]|\})/ 82853 93276 176129
221 }
222  
223 63 1;
224  
225 =pod
226  
227 =head1 SUPPORT
228  
229 See the L<support section|PPI/SUPPORT> in the main module.
230  
231 =head1 AUTHOR
232  
233 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
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
« PPI/Token/Number/Hex.pm ↑ Summary PPI/XSAccessor.pm »