Simple/Tokenizer.pm

filter: Statementstmt 80.9% 157 / 194 Branchbran 76.0% 70 / 92 Conditioncond 64.5% 20 / 31 MC/DCmcdc 52.6% 10 / 19 Subroutinesub 79.4% 27 / 34 Podpod 100% 4 / 4 totaltotal 77.0% 288 / 374 CC 63 SCAR 20.2
CC
63
Cov
78%
_chomp_token
17.0
_visualize_chomp
12.0
_visualize_tid
12.0
CRAP 7.5
« Simple/Caller.pm ↑ Summary Simple/Base/Include.pm »
1 package Text::Template::Simple::Tokenizer;
2 32 $Text::Template::Simple::Tokenizer::VERSION = '0.91';
3 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 32 ▶use constant CMD_CHAR => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 32 ▶use constant CMD_ID => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 32 ▶use constant CMD_CALLBACK => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 32 ▶use constant ID_DS => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 32 ▶use constant ID_DE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 32 ▶use constant ID_PRE_CHOMP => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 32 ▶use constant ID_POST_CHOMP => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 32 ▶use constant SUBSTR_OFFSET_FIRST => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 32 ▶use constant SUBSTR_OFFSET_SECOND => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 32 ▶use constant SUBSTR_LENGTH => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 32 ▶use Text::Template::Simple::Util qw( LOG DEBUG fatal );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 32 ▶use Text::Template::Simple::Constants qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 32 my @COMMANDS = ( # default command list
23 # command id
24 [ DIR_CAPTURE , T_CAPTURE ],
25 [ DIR_DYNAMIC , T_DYNAMIC, ],
26 [ DIR_STATIC , T_STATIC, ],
27 [ DIR_NOTADELIM, T_NOTADELIM ],
28 [ DIR_COMMENT , T_COMMENT ],
29 [ DIR_COMMAND , T_COMMAND ],
30 );
31  
32 251 ▶sub new {
Subroutine new: called CC=5 SCAR=16.6
Pod documented
33 251 my $class = shift;
34 251 my $self = [];
35 251 bless $self, $class;
36 251 ▶ $self->[ID_DS] = shift || fatal('tts.tokenizer.new.ds');
Condition33%1/3
shift() || fatal('tts.tokenizer.new.ds')
l!l&&r!l&&!r
25100
MC/DC0%0/2
shift() || fatal('tts.tokenizer.new.ds')
shift() fatal('tts.tokenizer.new.ds')
Truth table33%1/3
A || B
A= shift()B= fatal('tts.tokenizer.new.ds')
ABresult
1X1
011
000
37 251 ▶ $self->[ID_DE] = shift || fatal('tts.tokenizer.new.de');
Condition33%1/3
shift() || fatal('tts.tokenizer.new.de')
l!l&&r!l&&!r
25100
MC/DC0%0/2
shift() || fatal('tts.tokenizer.new.de')
shift() fatal('tts.tokenizer.new.de')
Truth table33%1/3
A || B
A= shift()B= fatal('tts.tokenizer.new.de')
ABresult
1X1
011
000
38 251 ▶ $self->[ID_PRE_CHOMP] = shift || CHOMP_NONE;
Condition100%2/2
shift() || 0
l!l
4247
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
39 251 ▶ $self->[ID_POST_CHOMP] = shift || CHOMP_NONE;
Condition100%2/2
shift() || 0
l!l
4247
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
40 251 return $self;
41 }
42  
43 251 ▶sub tokenize {
Subroutine tokenize: called CC=8 SCAR=20.8
Pod documented
44 # compile the template into a tree and optimize
45 251 my($self, $tmp, $map_keys) = @_;
46  
47 251 ▶ return $self->_empty_token( $tmp ) if ! $tmp;
Branch: 1 branch
BranchTrueFalseTotal
unless $tmp 2 249 251
48  
49 249 my($ds, $de) = @{ $self }[ ID_DS, ID_DE ];
50 249 my($qds, $qde) = map { quotemeta $_ } $ds, $de;
51  
52 249 my(@tokens, $inside);
53  
54 249 OUT_TOKEN: foreach my $i ( split /($qds)/xms, $tmp ) {
55  
56 855 ▶ if ( $i eq $ds ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i eq $ds) 303 552 855
57 303 push @tokens, [ $i, T_DELIMSTART, [], undef ];
58 303 $inside = 1;
59 303 next OUT_TOKEN;
60 }
61  
62 552 IN_TOKEN: foreach my $j ( split /($qde)/xms, $i ) {
63 796 ▶ if ( $j eq $de ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j eq $de) 302 494 796
64 302 my $last_token = $tokens[LAST_TOKEN];
65 302 ▶ if ( T_NOTADELIM == $last_token->[TOKEN_ID] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (6 == $last_token->[1]) { } 10 292 302
66 10 $last_token->[TOKEN_STR] = $self->tilde(
67 $last_token->[TOKEN_STR] . $de
68 );
69 }
70 else {
71 292 push @tokens, [ $j, T_DELIMEND, [], undef ];
72 }
73 302 $inside = 0;
74 302 next IN_TOKEN;
75 }
76 494 push @tokens, $self->_token_code( $j, $inside, $map_keys, \@tokens );
77 }
78 }
79  
80 249 ▶ $self->_debug_tokens( \@tokens ) if $self->can('DEBUG_TOKENS');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("DEBUG_TOKENS") 0 249 249
81  
82 249 return \@tokens;
83 }
84  
85 201 ▶sub tilde {
Subroutine tilde: called CC=1 SCAR=0.0
Pod documented
86 201 my(undef, @args) = @_;
87 201 return Text::Template::Simple::Util::escape( q{~} => @args );
88 }
89  
90 0 ▶sub quote {
Subroutine quote: not called CC=1 SCAR=6.9
Pod documented
91 0 my(undef, @args) = @_;
92 0 return Text::Template::Simple::Util::escape( q{"} => @args );
93 }
94  
95 2 ▶sub _empty_token {
Subroutine _empty_token: called CC=2 SCAR=7.0
96 2 my $self = shift;
97 2 my $tmp = shift;
98 2 ▶ fatal('tts.tokenizer.tokenize.tmp') if ! defined $tmp;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tmp 0 2 2
99 # empty string or zero
100 return [
101 2 [ $self->[ID_DS], T_DELIMSTART, [], undef ],
102 [ $tmp , T_RAW , [], undef ],
103 [ $self->[ID_DE], T_DELIMEND , [], undef ],
104 ]
105 }
106  
107 494 ▶sub _get_command_chars {
Subroutine _get_command_chars: called CC=4 SCAR=13.9
108 494 my($self, $str) = @_;
109 return
110 494 ▶ $str ne EMPTY_STRING # left
Branch: 3 branches
BranchTrueFalseTotal
$str ne '' ? : 493 1 494
$str ne '' ? : 493 1 494
$str ne '' ? : 493 1 494
111 ? substr $str, SUBSTR_OFFSET_FIRST , SUBSTR_LENGTH : EMPTY_STRING,
112 $str ne EMPTY_STRING # extra
113 ? substr $str, SUBSTR_OFFSET_SECOND, SUBSTR_LENGTH : EMPTY_STRING,
114 $str ne EMPTY_STRING # right
115 ? substr $str, length($str) - 1 , SUBSTR_LENGTH : EMPTY_STRING,
116 ;
117 }
118  
119 303 ▶sub _user_commands {
Subroutine _user_commands: called CC=2 SCAR=8.1
120 303 my $self = shift;
121 303 ▶ return +() if ! $self->can('commands');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can("commands") 303 0 303
122 0 return $self->commands;
123 }
124  
125 262 ▶sub _token_for_command {
Subroutine _token_for_command: called CC=9 SCAR=22.3
126 262 my($self, $tree, $map_keys, $str, $last_cmd, $second_cmd, $cmd, $inside) = @_;
127 262 my($copen, $cclose, $ctoken) = $self->_chomp_token( $second_cmd, $last_cmd );
128 262 my $len = length $str;
129 262 ▶ my $cb = $map_keys ? 'quote' : $cmd->[CMD_CALLBACK];
Branch: 1 branch
BranchTrueFalseTotal
$map_keys ? : 0 262 262
130 262 ▶ my $soff = $copen ? 2 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$copen ? : 2 260 262
131 262 ▶ my $slen = $len - ($cclose ? $soff+1 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$cclose ? : 9 253 262
132 262 my $buf = substr $str, $soff, $slen;
133  
134 262 ▶ if ( T_NOTADELIM == $cmd->[CMD_ID] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (6 == $cmd->[1]) 11 251 262
135 11 $buf = $self->[ID_DS] . $buf;
136 11 $tree->[LAST_TOKEN][TOKEN_ID] = T_DISCARD;
137 }
138  
139 262 my $needs_chomp = defined $ctoken;
140 262 ▶ $self->_chomp_prev($tree, $ctoken) if $needs_chomp;
Branch: 1 branch
BranchTrueFalseTotal
if $needs_chomp 262 0 262
141  
142 262 ▶ my $id = $map_keys ? T_RAW : $cmd->[CMD_ID];
Branch: 1 branch
BranchTrueFalseTotal
$map_keys ? : 0 262 262
143 262 ▶ my $val = $cb ? $self->$cb( $buf ) : $buf;
Branch: 1 branch
BranchTrueFalseTotal
$cb ? : 0 262 262
144  
145 return [
146 262 ▶ $val,
Branch: 1 branch
BranchTrueFalseTotal
$needs_chomp ? : 262 0 262
147 $id,
148 [ (CHOMP_NONE) x 2 ],
149 $needs_chomp ? $ctoken : undef # trigger
150 ];
151 }
152  
153 41 ▶sub _token_for_code {
Subroutine _token_for_code: called CC=6 SCAR=18.0
154 41 my($self, $tree, $map_keys, $str, $last_cmd, $first_cmd) = @_;
155 41 my($copen, $cclose, $ctoken) = $self->_chomp_token( $first_cmd, $last_cmd );
156 41 my $len = length $str;
157 41 ▶ my $soff = $copen ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$copen ? : 11 30 41
158 41 ▶ my $slen = $len - ( $cclose ? $soff+1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$cclose ? : 14 27 41
159  
160 41 my $needs_chomp = defined $ctoken;
161 41 ▶ $self->_chomp_prev($tree, $ctoken) if $needs_chomp;
Branch: 1 branch
BranchTrueFalseTotal
if $needs_chomp 41 0 41
162  
163 return [
164 41 ▶ substr($str, $soff, $slen),
Branch: 2 branches
BranchTrueFalseTotal
$map_keys ? : 6 35 41
$needs_chomp ? : 41 0 41
165 $map_keys ? T_MAPKEY : T_CODE,
166 [ (CHOMP_NONE) x 2 ],
167 $needs_chomp ? $ctoken : undef # trigger
168 ];
169 }
170  
171 494 ▶sub _token_code {
Subroutine _token_code: called CC=5 SCAR=16.1
172 494 my($self, $str, $inside, $map_keys, $tree) = @_;
173 494 my($first_cmd, $second_cmd, $last_cmd) = $self->_get_command_chars( $str );
174  
175 494 ▶ if ( $inside ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inside) 303 191 494
176 303 my @common = ($tree, $map_keys, $str, $last_cmd);
177 303 foreach my $cmd ( @COMMANDS, $self->_user_commands ) {
178 702 ▶ next if $first_cmd ne $cmd->[CMD_CHAR];
Branch: 1 branch
BranchTrueFalseTotal
if $first_cmd ne $cmd->[0] 440 262 702
179 262 return $self->_token_for_command( @common, $second_cmd, $cmd, $inside );
180 }
181 41 return $self->_token_for_code( @common, $first_cmd );
182 }
183  
184 191 my $prev = $tree->[PREVIOUS_TOKEN];
185  
186 return [
187 191 ▶ $self->tilde( $str ),
Branch: 1 branch
BranchTrueFalseTotal
$prev ? : 94 97 191
188 T_RAW,
189 [ $prev ? $prev->[TOKEN_TRIGGER] : undef, CHOMP_NONE ],
190 undef # trigger
191 ];
192 }
193  
194 303 ▶sub _chomp_token {
Subroutine _chomp_token: called CC=17 SCAR=28.3
195 303 my($self, $open_tok, $close_tok) = @_;
196 303 my($pre, $post) = ( $self->[ID_PRE_CHOMP], $self->[ID_POST_CHOMP] );
197 303 my $c = CHOMP_NONE;
198  
199 my $copen = $open_tok eq DIR_CHOMP_NONE ? RESET_FIELD
200 4 ▶ : $open_tok eq DIR_COLLAPSE ? do { $c |= COLLAPSE_LEFT; 1 }
Branch: 1 branch
BranchTrueFalseTotal
$pre & 64 ? : 2 297 299
201 2 ▶ : $pre & COLLAPSE_ALL ? do { $c |= COLLAPSE_LEFT; 1 }
Branch: 1 branch
BranchTrueFalseTotal
$pre & 2 ? : 2 295 297
202 2 ▶ : $pre & CHOMP_ALL ? do { $c |= CHOMP_LEFT; 1 }
Branch: 1 branch
BranchTrueFalseTotal
$open_tok eq '-' ? : 5 290 295
203 303 ▶ : $open_tok eq DIR_CHOMP ? do { $c |= CHOMP_LEFT; 1 }
Branch: 2 branches
BranchTrueFalseTotal
$open_tok eq '^' ? : 0 303 303
$open_tok eq '~' ? : 4 299 303
204 : 0
205 ;
206  
207 my $cclose = $close_tok eq DIR_CHOMP_NONE ? RESET_FIELD
208 3 ▶ : $close_tok eq DIR_COLLAPSE ? do { $c |= COLLAPSE_RIGHT; 1 }
Branch: 1 branch
BranchTrueFalseTotal
$post & 64 ? : 2 298 300
209 2 ▶ : $post & COLLAPSE_ALL ? do { $c |= COLLAPSE_RIGHT; 1 }
Branch: 1 branch
BranchTrueFalseTotal
$post & 2 ? : 2 296 298
210 2 ▶ : $post & CHOMP_ALL ? do { $c |= CHOMP_RIGHT; 1 }
Branch: 1 branch
BranchTrueFalseTotal
$close_tok eq '-' ? : 16 280 296
211 303 ▶ : $close_tok eq DIR_CHOMP ? do { $c |= CHOMP_RIGHT; 1 }
Branch: 2 branches
BranchTrueFalseTotal
$close_tok eq '^' ? : 0 303 303
$close_tok eq '~' ? : 3 300 303
212 : 0
213 ;
214  
215 303 ▶ my $cboth = $copen > 0 && $cclose > 0;
Condition100%3/3
$copen > 0 && $cclose > 0
!ll&&!rl&&r
29049
MC/DC100%2/2
$copen > 0 && $cclose > 0
$copen > 0 $cclose > 0
Truth table100%3/3
A && B
A= $copen > 0B= $cclose > 0
ABresult
0X0
100
111
216  
217 303 ▶ $c |= COLLAPSE_ALL if ( ( $c & COLLAPSE_LEFT ) && ( $c & COLLAPSE_RIGHT ) );
Branch: 1 branch
BranchTrueFalseTotal
if $c & 16 and $c & 32 3 300 303
Condition100%3/3
$c & 16 and $c & 32
!ll&&!rl&&r
29733
MC/DC100%2/2
$c & 16 and $c & 32
$c & 16 $c & 32
Truth table100%3/3
A and B
A= $c & 16B= $c & 32
ABresult
0X0
100
111
218 303 ▶ $c |= CHOMP_ALL if ( ( $c & CHOMP_LEFT ) && ( $c & CHOMP_RIGHT ) );
Branch: 1 branch
BranchTrueFalseTotal
if $c & 4 and $c & 8 4 299 303
Condition100%3/3
$c & 4 and $c & 8
!ll&&!rl&&r
29634
MC/DC100%2/2
$c & 4 and $c & 8
$c & 4 $c & 8
Truth table100%3/3
A and B
A= $c & 4B= $c & 8
ABresult
0X0
100
111
219  
220 303 ▶ return $copen, $cclose, $c || CHOMP_NONE;
Condition100%2/2
$c || 0
l!l
27276
MC/DC100%1/1
$c || 0
$c
Truth table100%2/2
A
A= $c
Aresult
11
00
221 }
222  
223 303 ▶sub _chomp_prev {
Subroutine _chomp_prev: called CC=5 SCAR=16.1
224 303 my($self, $tree, $ctoken) = @_;
225 303 ▶ my $prev = $tree->[PREVIOUS_TOKEN] || return; # no previous if this is first
Condition100%2/2
$tree->[-2] || (return)
l!l
151152
MC/DC100%1/1
$tree->[-2] || (return)
$tree->[-2]
Truth table100%2/2
A
A= $tree->[-2]
Aresult
11
00
226 151 ▶ return if T_RAW != $prev->[TOKEN_ID]; # only RAWs can be chomped
Branch: 1 branch
BranchTrueFalseTotal
if 5 != $prev->[1] 1 150 151
227  
228 150 my $tc_prev = $prev->[TOKEN_CHOMP][TOKEN_CHOMP_PREV];
229 150 my $tc_next = $prev->[TOKEN_CHOMP][TOKEN_CHOMP_NEXT];
230  
231 150 ▶ $prev->[TOKEN_CHOMP] = [
Branch: 2 branches
BranchTrueFalseTotal
$tc_next ? : 9 141 150
$tc_prev ? : 0 150 150
232 $tc_next ? $tc_next : CHOMP_NONE,
233 $tc_prev ? $tc_prev | $ctoken : $ctoken
234 ];
235 150 return;
236 }
237  
238 0 ▶sub _get_symbols {
Subroutine _get_symbols: not called CC=2 SCAR=12.6
239 # fetch the related constants
240 0 my $self = shift;
241 0 ▶ my $regex = shift || fatal('tts.tokenizer._get_symbols.regex');
Condition0%0/3
shift() || fatal('tts.tokenizer._get_symbols.regex')
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || fatal('tts.tokenizer._get_symbols.regex')
shift() fatal('tts.tokenizer._get_symbols.regex')
Truth table0%0/3
A || B
A= shift()B= fatal('tts.tokenizer._get_symbols.regex')
ABresult
1X1
011
000
242 32 ▶ no strict qw( refs );
Subroutine BEGIN: called CC=1 SCAR=0.0
243 0 return grep { $_ =~ $regex } keys %{ ref($self) . q{::} };
244 }
245  
246 0 ▶sub _visualize_chomp {
Subroutine _visualize_chomp: not called CC=3 SCAR=24.8
247 0 my $self = shift;
248 0 my $param = shift;
249 0 ▶ return 'undef' if ! defined $param;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param 0 0 0
250  
251 0 my @test = map { $_->[0] }
252 0 grep { $param & $_->[1] }
253 0 map { [ $_, $self->$_() ] }
254 $self->_get_symbols( qr{ \A (?: CHOMP|COLLAPSE ) }xms );
255  
256 0 ▶ return @test ? join( q{,}, @test ) : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
@test ? : 0 0 0
257 }
258  
259 0 ▶sub _visualize_tid {
Subroutine _visualize_tid: not called CC=3 SCAR=24.8
260 0 my $self = shift;
261 0 my $id = shift;
262 my @ids = (
263 undef,
264 0 sort { $self->$a() <=> $self->$b() }
265 0 grep { $_ ne 'T_MAXID' }
266 $self->_get_symbols( qr{ \A (?: T_ ) }xms )
267 );
268  
269 0 ▶ my $rv = $ids[ $id ] || ( defined $id ? $id : 'undef' );
Branch: 1 branch
BranchTrueFalseTotal
defined $id ? : 0 0 0
Condition0%0/3
$ids[$id] || (defined $id ? $id : 'undef')
l!l&&r!l&&!r
000
MC/DC0%0/2
$ids[$id] || (defined $id ? $id : 'undef')
$ids[$id] (defined $id ? $id : 'undef')
Truth table0%0/3
A || B
A= $ids[$id]B= (defined $id ? $id : 'undef')
ABresult
1X1
011
000
270 0 return $rv;
271 }
272  
273 0 ▶sub _debug_tokens {
Subroutine _debug_tokens: not called CC=3 SCAR=24.8
274 0 my $self = shift;
275 0 my $tokens = shift;
276 0 my $buf = $self->_debug_tokens_head;
277  
278 0 foreach my $t ( @{ $tokens } ) {
279 $buf .= $self->_debug_tokens_row(
280 $self->_visualize_tid( $t->[TOKEN_ID] ),
281 Text::Template::Simple::Util::visualize_whitespace(
282 $t->[TOKEN_STR]
283 ),
284 0 ▶ map { $_ eq 'undef' ? EMPTY_STRING : $_ }
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'undef' ? : 0 0 0
285 0 map { $self->_visualize_chomp( $_ ) }
286 $t->[TOKEN_CHOMP][TOKEN_CHOMP_NEXT],
287 $t->[TOKEN_CHOMP][TOKEN_CHOMP_PREV],
288 $t->[TOKEN_TRIGGER]
289 );
290 }
291 0 Text::Template::Simple::Util::LOG( DEBUG => $buf );
292 0 return;
293 }
294  
295 0 ▶sub _debug_tokens_head {
Subroutine _debug_tokens_head: not called CC=1 SCAR=6.9
296 0 my $self = shift;
297 0 return <<'HEAD';
298  
299 ---------------------------
300 TOKEN DUMP
301 ---------------------------
302 HEAD
303 }
304  
305 0 ▶sub _debug_tokens_row {
Subroutine _debug_tokens_row: not called CC=1 SCAR=6.9
306 0 my($self, @params) = @_;
307 0 return sprintf <<'DUMP', @params;
308 ID : %s
309 STRING : %s
310 CHOMP_NEXT: %s
311 CHOMP_PREV: %s
312 TRIGGER : %s
313 ---------------------------
314 DUMP
315 }
316  
317 251 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.7
318 251 ▶ my $self = shift || return;
Condition50%1/2
shift() || (return)
l!l
2510
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
319 251 ▶ LOG( DESTROY => ref $self ) if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() 0 251 251
320 251 return;
321 }
322  
323 32 1;
324  
325 __END__
« Simple/Caller.pm ↑ Summary Simple/Base/Include.pm »