| 1 | package PerlActor::LineTokenizer; | ||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 1 | ▶ | use base 'PerlActor::Object'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use fields qw( tokens currentToken inQuoteMode quoteChar placeholderValues ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | #=============================================================================================== | ||||||||||||||||||||
| 7 | # Public Methods | ||||||||||||||||||||
| 8 | #=============================================================================================== | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | sub getTokens | ||||||||||||||||||||
| 11 | 25 | ▶ | { | ||||||||||||||||||
|
Subroutine
getTokens: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 12 | 25 | my $self = shift; | |||||||||||||||||||
| 13 | 25 | my $line = shift; | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 25 | ▶ | return () unless $line; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 16 | 23 | ▶ | return if $line =~ /^#/; # Comment Line | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 22 | chomp $line; | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 22 | my @placeholderValues = @_; | |||||||||||||||||||
| 21 | 22 | $self->{placeholderValues} = \@placeholderValues; | |||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 22 | $self->_reset(); | |||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 22 | map { $self->_processChar($_)} (split //, $line); | |||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 22 | $self->_addCurrentToken(); | |||||||||||||||||||
| 29 | 22 | $self->_substitutePlaceHolders(); | |||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 22 | return @{$self->{tokens}}; | |||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | #=============================================================================================== | ||||||||||||||||||||
| 35 | # Protected Methods - Don't even think about calling these from outside the class. | ||||||||||||||||||||
| 36 | #=============================================================================================== | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | sub _reset | ||||||||||||||||||||
| 39 | 22 | ▶ | { | ||||||||||||||||||
|
Subroutine
_reset: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 40 | 22 | my $self = shift; | |||||||||||||||||||
| 41 | 22 | $self->{tokens} = []; | |||||||||||||||||||
| 42 | 22 | $self->_resetCurrentToken(); | |||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | sub _addCurrentToken | ||||||||||||||||||||
| 46 | 51 | ▶ | { | ||||||||||||||||||
|
Subroutine
_addCurrentToken: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 47 | 51 | my $self = shift; | |||||||||||||||||||
| 48 | 51 | ▶ | if ($self->_hasValidCurrentToken()) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 49 | { | ||||||||||||||||||||
| 50 | 36 | push @{$self->{tokens}}, $self->{currentToken}; | |||||||||||||||||||
| 51 | 36 | $self->_resetCurrentToken(); | |||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | sub _hasValidCurrentToken | ||||||||||||||||||||
| 56 | 51 | ▶ | { | ||||||||||||||||||
|
Subroutine
_hasValidCurrentToken: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 57 | 51 | my $self = shift; | |||||||||||||||||||
| 58 | 51 | ▶ | return (defined $self->{currentToken} and $self->{currentToken} =~ /[^\s]/); | ||||||||||||||||||
|
Condition66%2/3
defined $self->{'currentToken'} && $self->{'currentToken'} =~ /[^\s]/
MC/DC50%1/2
defined $self->{'currentToken'} && $self->{'currentToken'} =~ /[^\s]/
defined $self->{'currentToken'}
$self->{'currentToken'} =~ /[^\s]/
Truth table66%2/3
A && B
A= defined $self->{'currentToken'}B= $self->{'currentToken'} =~ /[^\s]/
| |||||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | sub _resetCurrentToken | ||||||||||||||||||||
| 62 | 58 | ▶ | { | ||||||||||||||||||
|
Subroutine
_resetCurrentToken: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 63 | 58 | my $self = shift; | |||||||||||||||||||
| 64 | 58 | $self->{currentToken} = ''; | |||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | sub _processChar | ||||||||||||||||||||
| 68 | 206 | ▶ | { | ||||||||||||||||||
|
Subroutine
_processChar: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 69 | 206 | my ($self, $char) = @_; | |||||||||||||||||||
| 70 | 206 | ▶ | if ($self->_inQuoteMode()) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | { | ||||||||||||||||||||
| 72 | 64 | $self->_processCharQuoteMode($char); | |||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | else | ||||||||||||||||||||
| 75 | { | ||||||||||||||||||||
| 76 | 142 | $self->_processCharNormalMode($char); | |||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | sub _processCharNormalMode | ||||||||||||||||||||
| 81 | 142 | ▶ | { | ||||||||||||||||||
|
Subroutine
_processCharNormalMode: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 82 | 142 | my ($self, $char) = @_; | |||||||||||||||||||
| 83 | 142 | ▶ | if ($char =~ /\s/) | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 84 | { | ||||||||||||||||||||
| 85 | 23 | $self->_addCurrentToken(); | |||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | elsif ($self->_isQuoteChar($char)) | ||||||||||||||||||||
| 88 | { | ||||||||||||||||||||
| 89 | 6 | $self->_setQuoteMode(); | |||||||||||||||||||
| 90 | 6 | $self->{quoteChar} = $char; | |||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | else | ||||||||||||||||||||
| 93 | { | ||||||||||||||||||||
| 94 | 113 | $self->{currentToken} .= $char; | |||||||||||||||||||
| 95 | } | ||||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | sub _processCharQuoteMode | ||||||||||||||||||||
| 99 | 64 | ▶ | { | ||||||||||||||||||
|
Subroutine
_processCharQuoteMode: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 100 | 64 | my ($self, $char) = @_; | |||||||||||||||||||
| 101 | 64 | ▶ | if ($char eq $self->{quoteChar}) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | { | ||||||||||||||||||||
| 103 | 6 | $self->_addCurrentToken(); | |||||||||||||||||||
| 104 | 6 | $self->_setNormalMode(); | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | else | ||||||||||||||||||||
| 107 | { | ||||||||||||||||||||
| 108 | 58 | $self->{currentToken} .= $char; | |||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | sub _inQuoteMode | ||||||||||||||||||||
| 113 | 206 | ▶ | { | ||||||||||||||||||
|
Subroutine
_inQuoteMode: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 114 | 206 | my $self = shift; | |||||||||||||||||||
| 115 | 206 | return $self->{inQuoteMode}; | |||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | sub _isQuoteChar | ||||||||||||||||||||
| 119 | 119 | ▶ | { | ||||||||||||||||||
|
Subroutine
_isQuoteChar: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 120 | 119 | my ($self, $char) = @_; | |||||||||||||||||||
| 121 | 119 | return $char =~ m/('|"|~)/; #' | |||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | sub _setQuoteMode | ||||||||||||||||||||
| 125 | 6 | ▶ | { | ||||||||||||||||||
|
Subroutine
_setQuoteMode: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 126 | 6 | my $self = shift; | |||||||||||||||||||
| 127 | 6 | $self->{inQuoteMode} = 1; | |||||||||||||||||||
| 128 | } | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | sub _setNormalMode | ||||||||||||||||||||
| 131 | 6 | ▶ | { | ||||||||||||||||||
|
Subroutine
_setNormalMode: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 132 | 6 | my $self = shift; | |||||||||||||||||||
| 133 | 6 | $self->{inQuoteMode} = 0; | |||||||||||||||||||
| 134 | } | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | sub _substitutePlaceHolders | ||||||||||||||||||||
| 137 | 22 | ▶ | { | ||||||||||||||||||
|
Subroutine
_substitutePlaceHolders: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 138 | 22 | my $self = shift; | |||||||||||||||||||
| 139 | 22 | foreach my $token (@{$self->{tokens}}) | |||||||||||||||||||
| 140 | { | ||||||||||||||||||||
| 141 | 36 | ▶ | while ($token =~ /(?<!\$)\$([0-9])/) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 142 | { | ||||||||||||||||||||
| 143 | 8 | my $index = $1; | |||||||||||||||||||
| 144 | 8 | my $value = $self->{placeholderValues}->[$index]; | |||||||||||||||||||
| 145 | 8 | ▶ | $token =~ s/(?<!\$)\$$index/$value/ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 146 | if $value; | ||||||||||||||||||||
| 147 | } | ||||||||||||||||||||
| 148 | 36 | $token =~ s/\${2}/\$/g; | |||||||||||||||||||
| 149 | } | ||||||||||||||||||||
| 150 | } | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | # Keep Perl happy. | ||||||||||||||||||||
| 153 | 1 | 1; | |||||||||||||||||||