MDOM/Token.pm

filter: Statementstmt 63.5% 47 / 74 Branchbran 22.2% 8 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 76.1% 16 / 21 Podpod 88.8% 8 / 9 totaltotal 56.4% 79 / 140 SCAR 43.3
CC
21
Cov
50%
set_class
90.0
insert_before
20.0
insert_after
20.0
CRAP 76.1
« MDOM/Document/Gmake.pm ↑ Summary MDOM/Rule/Simple.pm »
1 package MDOM::Token;
2  
3 =pod
4  
5 =head1 NAME
6  
7 MDOM::Token - A single token of Makefile source code
8  
9 =head1 INHERITANCE
10  
11 MDOM::Token
12 isa MDOM::Element
13  
14 =head1 DESCRIPTION
15  
16 C<MDOM::Token> is the abstract base class for all Tokens. In MDOM terms, a "Token" is
17 a L<MDOM::Element> that directly represents bytes of source code.
18  
19 The implementation and POD are borrowed directly from L<PPI::Token>.
20  
21 =head1 METHODS
22  
23 =cut
24  
25 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 18 ▶use base 'MDOM::Element';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 18 ▶use Params::Util '_INSTANCE';
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 18 ▶use vars qw{$VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
30 BEGIN {
31 18 ▶ $VERSION = '0.007';
Subroutine BEGIN: called CC=1 SCAR=0.0
32 }
33  
34 # We don't load the abstracts, they are loaded
35 # as part of the 'use base' statements.
36  
37 # Load the token classes
38 18 ▶use MDOM::Token::Whitespace ();
Subroutine BEGIN: called CC=1 SCAR=0.0
39 18 ▶use MDOM::Token::Comment ();
Subroutine BEGIN: called CC=1 SCAR=0.0
40 18 ▶use MDOM::Token::Separator ();
Subroutine BEGIN: called CC=1 SCAR=0.0
41 18 ▶use MDOM::Token::Continuation ();
Subroutine BEGIN: called CC=1 SCAR=0.0
42 18 ▶use MDOM::Token::Bare ();
Subroutine BEGIN: called CC=1 SCAR=0.0
43 18 ▶use MDOM::Token::Interpolation ();
Subroutine BEGIN: called CC=1 SCAR=0.0
44 18 ▶use MDOM::Token::Modifier ();
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 #####################################################################
47 # Constructor and Related
48  
49 sub new {
50 858 ▶ if ( @_ == 2 ) {
Subroutine new: called CC=7 SCAR=20.2
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 2) { } 852 6 858
elsif (@_ == 3) { } 6 0 6
51 # MDOM::Token->new( $content );
52 852 my $class;
53 852 ▶ if ($_[0] eq __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] eq __PACKAGE__) { } 4 848 852
54 4 $class = 'MDOM::Token::Bare';
55 4 shift;
56 } else {
57 848 $class = shift;
58 }
59 852 ▶ return bless {
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 852 0 852
60 content => (defined $_[0] ? "$_[0]" : ''),
61 lineno => $.,
62 }, $class;
63 } elsif ( @_ == 3 ) {
64 # MDOM::Token->new( $class, $content );
65 6 ▶ my $class = substr( $_[0], 0, 12 ) eq 'MDOM::Token::' ? $_[1] : "MDOM::Token::$_[1]";
Branch: 1 branch
BranchTrueFalseTotal
substr($_[0], 0, 12) eq 'MDOM::Token::' ? : 0 6 6
66 6 ▶ return bless {
Branch: 1 branch
BranchTrueFalseTotal
defined $_[2] ? : 6 0 6
67 content => (defined $_[2] ? "$_[2]" : ''),
68 lineno => $.,
69 }, $class;
70 }
71  
72 # Invalid argument count
73 0 undef;
74 }
75  
76 =head2 set_class
77  
78 Set a specific class for a token.
79  
80 =cut
81  
82 sub set_class {
83 0 ▶ my $self = shift; @_ or return undef;
Subroutine set_class: not called CC=9 SCAR=45.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ or return undef 0 0 0
84 0 ▶ my $class = substr( $_[0], 0, 12 ) eq 'MDOM::Token::' ? shift : 'MDOM::Token::' . shift;
Branch: 1 branch
BranchTrueFalseTotal
substr($_[0], 0, 12) eq 'MDOM::Token::' ? : 0 0 0
85  
86 # Find out if the current and new classes are complex
87 0 ▶ my $old_quote = (ref($self) =~ /\b(?:Quote|Regex)\b/o) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
ref($self) =~ /\b(?:Quote|Regex)\b/o ? : 0 0 0
88 0 ▶ my $new_quote = ($class =~ /\b(?:Quote|Regex)\b/o) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$class =~ /\b(?:Quote|Regex)\b/o ? : 0 0 0
89  
90 # No matter what happens, we will have to rebless
91 0 bless $self, $class;
92  
93 # If we are changing to or from a Quote style token, we
94 # can't just rebless and need to do some extra thing
95 # Otherwise, we have done enough
96 0 ▶ return 1 if ($old_quote - $new_quote) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $old_quote - $new_quote == 0 0 0 0
97  
98 # Make a new token from the old content, and overwrite the current
99 # token's attributes with the new token's attributes.
100 0 ▶ my $token = $class->new( $self->{content} ) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $token = $class->new($self->{"content"}) or return undef 0 0 0
101 0 delete $self->{$_} foreach keys %$self;
102 0 $self->{$_} = $token->{$_} foreach keys %$token;
103  
104 0 1;
105 }
106  
107  
108  
109 #####################################################################
110 # MDOM::Token Methods
111  
112 =pod
113  
114 =head2 set_content $string
115  
116 The C<set_content> method allows to set/change the string that the
117 C<MDOM::Token> object represents.
118  
119 Returns the string you set the Token to
120  
121 =cut
122  
123 sub set_content {
124 3 ▶ $_[0]->{content} = $_[1];
Subroutine set_content: called CC=1 SCAR=0.0
Pod documented
125 }
126  
127 =pod
128  
129 =head2 add_content $string
130  
131 The C<add_content> method allows you to add additional bytes of code
132 to the end of the Token.
133  
134 Returns the new full string after the bytes have been added.
135  
136 =cut
137  
138 31 ▶sub add_content { $_[0]->{content} .= $_[1] }
Subroutine add_content: called CC=1 SCAR=0.0
Pod documented
139  
140 =pod
141  
142 =head2 length
143  
144 The C<length> method returns the length of the string in a Token.
145  
146 =cut
147  
148 0 ▶sub length { &CORE::length($_[0]->{content}) }
Subroutine length: not called CC=1 SCAR=6.9
Pod documented
149  
150  
151  
152  
153  
154 #####################################################################
155 # Overloaded MDOM::Element methods
156  
157 sub content {
158 2054 ▶ $_[0]->{content};
Subroutine content: called CC=1 SCAR=0.0
Pod documented
159 }
160  
161 # You can insert either a statement, or a non-significant token.
162 sub insert_before {
163 0 ▶ my $self = shift;
Subroutine insert_before: not called CC=4 SCAR=30.0
Pod documented
164 0 ▶ my $Element = _INSTANCE(shift, 'MDOM::Element') or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $Element = &_INSTANCE(shift(), "MDOM::Element") or return undef 0 0 0
165 0 ▶ if ( $Element->isa('MDOM::Structure') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Element->isa('MDOM::Structure')) { } 0 0 0
elsif ($Element->isa('MDOM::Token')) { } 0 0 0
166 0 return $self->__insert_before($Element);
167 } elsif ( $Element->isa('MDOM::Token') ) {
168 0 return $self->__insert_before($Element);
169 }
170 0 '';
171 }
172  
173 # As above, you can insert a statement, or a non-significant token
174 sub insert_after {
175 0 ▶ my $self = shift;
Subroutine insert_after: not called CC=4 SCAR=30.0
Pod documented
176 0 ▶ my $Element = _INSTANCE(shift, 'MDOM::Element') or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $Element = &_INSTANCE(shift(), "MDOM::Element") or return undef 0 0 0
177 0 ▶ if ( $Element->isa('MDOM::Structure') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Element->isa('MDOM::Structure')) { } 0 0 0
elsif ($Element->isa('MDOM::Token')) { } 0 0 0
178 0 return $self->__insert_after($Element);
179 } elsif ( $Element->isa('MDOM::Token') ) {
180 0 return $self->__insert_after($Element);
181 }
182 0 '';
183 }
184  
185 =pod
186  
187 =head2 source
188  
189 Returns the makefile source for the current token
190  
191 =cut
192  
193 sub source {
194 0 ▶ my $self = shift;
Subroutine source: not called CC=1 SCAR=6.9
Pod documented
195 0 return $self->content;
196 }
197  
198 18 1;
« MDOM/Document/Gmake.pm ↑ Summary MDOM/Rule/Simple.pm »