| 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.006'; | ||||||||||||
|
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
| |||||||||||||||
| 51 | # MDOM::Token->new( $content ); | ||||||||||||||
| 52 | 852 | my $class; | |||||||||||||
| 53 | 852 | ▶ | if ($_[0] eq __PACKAGE__) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 54 | 4 | $class = 'MDOM::Token::Bare'; | |||||||||||||
| 55 | 4 | shift; | |||||||||||||
| 56 | } else { | ||||||||||||||
| 57 | 848 | $class = shift; | |||||||||||||
| 58 | } | ||||||||||||||
| 59 | 852 | ▶ | return bless { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 66 | 6 | ▶ | return bless { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 67 | content => (defined $_[2] ? "$_[2]" : ''), | ||||||||||||||
| 68 | lineno => $., | ||||||||||||||
| 69 | }, $class; | ||||||||||||||
| 70 | } | ||||||||||||||
| 71 | |||||||||||||||
| 72 | # Invalid argument count | ||||||||||||||
| 73 | 0 | undef; | |||||||||||||
| 74 | } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | sub set_class { | ||||||||||||||
| 77 | 0 | ▶ | my $self = shift; @_ or return undef; | ||||||||||||
|
Subroutine
set_class: not called CC=9 SCAR=45.0
Pod
undocumented
Branch: 1 branch
| |||||||||||||||
| 78 | 0 | ▶ | my $class = substr( $_[0], 0, 12 ) eq 'MDOM::Token::' ? shift : 'MDOM::Token::' . shift; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 79 | |||||||||||||||
| 80 | # Find out if the current and new classes are complex | ||||||||||||||
| 81 | 0 | ▶ | my $old_quote = (ref($self) =~ /\b(?:Quote|Regex)\b/o) ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 82 | 0 | ▶ | my $new_quote = ($class =~ /\b(?:Quote|Regex)\b/o) ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 83 | |||||||||||||||
| 84 | # No matter what happens, we will have to rebless | ||||||||||||||
| 85 | 0 | bless $self, $class; | |||||||||||||
| 86 | |||||||||||||||
| 87 | # If we are changing to or from a Quote style token, we | ||||||||||||||
| 88 | # can't just rebless and need to do some extra thing | ||||||||||||||
| 89 | # Otherwise, we have done enough | ||||||||||||||
| 90 | 0 | ▶ | return 1 if ($old_quote - $new_quote) == 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 91 | |||||||||||||||
| 92 | # Make a new token from the old content, and overwrite the current | ||||||||||||||
| 93 | # token's attributes with the new token's attributes. | ||||||||||||||
| 94 | 0 | ▶ | my $token = $class->new( $self->{content} ) or return undef; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 95 | 0 | delete $self->{$_} foreach keys %$self; | |||||||||||||
| 96 | 0 | $self->{$_} = $token->{$_} foreach keys %$token; | |||||||||||||
| 97 | |||||||||||||||
| 98 | 0 | 1; | |||||||||||||
| 99 | } | ||||||||||||||
| 100 | |||||||||||||||
| 101 | |||||||||||||||
| 102 | |||||||||||||||
| 103 | ##################################################################### | ||||||||||||||
| 104 | # MDOM::Token Methods | ||||||||||||||
| 105 | |||||||||||||||
| 106 | =pod | ||||||||||||||
| 107 | |||||||||||||||
| 108 | =head2 set_content $string | ||||||||||||||
| 109 | |||||||||||||||
| 110 | The C<set_content> method allows to set/change the string that the | ||||||||||||||
| 111 | C<MDOM::Token> object represents. | ||||||||||||||
| 112 | |||||||||||||||
| 113 | Returns the string you set the Token to | ||||||||||||||
| 114 | |||||||||||||||
| 115 | =cut | ||||||||||||||
| 116 | |||||||||||||||
| 117 | sub set_content { | ||||||||||||||
| 118 | 3 | ▶ | $_[0]->{content} = $_[1]; | ||||||||||||
|
Subroutine
set_content: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 119 | } | ||||||||||||||
| 120 | |||||||||||||||
| 121 | =pod | ||||||||||||||
| 122 | |||||||||||||||
| 123 | =head2 add_content $string | ||||||||||||||
| 124 | |||||||||||||||
| 125 | The C<add_content> method allows you to add additional bytes of code | ||||||||||||||
| 126 | to the end of the Token. | ||||||||||||||
| 127 | |||||||||||||||
| 128 | Returns the new full string after the bytes have been added. | ||||||||||||||
| 129 | |||||||||||||||
| 130 | =cut | ||||||||||||||
| 131 | |||||||||||||||
| 132 | 31 | ▶ | sub add_content { $_[0]->{content} .= $_[1] } | ||||||||||||
|
Subroutine
add_content: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 133 | |||||||||||||||
| 134 | =pod | ||||||||||||||
| 135 | |||||||||||||||
| 136 | =head2 length | ||||||||||||||
| 137 | |||||||||||||||
| 138 | The C<length> method returns the length of the string in a Token. | ||||||||||||||
| 139 | |||||||||||||||
| 140 | =cut | ||||||||||||||
| 141 | |||||||||||||||
| 142 | 0 | ▶ | sub length { &CORE::length($_[0]->{content}) } | ||||||||||||
|
Subroutine
length: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 143 | |||||||||||||||
| 144 | |||||||||||||||
| 145 | |||||||||||||||
| 146 | |||||||||||||||
| 147 | |||||||||||||||
| 148 | ##################################################################### | ||||||||||||||
| 149 | # Overloaded MDOM::Element methods | ||||||||||||||
| 150 | |||||||||||||||
| 151 | sub content { | ||||||||||||||
| 152 | 2054 | ▶ | $_[0]->{content}; | ||||||||||||
|
Subroutine
content: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 153 | } | ||||||||||||||
| 154 | |||||||||||||||
| 155 | # You can insert either a statement, or a non-significant token. | ||||||||||||||
| 156 | sub insert_before { | ||||||||||||||
| 157 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
insert_before: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||
| 158 | 0 | ▶ | my $Element = _INSTANCE(shift, 'MDOM::Element') or return undef; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 159 | 0 | ▶ | if ( $Element->isa('MDOM::Structure') ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 160 | 0 | return $self->__insert_before($Element); | |||||||||||||
| 161 | } elsif ( $Element->isa('MDOM::Token') ) { | ||||||||||||||
| 162 | 0 | return $self->__insert_before($Element); | |||||||||||||
| 163 | } | ||||||||||||||
| 164 | 0 | ''; | |||||||||||||
| 165 | } | ||||||||||||||
| 166 | |||||||||||||||
| 167 | # As above, you can insert a statement, or a non-significant token | ||||||||||||||
| 168 | sub insert_after { | ||||||||||||||
| 169 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
insert_after: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||
| 170 | 0 | ▶ | my $Element = _INSTANCE(shift, 'MDOM::Element') or return undef; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 171 | 0 | ▶ | if ( $Element->isa('MDOM::Structure') ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 172 | 0 | return $self->__insert_after($Element); | |||||||||||||
| 173 | } elsif ( $Element->isa('MDOM::Token') ) { | ||||||||||||||
| 174 | 0 | return $self->__insert_after($Element); | |||||||||||||
| 175 | } | ||||||||||||||
| 176 | 0 | ''; | |||||||||||||
| 177 | } | ||||||||||||||
| 178 | |||||||||||||||
| 179 | =pod | ||||||||||||||
| 180 | |||||||||||||||
| 181 | =head2 source | ||||||||||||||
| 182 | |||||||||||||||
| 183 | Returns the makefile source for the current token | ||||||||||||||
| 184 | |||||||||||||||
| 185 | =cut | ||||||||||||||
| 186 | |||||||||||||||
| 187 | sub source { | ||||||||||||||
| 188 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
source: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 189 | 0 | return $self->content; | |||||||||||||
| 190 | } | ||||||||||||||
| 191 | |||||||||||||||
| 192 | 18 | 1; | |||||||||||||