Sandbox.pm

filter: Statementstmt 98.3% 1188 / 1208 Branchbran 92.1% 678 / 736 Conditioncond 88.6% 210 / 237 MC/DCmcdc 76.5% 98 / 128 Subroutinesub 98.3% 117 / 119 Podpod 100% 47 / 47 totaltotal 94.4% 2338 / 2475 CC 475 SCAR 32.9
CC
475
Cov
95%
_compile_template
91.4
_optimize_template
45.0
run
38.1
CRAP 26.7
↑ Summary Sandbox/Library.pm »
1 package Template::Sandbox;
2 # ABSTRACT: Fast pure-perl template engine sandboxed from your application.
3  
4 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 32 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 32 ▶use Class::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 32 ▶use Clone;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 32 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 32 ▶use Digest::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 32 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 32 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 32 ▶use Log::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 32 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 32 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 #use Time::HiRes;
18  
19 #my ( @function_table );
20  
21 # Array indices.
22 sub SELF() { 0; }
23 sub OP_LHS() { 1; }
24 sub OP_RHS() { 2; }
25  
26 # Compiled line indices.
27 # TODO: currently unused.
28 sub LINE_INSTR() { 0; }
29 sub LINE_POS() { 1; }
30 sub LINE_ARG() { 2; }
31  
32 # Instruction opcodes.
33 sub LITERAL() { 0; }
34 sub DEBUG() { 1; }
35 sub EXPR() { 2; }
36 sub JUMP() { 3; }
37 sub JUMP_IF() { 4; }
38 sub FOR() { 5; }
39 sub END_FOR() { 6; }
40 sub CONTEXT_PUSH() { 7; }
41 sub CONTEXT_POP() { 8; }
42  
43 # Starting point for opcodes for locally registered syntaxes.
44 sub LOCAL_SYNTAX() { 1_000_000; }
45  
46 # Expression opcodes.
47 sub OP_TREE() { 100; }
48 sub UNARY_OP() { 101; }
49 sub FUNC() { 102; }
50 sub METHOD() { 103; }
51 sub VAR() { 104; }
52 sub TEMPLATE() { 105; }
53  
54 # Template function array indices.
55 sub FUNC_FUNC() { 0; }
56 sub FUNC_ARG_NUM() { 1; }
57 sub FUNC_NEEDS_TEMPLATE() { 2; }
58 sub FUNC_INCONST() { 3; }
59 sub FUNC_UNDEF_OK() { 4; }
60  
61 # Special values in loop vars.
62 sub LOOP_COUNTER() { 0; };
63 sub LOOP_EVEN() { 1; };
64 sub LOOP_ODD() { 2; };
65 sub LOOP_FIRST() { 3; };
66 sub LOOP_INNER() { 4; };
67 sub LOOP_LAST() { 5; };
68 sub LOOP_PREV() { 6; };
69 sub LOOP_NEXT() { 7; };
70 sub LOOP_VALUE() { 8; };
71  
72 32 my %special_values_names = (
73 __counter__ => LOOP_COUNTER,
74 __even__ => LOOP_EVEN,
75 __odd__ => LOOP_ODD,
76 __first__ => LOOP_FIRST,
77 __inner__ => LOOP_INNER,
78 __last__ => LOOP_LAST,
79 __prev__ => LOOP_PREV,
80 __next__ => LOOP_NEXT,
81 __value__ => LOOP_VALUE,
82 );
83  
84 sub LOOP_STACK_COUNTER() { 0; }
85 sub LOOP_STACK_LAST() { 1; }
86 sub LOOP_STACK_SET() { 2; }
87 sub LOOP_STACK_HASH() { 3; }
88 sub LOOP_STACK_CONTEXT() { 4; }
89 sub LOOP_STACK_SPECIALS() { 5; }
90  
91 # The lower the weight the tighter it binds.
92 my %operators = (
93 # Logic operators
94 16 ▶ 'or' => [ 100, sub { $_[ SELF ]->_eval_expression( $_[ OP_LHS ], 1 ) or
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_eval_expression($_[1], 1) or $_[0]->_eval_expression($_[2], 1) 8 8 16
95 $_[ SELF ]->_eval_expression( $_[ OP_RHS ], 1 ) },
96 1 ],
97 16 ▶ 'and' => [ 99, sub { $_[ SELF ]->_eval_expression( $_[ OP_LHS ], 1 ) and
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_eval_expression($_[1], 1) and $_[0]->_eval_expression($_[2], 1) 8 8 16
98 $_[ SELF ]->_eval_expression( $_[ OP_RHS ], 1 ) },
99 1 ],
100 16 ▶ '||' => [ 98, sub { $_[ SELF ]->_eval_expression( $_[ OP_LHS ], 1 ) ||
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_eval_expression($_[1], 1) or $_[0]->_eval_expression($_[2], 1) 8 8 16
101 $_[ SELF ]->_eval_expression( $_[ OP_RHS ], 1 ) },
102 1 ],
103 16 ▶ '&&' => [ 96, sub { $_[ SELF ]->_eval_expression( $_[ OP_LHS ], 1 ) &&
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_eval_expression($_[1], 1) and $_[0]->_eval_expression($_[2], 1) 8 8 16
104 $_[ SELF ]->_eval_expression( $_[ OP_RHS ], 1 ) },
105 1 ],
106 # Comparison operators
107 16 ▶ 'cmp' => [ 95, sub { $_[ OP_LHS ] cmp $_[ OP_RHS ] } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
108 16 ▶ 'ne' => [ 94, sub { $_[ OP_LHS ] ne $_[ OP_RHS ] ? 1 : 0 } ],
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ne $_[2] ? : 8 8 16
109 16 ▶ 'eq' => [ 93, sub { $_[ OP_LHS ] eq $_[ OP_RHS ] ? 1 : 0 } ],
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] eq $_[2] ? : 8 8 16
110 16 ▶ '<=>' => [ 92, sub { $_[ OP_LHS ] <=> $_[ OP_RHS ] } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
111 16 ▶ '!=' => [ 91, sub { $_[ OP_LHS ] != $_[ OP_RHS ] ? 1 : 0 } ],
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] != $_[2] ? : 8 8 16
112 16 ▶ '==' => [ 90, sub { $_[ OP_LHS ] == $_[ OP_RHS ] ? 1 : 0 } ],
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] == $_[2] ? : 8 8 16
113 16 ▶ 'ge' => [ 89, sub { $_[ OP_LHS ] ge $_[ OP_RHS ] ? 1 : 0 } ],
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ge $_[2] ? : 12 4 16
114 16 ▶ 'le' => [ 88, sub { $_[ OP_LHS ] le $_[ OP_RHS ] ? 1 : 0 } ],
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] le $_[2] ? : 12 4 16
115 16 ▶ 'gt' => [ 87, sub { $_[ OP_LHS ] gt $_[ OP_RHS ] ? 1 : 0 } ],
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] gt $_[2] ? : 4 12 16
116 16 ▶ 'lt' => [ 86, sub { $_[ OP_LHS ] lt $_[ OP_RHS ] ? 1 : 0 } ],
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] lt $_[2] ? : 4 12 16
117 16 ▶ '>=' => [ 85, sub { $_[ OP_LHS ] >= $_[ OP_RHS ] ? 1 : 0 } ],
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] >= $_[2] ? : 12 4 16
118 16 ▶ '<=' => [ 84, sub { $_[ OP_LHS ] <= $_[ OP_RHS ] ? 1 : 0 } ],
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] <= $_[2] ? : 12 4 16
119 16 ▶ '>' => [ 83, sub { $_[ OP_LHS ] > $_[ OP_RHS ] ? 1 : 0 } ],
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] > $_[2] ? : 4 12 16
120 16 ▶ '<' => [ 82, sub { $_[ OP_LHS ] < $_[ OP_RHS ] ? 1 : 0 } ],
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] < $_[2] ? : 4 12 16
121  
122 # Assignment
123 1 ▶ '=' => [ 75, sub { $_[ SELF ]->_assign_var( $_[ OP_LHS ],
Subroutine __ANON__: called CC=1 SCAR=0.0
124 $_[ SELF ]->_eval_expression( $_[ OP_RHS ] ) )
125 },
126 1 ],
127  
128 # Arithmetic/concat
129 4 ▶ '.' => [ 70, sub { $_[ OP_LHS ] . $_[ OP_RHS ] } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
130 11 ▶ '+' => [ 69, sub { $_[ OP_LHS ] + $_[ OP_RHS ] } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
131 5 ▶ '-' => [ 68, sub { $_[ OP_LHS ] - $_[ OP_RHS ] } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
132 4 ▶ '%' => [ 67, sub { $_[ OP_LHS ] % $_[ OP_RHS ] } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
133 5 ▶ '/' => [ 66, sub { $_[ OP_LHS ] / $_[ OP_RHS ] } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
134 6 ▶ '*' => [ 65, sub { $_[ OP_LHS ] * $_[ OP_RHS ] } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
135 );
136  
137 sub def_func
138 578 ▶{
Subroutine def_func: called CC=2 SCAR=6.9
Pod documented
139 578 my ( $ret, $flag, $val ) = @_;
140 578 ▶ $ret = [ $ret ] if ref( $ret ) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $ret ne "ARRAY" 536 42 578
141 578 $ret->[ $flag ] = $val;
142 #warn "def_func: ..." . _tersedump( $ret );
143 578 return( $ret );
144 }
145  
146 7 ▶sub inconstant { return( def_func( @_, FUNC_INCONST, 1 ) ); }
Subroutine inconstant: called CC=1 SCAR=0.0
Pod documented
147 2 ▶sub needs_template { return( def_func( @_, FUNC_NEEDS_TEMPLATE, 1 ) ); }
Subroutine needs_template: called CC=1 SCAR=0.0
Pod documented
148 33 ▶sub undef_ok { return( def_func( @_, FUNC_UNDEF_OK, 1 ) ); }
Subroutine undef_ok: called CC=1 SCAR=0.0
Pod documented
149 536 ▶sub has_args { return( def_func( $_[ 0 ], FUNC_ARG_NUM, $_[ 1 ] ) ); }
Subroutine has_args: called CC=1 SCAR=0.0
Pod documented
150 26 ▶sub no_args { return( has_args( @_, 0 ) ); }
Subroutine no_args: called CC=1 SCAR=0.0
Pod documented
151 457 ▶sub one_arg { return( has_args( @_, 1 ) ); }
Subroutine one_arg: called CC=1 SCAR=0.0
Pod documented
152 14 ▶sub two_args { return( has_args( @_, 2 ) ); }
Subroutine two_args: called CC=1 SCAR=0.0
Pod documented
153 6 ▶sub three_args { return( has_args( @_, 3 ) ); }
Subroutine three_args: called CC=1 SCAR=0.0
Pod documented
154 33 ▶sub any_args { return( has_args( @_, -1 ) ); }
Subroutine any_args: called CC=1 SCAR=0.0
Pod documented
155  
156  
157 # These void() and size() are required since they get used internally
158 # for certain backwards-compat behaviours/syntax sugars.
159 # defined() is required by the test suite, so it stays here too.
160 my %functions = (
161 # Takes any arg and returns '', useful for hiding expression results.
162 0 ▶ void => ( any_args sub { '' } ),
Subroutine __ANON__: not called CC=1 SCAR=6.9
163  
164 size => ( one_arg
165 sub
166 4 ▶ {
Subroutine __ANON__: called CC=5 SCAR=17.2
167 4 ▶ return( undef ) unless defined( $_[ 0 ] );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 4 4
168 4 my $type = Scalar::Util::reftype( $_[ 0 ] );
169 return( !$type ? length( $_[ 0 ] ) :
170 2 ▶ $type eq 'HASH' ? scalar( keys( %{$_[ 0 ]} ) ) :
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'ARRAY' ? : 2 0 2
171 2 $type eq 'ARRAY' ? scalar( @{$_[ 0 ]} ) :
172 0 ▶ length( ${$_[ 0 ]} ) );
Branch: 2 branches
BranchTrueFalseTotal
!$type ? : 0 4 4
$type eq 'HASH' ? : 2 2 4
173 } ),
174  
175 27 ▶ defined => ( one_arg undef_ok sub { defined( $_[ 0 ] ) ? 1 : 0 } ),
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 21 6 27
176 );
177  
178 #print "Content-type: text/plain\n\n" . Data::Dumper::Dumper( \%functions );
179  
180 32 my %token_aliases = (
181 'foreach' => 'for',
182 'end for' => 'endfor',
183 'endforeach' => 'endfor',
184 'end foreach' => 'endfor',
185 'end include' => 'endinclude',
186 'els if' => 'elsif',
187 'else if' => 'elsif',
188 'elseif' => 'elsif',
189 'end if' => 'endif',
190 'els unless' => 'elsunless',
191 'else unless' => 'elsunless',
192 'elseunless' => 'elsunless',
193 'end unless' => 'endunless',
194 );
195  
196 # zero_width => boolean,
197 # Zero-width tokens gobble one of the surrounding \n if they're
198 # on a line by themselves, preventing "blank-line spam" in the
199 # template output.
200 # TODO: move syntaxes into substructure to avoid .key hackery.
201 32 my %syntaxes = (
202 # Faux values to define the auto opcode generation for local syntaxes.
203 '.next_instr' => LOCAL_SYNTAX,
204 '.instr_increment' => 1,
205 '.instr' => {},
206  
207 # Actual syntax definitions.
208 'var' => {
209 positional_args => [ 'var' ],
210 valid_args => [ 'var' ],
211 },
212 'debug' => {
213 positional_args => [ 'type', 'state' ],
214 valid_args => [ 'type', 'state' ],
215 zero_width => 1,
216 },
217 '#' => {
218 zero_width => 1,
219 },
220 'include' => {
221 positional_args => [ 'filename' ],
222 zero_width => 1,
223 },
224 'endinclude' => {
225 zero_width => 1,
226 },
227 'for' => {
228 zero_width => 1,
229 },
230 'endfor' => {
231 zero_width => 1,
232 },
233 'if' => {
234 zero_width => 1,
235 },
236 'unless' => {
237 zero_width => 1,
238 },
239 'else' => {
240 zero_width => 1,
241 },
242 'elsif' => {
243 zero_width => 1,
244 },
245 'elsunless' => {
246 zero_width => 1,
247 },
248 'endif' => {
249 zero_width => 1,
250 },
251 'endunless' => {
252 zero_width => 1,
253 },
254 );
255  
256 # Special vars that are symbolic literals.
257 32 my %symbolic_literals = (
258 'undef' => [ LITERAL, 'undef', undef ],
259 'null' => [ LITERAL, 'null', undef ],
260 'cr' => [ LITERAL, 'cr', "\n" ],
261 );
262  
263 # "our" declarations are to work around problem in some perls where
264 # "my" scope variables aren't seen by (??{ ... }).
265 32 our ( $single_quoted_text_regexp );
266  
267 32 $single_quoted_text_regexp = qr/
268 \'
269 (?:
270 # Quoteless, backslashless text.
271 (?> [^\'\\]+ )
272 |
273 # Escaped characters.
274 (?> (?:\\\\)* \\ . )
275 )*
276 \'
277 /sxo;
278  
279 32 our ( $double_quoted_text_regexp );
280  
281 32 $double_quoted_text_regexp = qr/
282 \"
283 (?:
284 # Quoteless, backslashless text.
285 (?> [^\"\\]+ )
286 |
287 # Escaped characters.
288 (?> (?:\\\\)* \\ . )
289 )*
290 \"
291 /sxo;
292  
293 32 our ( $matching_square_brackets_regexp );
294  
295 $matching_square_brackets_regexp = qr/
296 \[
297 (?:
298 # Bracketless, quoteless subtext.
299 (?> [^\[\]\"\']+ )
300 |
301 # Quoted text.
302 (??{ $double_quoted_text_regexp }) |
303 (??{ $single_quoted_text_regexp })
304 |
305 # Expression containing sub-brackets.
306 (??{ $matching_square_brackets_regexp })
307 )*
308 \]
309 32 /sxo;
310  
311 32 our ( $matching_round_brackets_regexp );
312  
313 $matching_round_brackets_regexp = qr/
314 \(
315 (?:
316 # Bracketless, quoteless subtext.
317 (?> [^\(\)\"\']+ )
318 |
319 # Quoted text.
320 (??{ $double_quoted_text_regexp }) |
321 (??{ $single_quoted_text_regexp })
322 |
323 # Expression containing sub-brackets.
324 (??{ $matching_round_brackets_regexp })
325 )*
326 \)
327 32 /sxo;
328  
329 32 my $bare_identifier_regexp = qr/
330 [a-zA-Z_][a-zA-Z0-9_]*
331 /sxo;
332  
333 32 my $function_regexp = qr/
334 # abc( expr )
335 $bare_identifier_regexp
336 $matching_round_brackets_regexp
337 /sxo;
338  
339 32 my $capture_function_regexp = qr/
340 ^
341 # abc( expr )
342 ($bare_identifier_regexp)
343 \(
344 \s*
345 (.*?)
346 \s*
347 \)
348 $
349 /sxo;
350  
351 # Chained structure:
352 # var.sub.sub
353 # var.sub['sub']
354 # var['sub'].sub
355 # var['sub']['sub'] fails!
356 # var.sub.method()
357 # var['sub'].method()
358 # var.method().sub
359 # var.method()['sub']
360 # func().sub.sub ?
361 # func().method().sub
362  
363 32 my $subscript_operator_regexp = qr/
364 (?: \. | \-\> )
365 /sxo;
366  
367 32 my $expr_subscript_regexp = $matching_square_brackets_regexp;
368 32 my $capture_expr_subscript_regexp = qr/
369 ^
370 \[
371 \s*
372 (.*?)
373 \s*
374 \]
375 $
376 /sxo;
377 32 my $literal_subscript_regexp = qr/
378 $subscript_operator_regexp
379 $bare_identifier_regexp
380 /sxo;
381 32 my $capture_literal_subscript_regexp = qr/
382 ^
383 $subscript_operator_regexp
384 ($bare_identifier_regexp)
385 $
386 /sxo;
387 32 my $method_subscript_regexp = qr/
388 $subscript_operator_regexp
389 $function_regexp
390 /sxo;
391 32 my $capture_method_subscript_regexp = qr/
392 ^
393 # . | ->
394 $subscript_operator_regexp
395 # abc( expr )
396 ($bare_identifier_regexp)
397 \(
398 \s*
399 (.*?)
400 \s*
401 \)
402 # ($matching_round_brackets_regexp)
403 $
404 /sxo;
405  
406 32 my $chained_operation_top_regexp = qr/
407 (?:
408 # Function goes first to take matching precedence over bareword
409 $function_regexp |
410 $bare_identifier_regexp
411 )
412 /sxo;
413 32 my $chained_operation_subscript_regexp = qr/
414 (?:
415 $expr_subscript_regexp |
416 # Method goes first to take matching precedence over bareword
417 $method_subscript_regexp |
418 $literal_subscript_regexp
419 )
420 /sxo;
421 32 my $chained_operation_regexp = qr/
422 $chained_operation_top_regexp
423 (?: $chained_operation_subscript_regexp )*
424 /sxo;
425  
426 32 my $capture_chained_operation_top_regexp = qr/
427 ^
428 ($chained_operation_top_regexp)
429 # we don't care at this point what the rest of the crud is.
430 (.*)
431 $
432 /sxo;
433 32 my $capture_chained_operation_subscript_regexp = qr/
434 ^
435 ($chained_operation_subscript_regexp)
436 # we don't care at this point what the rest of the crud is.
437 (.*)
438 $
439 /sxo;
440  
441 32 my $literal_number_regexp = qr/
442 # 1 or more digits.
443 \d+
444 # Optionally a decimal fraction.
445 (?: \. \d+ )?
446 /sxo;
447  
448 32 my $unary_operator_regexp = qr/
449 (?: \! | not (?=\s) | - )
450 /sxo;
451 32 my $capture_unary_operator_regexp = qr/
452 ^
453 ( \! | not (?=\s) | - )
454 \s*
455 (.*)
456 $
457 /sxo;
458  
459 32 my $atomic_expr_regexp = qr/
460 # Optionally a unary operator
461 (?: $unary_operator_regexp \s* )?
462 # Followed by an atomic value
463 (?:
464 # A bracketed sub-expression.
465 $matching_round_brackets_regexp
466 |
467 # A chained operation.
468 $chained_operation_regexp
469 |
470 # A literal number
471 $literal_number_regexp
472 |
473 # A literal string
474 $single_quoted_text_regexp
475 )
476 /sxo;
477  
478 32 my $operator_regexp = join( '|', map { "\Q$_\E" } keys( %operators ) );
479 32 $operator_regexp = qr/
480 (?: $operator_regexp )
481 /sxo;
482  
483 32 my $expr_regexp = qr/
484 \s*
485 (?:
486 # A sequence of atomic epressions and operators.
487 $atomic_expr_regexp
488 (?:
489 \s+
490 $operator_regexp
491 \s+
492 $atomic_expr_regexp
493 )*
494 )
495 \s*
496 /sxo;
497 # (?:
498 # \s+
499 # $operator_regexp
500 # \s+
501 # $atomic_expr_regexp
502 # )
503 # |
504 # (?:
505 # \s+
506 # \?
507 # \s+
508 # $atomic_expr_regexp
509 # \s+
510 # \:
511 # $atomic_expr_regexp
512 # )
513  
514 32 my $capture_expr_op_remain_regexp = qr/
515 ^
516 \s*
517 ($atomic_expr_regexp)
518 \s+
519 ($operator_regexp)
520 \s+
521 (.*)
522 $
523 /sxo;
524  
525 #my $capture_expr_if_else_remain_regexp = qr/
526 # ^
527 # \s*
528 # ($atomic_expr_regexp)
529 # \s+ \? \s+
530 # ($atomic_expr_regexp)
531 # \s+ \: \s+
532 # ($atomic_expr_regexp)
533 # \s*
534 # (.*?)
535 # \s*
536 # $
537 # /sx;
538  
539 32 my $capture_expr_comma_remain_regexp = qr/
540 ^
541 ($expr_regexp)
542 (?:
543 (?: , | => )
544 \s*
545 (.*)
546 )?
547 $
548 /sxo;
549  
550 BEGIN
551 32 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
552 32 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
553  
554 32 $Template::Sandbox::VERSION = '1.04_01';
555 32 @Template::Sandbox::ISA = qw( Exporter );
556  
557 32 @Template::Sandbox::EXPORT = qw();
558 32 @Template::Sandbox::EXPORT_OK = qw(
559 inconstant
560 needs_template
561 undef_ok
562 has_args no_args
563 one_arg two_args three_args any_args
564 def_func
565 );
566 32 %Template::Sandbox::EXPORT_TAGS = (
567 function_sugar => [ qw(
568 inconstant
569 needs_template
570 undef_ok
571 has_args no_args
572 one_arg two_args three_args any_args
573 ) ],
574 );
575 }
576  
577 sub _find_local_functions
578 427 ▶{
Subroutine _find_local_functions: called CC=3 SCAR=11.0
579 427 my ( $self ) = @_;
580  
581 427 ▶ return( \%functions ) unless ref( $self );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 31 396 427
582  
583 396 ▶ $self->{ local_functions } ||= {};
Condition100%2/2
$self->{"local_functions"} ||= {}
l!l
22374
MC/DC100%1/1
$self->{"local_functions"} ||= {}
$self->{"local_functions"}
Truth table100%2/2
A
A= $self->{"local_functions"}
Aresult
11
00
584 396 return( $self->{ local_functions } );
585 }
586  
587 sub register_template_function
588 388 ▶{
Subroutine register_template_function: called CC=8 SCAR=20.8
Pod documented
589 388 my $self = shift;
590 388 my ( $local_functions );
591  
592 388 $local_functions = $self->_find_local_functions();
593  
594 388 ▶ while( my $name = shift )
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = shift()) 421 384 805
595 {
596 421 my ( $func );
597  
598 421 $func = shift;
599  
600 # TODO: Carp has errors croaking from here.
601 421 ▶ $self->caller_error(
Branch: 1 branch
BranchTrueFalseTotal
unless ref $func eq "ARRAY" or ref $func eq "CODE" 4 417 421
Condition100%5/5
ref $func eq "ARRAY" or ref $func eq "CODE"
l!l&&r!l&&!r
41614
ref $func || "'${func}'"
l!l
22
MC/DC100%2/2
ref $func eq "ARRAY" or ref $func eq "CODE"
ref $func eq "ARRAY" ref $func eq "CODE"
MC/DC100%1/1
ref $func || "'${func}'"
ref $func
Truth table100%3/3
A or B
A= ref $func eq "ARRAY"B= ref $func eq "CODE"
ABresult
1X1
011
000
Truth table100%2/2
A
A= ref $func
Aresult
11
00
602 "Bad template function '$name' to register_template_function(), " .
603 "expected sub ref or 'function_sugar'ed sub ref, got: " .
604 ( ref( $func ) || "'$func'" ) )
605 unless ref( $func ) eq 'ARRAY' or ref( $func ) eq 'CODE';
606  
607 # do local $^W = undef; in calling block to suppress.
608 $self->caller_warning(
609 "Template function '$name' exists, overwriting." )
610 417 ▶ if $^W and $local_functions->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
if $^W and $local_functions->{$name} 1 416 417
Condition66%2/3
$^W and $local_functions->{$name}
!ll&&!rl&&r
41601
MC/DC50%1/2
$^W and $local_functions->{$name}
$^W $local_functions->{$name}
Truth table66%2/3
A and B
A= $^WB= $local_functions->{$name}
ABresult
0X0
100
111
611  
612 # If they don't use the function sugar, we assume they're not fussy
613 # TODO: probably safer to error since constant/inconstant shouldn't be assumed
614 417 ▶ $func = any_args $func if ref( $func ) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $func eq "CODE" 1 416 417
615  
616 417 $local_functions->{ $name } = $func;
617 }
618 }
619  
620 sub add_template_function
621 1 ▶{
Subroutine add_template_function: called CC=1 SCAR=0.0
Pod documented
622 1 my $self = shift;
623  
624 1 $self->register_template_function( @_ );
625 }
626  
627 sub unregister_template_function
628 15 ▶{
Subroutine unregister_template_function: called CC=4 SCAR=13.9
Pod documented
629 15 my $self = shift;
630 15 my ( $local_functions );
631  
632 15 $local_functions = $self->_find_local_functions();
633  
634 15 ▶ while( my $name = shift )
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = shift()) 20 15 35
635 {
636 $self->caller_warning(
637 "Template function '$name' does not exist, cannot be removed." )
638 20 ▶ if $^W and not $local_functions->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
if $^W and not $local_functions->{$name} 1 19 20
Condition66%2/3
$^W and not $local_functions->{$name}
!ll&&!rl&&r
1901
MC/DC50%1/2
$^W and not $local_functions->{$name}
$^W not $local_functions->{$name}
Truth table66%2/3
A and not(B)
A= $^WB= not $local_functions->{$name}
ABresult
0X0
100
111
639  
640 20 delete $local_functions->{ $name };
641 }
642 }
643  
644 sub delete_template_function
645 1 ▶{
Subroutine delete_template_function: called CC=1 SCAR=0.0
Pod documented
646 1 my $self = shift;
647  
648 1 $self->unregister_template_function( @_ );
649 }
650  
651 sub _find_local_syntaxes
652 30 ▶{
Subroutine _find_local_syntaxes: called CC=4 SCAR=13.9
653 30 my ( $self ) = @_;
654  
655 30 ▶ return( \%syntaxes ) unless ref( $self );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 4 26 30
656  
657 $self->{ local_syntaxes } ||= {
658 # Faux values to define the auto opcode generation for local syntaxes.
659 # We use negative values to avoid clash with class-level opcodes.
660 26 ▶ '.next_instr' => -(LOCAL_SYNTAX),
Condition100%2/2
$self->{"local_syntaxes"} ||= {".next_instr", -1000000, ".instr_increment", -1, ".instr", {}}
l!l
917
MC/DC100%1/1
$self->{"local_syntaxes"} ||= {".next_instr", -1000000, ".instr_increment", -1, ".instr", {}}
$self->{"local_syntaxes"}
Truth table100%2/2
A
A= $self->{"local_syntaxes"}
Aresult
11
00
661 '.instr_increment' => -1,
662 '.instr' => {},
663 };
664 26 ▶ $self->{ local_token_aliases } ||= {};
Condition100%2/2
$self->{"local_token_aliases"} ||= {}
l!l
917
MC/DC100%1/1
$self->{"local_token_aliases"} ||= {}
$self->{"local_token_aliases"}
Truth table100%2/2
A
A= $self->{"local_token_aliases"}
Aresult
11
00
665 26 return( $self->{ local_syntaxes } );
666 }
667  
668 sub register_template_syntax
669 24 ▶{
Subroutine register_template_syntax: called CC=8 SCAR=20.8
Pod documented
670 24 my $self = shift;
671 24 my ( $local_syntaxes );
672  
673 24 $local_syntaxes = $self->_find_local_syntaxes();
674  
675 24 ▶ while( my $name = shift )
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = shift()) 24 16 40
676 {
677 24 my ( $syntax );
678  
679 24 $syntax = shift;
680  
681 # TODO: Carp has errors when croaking from here.
682 24 ▶ $self->caller_error(
Branch: 1 branch
BranchTrueFalseTotal
unless ref $syntax eq "HASH" 4 20 24
Condition100%2/2
ref $syntax || "'${syntax}'"
l!l
22
MC/DC100%1/1
ref $syntax || "'${syntax}'"
ref $syntax
Truth table100%2/2
A
A= ref $syntax
Aresult
11
00
683 "Bad template syntax '$name' to register_template_syntax(), " .
684 "expected hash ref, got: " . ( ref( $syntax ) || "'$syntax'" ) )
685 unless ref( $syntax ) eq 'HASH';
686  
687 $self->caller_error( "Missing compile callback for syntax $name" )
688 20 ▶ unless $syntax->{ compile };
Branch: 1 branch
BranchTrueFalseTotal
unless $syntax->{"compile"} 2 18 20
689 $self->caller_error( "Missing run callback for syntax $name" )
690 18 ▶ unless $syntax->{ run };
Branch: 1 branch
BranchTrueFalseTotal
unless $syntax->{"run"} 2 16 18
691  
692 # do local $^W = undef; in calling block to suppress.
693 $self->caller_warning(
694 "Template syntax '$name' exists, overwriting." )
695 16 ▶ if $^W and $local_syntaxes->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
if $^W and $local_syntaxes->{$name} 1 15 16
Condition66%2/3
$^W and $local_syntaxes->{$name}
!ll&&!rl&&r
1501
MC/DC50%1/2
$^W and $local_syntaxes->{$name}
$^W $local_syntaxes->{$name}
Truth table66%2/3
A and B
A= $^WB= $local_syntaxes->{$name}
ABresult
0X0
100
111
696  
697 16 $syntax = { %{$syntax} };
698  
699 # Icky.
700 16 $syntax->{ instr } = $local_syntaxes->{ '.next_instr' };
701 $local_syntaxes->{ '.next_instr' } +=
702 16 $local_syntaxes->{ '.instr_increment' };
703  
704 16 $local_syntaxes->{ $name } = { %{$syntax} };
705 16 $local_syntaxes->{ '.instr' }->{ $syntax->{ instr } } = $name;
706 }
707 }
708  
709 sub add_template_syntax
710 1 ▶{
Subroutine add_template_syntax: called CC=1 SCAR=0.0
Pod documented
711 1 my $self = shift;
712  
713 1 $self->register_template_syntax( @_ );
714 }
715  
716 sub unregister_template_syntax
717 6 ▶{
Subroutine unregister_template_syntax: called CC=4 SCAR=13.9
Pod documented
718 6 my $self = shift;
719 6 my ( $local_syntaxes );
720  
721 6 $local_syntaxes = $self->_find_local_syntaxes();
722  
723 6 ▶ while( my $name = shift )
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = shift()) 6 6 12
724 {
725 6 ▶ unless( $local_syntaxes->{ $name } )
Branch: 1 branch
BranchTrueFalseTotal
unless ($local_syntaxes->{$name}) 2 4 6
726 {
727 2 ▶ $self->caller_warning(
Branch: 1 branch
BranchTrueFalseTotal
if $^W 1 1 2
728 "Template syntax '$name' does not exist, cannot be removed." )
729 if $^W;
730 2 next;
731 }
732  
733 delete $local_syntaxes->{ '.instr' }->{
734 4 $local_syntaxes->{ $name }->{ instr } };
735 4 delete $local_syntaxes->{ $name };
736 }
737 }
738  
739 sub delete_template_syntax
740 1 ▶{
Subroutine delete_template_syntax: called CC=1 SCAR=0.0
Pod documented
741 1 my $self = shift;
742  
743 1 $self->unregister_template_syntax( @_ );
744 }
745  
746 sub get_valid_singular_constructor_param
747 1479 ▶{
Subroutine get_valid_singular_constructor_param: called CC=1 SCAR=0.0
Pod documented
748 1479 my ( $self ) = @_;
749  
750 1479 return( qw/template cache logger template_root allow_bare_expr
751 ignore_module_dependencies open_delimiter close_delimiter
752 vmethods template_toolkit_compat/ );
753 }
754  
755 sub get_valid_multiple_constructor_param
756 1479 ▶{
Subroutine get_valid_multiple_constructor_param: called CC=1 SCAR=0.0
Pod documented
757 1479 my ( $self ) = @_;
758  
759 1479 return( qw/copy_global_functions template_function template_syntax
760 library/ );
761 }
762  
763 # TODO: implement these constructor options:
764 # -
765 sub new
766 1479 ▶{
Subroutine new: called CC=6 SCAR=17.9
Pod documented
767 1479 my $this = shift;
768 1479 my ( $self, %param, %valid_singular, %valid_multiple );
769  
770 1479 ▶ $self = bless {}, ref( $this ) || $this;
Condition66%2/3
ref $this || $this
l!l&&r!l&&!r
214770
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table66%2/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
771  
772 %valid_singular =
773 1479 map { $_ => 1 } $self->get_valid_singular_constructor_param();
774 %valid_multiple =
775 1479 map { $_ => 1 } $self->get_valid_multiple_constructor_param();
776  
777 # Read remaining args.
778 1479 %param = ();
779 1479 ▶ while( my $param_name = shift )
Branch: 1 branch
BranchTrueFalseTotal
if (my $param_name = shift()) 418 1478 1896
780 {
781 418 my $param_value = shift;
782  
783 418 ▶ if( $valid_singular{ $param_name } )
Branch: 2 branches
BranchTrueFalseTotal
if ($valid_singular{$param_name}) { } 381 37 418
elsif ($valid_multiple{$param_name}) { } 36 1 37
784 {
785 381 $param{ $param_name } = $param_value;
786 }
787 elsif( $valid_multiple{ $param_name } )
788 {
789 36 ▶ $param{ $param_name } ||= [];
Condition100%2/2
$param{$param_name} ||= []
l!l
135
MC/DC100%1/1
$param{$param_name} ||= []
$param{$param_name}
Truth table100%2/2
A
A= $param{$param_name}
Aresult
11
00
790 36 push @{$param{ $param_name }}, $param_value;
791 }
792 else
793 {
794 1 $self->caller_error( "Unknown constructor param: '$param_name'" );
795 }
796 }
797  
798 1478 $self->{ phase } = 'initialization';
799 1478 $self->initialize( \%param );
800 1470 $self->{ phase } = 'post-initialization';
801  
802 1470 return( $self );
803 }
804  
805 sub initialize
806 1478 ▶{
Subroutine initialize: called CC=22 SCAR=30.9
Pod documented
807 1478 my ( $self, $param ) = @_;
808  
809 # Do this early in case anything needs logging.
810 1478 ▶ $self->{ logger } = $param->{ logger } if exists $param->{ logger };
Branch: 1 branch
BranchTrueFalseTotal
if exists $param->{"logger"} 3 1475 1478
811  
812 # For the paranoid, to prevent other code changing them after
813 # we initialize.
814 $self->{ local_functions } = Clone::clone( \%functions )
815 1478 ▶ if exists $param->{ copy_global_functions };
Branch: 1 branch
BranchTrueFalseTotal
if exists $param->{"copy_global_functions"} 1 1477 1478
816  
817 1478 ▶ if( exists $param->{ template_function } )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param->{"template_function"}) 19 1459 1478
818 {
819 19 foreach my $arg ( @{$param->{ template_function }} )
820 {
821 20 $self->register_template_function( @{$arg} );
822 }
823 }
824 1476 ▶ if( exists $param->{ template_syntax } )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param->{"template_syntax"}) 9 1467 1476
825 {
826 9 foreach my $arg ( @{$param->{ template_syntax }} )
827 {
828 9 $self->register_template_syntax( @{$arg} );
829 }
830 }
831 1472 ▶ if( exists $param->{ library } )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param->{"library"}) 6 1466 1472
832 {
833 6 foreach my $arg ( @{$param->{ library }} )
834 {
835 # Neccessary?
836 # eval "use $arg->[ 0 ];";
837 $arg->[ 0 ]->export_template_functions( $self,
838 6 @{$arg}[ 1..@{$arg} - 1 ] );
839 }
840 }
841  
842 $self->{ ignore_module_dependencies } =
843 $param->{ ignore_module_dependencies }
844 1472 ▶ if exists $param->{ ignore_module_dependencies };
Branch: 1 branch
BranchTrueFalseTotal
if exists $param->{"ignore_module_dependencies"} 8 1464 1472
845  
846 1472 ▶ if( $param->{ template_toolkit_compat } )
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"template_toolkit_compat"}) 5 1467 1472
847 {
848 $self->{ open_delimiter } = '[%'
849 5 ▶ unless exists $param->{ open_delimiter };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $param->{"open_delimiter"} 4 1 5
850 $self->{ close_delimiter } = '%]'
851 5 ▶ unless exists $param->{ close_delimiter };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $param->{"close_delimiter"} 4 1 5
852 $self->{ allow_bare_expr } = 1
853 5 ▶ unless exists $param->{ allow_bare_expr };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $param->{"allow_bare_expr"} 4 1 5
854 $self->{ vmethods } = 1
855 5 ▶ unless exists $param->{ vmethods };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $param->{"vmethods"} 4 1 5
856 }
857  
858 $self->{ open_delimiter } = $param->{ open_delimiter }
859 1472 ▶ if exists $param->{ open_delimiter };
Branch: 1 branch
BranchTrueFalseTotal
if exists $param->{"open_delimiter"} 65 1407 1472
860 $self->{ close_delimiter } = $param->{ close_delimiter }
861 1472 ▶ if exists $param->{ close_delimiter };
Branch: 1 branch
BranchTrueFalseTotal
if exists $param->{"close_delimiter"} 65 1407 1472
862 $self->{ allow_bare_expr } = $param->{ allow_bare_expr }
863 1472 ▶ if exists $param->{ allow_bare_expr };
Branch: 1 branch
BranchTrueFalseTotal
if exists $param->{"allow_bare_expr"} 212 1260 1472
864 $self->{ vmethods } = $param->{ vmethods }
865 1472 ▶ if exists $param->{ vmethods };
Branch: 1 branch
BranchTrueFalseTotal
if exists $param->{"vmethods"} 1 1471 1472
866  
867 # No need to use set_cache(), from initialize we're the first set
868 # so don't need to clear flags.
869 $self->{ cache } = $param->{ cache }
870 1472 ▶ if exists $param->{ cache };
Branch: 1 branch
BranchTrueFalseTotal
if exists $param->{"cache"} 16 1456 1472
871 $self->{ template_root } = $param->{ template_root }
872 1472 ▶ if exists $param->{ template_root };
Branch: 1 branch
BranchTrueFalseTotal
if exists $param->{"template_root"} 2 1470 1472
873 $self->set_template( $param->{ template } )
874 1472 ▶ if exists $param->{ template };
Branch: 1 branch
BranchTrueFalseTotal
if exists $param->{"template"} 4 1468 1472
875  
876 1470 $self->{ vars } = {};
877 1470 $self->{ debug } = {};
878 }
879  
880 sub set_cache
881 0 ▶{
Subroutine set_cache: not called CC=2 SCAR=17.9
Pod documented
882 0 my ( $self, $cache ) = @_;
883  
884 0 $self->{ cache } = $cache;
885 delete $self->{ cache_uses_extended_set }
886 0 ▶ if exists $self->{ cache_uses_extended_set };
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"cache_uses_extended_set"} 0 0 0
887 }
888  
889 sub _cache_uses_extended_set
890 8 ▶{
Subroutine _cache_uses_extended_set: called CC=5 SCAR=16.8
891 8 my ( $self ) = @_;
892 8 my ( $cache );
893  
894 return( $self->{ cache_uses_extended_set } )
895 8 ▶ if exists $self->{ cache_uses_extended_set };
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"cache_uses_extended_set"} 0 8 8
896  
897 8 $cache = $self->{ cache };
898 8 ▶ return( $self->{ cache_uses_extended_set } = 1 )
Branch: 1 branch
BranchTrueFalseTotal
if $cache->isa("Cache::CacheFactory") or $cache->can("set_takes_named_param") and $cache->set_takes_named_param 4 4 8
Condition50%3/6
$cache->isa("Cache::CacheFactory") or $cache->can("set_takes_named_param") and $cache->set_takes_named_param
l!l&&r!l&&!r
404
$cache->can("set_takes_named_param") and $cache->set_takes_named_param
!ll&&!rl&&r
400
MC/DC33%1/3
$cache->isa("Cache::CacheFactory") or $cache->can("set_takes_named_param") and $cache->set_takes_named_param
$cache->isa("Cache::CacheFactory") $cache->can("set_takes_named_param") $cache->set_takes_named_param
Truth table50%2/4
A or B and C
A= $cache->isa("Cache::CacheFactory")B= $cache->can("set_takes_named_param")C= $cache->set_takes_named_param
ABCresult
1XX1
0111
00X0
0100
899 if $cache->isa( 'Cache::CacheFactory' ) or
900 ( $cache->can( 'set_takes_named_param' ) and
901 $cache->set_takes_named_param() );
902 4 return( $self->{ cache_uses_extended_set } = 0 );
903 }
904  
905 sub set_template_root
906 2 ▶{
Subroutine set_template_root: called CC=1 SCAR=0.0
Pod documented
907 2 my ( $self, $dir ) = @_;
908  
909 2 $self->{ template_root } = $dir;
910 }
911  
912 sub get_template_candidates
913 31 ▶{
Subroutine get_template_candidates: called CC=2 SCAR=6.9
Pod documented
914 31 my ( $self, $filename, $current_dir ) = @_;
915  
916 return( $self->{ template_root } ?
917 31 ▶ File::Spec->catfile( $self->{ template_root }, $filename ) :
Branch: 1 branch
BranchTrueFalseTotal
$self->{'template_root'} ? : 4 27 31
918 $filename );
919 }
920  
921 sub get_include_candidates
922 15 ▶{
Subroutine get_include_candidates: called CC=2 SCAR=7.2
Pod documented
923 15 my ( $self, $filename, $current_dir ) = @_;
924  
925 15 ▶ return( $current_dir ?
Branch: 1 branch
BranchTrueFalseTotal
$current_dir ? : 15 0 15
926 File::Spec->catfile( $current_dir, $filename ) :
927 $filename );
928 }
929  
930 sub find_template
931 31 ▶{
Subroutine find_template: called CC=3 SCAR=11.0
Pod documented
932 31 my ( $self, $filename, $current_dir ) = @_;
933 31 my ( @candidates );
934  
935 31 @candidates = $self->get_template_candidates( $filename, $current_dir );
936 31 foreach my $candidate ( @candidates )
937 {
938 31 ▶ return( $candidate ) if -e $candidate;
Branch: 1 branch
BranchTrueFalseTotal
if -e $candidate 29 2 31
939 }
940  
941 2 $self->error( "Unable to find matching template from candidates:\n" .
942 join( "\n", @candidates ) );
943 }
944  
945 sub find_include
946 15 ▶{
Subroutine find_include: called CC=3 SCAR=11.0
Pod documented
947 15 my ( $self, $filename, $current_dir ) = @_;
948 15 my ( @candidates );
949  
950 15 @candidates = $self->get_include_candidates( $filename, $current_dir );
951  
952 15 foreach my $candidate ( @candidates )
953 {
954 15 ▶ return( $candidate ) if -e $candidate;
Branch: 1 branch
BranchTrueFalseTotal
if -e $candidate 14 1 15
955 }
956  
957 1 $self->error( "Unable to find matching include from candidates:\n" .
958 join( "\n", @candidates ) );
959 }
960  
961 sub cache_key
962 17 ▶{
Subroutine cache_key: called CC=2 SCAR=7.0
Pod documented
963 17 my ( $self, $keys ) = @_;
964  
965 return( Digest::MD5::md5_hex(
966 join( '',
967 17 ▶ map { $_ . ( $keys->{ $_ } || '' ) } sort( keys( %{$keys} ) ) )
Condition50%1/2
$keys->{$_} || ''
l!l
170
MC/DC0%0/1
$keys->{$_} || ''
$keys->{$_}
Truth table50%1/2
A
A= $keys->{$_}
Aresult
11
00
968 ) );
969  
970 # local $Storable::canonical = 1;
971 #
972 # return( Digest::MD5::md5_hex( Storable::nfreeze( $keys ) ) );
973 }
974  
975 sub get_additional_dependencies
976 1442 ▶{
Subroutine get_additional_dependencies: called CC=1 SCAR=0.0
Pod documented
977 # my ( $self ) = @_;
978  
979 1442 return( [] );
980 }
981  
982 sub set_template
983 31 ▶{
Subroutine set_template: called CC=8 SCAR=20.8
Pod documented
984 31 my ( $self, $filename, $defines ) = @_;
985 31 my ( $cache_key );
986  
987 # Shallow copy is safe, keys/values should only be scalars.
988 31 ▶ $self->{ defines } = $defines = $defines ? { %{$defines} } : {};
Branch: 1 branch
BranchTrueFalseTotal
$defines ? : 1 30 31
989 31 $self->{ special_values } = {};
990 31 delete $self->{ template };
991  
992 $defines->{ FILENAME } = $self->{ filename } =
993 31 $self->find_template( $filename );
994  
995 # $defines at this stage includes all unique compile-time
996 # parameters that effect the final compiled template, this
997 # is more than just the filename, so we need to generate
998 # a simple string key from multiple inputs.
999 return if $self->{ cache } and
1000 ( $self->{ template } = $self->{ cache }->get(
1001 29 ▶ $cache_key = $self->cache_key( $defines )
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cache"} and $self->{"template"} = $self->{"cache"}->get($cache_key = $self->cache_key($defines)) 4 25 29
Condition100%3/3
$self->{"cache"} and $self->{"template"} = $self->{"cache"}->get($cache_key = $self->cache_key($defines))
!ll&&!rl&&r
2144
MC/DC100%2/2
$self->{"cache"} and $self->{"template"} = $self->{"cache"}->get($cache_key = $self->cache_key($defines))
$self->{"cache"} $self->{"template"} = $self->{"cache"}->get($cache_key = $self->cache_key($defines))
Truth table100%3/3
A and B
A= $self->{"cache"}B= $self->{"template"} = $self->{"cache"}->get($cache_key = $self->cache_key($defines))
ABresult
0X0
100
111
1002 ) );
1003  
1004 25 my $compiletime = time(); # Before the compile, to be safe.
1005  
1006 25 $self->{ dependencies } = $self->get_additional_dependencies();
1007  
1008 # If we're caching, the validity of the cache depends on the
1009 # last-modified of the template module as well as the template
1010 # files, unless we've been told to ignore it.
1011 25 ▶ if( $self->{ cache } and not $self->{ ignore_module_dependencies } )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cache"} and not $self->{"ignore_module_dependencies"}) 2 23 25
Condition100%3/3
$self->{"cache"} and not $self->{"ignore_module_dependencies"}
!ll&&!rl&&r
2122
MC/DC100%2/2
$self->{"cache"} and not $self->{"ignore_module_dependencies"}
$self->{"cache"} not $self->{"ignore_module_dependencies"}
Truth table100%3/3
A and not(B)
A= $self->{"cache"}B= not $self->{"ignore_module_dependencies"}
ABresult
0X0
100
111
1012 {
1013 2 my ( $class_handle );
1014  
1015 2 $class_handle = Class::Handle->new( ref( $self ) );
1016 2 push @{$self->{ dependencies }},
1017 # TODO: Ew, ugly and non-portable.
1018 4 grep { defined( $_ ) }
1019 2 map { s/\:\:/\//g; s/$/\.pm/; $INC{ $_ }; }
1020 $class_handle->self_and_super_path();
1021 }
1022  
1023 $self->{ template } =
1024 25 $self->_read_template( $self->{ filename }, $defines );
1025  
1026 23 $self->_compile_template();
1027  
1028 18 ▶ if( $self->{ cache } )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cache"}) 4 14 18
1029 {
1030 # If they're using Cache::CacheFactory we can make use of
1031 # the dependencies and created at timestamps, if not we
1032 # fall back on the basic Cache::Cache style API.
1033 # TODO: wrap compat cache behaviour with our own dependencies checking.
1034 4 ▶ if( $self->_cache_uses_extended_set() )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_cache_uses_extended_set) { } 2 2 4
1035 {
1036 $self->{ cache }->set(
1037 key => $cache_key,
1038 data => $self->{ template },
1039 dependencies => $self->{ dependencies },
1040 2 created_at => $compiletime,
1041 );
1042 }
1043 else
1044 {
1045 2 $self->{ cache }->set( $cache_key, $self->{ template } );
1046 }
1047 }
1048 }
1049  
1050 # TODO: split/merge parts from set_template() above.
1051 sub set_template_string
1052 1421 ▶{
Subroutine set_template_string: called CC=8 SCAR=20.8
Pod documented
1053 1421 my ( $self, $template_string, $defines ) = @_;
1054 1421 my ( $cache_key );
1055  
1056 # Shallow copy is safe, keys/values should only be scalars.
1057 1421 ▶ $self->{ defines } = $defines = $defines ? { %{$defines} } : {};
Branch: 1 branch
BranchTrueFalseTotal
$defines ? : 5 1416 1421
1058 1421 $self->{ special_values } = {};
1059 1421 delete $self->{ template };
1060  
1061 # Erk. Better way of making this cacheable surely?
1062 $defines->{ FILENAME } = $self->{ filename } =
1063 1421 'string:///' . $template_string;
1064  
1065 # $defines at this stage includes all unique compile-time
1066 # parameters that effect the final compiled template, this
1067 # is more than just the filename, so we need to generate
1068 # a simple string key from multiple inputs.
1069 return if $self->{ cache } and
1070 ( $self->{ template } = $self->{ cache }->get(
1071 1421 ▶ $cache_key = $self->cache_key( $defines )
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cache"} and $self->{"template"} = $self->{"cache"}->get($cache_key = $self->cache_key($defines)) 4 1417 1421
Condition100%3/3
$self->{"cache"} and $self->{"template"} = $self->{"cache"}->get($cache_key = $self->cache_key($defines))
!ll&&!rl&&r
141344
MC/DC100%2/2
$self->{"cache"} and $self->{"template"} = $self->{"cache"}->get($cache_key = $self->cache_key($defines))
$self->{"cache"} $self->{"template"} = $self->{"cache"}->get($cache_key = $self->cache_key($defines))
Truth table100%3/3
A and B
A= $self->{"cache"}B= $self->{"template"} = $self->{"cache"}->get($cache_key = $self->cache_key($defines))
ABresult
0X0
100
111
1072 ) );
1073  
1074 1417 my $compiletime = time(); # Before the compile, to be safe.
1075  
1076 1417 $self->{ dependencies } = $self->get_additional_dependencies();
1077  
1078 # If we're caching, the validity of the cache depends on the
1079 # last-modified of the template module as well as the template
1080 # files, unless we've been told to ignore it.
1081 1417 ▶ if( $self->{ cache } and not $self->{ ignore_module_dependencies } )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cache"} and not $self->{"ignore_module_dependencies"}) 2 1415 1417
Condition100%3/3
$self->{"cache"} and not $self->{"ignore_module_dependencies"}
!ll&&!rl&&r
141322
MC/DC100%2/2
$self->{"cache"} and not $self->{"ignore_module_dependencies"}
$self->{"cache"} not $self->{"ignore_module_dependencies"}
Truth table100%3/3
A and not(B)
A= $self->{"cache"}B= not $self->{"ignore_module_dependencies"}
ABresult
0X0
100
111
1082 {
1083 2 my ( $class_handle );
1084  
1085 2 $class_handle = Class::Handle->new( ref( $self ) );
1086 2 push @{$self->{ dependencies }},
1087 # TODO: Ew, ugly and non-portable.
1088 4 grep { defined( $_ ) }
1089 2 map { s/\:\:/\//g; s/$/\.pm/; $INC{ $_ }; }
1090 $class_handle->self_and_super_path();
1091 }
1092  
1093 $self->{ template } =
1094 1417 $self->_read_template_from_string( $template_string, $defines );
1095  
1096 1417 $self->_compile_template();
1097  
1098 1318 ▶ if( $self->{ cache } )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cache"}) 4 1314 1318
1099 {
1100 # If they're using Cache::CacheFactory we can make use of
1101 # the dependencies and created at timestamps, if not we
1102 # fall back on the basic Cache::Cache style API.
1103 # TODO: wrap compat cache behaviour with our own dependencies checking.
1104 4 ▶ if( $self->_cache_uses_extended_set() )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_cache_uses_extended_set) { } 2 2 4
1105 {
1106 $self->{ cache }->set(
1107 key => $cache_key,
1108 data => $self->{ template },
1109 dependencies => $self->{ dependencies },
1110 2 created_at => $compiletime,
1111 );
1112 }
1113 else
1114 {
1115 2 $self->{ cache }->set( $cache_key, $self->{ template } );
1116 }
1117 }
1118 }
1119  
1120 sub _error_message
1121 162 ▶{
Subroutine _error_message: called CC=11 SCAR=24.1
1122 162 my $self = shift;
1123 162 my ( $error, $pos );
1124  
1125 162 ▶ $self = {} unless ref( $self ); # Hack for calling as a class method.
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 2 160 162
1126  
1127 162 $error = join( '', @_ );
1128 162 ▶ $error = "Template " . ( $self->{ phase } ? $self->{ phase } . ' ' : '' ) .
Branch: 1 branch
BranchTrueFalseTotal
$self->{'phase'} ? : 151 11 162
1129 "error: $error";
1130 162 $pos = $self->{ current_pos };
1131 162 ▶ if( $pos )
Branch: 1 branch
BranchTrueFalseTotal
if ($pos) 127 35 162
1132 {
1133 127 my ( $files );
1134  
1135 127 ▶ if( $self->{ template } and
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'template'} and ref $self->{'template'} eq 'HASH' and $self->{'template'}{'files'}) { } 23 104 127
elsif ($self->{'files'}) { } 104 0 104
Condition66%4/6
$self->{"template"} and ref $self->{"template"} eq "HASH" and $self->{"template"}{"files"}
!ll&&!rl&&r
104023
$self->{"template"} and ref $self->{"template"} eq "HASH"
!ll&&!rl&&r
104023
MC/DC33%1/3
$self->{"template"} and ref $self->{"template"} eq "HASH" and $self->{"template"}{"files"}
$self->{"template"} ref $self->{"template"} eq "HASH" $self->{"template"}{"files"}
Truth table50%2/4
A and B and C
A= $self->{"template"}B= ref $self->{"template"} eq "HASH"C= $self->{"template"}{"files"}
ABCresult
0XX0
10X0
1100
1111
1136 ( ref( $self->{ template } ) eq 'HASH' ) and
1137 $self->{ template }->{ files } )
1138 {
1139 23 $files = $self->{ template }->{ files };
1140 }
1141 elsif( $self->{ files } )
1142 {
1143 104 $files = $self->{ files };
1144 }
1145 else
1146 {
1147 0 $files = [];
1148 }
1149 127 $error .= " at line $pos->[ 1 ], char $pos->[ 2 ] of " .
1150 "'$files->[ $pos->[ 0 ] ]'";
1151 127 ▶ if( $self->{ pos_stack } )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pos_stack"}) 104 23 127
1152 {
1153 104 my ( $first );
1154  
1155 104 $first = 1;
1156 104 foreach $pos ( @{$self->{ pos_stack }} )
1157 {
1158 105 ▶ $error .= "\n called from " .
Branch: 1 branch
BranchTrueFalseTotal
unless $first 1 104 105
1159 "line $pos->[ 1 ], char $pos->[ 2 ] of " .
1160 "'$files->[ $pos->[ 0 ] ]'"
1161 unless $first;
1162 105 $first = 0;
1163 }
1164 }
1165 }
1166 162 return( $error );
1167 }
1168  
1169 sub _get_logger
1170 160 ▶{
Subroutine _get_logger: called CC=2 SCAR=6.9
1171 160 my ( $self ) = @_;
1172  
1173 160 ▶ $self->{ logger } = Log::Any->get_logger() unless exists $self->{ logger };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"logger"} 138 22 160
1174 }
1175  
1176 sub log_error
1177 142 ▶{
Subroutine log_error: called CC=3 SCAR=11.0
Pod documented
1178 142 my ( $self, $message ) = @_;
1179  
1180 142 ▶ return unless ref( $self ); # No logging if class method.
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 1 141 142
1181 141 $self->_get_logger();
1182 141 ▶ $self->{ logger }->error( $message ) if $self->{ logger };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"logger"} 140 1 141
1183 }
1184  
1185 sub log_warning
1186 20 ▶{
Subroutine log_warning: called CC=3 SCAR=11.0
Pod documented
1187 20 my ( $self, $message ) = @_;
1188  
1189 20 ▶ return unless ref( $self ); # No logging if class method.
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 1 19 20
1190 19 $self->_get_logger();
1191 19 ▶ $self->{ logger }->warning( $message ) if $self->{ logger };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"logger"} 18 1 19
1192 }
1193  
1194 sub error
1195 123 ▶{
Subroutine error: called CC=1 SCAR=0.0
Pod documented
1196 123 my $self = shift;
1197 123 my ( $message );
1198  
1199 123 $message = $self->_error_message( @_ );
1200 123 $self->log_error( $message );
1201 123 $self->fatal_exit( $message );
1202 }
1203  
1204 sub caller_error
1205 19 ▶{
Subroutine caller_error: called CC=1 SCAR=0.0
Pod documented
1206 19 my $self = shift;
1207 19 my ( $message );
1208  
1209 19 $message = $self->_error_message( @_ );
1210 19 $self->log_error( $message );
1211 19 $self->caller_fatal_exit( $message );
1212 }
1213  
1214 sub fatal_exit
1215 123 ▶{
Subroutine fatal_exit: called CC=1 SCAR=0.0
Pod documented
1216 123 my ( $self, $message ) = @_;
1217  
1218 123 die $message;
1219 }
1220  
1221 sub caller_fatal_exit
1222 19 ▶{
Subroutine caller_fatal_exit: called CC=1 SCAR=0.0
Pod documented
1223 19 my ( $self, $message ) = @_;
1224  
1225 # TODO: restore once Carp stops dying with:
1226 # Bizarre copy of HASH in sassign at [...]/Carp/Heavy.pm line 96.
1227 # croak $message;
1228 19 die $message;
1229 }
1230  
1231 sub warning
1232 15 ▶{
Subroutine warning: called CC=1 SCAR=0.0
Pod documented
1233 15 my $self = shift;
1234 15 my ( $message );
1235  
1236 15 $message = $self->_error_message( @_ );
1237 15 $self->log_warning( $message );
1238 15 warn $message;
1239 }
1240  
1241 sub caller_warning
1242 5 ▶{
Subroutine caller_warning: called CC=1 SCAR=0.0
Pod documented
1243 5 my $self = shift;
1244 5 my ( $message );
1245  
1246 5 $message = $self->_error_message( @_ );
1247 5 $self->log_warning( $message );
1248 5 carp $message;
1249 }
1250  
1251 sub add_var
1252 514 ▶{
Subroutine add_var: called CC=2 SCAR=6.9
Pod documented
1253 514 my ( $self, $var, $value ) = @_;
1254  
1255 514 ▶ $self->caller_error(
Branch: 1 branch
BranchTrueFalseTotal
if $var =~ /\./o 1 513 514
1256 "Bad argument to add_var, expected top-level variable name, got: $var"
1257 )
1258 if $var =~ /\./o;
1259  
1260 513 $self->{ vars }->{ $var } = $value;
1261 }
1262  
1263 sub add_vars
1264 369 ▶{
Subroutine add_vars: called CC=3 SCAR=11.0
Pod documented
1265 369 my ( $self, $vars ) = @_;
1266 369 my ( @bad_vars );
1267  
1268 $self->caller_error(
1269 "Bad var(s) in add_vars, expected top-level variable name, got: " .
1270 join( ', ', @bad_vars )
1271 )
1272 369 ▶ if @bad_vars = grep /\./o, keys( %{$vars} );
Branch: 1 branch
BranchTrueFalseTotal
if @bad_vars = grep(/\./o, keys %{$vars;}) 1 368 369
1273  
1274 368 foreach my $var ( keys( %{$vars} ) )
1275 {
1276 715 $self->{ vars }->{ $var } = $vars->{ $var };
1277 }
1278 }
1279  
1280 sub _var_value
1281 16 ▶{
Subroutine _var_value: called CC=1 SCAR=0.0
1282 16 my ( $self, $var ) = @_;
1283  
1284 16 return( $self->{ vars }->{ $var } );
1285 }
1286  
1287 sub merge_var
1288 10 ▶{
Subroutine merge_var: called CC=10 SCAR=23.6
Pod documented
1289 10 my ( $self, $var, $value, $ref ) = @_;
1290  
1291 10 ▶ $ref = $self->{ vars } unless $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref 8 2 10
1292  
1293 #CORE::warn( "merge_var( ",
1294 # Data::Dumper::Dumper( $var ), ", ",
1295 # Data::Dumper::Dumper( $value ), ", ",
1296 # Data::Dumper::Dumper( $ref->{ $var } ), ")\n" );
1297  
1298 10 ▶ unless( exists( $ref->{ $var } ) and ref( $value ) and
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $ref->{$var} and ref $value and ref $value eq "HASH" || ref $value eq "ARRAY") 6 4 10
Condition66%6/9
exists $ref->{$var} and ref $value and ref $value eq "HASH" || ref $value eq "ARRAY"
!ll&&!rl&&r
604
exists $ref->{$var} and ref $value
!ll&&!rl&&r
604
ref $value eq "HASH" or ref $value eq "ARRAY"
l!l&&r!l&&!r
220
MC/DC25%1/4
exists $ref->{$var} and ref $value and ref $value eq "HASH" || ref $value eq "ARRAY"
exists $ref->{$var} ref $value ref $value eq "HASH" ref $value eq "ARRAY"
Truth table60%3/5
A and B and C or D
A= exists $ref->{$var}B= ref $valueC= ref $value eq "HASH"D= ref $value eq "ARRAY"
ABCDresult
0XXX0
10XX0
11000
111X1
11011
1299 ( ref( $value ) eq 'HASH' or ref( $value ) eq 'ARRAY' ) )
1300 {
1301 #CORE::warn( "Doesn't exist, setting\n" );
1302 6 $ref->{ $var } = $value;
1303 6 return;
1304 }
1305  
1306 4 ▶ if( ref( $value ) eq 'HASH' )
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'HASH') { } 2 2 4
elsif (ref $value eq 'ARRAY') { } 2 0 2
1307 {
1308 2 foreach my $key ( keys( %{$value} ) )
1309 {
1310 2 $self->merge_var( $key, $value->{ $key }, $ref->{ $var } );
1311 }
1312 }
1313 elsif( ref( $value ) eq 'ARRAY' )
1314 {
1315 2 ▶ if( ref( $ref->{ $var } ) eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ref->{$var} eq 'ARRAY') { } 2 0 2
1316 {
1317 2 push @{$ref->{ $var }}, @{$value};
1318 }
1319 else
1320 {
1321 # Ew, trying to merge array with non-array?
1322 # TODO: error?
1323 0 $ref->{ $var } = $value;
1324 }
1325 }
1326 }
1327  
1328 sub merge_vars
1329 1 ▶{
Subroutine merge_vars: called CC=2 SCAR=6.9
Pod documented
1330 1 my ( $self, $vars ) = @_;
1331  
1332 1 foreach my $var ( keys( %{$vars} ) )
1333 {
1334 4 $self->merge_var( $var, $vars->{ $var } );
1335 }
1336 }
1337  
1338 sub clear_vars
1339 2 ▶{
Subroutine clear_vars: called CC=1 SCAR=0.0
Pod documented
1340 2 my ( $self ) = @_;
1341  
1342 2 $self->{ vars } = {};
1343 }
1344  
1345 sub _escape_string
1346 4 ▶{
Subroutine _escape_string: called CC=1 SCAR=0.0
1347 4 my ( $self, $string ) = @_;
1348  
1349 4 $string =~ s/\'/\\\'/go;
1350 4 return( $string );
1351 }
1352  
1353 sub _define_value
1354 19 ▶{
Subroutine _define_value: called CC=9 SCAR=22.0
1355 19 my ( $self, $defines, $define, $default, $quote, $pos ) = @_;
1356 19 my ( $value );
1357  
1358 #$self->warning( "replacing define '$define', default '$default', quote is $quote, pos '$pos'" );
1359 19 ▶ if( $self->{ seen_defines }->{ $define }++ )
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'seen_defines'}{$define}++) { } 1 18 19
elsif (defined $defines->{$define}) { } 13 5 18
elsif (defined $default) { } 3 2 5
1360 {
1361 1 $value = "[recursive define '$define']";
1362 }
1363 elsif( defined( $defines->{ $define } ) )
1364 {
1365 13 $value = $defines->{ $define };
1366 }
1367 elsif( defined( $default ) )
1368 {
1369 3 $value = $default;
1370 }
1371 else
1372 {
1373 2 $value = "[undefined preprocessor define '$define']";
1374 }
1375  
1376 19 $value = $self->_replace_defines( $value, $defines );
1377 19 $self->{ seen_defines }->{ $define }--;
1378  
1379 19 ▶ $value = "'" . $self->_escape_string( $value ) . "'" if $quote;
Branch: 1 branch
BranchTrueFalseTotal
if $quote 4 15 19
1380  
1381 19 ▶ if( defined( $pos ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pos) 18 1 19
1382 {
1383 18 my ( $lines, $definelen, $valuelen );
1384  
1385 18 $lines = $value =~ tr/\n//;
1386 18 ▶ $definelen = 3 + ( $quote ? 2 : 0 ) + length( $define ) +
Branch: 2 branches
BranchTrueFalseTotal
$quote ? : 4 14 18
defined $default ? : 3 15 18
1387 ( defined( $default ) ? length( $default ) : 0 );
1388 18 $valuelen = length( $value );
1389  
1390 18 ▶ if( $lines )
Branch: 1 branch
BranchTrueFalseTotal
if ($lines) { } 1 17 18
1391 {
1392 1 push @{$self->{ offsets }}, [ $pos, $valuelen, -$lines,
1393 $definelen - $valuelen,
1394 ]
1395 }
1396 else
1397 {
1398 17 push @{$self->{ offsets }}, [ $pos, $valuelen, 0,
1399 $definelen - $valuelen,
1400 ]
1401 }
1402 }
1403  
1404 19 return( $value );
1405 }
1406  
1407 sub _replace_defines
1408 1470 ▶{
Subroutine _replace_defines: called CC=8 SCAR=20.8
1409 1470 my ( $self, $template_content, $defines ) = @_;
1410 1470 my ( $top );
1411  
1412 # Replace any preprocessor defines.
1413 1470 ▶ unless( $self->{ seen_defines } )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"seen_defines"}) 1451 19 1470
1414 {
1415 1451 $self->{ seen_defines } = {};
1416 1451 $self->{ offsets } = [];
1417 1451 $top = 1;
1418 }
1419 1470 ▶ 1 while $template_content =~ s/\$\{('?)([A-Z0-9_]+)(?::([^\}]*))?\1\}/
Branch: 1 branch
BranchTrueFalseTotal
$template_content =~ s/\$\{('?)([A-Z0-9_]+)(?::([^\}]*))?\1\}/$self->_define_value($defines, $2, $3, $1, $top ? pos $template_content : undef);/egox and do { '???' } 14 1470 1484
1420 19 ▶ $self->_define_value( $defines, $2, $3, $1,
Branch: 1 branch
BranchTrueFalseTotal
$top ? : 18 1 19
1421 $top ? pos( $template_content ) : undef )/geox;
1422 1470 ▶ if( $top )
Branch: 1 branch
BranchTrueFalseTotal
if ($top) 1451 19 1470
1423 {
1424 1451 delete $self->{ seen_defines };
1425 1451 ▶ delete $self->{ offsets } unless @{$self->{ offsets }};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"offsets"};} 1438 13 1451
1426 1451 ▶ if( $self->{ offsets } )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"offsets"}) 13 1438 1451
1427 {
1428 # pos() gives us position in original string, we need to
1429 # renumber to be position in replaced string.
1430 13 my $carry = 0;
1431 13 foreach my $offset ( @{$self->{ offsets }} )
1432 {
1433 18 $offset->[ 0 ] -= $carry;
1434 18 $carry += $offset->[ 3 ];
1435 }
1436 #my $t = $template_content;
1437 #foreach my $offset ( reverse( @{$self->{ offsets }} ) )
1438 #{
1439 # substr( $t, $offset->[ 0 ] + $offset->[ 1 ], 0 ) = "XXX";
1440 # substr( $t, $offset->[ 0 ], 0 ) = "XXX";
1441 #}
1442 #print "Replaced template:\n$t\n";
1443 #use Data::Dumper;
1444 #print "Using offsets: " . Data::Dumper::Dumper( $self->{ offsets } ) . "\n";
1445 }
1446 }
1447  
1448 1470 return( $template_content );
1449 }
1450  
1451 sub _read_template
1452 37 ▶{
Subroutine _read_template: called CC=2 SCAR=6.9
1453 37 my ( $self, $filename, $defines ) = @_;
1454 37 my ( $fh, $template );
1455  
1456 37 push @{$self->{ dependencies }}, $filename;
1457  
1458 37 $fh = IO::File->new( $filename, '<' );
1459 # TODO: $! can get trashed if $filename is interpolated - investigate
1460 # TODO: is this perl 5.10.0's $! bug, or mine?
1461 # $self->caller_error( "Unable to read $filename: $!" ) unless $fh;
1462 37 ▶ $self->caller_error( "Unable to read ", $filename, ": $!" ) unless $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 3 34 37
1463 {
1464 34 local $/;
1465 34 $template = <$fh>;
1466 }
1467 34 $fh->close;
1468  
1469 # Replace any preprocessor defines.
1470 34 $template = $self->_replace_defines( $template, $defines );
1471  
1472 34 return( $template );
1473 }
1474  
1475 sub _read_template_from_string
1476 1417 ▶{
Subroutine _read_template_from_string: called CC=1 SCAR=0.0
1477 1417 my ( $self, $template, $defines ) = @_;
1478  
1479 # Replace any preprocessor defines.
1480 1417 $template = $self->_replace_defines( $template, $defines );
1481  
1482 1417 return( $template );
1483 }
1484  
1485 # Looks for combination of positional and named parameters to a syntax
1486 # token and returns a hashref of named parameters.
1487 # TODO: this is largely obsolete for everything except includes,
1488 # TODO: should be retired in favour of something specialized and faster.
1489 sub _parse_args
1490 32 ▶{
Subroutine _parse_args: called CC=20 SCAR=30.0
1491 32 my ( $self, $args, $type ) = @_;
1492 32 my ( $count, %param, @words, @pos_param, @keyword_param, $instr,
1493 @positions, @valid, $syntax );
1494  
1495 # # Heeeello hackery.
1496 # $args = "iterator=\"$1\" set=\"$2\""
1497 # if $type eq 'for' and $args =~ /^(.*) in (.*)$/o;
1498  
1499 32 ▶ $syntax = $self->{ local_syntaxes }->{ $type } || $syntaxes{ $type };
Condition66%2/3
$self->{'local_syntaxes'}{$type} || $syntaxes{$type}
l!l&&r!l&&!r
12200
MC/DC0%0/2
$self->{'local_syntaxes'}{$type} || $syntaxes{$type}
$self->{'local_syntaxes'}{$type} $syntaxes{$type}
Truth table66%2/3
A || B
A= $self->{'local_syntaxes'}{$type}B= $syntaxes{$type}
ABresult
1X1
011
000
1500  
1501 @positions = $syntax->{ positional_args } ?
1502 32 ▶ @{$syntax->{ positional_args }} : ();
Branch: 1 branch
BranchTrueFalseTotal
$syntax->{'positional_args'} ? : 18 14 32
1503 32 ▶ @valid = $syntax->{ valid_args } ? @{$syntax->{ valid_args }} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$syntax->{'valid_args'} ? : 1 31 32
1504  
1505 32 %param = ();
1506  
1507 32 @words = split( /\s+/, $args );
1508 # Merge quoted args.
1509 # TODO: rename instr to in_str for semantic clarity vs "instr"uction.
1510 32 $instr = 0;
1511 32 ▶ for( $count = 0; $count < @words; $count++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($count < @words) 39 32 71
1512 {
1513 39 ▶ if( $instr )
Branch: 1 branch
BranchTrueFalseTotal
if ($instr) { } 5 34 39
1514 {
1515 5 ▶ $instr = 0 if $words[ $count ] =~ /\"$/;
Branch: 1 branch
BranchTrueFalseTotal
if $words[$count] =~ /\"$/ 2 3 5
1516 5 $words[ $count - 1 ] .= ' ' . $words[ $count ];
1517 5 @words =
1518 ( @words[ 0..$count - 1 ], @words[ $count + 1..@words - 1 ] );
1519 5 $count--;
1520 }
1521 else
1522 {
1523 34 ▶ next unless $words[ $count ] =~ /^\"/ or $words[ $count ] =~ /=\"/;
Branch: 1 branch
BranchTrueFalseTotal
unless $words[$count] =~ /^\"/ or $words[$count] =~ /=\"/ 28 6 34
Condition100%3/3
$words[$count] =~ /^\"/ or $words[$count] =~ /=\"/
l!l&&r!l&&!r
4228
MC/DC100%2/2
$words[$count] =~ /^\"/ or $words[$count] =~ /=\"/
$words[$count] =~ /^\"/ $words[$count] =~ /=\"/
Truth table100%3/3
A or B
A= $words[$count] =~ /^\"/B= $words[$count] =~ /=\"/
ABresult
1X1
011
000
1524 6 ▶ next if $words[ $count ] =~ /\"$/;
Branch: 1 branch
BranchTrueFalseTotal
if $words[$count] =~ /\"$/ 4 2 6
1525 2 $instr = 1;
1526 }
1527 }
1528  
1529 # Split into positional parameters and keyword paramaters.
1530 32 ▶ for( $count = 0; $count < @words; $count++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($count < @words) 28 22 50
1531 {
1532 28 ▶ last if $words[ $count ] =~ /=/;
Branch: 1 branch
BranchTrueFalseTotal
if $words[$count] =~ /=/ 10 18 28
1533 }
1534  
1535 32 ▶ @pos_param = $count ? @words[ 0..$count - 1 ] : ();
Branch: 1 branch
BranchTrueFalseTotal
$count ? : 17 15 32
1536 32 ▶ @keyword_param = $count < @words ?
Branch: 1 branch
BranchTrueFalseTotal
$count < @words ? : 10 22 32
1537 @words[ $count..@words - 1 ] : ();
1538  
1539 # Squidge any "overshoot" positional param onto the final pos param.
1540 # TODO: splice!
1541 32 ▶ @pos_param = ( @pos_param[ 0..@positions - 2 ],
Branch: 1 branch
BranchTrueFalseTotal
if @pos_param > @positions 0 32 32
1542 join( ' ', @pos_param[ @positions - 1..@pos_param - 1 ] ) )
1543 if @pos_param > @positions;
1544  
1545 32 $count = 0;
1546 32 foreach my $word ( @pos_param )
1547 {
1548 18 ▶ $word = $1 if $word =~ /^\"(.*)\"$/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ /^\"(.*)\"$/ 4 14 18
1549 18 $param{ $positions[ $count++ ] } = $word;
1550 }
1551  
1552 32 foreach my $word ( @keyword_param )
1553 {
1554 16 my ( $keyword, $value );
1555  
1556 16 ( $keyword, $value ) = split( /=/, $word, 2 );
1557  
1558 16 ▶ unless( defined( $value ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 1 15 16
1559 {
1560 1 $self->error( "Undefined value for keyword: '$keyword' on " .
1561 "parse_args( $args, $type )" );
1562 }
1563  
1564 15 ▶ $value = $1 if $value =~ /^\"(.*)\"$/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^\"(.*)\"$/ 2 13 15
1565  
1566 # TODO: validate arg names.
1567 15 $param{ $keyword } = $value;
1568 }
1569  
1570 31 return( { %param } );
1571 }
1572  
1573 sub _compile_template
1574 1440 ▶{
Subroutine _compile_template: called CC=91 SCAR=45.1
1575 1440 my ( $self ) = @_;
1576 1440 my ( $i, @hunks, @files, @pos_stack, @nest_stack, @compiled, %includes,
1577 %trim, $trim_next, %file_numbers, @define_stack,
1578 $local_syntaxes, $local_token_aliases, $local_syntax_regexp,
1579 $hunk_regexp, $syntax_regexp,
1580 $open_delimiter, $close_delimiter, $open_regexp, $close_regexp );
1581  
1582 1440 @files = ( $self->{ filename } );
1583 1440 %file_numbers = ( $self->{ filename } => 0 );
1584 1440 $self->{ files } = \@files;
1585
1586 # Stack of what position in which file we're currently at.
1587 @pos_stack = ( [
1588 $file_numbers{ $self->{ filename } }, 1, 1, 0, $self->{ offsets },
1589 1440 ] );
1590 1440 delete $self->{ offsets };
1591 # Stack of what defines are available.
1592 1440 @define_stack = ( $self->{ defines } );
1593 # Stack of unclosed block-level statements.
1594 1440 @nest_stack = ();
1595 # The tokenized/compiled template.
1596 1440 @compiled = ();
1597 # Files we're currently including.
1598 1440 %includes = ( $self->{ filename } => 1 );
1599 # Stuff we're going to trim later.
1600 1440 %trim = ();
1601  
1602 1440 ▶ $open_delimiter = $self->{ open_delimiter } || '<:';
Condition100%2/2
$self->{'open_delimiter'} || '<:'
l!l
651375
MC/DC100%1/1
$self->{'open_delimiter'} || '<:'
$self->{'open_delimiter'}
Truth table100%2/2
A
A= $self->{'open_delimiter'}
Aresult
11
00
1603 1440 ▶ $close_delimiter = $self->{ close_delimiter } || ':>';
Condition100%2/2
$self->{'close_delimiter'} || ':>'
l!l
651375
MC/DC100%1/1
$self->{'close_delimiter'} || ':>'
$self->{'close_delimiter'}
Truth table100%2/2
A
A= $self->{'close_delimiter'}
Aresult
11
00
1604  
1605 1440 $open_regexp = qr/\Q$open_delimiter\E/;
1606 1440 $close_regexp = qr/\Q$close_delimiter\E/;
1607  
1608 1440 ▶ $local_token_aliases = $self->{ local_token_aliases } || {};
Condition100%2/2
$self->{'local_token_aliases'} || {}
l!l
91431
MC/DC100%1/1
$self->{'local_token_aliases'} || {}
$self->{'local_token_aliases'}
Truth table100%2/2
A
A= $self->{'local_token_aliases'}
Aresult
11
00
1609 1440 ▶ $local_syntaxes = $self->{ local_syntaxes } || {};
Condition100%2/2
$self->{'local_syntaxes'} || {}
l!l
91431
MC/DC100%1/1
$self->{'local_syntaxes'} || {}
$self->{'local_syntaxes'}
Truth table100%2/2
A
A= $self->{'local_syntaxes'}
Aresult
11
00
1610  
1611 # TODO: class-level syntax aliases
1612 # TODO: split into class/instance versions and unroll to construct time?
1613 # TODO: or generate-on-demand but class/instance copy invalidated on change.
1614 # Egads!
1615 $local_syntax_regexp = join( ' | ',
1616 10 map { join( ' \s+ ', split( /\s+/, $_ ) ) }
1617 grep( /^[^\.]/,
1618 1440 keys( %{$local_token_aliases} ), keys( %{$local_syntaxes} ),
1619 1440 values( %{$syntaxes{ '.instr' }} ) ) );
1620 1440 ▶ $local_syntax_regexp = ' | ' . $local_syntax_regexp
Branch: 1 branch
BranchTrueFalseTotal
if $local_syntax_regexp 9 1431 1440
1621 if $local_syntax_regexp;
1622  
1623 1440 $syntax_regexp = qr/(
1624 var | expr |
1625 (?:if|unless) | else? \s* (?:if|unless) | else |
1626 end \s* (?:if|unless) |
1627 for(?:each)? | end \s* for(?:each)? | end |
1628 include | end \s* include |
1629 \# |
1630 debug
1631 $local_syntax_regexp
1632 ) \s+
1633 /ix;
1634 1440 ▶ $syntax_regexp = qr/(?:$syntax_regexp)?/ if $self->{ allow_bare_expr };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"allow_bare_expr"} 212 1228 1440
1635 1440 $hunk_regexp = qr/^$open_regexp \s*
1636 $syntax_regexp (.*?) \s* $close_regexp (.+)? $/sx;
1637  
1638 1440 @hunks = split( /(?=$open_regexp)/s, $self->{ template }, -1 );
1639 1440 delete $self->{ template };
1640  
1641 1440 $self->{ pos_stack } = \@pos_stack;
1642 1440 $self->{ phase } = 'compile';
1643  
1644 #my ( $dumpme );
1645 1440 ▶ for( $i = 0; $i < @hunks; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @hunks) 3552 1338 4890
1646 {
1647 3552 my ( $hunk, $pos, $lines, $queue_pos, $last, $hunklen, $hunkstart,
1648 $offset_index );
1649  
1650 3552 $hunk = $hunks[ $i ];
1651  
1652 3552 $pos = [ @{$pos_stack[ 0 ]}[ 0..2 ] ];
1653 3552 $self->{ current_pos } = $pos;
1654  
1655 3552 ▶ if( $hunk =~ $hunk_regexp )
Branch: 1 branch
BranchTrueFalseTotal
if ($hunk =~ /$hunk_regexp/) { } 2384 1168 3552
1656 {
1657 2384 my ( $token, $syntax, $args, $rest );
1658  
1659 2384 ▶ $token = lc( $1 || 'expr' );
Condition100%2/2
$1 || 'expr'
l!l
2152232
MC/DC100%1/1
$1 || 'expr'
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
1660 2384 $args = $2;
1661 2384 $rest = $3;
1662  
1663 # TODO: still possible? What triggers it?
1664 # error, unclosed token?
1665 2384 ▶ $self->error( "unexepected $open_delimiter, ",
Branch: 1 branch
BranchTrueFalseTotal
if $args =~ /$open_regexp/ 0 2384 2384
1666 "possibly unterminated $close_delimiter" )
1667 if $args =~ $open_regexp;
1668  
1669 2384 ▶ if( defined( $rest ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rest) 990 1394 2384
1670 {
1671 990 $hunk =~ s/$close_regexp(?:.*)$/$close_delimiter/s;
1672 990 splice( @hunks, $i, 1, $hunk, $rest );
1673 }
1674  
1675 2384 $token =~ s/\s+/ /go;
1676  
1677 2384 ▶ if( $token eq 'end' )
Branch: 1 branch
BranchTrueFalseTotal
if ($token eq "end") 55 2329 2384
1678 {
1679 55 ▶ $self->error( "end found without opening block" )
Branch: 1 branch
BranchTrueFalseTotal
unless @nest_stack 0 55 55
1680 unless @nest_stack;
1681 55 ▶ $token = ( $nest_stack[ 0 ][ 0 ] eq FOR ) ?
Branch: 1 branch
BranchTrueFalseTotal
$nest_stack[0][0] eq 5 ? : 5 50 55
1682 'endfor' : 'endif';
1683 }
1684  
1685 $token = $local_token_aliases->{ $token }
1686 2384 ▶ if $local_token_aliases->{ $token };
Branch: 1 branch
BranchTrueFalseTotal
if $local_token_aliases->{$token} 0 2384 2384
1687 $token = $token_aliases{ $token }
1688 2384 ▶ if $token_aliases{ $token };
Branch: 1 branch
BranchTrueFalseTotal
if $token_aliases{$token} 220 2164 2384
1689 2384 ▶ $syntax = $local_syntaxes->{ $token } || $syntaxes{ $token };
Condition100%3/3
$local_syntaxes->{$token} || $syntaxes{$token}
l!l&&r!l&&!r
1213341038
MC/DC100%2/2
$local_syntaxes->{$token} || $syntaxes{$token}
$local_syntaxes->{$token} $syntaxes{$token}
Truth table100%3/3
A || B
A= $local_syntaxes->{$token}B= $syntaxes{$token}
ABresult
1X1
011
000
1690  
1691 # Fudge things a little so that flow-control tokens
1692 # on a line by themselves don't produce a bunch of
1693 # empty lines in the output.
1694 # Are we a zero-width token on a line by itself?
1695 2384 ▶ if( $syntax->{ zero_width } and
Branch: 1 branch
BranchTrueFalseTotal
if ($syntax->{'zero_width'} and $i < @hunks - 1 and !@compiled || $compiled[@compiled - 1][0] == 0 && $compiled[@compiled - 1][2] =~ /\n\ *$/ || $compiled[@compiled - 1][0] == 7 and $hunks[$i + 1] =~ /^\n\ */) { } 59 2325 2384
Condition100%18/18
$syntax->{"zero_width"} and $i < @hunks - 1 and !@compiled || $compiled[@compiled - 1][0] == 0 && $compiled[@compiled - 1][2] =~ /\n\ *$/ || $compiled[@compiled - 1][0] == 7 and $hunks[$i + 1] =~ /^\n\ */
!ll&&!rl&&r
195836759
$syntax->{"zero_width"} and $i < @hunks - 1 and !@compiled || $compiled[@compiled - 1][0] == 0 && $compiled[@compiled - 1][2] =~ /\n\ *$/ || $compiled[@compiled - 1][0] == 7
!ll&&!rl&&r
1407551426
$syntax->{"zero_width"} and $i < @hunks - 1
!ll&&!rl&&r
1053354977
not @compiled or $compiled[@compiled - 1][0] == 0 and $compiled[@compiled - 1][2] =~ /\n\ *$/ or $compiled[@compiled - 1][0] == 7
l!l&&r!l&&!r
4251551
not @compiled or $compiled[@compiled - 1][0] == 0 and $compiled[@compiled - 1][2] =~ /\n\ *$/
l!l&&r!l&&!r
36956552
$compiled[@compiled - 1][0] == 0 and $compiled[@compiled - 1][2] =~ /\n\ *$/
!ll&&!rl&&r
2652656
MC/DC85%6/7
$syntax->{"zero_width"} and $i < @hunks - 1 and !@compiled || $compiled[@compiled - 1][0] == 0 && $compiled[@compiled - 1][2] =~ /\n\ *$/ || $compiled[@compiled - 1][0] == 7 and $hunks[$i + 1] =~ /^\n\ */
$syntax->{"zero_width"} $i < @hunks - 1 not @compiled $compiled[@compiled - 1][0] == 0 $compiled[@compiled - 1][2] =~ /\n\ *$/ $compiled[@compiled - 1][0] == 7 $hunks[$i + 1] =~ /^\n\ */
Truth table75%9/12
A and B and not(C) or D and E or F and G
A= $syntax->{"zero_width"}B= $i < @hunks - 1C= not @compiledD= $compiled[@compiled - 1][0] == 0E= $compiled[@compiled - 1][2] =~ /\n\ *$/F= $compiled[@compiled - 1][0] == 7G= $hunks[$i + 1] =~ /^\n\ */
ABCDEFGresult
0XXXXXX0
10XXXXX0
1100X0X0
110100X0
111XXX00
11011X00
1100X100
11010100
111XXX11
11011X11
1100X111
11010111
1696 $i < @hunks - 1 and
1697 ( ( not @compiled ) or
1698 ( $compiled[ @compiled - 1 ]->[ 0 ] == LITERAL and
1699 $compiled[ @compiled - 1 ]->[ 2 ] =~ /\n\ *$/ ) or
1700 ( $compiled[ @compiled - 1 ]->[ 0 ] == CONTEXT_PUSH ) ) and
1701 $hunks[ $i + 1 ] =~ /^\n\ */ )
1702 {
1703 59 $trim_next = 1;
1704 }
1705 else
1706 {
1707 2325 $trim_next = 0;
1708 }
1709  
1710 2384 ▶ if( $syntax->{ compile } )
Branch: 12 branches
BranchTrueFalseTotal
if ($syntax->{'compile'}) { } 14 2370 2384
elsif ($token eq 'debug') { } 1 2369 2370
elsif ($token eq 'expr' or $token eq 'var') { } 1039 1330 2369
elsif ($token eq 'if' or $token eq 'unless') { } 352 978 1330
elsif ($token eq 'elsif' or $token eq 'elsunless') { } 316 662 978
elsif ($token eq 'else') { } 157 505 662
elsif ($token eq 'endif' or $token eq 'endunless') { } 349 156 505
elsif ($token eq 'for') { } 65 91 156
elsif ($token eq 'endfor') { } 62 29 91
elsif ($token eq 'include') { } 17 12 29
elsif ($token eq 'endinclude') { } 10 2 12
elsif ($token eq '#') { } 2 0 2
1711 {
1712 14 my ( $compiler, $opcode );
1713  
1714 14 $args = $self->_parse_args( $args, $token );
1715  
1716 14 $compiler = $syntax->{ compile };
1717 14 $opcode = $syntax->{ instr };
1718 14 $args = $compiler->( $self, $token, $pos, $args );
1719 14 ▶ push @compiled, [ $opcode, $pos, $args ] if defined $args;
Branch: 1 branch
BranchTrueFalseTotal
if defined $args 13 1 14
1720 }
1721 elsif( $token eq 'debug' )
1722 {
1723 1 $args = $self->_parse_args( $args, $token );
1724  
1725 1 ▶ $args = 0 unless keys( %{$args} );
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$args;} 0 1 1
1726  
1727 1 push @compiled,
1728 [ DEBUG, $pos, $args ];
1729 }
1730 ▶ elsif( $token eq 'expr' or $token eq 'var' )
Condition100%3/3
$token eq "expr" or $token eq "var"
l!l&&r!l&&!r
103811330
MC/DC100%2/2
$token eq "expr" or $token eq "var"
$token eq "expr" $token eq "var"
Truth table100%3/3
A or B
A= $token eq "expr"B= $token eq "var"
ABresult
1X1
011
000
1731 {
1732 1039 my ( $expr );
1733  
1734 1039 $expr = $self->_compile_expression( $args );
1735  
1736 992 ▶ push @compiled,
Branch: 1 branch
BranchTrueFalseTotal
$expr->[0] == 100 && $expr->[2] eq '=' ? : 1 991 992
Condition100%3/3
$expr->[0] == 100 and $expr->[2] eq "="
!ll&&!rl&&r
7542371
MC/DC100%2/2
$expr->[0] == 100 and $expr->[2] eq "="
$expr->[0] == 100 $expr->[2] eq "="
Truth table100%3/3
A and B
A= $expr->[0] == 100B= $expr->[2] eq "="
ABresult
0X0
100
111
1737 [ EXPR, $pos, $expr,
1738 # Void-wrap assign expressions.
1739 ( ( $expr->[ 0 ] == OP_TREE ) and
1740 ( $expr->[ 2 ] eq '=' ) ) ? 1 : 0 ];
1741 }
1742 ▶ elsif( $token eq 'if' or $token eq 'unless' )
Condition100%3/3
$token eq "if" or $token eq "unless"
l!l&&r!l&&!r
199153978
MC/DC100%2/2
$token eq "if" or $token eq "unless"
$token eq "if" $token eq "unless"
Truth table100%3/3
A or B
A= $token eq "if"B= $token eq "unless"
ABresult
1X1
011
000
1743 {
1744 352 my ( $expr );
1745  
1746 352 $expr = $self->_compile_expression( $args );
1747 352 ▶ if( $token ne 'if' )
Branch: 1 branch
BranchTrueFalseTotal
if ($token ne "if") 153 199 352
1748 {
1749 153 ▶ if( $expr->[ 0 ] == LITERAL )
Branch: 1 branch
BranchTrueFalseTotal
if ($expr->[0] == 0) { } 24 129 153
1750 {
1751 24 $expr->[ 2 ] = not $expr->[ 2 ];
1752 }
1753 else
1754 {
1755 129 $expr = [ UNARY_OP, 'unless', 'not', $expr ];
1756 }
1757 }
1758 352 push @compiled,
1759 [ JUMP_IF, $pos, undef, $expr ];
1760 # push @compiled,
1761 # [ JUMP_IF, $pos, undef,
1762 # $self->_compile_expression( $args ),
1763 # $token eq 'if' ? 1 : 0 ];
1764 352 unshift @nest_stack, [ 'if', @compiled - 1 ];
1765 }
1766 ▶ elsif( $token eq 'elsif' or $token eq 'elsunless' )
Condition100%3/3
$token eq "elsif" or $token eq "elsunless"
l!l&&r!l&&!r
194122662
MC/DC100%2/2
$token eq "elsif" or $token eq "elsunless"
$token eq "elsif" $token eq "elsunless"
Truth table100%3/3
A or B
A= $token eq "elsif"B= $token eq "elsunless"
ABresult
1X1
011
000
1767 {
1768 316 my ( $expr );
1769  
1770 316 ▶ if( ( not @nest_stack ) or
Branch: 1 branch
BranchTrueFalseTotal
if (not @nest_stack or $nest_stack[0][0] ne "if" and $nest_stack[0][0] ne "elsif") 2 314 316
Condition100%6/6
not @nest_stack or $nest_stack[0][0] ne "if" and $nest_stack[0][0] ne "elsif"
l!l&&r!l&&!r
11314
$nest_stack[0][0] ne "if" and $nest_stack[0][0] ne "elsif"
!ll&&!rl&&r
254601
MC/DC100%3/3
not @nest_stack or $nest_stack[0][0] ne "if" and $nest_stack[0][0] ne "elsif"
not @nest_stack $nest_stack[0][0] ne "if" $nest_stack[0][0] ne "elsif"
Truth table100%4/4
not(A) or B and C
A= not @nest_stackB= $nest_stack[0][0] ne "if"C= $nest_stack[0][0] ne "elsif"
ABCresult
1XX1
0111
00X0
0100
1771 ( $nest_stack[ 0 ][ 0 ] ne 'if' and
1772 $nest_stack[ 0 ][ 0 ] ne 'elsif' ) )
1773 {
1774 2 $self->error( "elsif found without opening if or elsif" );
1775 }
1776 # Closing jump of previous block.
1777 314 push @compiled,
1778 [ JUMP, $pos, undef ];
1779  
1780 314 $expr = $self->_compile_expression( $args );
1781 314 ▶ if( $token ne 'elsif' )
Branch: 1 branch
BranchTrueFalseTotal
if ($token ne "elsif") 122 192 314
1782 {
1783 122 ▶ if( $expr->[ 0 ] == LITERAL )
Branch: 1 branch
BranchTrueFalseTotal
if ($expr->[0] == 0) { } 16 106 122
1784 {
1785 16 $expr->[ 2 ] = not $expr->[ 2 ];
1786 }
1787 else
1788 {
1789 106 $expr = [ UNARY_OP, 'elsunless', 'not', $expr ];
1790 }
1791 }
1792 314 push @compiled, [ JUMP_IF, $pos, undef, $expr ];
1793 # push @compiled,
1794 # [ JUMP_IF, $pos, undef,
1795 # $self->_compile_expression( $args ),
1796 # $token eq 'elsif' ? 1 : 0 ];
1797 # Now, update jump address of previous if/elsif
1798 314 $compiled[ $nest_stack[ 0 ][ 1 ] ][ 2 ] =
1799 @compiled - 1;
1800 314 unshift @nest_stack, [ 'elsif', @compiled - 1 ];
1801 }
1802 elsif( $token eq 'else' )
1803 {
1804 157 ▶ if( ( not @nest_stack ) or
Branch: 1 branch
BranchTrueFalseTotal
if (not @nest_stack or $nest_stack[0][0] ne "if" and $nest_stack[0][0] ne "elsif") 2 155 157
Condition100%6/6
not @nest_stack or $nest_stack[0][0] ne "if" and $nest_stack[0][0] ne "elsif"
l!l&&r!l&&!r
11155
$nest_stack[0][0] ne "if" and $nest_stack[0][0] ne "elsif"
!ll&&!rl&&r
63921
MC/DC100%3/3
not @nest_stack or $nest_stack[0][0] ne "if" and $nest_stack[0][0] ne "elsif"
not @nest_stack $nest_stack[0][0] ne "if" $nest_stack[0][0] ne "elsif"
Truth table100%4/4
not(A) or B and C
A= not @nest_stackB= $nest_stack[0][0] ne "if"C= $nest_stack[0][0] ne "elsif"
ABCresult
1XX1
0111
00X0
0100
1805 ( $nest_stack[ 0 ][ 0 ] ne 'if' and
1806 $nest_stack[ 0 ][ 0 ] ne 'elsif' ) )
1807 {
1808 2 $self->error( "else found without opening if or elsif" );
1809 }
1810 # Closing jump of previous block.
1811 155 push @compiled,
1812 [ JUMP, $pos, undef ];
1813 # Now, update jump address of previous if/elsif
1814 155 $compiled[ $nest_stack[ 0 ][ 1 ] ][ 2 ] = @compiled;
1815 155 unshift @nest_stack, [ 'else', scalar( @compiled ) ];
1816 }
1817 ▶ elsif( $token eq 'endif' or $token eq 'endunless' )
Condition100%3/3
$token eq "endif" or $token eq "endunless"
l!l&&r!l&&!r
26584156
MC/DC100%2/2
$token eq "endif" or $token eq "endunless"
$token eq "endif" $token eq "endunless"
Truth table100%3/3
A or B
A= $token eq "endif"B= $token eq "endunless"
ABresult
1X1
011
000
1818 {
1819 349 ▶ if( ( not @nest_stack ) or
Branch: 1 branch
BranchTrueFalseTotal
if (not @nest_stack or $nest_stack[0][0] ne "if" and $nest_stack[0][0] ne "elsif" and $nest_stack[0][0] ne "else") 2 347 349
Condition100%9/9
not @nest_stack or $nest_stack[0][0] ne "if" and $nest_stack[0][0] ne "elsif" and $nest_stack[0][0] ne "else"
l!l&&r!l&&!r
11347
$nest_stack[0][0] ne "if" and $nest_stack[0][0] ne "elsif" and $nest_stack[0][0] ne "else"
!ll&&!rl&&r
1921551
$nest_stack[0][0] ne "if" and $nest_stack[0][0] ne "elsif"
!ll&&!rl&&r
30162156
MC/DC100%4/4
not @nest_stack or $nest_stack[0][0] ne "if" and $nest_stack[0][0] ne "elsif" and $nest_stack[0][0] ne "else"
not @nest_stack $nest_stack[0][0] ne "if" $nest_stack[0][0] ne "elsif" $nest_stack[0][0] ne "else"
Truth table100%5/5
not(A) or B and C and D
A= not @nest_stackB= $nest_stack[0][0] ne "if"C= $nest_stack[0][0] ne "elsif"D= $nest_stack[0][0] ne "else"
ABCDresult
1XXX1
01111
00XX0
010X0
01100
1820 ( $nest_stack[ 0 ][ 0 ] ne 'if' and
1821 $nest_stack[ 0 ][ 0 ] ne 'elsif' and
1822 $nest_stack[ 0 ][ 0 ] ne 'else' ) )
1823 {
1824 2 $self->error(
1825 "endif found without opening if, elsif or else" );
1826 }
1827  
1828 # Update jump address of previous if/elsif
1829 347 ▶ $compiled[ $nest_stack[ 0 ][ 1 ] ][ 2 ] = @compiled
Branch: 1 branch
BranchTrueFalseTotal
unless $nest_stack[0][0] eq "else" 192 155 347
1830 unless $nest_stack[ 0 ][ 0 ] eq 'else';
1831  
1832 347 ▶ while( @nest_stack )
Branch: 1 branch
BranchTrueFalseTotal
if (@nest_stack) 816 0 816
1833 {
1834 816 my $last = shift @nest_stack;
1835  
1836 816 ▶ if( $last->[ 0 ] eq 'if' )
Branch: 2 branches
BranchTrueFalseTotal
if ($last->[0] eq 'if') { } 347 469 816
elsif ($last->[0] eq 'elsif' or $last->[0] eq 'else') { } 469 0 469
1837 {
1838 # It's our opening if, stop popping.
1839 347 last;
1840 }
1841 ▶ elsif( $last->[ 0 ] eq 'elsif' or $last->[ 0 ] eq 'else' )
Condition66%2/3
$last->[0] eq "elsif" or $last->[0] eq "else"
l!l&&r!l&&!r
3141550
MC/DC0%0/2
$last->[0] eq "elsif" or $last->[0] eq "else"
$last->[0] eq "elsif" $last->[0] eq "else"
Truth table66%2/3
A or B
A= $last->[0] eq "elsif"B= $last->[0] eq "else"
ABresult
1X1
011
000
1842 {
1843 # Need to update the jump address of the closing
1844 # jump of the block _prior_ to this elsif/else.
1845 469 $compiled[ $last->[ 1 ] - 1 ][ 2 ] = @compiled;
1846 }
1847 else
1848 {
1849 # "cannot happen".
1850 0 $self->error(
1851 "nesting stack appears to be corrupted" );
1852 }
1853 }
1854 }
1855 elsif( $token eq 'for' )
1856 {
1857 65 my ( $iterator, $set );
1858  
1859 # TODO: syntax checking/error check needed here.
1860 65 ( $iterator, $set ) = $args =~ /^(.*) in (.*)$/io;
1861  
1862 65 push @compiled,
1863 [ FOR, $pos, undef, $iterator,
1864 $self->_compile_expression( $set ),
1865 1 ];
1866 65 unshift @nest_stack, [ FOR, @compiled - 1 ];
1867 }
1868 elsif( $token eq 'endfor' )
1869 {
1870 62 ▶ if( ( not @nest_stack ) or
Branch: 1 branch
BranchTrueFalseTotal
if (not @nest_stack or $nest_stack[0][0] ne 5) 2 60 62
Condition100%3/3
not @nest_stack or $nest_stack[0][0] ne 5
l!l&&r!l&&!r
1160
MC/DC100%2/2
not @nest_stack or $nest_stack[0][0] ne 5
not @nest_stack $nest_stack[0][0] ne 5
Truth table100%3/3
not(A) or B
A= not @nest_stackB= $nest_stack[0][0] ne 5
ABresult
1X1
011
000
1871 $nest_stack[ 0 ][ 0 ] ne FOR )
1872 {
1873 2 $self->error(
1874 "endfor found without opening for" );
1875 }
1876  
1877 60 my $last = shift @nest_stack;
1878  
1879 # Grab our iterator and set from the opening for
1880 # TODO: needed anymore? run grabs it from for-stack.
1881 60 push @compiled,
1882 [ END_FOR, $pos, $last->[ 1 ] + 1,
1883 $compiled[ $last->[ 1 ] ][ 3 ],
1884 $compiled[ $last->[ 1 ] ][ 4 ] ];
1885 # Update jump address of opening for.
1886 60 $compiled[ $last->[ 1 ] ][ 2 ] = @compiled;
1887 }
1888 elsif( $token eq 'include' )
1889 {
1890 17 my ( $filename, $inc_template, @inc_hunks, %defines );
1891  
1892 # We support var renaming:
1893 # ie: <: include pagerwidget offset=param.offset
1894 # total=results.__size__ pagesize=param.n :>
1895  
1896 17 $args = $self->_parse_args( $args, 'include' );
1897  
1898 # Extract the filename.
1899 # If the filename is empty-string then we ignore the
1900 # include statement, allowing us to do things like
1901 # <: include ${DEFINE:} :> without knowing if the define
1902 # exists or not.
1903 16 ▶ if( $filename = $args->{ filename } )
Branch: 1 branch
BranchTrueFalseTotal
if ($filename = $args->{"filename"}) 15 1 16
1904 {
1905 15 my ( $volume, $current_dir );
1906  
1907 15 delete $args->{ filename };
1908  
1909 ( $volume, $current_dir ) = File::Spec->splitpath(
1910 15 $define_stack[ 0 ]->{ FILENAME } );
1911 # Make sure volume is part of the current dir so
1912 # windows doesn't choke.
1913 15 $current_dir = File::Spec->catpath(
1914 $volume, $current_dir, '' );
1915  
1916 15 %defines = %{$define_stack[ 0 ]};
1917  
1918 15 $self->{ defines } = \%defines;
1919 15 unshift @define_stack, \%defines;
1920  
1921 # Parse out any defines.
1922 15 foreach my $key (
1923 8 grep { $_ eq uc( $_ ) } keys( %{$args} ) )
1924 {
1925 3 $defines{ $key } = $args->{ $key };
1926 3 delete $args->{ $key };
1927 }
1928 $args = { map
1929 5 { $_ => $self->_compile_expression( $args->{ $_ } ) }
1930 15 keys( %{$args} ) };
1931 15 ▶ $args = 0 unless keys( %{$args} );
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$args;} 12 3 15
1932  
1933 15 $filename = $self->find_include( $filename, $current_dir );
1934  
1935 $self->error( "recursive include of $filename" )
1936 14 ▶ if $includes{ $filename };
Branch: 1 branch
BranchTrueFalseTotal
if $includes{$filename} 2 12 14
1937  
1938 12 $defines{ FILENAME } = $filename;
1939  
1940 12 $includes{ $filename } = 1;
1941 12 $inc_template =
1942 $self->_read_template( $filename, \%defines );
1943 11 $inc_template =~ s/\n$//;
1944 11 @inc_hunks = split( /(?=<:)/, $inc_template, -1 );
1945 11 $inc_template = 0;
1946  
1947 11 splice( @hunks, $i + 1, 0,
1948 @inc_hunks, '<: endinclude :>' );
1949  
1950 11 push @compiled,
1951 [ CONTEXT_PUSH, $pos, $args ];
1952 11 ▶ unless( exists( $file_numbers{ $filename } ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $file_numbers{$filename}) 10 1 11
1953 {
1954 10 $file_numbers{ $filename } = @files;
1955 10 push @files, $filename;
1956 }
1957 $queue_pos = [ $file_numbers{ $filename }, 1, 1, 0,
1958 11 $self->{ offsets } ];
1959 11 delete $self->{ offsets };
1960 }
1961 }
1962 elsif( $token eq 'endinclude' )
1963 {
1964 # <: endinclude :> is a faux-token, it never gets read
1965 # in from a template (isn't valid syntax even), but gets
1966 # inserted to mark the end of the inserted hunks from
1967 # an <: include :>
1968 # "cannot happen".
1969 10 ▶ $self->error( "endinclude found while not within an include" )
Branch: 1 branch
BranchTrueFalseTotal
unless @pos_stack 0 10 10
1970 unless @pos_stack;
1971  
1972 10 my $last = shift @pos_stack;
1973 10 delete $includes{ $files[ $last->[ 0 ] ] };
1974 10 shift @define_stack;
1975 10 $self->{ defines } = $define_stack[ 0 ];
1976 10 push @compiled,
1977 [ CONTEXT_POP, $pos ];
1978 10 next; # So we don't update pos with this faux-token.
1979 }
1980 elsif( $token eq '#' )
1981 {
1982 # We're a comment, don't compile it.
1983 }
1984 else
1985 {
1986 # Shouldn't be possible to get through the regexp to this.
1987 0 $self->error( "unrecognised token ($token)" );
1988 }
1989 }
1990 else
1991 {
1992 # We're a literal unless we're a malformed token
1993 1168 ▶ if( $hunk =~ /^$open_regexp/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($hunk =~ /^$open_regexp/) 42 1126 1168
1994 {
1995 # Trim bits after the close token if there is one,
1996 # makes a clearer error message.
1997 42 $hunk =~ s/($close_regexp).*$/$1/;
1998 42 $self->error( "unrecognised token ($hunk)" );
1999 }
2000 1126 ▶ if( length( $hunk ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $hunk) 1126 0 1126
2001 {
2002 1126 ▶ $trim{ @compiled } = 1 if $trim_next;
Branch: 1 branch
BranchTrueFalseTotal
if $trim_next 54 1072 1126
2003 1126 push @compiled, [ LITERAL, $pos, $hunk ];
2004 }
2005 1126 $trim_next = 0;
2006 }
2007  
2008 # +--------------------+------------------------+-----------------------------+
2009 # | define > | nl | !nl |
2010 # +--- hunk v ---------+------------------------+-----------------------------+
2011 # | !nl | not possible (1) | offsets: char |
2012 # | nl after defines | offsets: nl, !char | offsets: nl, !char (no-op) |
2013 # | nl before defines | offset: nl | offsets: char |
2014 # | | char: | |
2015 # | | chars between nl & | |
2016 # | | start of define (2) + | |
2017 # | | original define len + | |
2018 # | | chars after define | |
2019 # | nl between defines | treat in reverse series as after/before |
2020 # +--------------------+------------------------+-----------------------------+
2021 #
2022 # TODO: (1) define spans hunks? (add test case!)
2023 # TODO: define spans defines? (add test case!)
2024 # (2) characters is "fudged count", there may be other defines there. :(
2025 #
2026 # Detection:
2027 # nl in define: nl offset != 0
2028 # nl in hunk: nl in final hunk != total nl in offsets
2029 #
2030  
2031  
2032 # Update pos.
2033 3440 $hunklen = length( $hunk );
2034 3440 $pos = $pos_stack[ 0 ];
2035 3440 $hunkstart = $pos->[ 3 ];
2036 3440 $pos->[ 3 ] += $hunklen;
2037  
2038 #use Data::Dumper;
2039 #print "After hunk: xxx${hunk}xxx\nPos is: " . Data::Dumper::Dumper( $pos ) . "\n";
2040  
2041 # Do we have offsets, and have we just passed one?
2042 3440 $offset_index = -1;
2043 3440 ▶ while( $pos->[ 4 ] and $offset_index < @{$pos->[ 4 ]} - 1 and
Branch: 1 branch
BranchTrueFalseTotal
if ($pos->[4] and $offset_index < @{$pos->[4];} - 1 and $pos->[4][$offset_index + 1][0] <= $pos->[3]) 18 3440 3458
Condition100%6/6
$pos->[4] and $offset_index < @{$pos->[4];} - 1 and $pos->[4][$offset_index + 1][0] <= $pos->[3]
!ll&&!rl&&r
3435518
$pos->[4] and $offset_index < @{$pos->[4];} - 1
!ll&&!rl&&r
34221323
MC/DC100%3/3
$pos->[4] and $offset_index < @{$pos->[4];} - 1 and $pos->[4][$offset_index + 1][0] <= $pos->[3]
$pos->[4] $offset_index < @{$pos->[4];} - 1 $pos->[4][$offset_index + 1][0] <= $pos->[3]
Truth table100%4/4
A and B and C
A= $pos->[4]B= $offset_index < @{$pos->[4];} - 1C= $pos->[4][$offset_index + 1][0] <= $pos->[3]
ABCresult
0XX0
10X0
1100
1111
2044 $pos->[ 4 ]->[ $offset_index + 1 ]->[ 0 ] <= $pos->[ 3 ] )
2045 {
2046 18 my ( $offset );
2047  
2048 18 $offset_index++;
2049 18 $offset = $pos->[ 4 ]->[ $offset_index ];
2050 # Replace any newlines in the section that was the contents
2051 # of a define, this is so that they don't count towards line
2052 # counts or finding the "most recent newline" for character
2053 # position counts.
2054 # This is inelegant but much simpler (and possibly faster)
2055 # than trying to compensate and find the "right" newline
2056 # to count from, especially since defines containing newlines
2057 # are hopefully a corner-case.
2058 #print "Offset index: $offset_index\nOffset: " . Data::Dumper::Dumper( $offset ) . "\n";
2059 #print "substr( hunk, " . ( $offset->[ 0 ] - $hunkstart ) . ", " . ( $offset->[ 1 ] ) . " )\n" if $offset->[ 2 ];
2060  
2061 18 ▶ substr( $hunk, $offset->[ 0 ] - $hunkstart, $offset->[ 1 ] ) =~
Branch: 1 branch
BranchTrueFalseTotal
if $offset->[2] 1 17 18
2062 s/\n/ /go
2063 if $offset->[ 2 ];
2064 }
2065  
2066 # $lines = () = $hunk =~ /\n/g;
2067 # $lines = $#{ [ $hunk =~ /\n/g ] } + 1;
2068 3440 $lines = $hunk =~ tr/\n//;
2069 3440 ▶ if( $lines )
Branch: 1 branch
BranchTrueFalseTotal
if ($lines) { } 107 3333 3440
2070 {
2071 107 $pos->[ 1 ] += $lines;
2072 107 ▶ $pos->[ 2 ] =
Branch: 1 branch
BranchTrueFalseTotal
$hunk =~ /\n(.+)\z/mo ? : 11 96 107
2073 ( $hunk =~ /\n(.+)\z/mo ) ? ( length( $1 ) + 1 ) : 1;
2074 }
2075 else
2076 {
2077 3333 $pos->[ 2 ] += $hunklen;
2078 }
2079  
2080 3440 ▶ if( $offset_index != -1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($offset_index != -1) 15 3425 3440
2081 {
2082 15 my ( @offsets, $nlpos );
2083  
2084 15 @offsets = splice( @{$pos->[ 4 ]}, 0, $offset_index + 1 );
2085 15 ▶ $pos->[ 4 ] = undef unless @{$pos->[ 4 ]};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$pos->[4];} 13 2 15
2086  
2087 15 ▶ $nlpos = $lines ? ( $pos->[ 3 ] - $pos->[ 2 ] ) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$lines ? : 5 10 15
2088  
2089 15 foreach my $offset ( @offsets )
2090 {
2091 # Don't apply the offset if it was before the final
2092 # non-define newline
2093 18 ▶ next if $offset->[ 0 ] < $nlpos;
Branch: 1 branch
BranchTrueFalseTotal
if $offset->[0] < $nlpos 6 12 18
2094 #use Data::Dumper;
2095 #print "Applying offset: " . Data::Dumper::Dumper( $offset ) . "\n";
2096 # $pos->[ 1 ] += $offset->[ 2 ];
2097 12 $pos->[ 2 ] += $offset->[ 3 ];
2098 }
2099 }
2100  
2101 3440 ▶ unshift @pos_stack, $queue_pos if $queue_pos;
Branch: 1 branch
BranchTrueFalseTotal
if $queue_pos 11 3429 3440
2102 }
2103  
2104 1338 ▶ $self->error( "unterminated if or for block" ) if @nest_stack;
Branch: 1 branch
BranchTrueFalseTotal
if @nest_stack 2 1336 1338
2105  
2106 # "cannot happen".
2107 1336 ▶ $self->error( "include stack not empty, corrupted?" ) if @pos_stack > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @pos_stack > 1 0 1336 1336
2108  
2109 # TODO: scan for undef jump addresses.
2110  
2111 1336 foreach my $addr ( keys( %trim ) )
2112 {
2113 # "cannot happen".
2114 54 ▶ $self->error( "trim on non-literal, trim-stack corrupted?" )
Branch: 1 branch
BranchTrueFalseTotal
unless $compiled[$addr][0] == 0 0 54 54
2115 unless $compiled[ $addr ]->[ 0 ] == LITERAL;
2116 54 $compiled[ $addr ]->[ 2 ] =~ s/^\n//o;
2117 }
2118  
2119 # We're done.
2120 # $self->{ template } = {
2121 # program => [ @compiled ],
2122 # files => [ @files ],
2123 # };
2124 $self->{ template } = {
2125 1336 program => \@compiled,
2126 files => \@files,
2127 };
2128 1336 $self->_optimize_template();
2129 $self->{ template }->{ last_instr } =
2130 1336 @{$self->{ template }->{ program }} - 1;
2131  
2132 1336 delete $self->{ current_pos };
2133 1336 delete $self->{ pos_stack };
2134 1336 delete $self->{ files };
2135 1336 delete $self->{ phase };
2136  
2137 #$dumpme = 1;
2138 #use CGI;
2139 #print CGI->header('text/plain');
2140  
2141 #if( $dumpme )
2142 #{
2143 #print "\n----\n" . $self->dumpable_template() . "----\n";
2144 #exit(0);
2145 #}
2146 }
2147  
2148 sub _optimize_template
2149 1336 ▶{
Subroutine _optimize_template: called CC=45 SCAR=38.1
2150 1336 my ( $self ) = @_;
2151 1336 my ( $program, @nest_stack, %deletes, %jump_targets, @loop_blocks,
2152 $value );
2153  
2154 # my ( @function_table, %function_index );
2155  
2156 # Optimization pass:
2157 # TODO: unroll constant low-count fors?
2158  
2159 1336 $program = $self->{ template }->{ program };
2160  
2161 # Fold constant expr into constant instr.
2162 1336 ▶ for( my $i = 0; $i < @{$program}; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$program;}) 3375 1336 4711
2163 {
2164 # Are we an EXPR instr and is our expr a LITERAL expr?
2165 3375 ▶ next unless $program->[ $i ]->[ 0 ] == EXPR and
Branch: 1 branch
BranchTrueFalseTotal
unless $program->[$i][0] == 2 and $program->[$i][2][0] == 0 3163 212 3375
Condition100%3/3
$program->[$i][0] == 2 and $program->[$i][2][0] == 0
!ll&&!rl&&r
2383780212
MC/DC100%2/2
$program->[$i][0] == 2 and $program->[$i][2][0] == 0
$program->[$i][0] == 2 $program->[$i][2][0] == 0
Truth table100%3/3
A and B
A= $program->[$i][0] == 2B= $program->[$i][2][0] == 0
ABresult
0X0
100
111
2166 $program->[ $i ]->[ 2 ]->[ 0 ] == LITERAL;
2167  
2168 #warn "Folding literal expr $i (val: " . $program->[ $i ]->[ 2 ]->[ 2 ] . ") (orig: " . $program->[ $i ]->[ 2 ]->[ 1 ] . ") into literal instr.";
2169  
2170 212 $program->[ $i ]->[ 0 ] = LITERAL;
2171 212 $program->[ $i ]->[ 2 ] = $program->[ $i ]->[ 2 ]->[ 2 ];
2172 }
2173  
2174  
2175 # Fold constant JUMP_IF into JUMP or delete.
2176 1336 %deletes = ();
2177 1336 ▶ for( my $i = 0; $i < @{$program}; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$program;}) 3375 1336 4711
2178 {
2179 3375 ▶ next unless $program->[ $i ]->[ 0 ] == JUMP_IF and
Branch: 1 branch
BranchTrueFalseTotal
unless $program->[$i][0] == 4 and $program->[$i][3][0] == 0 3281 94 3375
Condition100%3/3
$program->[$i][0] == 4 and $program->[$i][3][0] == 0
!ll&&!rl&&r
271456794
MC/DC100%2/2
$program->[$i][0] == 4 and $program->[$i][3][0] == 0
$program->[$i][0] == 4 $program->[$i][3][0] == 0
Truth table100%3/3
A and B
A= $program->[$i][0] == 4B= $program->[$i][3][0] == 0
ABresult
0X0
100
111
2180 $program->[ $i ]->[ 3 ]->[ 0 ] == LITERAL;
2181  
2182 94 $value = $self->_eval_expression( $program->[ $i ]->[ 3 ], 1 );
2183 # $value = not $value if $program->[ $i ]->[ 4 ];
2184  
2185 94 ▶ if( $value )
Branch: 1 branch
BranchTrueFalseTotal
if ($value) { } 48 46 94
2186 {
2187 # Always true, remove the JUMP.
2188 #warn "Folding constant JUMP_IF into no-op.";
2189 48 $deletes{ $i } = 1;
2190 }
2191 else
2192 {
2193 # Always false, fold it into a JUMP.
2194 #warn "Folding constant JUMP_IF into JUMP.";
2195 46 $program->[ $i ] = [ JUMP, $program->[ $i ]->[ 1 ],
2196 $program->[ $i ]->[ 2 ] ];
2197 }
2198 }
2199 1336 ▶ $self->_delete_instr( $program, keys( %deletes ) ) if %deletes;
Branch: 1 branch
BranchTrueFalseTotal
if %deletes 36 1300 1336
2200  
2201  
2202 # Trim empty context pushes (TODO: that have no assigns in top level)
2203 1336 %deletes = ();
2204 1336 @nest_stack = ();
2205 1336 ▶ for( my $i = 0; $i < @{$program}; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$program;}) 3327 1336 4663
2206 {
2207 3327 ▶ if( $program->[ $i ]->[ 0 ] == CONTEXT_PUSH )
Branch: 1 branch
BranchTrueFalseTotal
if ($program->[$i][0] == 7) 10 3317 3327
2208 {
2209 10 unshift @nest_stack, $i;
2210 10 ▶ $deletes{ $i } = 1 unless $program->[ $i ]->[ 2 ];
Branch: 1 branch
BranchTrueFalseTotal
unless $program->[$i][2] 7 3 10
2211 10 next;
2212 }
2213 3317 ▶ if( $program->[ $i ]->[ 0 ] == CONTEXT_POP )
Branch: 1 branch
BranchTrueFalseTotal
if ($program->[$i][0] == 8) 10 3307 3317
2214 {
2215 10 my ( $match );
2216  
2217 10 $match = shift @nest_stack;
2218 10 ▶ $deletes{ $i } = 1 if $deletes{ $match };
Branch: 1 branch
BranchTrueFalseTotal
if $deletes{$match} 7 3 10
2219 10 next;
2220 }
2221 }
2222 1336 ▶ $self->_delete_instr( $program, keys( %deletes ) ) if %deletes;
Branch: 1 branch
BranchTrueFalseTotal
if %deletes 6 1330 1336
2223  
2224  
2225 # Now scan for adjacent literals to merge where the second
2226 # isn't a jump target.
2227 1336 %deletes = ();
2228  
2229 # For speed, prebuild a list of all jump targets.
2230 1336 %jump_targets = ();
2231 1336 foreach my $line ( @{$program} )
2232 {
2233 3313 ▶ next unless $line->[ 0 ] == JUMP or
Branch: 1 branch
BranchTrueFalseTotal
unless $line->[0] == 3 or $line->[0] == 4 or $line->[0] == 5 or $line->[0] == 6 2111 1202 3313
Condition100%9/9
$line->[0] == 3 or $line->[0] == 4 or $line->[0] == 5 or $line->[0] == 6
l!l&&r!l&&!r
1142602111
$line->[0] == 3 or $line->[0] == 4 or $line->[0] == 5
l!l&&r!l&&!r
1082602171
$line->[0] == 3 or $line->[0] == 4
l!l&&r!l&&!r
5155672231
MC/DC100%4/4
$line->[0] == 3 or $line->[0] == 4 or $line->[0] == 5 or $line->[0] == 6
$line->[0] == 3 $line->[0] == 4 $line->[0] == 5 $line->[0] == 6
Truth table100%5/5
A or B or C or D
A= $line->[0] == 3B= $line->[0] == 4C= $line->[0] == 5D= $line->[0] == 6
ABCDresult
1XXX1
01XX1
001X1
00011
00000
2234 $line->[ 0 ] == JUMP_IF or
2235 $line->[ 0 ] == FOR or
2236 $line->[ 0 ] == END_FOR;
2237 1202 $jump_targets{ $line->[ 2 ] } = 1;
2238 }
2239  
2240 # Now scan for adjacent literals.
2241 1336 ▶ for( my $i = @{$program} - 1; $i > 0; $i-- )
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 1981 1336 3317
2242 {
2243 # Are both ourself and our previous instr a literal?
2244 1981 ▶ next if $program->[ $i ]->[ 0 ] != LITERAL or
Branch: 1 branch
BranchTrueFalseTotal
if $program->[$i][0] != 0 or $$program[$i - 1][0] != 0 1898 83 1981
Condition100%3/3
$program->[$i][0] != 0 or $$program[$i - 1][0] != 0
l!l&&r!l&&!r
99790183
MC/DC100%2/2
$program->[$i][0] != 0 or $$program[$i - 1][0] != 0
$program->[$i][0] != 0 $$program[$i - 1][0] != 0
Truth table100%3/3
A or B
A= $program->[$i][0] != 0B= $$program[$i - 1][0] != 0
ABresult
1X1
011
000
2245 $program->[ $i - 1 ]->[ 0 ] != LITERAL;
2246  
2247 # Do any jumps lead to the second literal?
2248 83 ▶ next if $jump_targets{ $i };
Branch: 1 branch
BranchTrueFalseTotal
if $jump_targets{$i} 32 51 83
2249  
2250 #warn "Merging literal $i to previous.";
2251 #warn "Merging literals [" . $program->[ $i - 1 ]->[ 2 ] . "] and [" . $program->[ $i ]->[ 2 ] . "]";
2252  
2253 # Ok, no reason for us to remain apart, let's get married.
2254 51 $program->[ $i - 1 ]->[ 2 ] .= $program->[ $i ]->[ 2 ];
2255 51 $deletes{ $i } = 1;
2256 }
2257 #warn "Literal merges: " . scalar( keys( %deletes ) );
2258 1336 ▶ $self->_delete_instr( $program, keys( %deletes ) ) if %deletes;
Branch: 1 branch
BranchTrueFalseTotal
if %deletes 37 1299 1336
2259  
2260 # Look for loops that make no use of special loop vars.
2261 1336 @loop_blocks = ();
2262 1336 ▶ for( my $i = 0; $i < @{$program}; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$program;}) 3262 1336 4598
2263 {
2264 # Are we a for statement?
2265 3262 ▶ next if $program->[ $i ]->[ 0 ] != FOR;
Branch: 1 branch
BranchTrueFalseTotal
if $program->[$i][0] != 5 3202 60 3262
2266 60 push @loop_blocks,
2267 [ $i, $program->[ $i ]->[ 2 ], $program->[ $i ]->[ 3 ] ];
2268 }
2269 # TODO: this should be moved into the above loop to keep it single-pass.
2270 1336 foreach my $block ( @loop_blocks )
2271 {
2272 60 my ( $special_vars_needed, $line );
2273  
2274 60 $special_vars_needed = 0;
2275 60 ▶ FORBLOCK: for( my $i = $block->[ 0 ] + 1; $i < $block->[ 1 ]; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $block->[1]) 128 29 157
2276 {
2277 128 my ( @exprs );
2278  
2279 128 $line = $program->[ $i ];
2280 128 ▶ if( $line->[ 0 ] == EXPR )
Branch: 4 branches
BranchTrueFalseTotal
if ($line->[0] == 2) { } 47 81 128
elsif ($line->[0] == 5) { } 3 78 81
elsif ($line->[0] == 4) { } 4 74 78
elsif ($line->[0] == 7) { } 1 73 74
2281 {
2282 47 @exprs = ( $line->[ 2 ] );
2283 }
2284 elsif( $line->[ 0 ] == FOR )
2285 {
2286 3 @exprs = ( $line->[ 4 ] );
2287 }
2288 elsif( $line->[ 0 ] == JUMP_IF )
2289 {
2290 4 @exprs = ( $line->[ 3 ] );
2291 }
2292 elsif( $line->[ 0 ] == CONTEXT_PUSH )
2293 {
2294 1 @exprs = values( %{$line->[ 2 ]} );
2295 }
2296  
2297 128 ▶ next unless @exprs;
Branch: 1 branch
BranchTrueFalseTotal
unless @exprs 73 55 128
2298  
2299 55 ▶ while( my $expr = shift( @exprs ) )
Branch: 1 branch
BranchTrueFalseTotal
if (my $expr = shift @exprs) 63 24 87
2300 {
2301 63 my ( $type );
2302  
2303 63 $type = $expr->[ 0 ];
2304 63 ▶ if( $type == VAR )
Branch: 5 branches
BranchTrueFalseTotal
if ($type == 104) { } 54 9 63
elsif ($type == 100) { } 1 8 9
elsif ($type == 101) { } 1 7 8
elsif ($type == 102) { } 6 1 7
elsif ($type == 103) { } 1 0 1
2305 {
2306 54 my ( $segments );
2307  
2308 54 $segments = $expr->[ 2 ];
2309 # Needs to have two or more segments.
2310 54 ▶ next unless $expr->[ 4 ] > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $expr->[4] > 0 20 34 54
2311 # Top stem isn't our loop var, we're not interested.
2312 34 ▶ next unless $segments->[ 0 ] eq $block->[ 2 ];
Branch: 1 branch
BranchTrueFalseTotal
unless $segments->[0] eq $block->[2] 1 33 34
2313  
2314 # OK, it's refering to our loop var, is it a special?
2315 33 ▶ if( ref( $segments->[ 1 ] ) or
Branch: 1 branch
BranchTrueFalseTotal
if (ref $segments->[1] or exists $special_values_names{$segments->[1]}) 31 2 33
Condition100%3/3
ref $segments->[1] or exists $special_values_names{$segments->[1]}
l!l&&r!l&&!r
1302
MC/DC100%2/2
ref $segments->[1] or exists $special_values_names{$segments->[1]}
ref $segments->[1] exists $special_values_names{$segments->[1]}
Truth table100%3/3
A or B
A= ref $segments->[1]B= exists $special_values_names{$segments->[1]}
ABresult
1X1
011
000
2316 exists( $special_values_names{ $segments->[ 1 ] } ) )
2317 {
2318 # Yes, it's either a special or an inconstant
2319 # expression subscript that we can't rule out
2320 # as evaluating to a special at runtime.
2321 31 $special_vars_needed = 1;
2322 31 last FORBLOCK;
2323 }
2324 }
2325 elsif( $type == OP_TREE )
2326 {
2327 1 push @exprs, $expr->[ 3 ], $expr->[ 4 ];
2328 }
2329 elsif( $type == UNARY_OP )
2330 {
2331 1 push @exprs, $expr->[ 3 ];
2332 }
2333 elsif( $type == FUNC )
2334 {
2335 6 push @exprs, @{$expr->[ 3 ]};
2336 }
2337 elsif( $type == METHOD )
2338 {
2339 1 push @exprs, @{$expr->[ 4 ]};
2340 }
2341 }
2342 }
2343 60 ▶ $program->[ $block->[ 0 ] ]->[ 5 ] = 0 unless $special_vars_needed;
Branch: 1 branch
BranchTrueFalseTotal
unless $special_vars_needed 29 31 60
2344 }
2345  
2346  
2347 # # walk program looking for functions, adding to function table.
2348 # # NOTE: turned out to not make a difference in run-time, but may revisit.
2349 # @function_table = ();
2350 # %function_index = ();
2351 # foreach my $line ( @{$program} )
2352 # {
2353 # my ( $op, @op_queue );
2354 # @op_queue = ();
2355 #
2356 # if( $line->[ 0 ] == EXPR )
2357 # {
2358 # push @op_queue, $line->[ 2 ];
2359 # }
2360 # elsif( $line->[ 0 ] == JUMP_IF )
2361 # {
2362 # push @op_queue, $line->[ 3 ];
2363 # }
2364 # elsif( $line->[ 0 ] == URL )
2365 # {
2366 # push @op_queue, %{$line->[ 2 ]};
2367 # }
2368 # elsif( $line->[ 0 ] == FOR )
2369 # {
2370 # push @op_queue, $line->[ 4 ];
2371 # }
2372 # elsif( $line->[ 0 ] == CONTEXT_PUSH )
2373 # {
2374 # push @op_queue, values( %{$line->[ 2 ]} );
2375 # }
2376 # while( defined( $op = shift( @op_queue ) ) )
2377 # {
2378 # next if not ref( $op ) or $op->[ 0 ] == VAR or
2379 # $op->[ 0 ] == LITERAL or $op->[ 0 ] == TEMPLATE;
2380 # if( $op->[ 0 ] == OP_TREE )
2381 # {
2382 # push @op_queue, $op->[ 3 ], $op->[ 4 ];
2383 # next;
2384 # }
2385 # if( $op->[ 0 ] == UNARY_OP )
2386 # {
2387 # push @op_queue, $op->[ 3 ];
2388 # next;
2389 # }
2390 # if( $op->[ 0 ] == METHOD )
2391 # {
2392 # push @op_queue, @{$op->[ 4 ]};
2393 # next;
2394 # }
2395 # $self->error( "Unknown EXPR opcode: " . $op->[ 0 ] .
2396 # " in function table construction." )
2397 # unless $op->[ 0 ] == FUNC;
2398 #
2399 ##warn "Looking at op " . _tinydump( $op );
2400 ##warn " Is function $op->[ 2 ]().";
2401 # if( not $function_index{ $op->[ 2 ] } )
2402 # {
2403 # push @function_table, $op->[ 2 ];
2404 # $function_index{ $op->[ 2 ] } = $#function_table;
2405 # }
2406 # $op->[ 2 ] = $function_index{ $op->[ 2 ] };
2407 ##warn " Replaced with $op->[ 2 ].";
2408 # push @op_queue, @{$op->[ 3 ]};
2409 # }
2410 # }
2411 # $template->{ function_table } = [ @function_table ];
2412 }
2413  
2414 # Warning, pass-by-ref: modifies $program.
2415 sub _delete_instr
2416 79 ▶{
Subroutine _delete_instr: called CC=13 SCAR=25.6
2417 79 my ( $self, $program, @addrs ) = @_;
2418 79 my ( %renumbers, $instr, $num, $lastnum, $lastoffset, $offset, $numaddr );
2419  
2420 #warn "** Deleting instr: " . join( ', ', @addrs ) . ".";
2421 #warn "-- Pre:\n" . $self->dumpable_template();
2422  
2423 # Delete all the stuff we've marked for deletion.
2424  
2425 # First we need to sort the deletes.
2426 79 @addrs = sort { $a <=> $b } @addrs;
2427  
2428 # Then we delete the instructions from last to first.
2429 # (To avoid renumbering issues).
2430 79 foreach my $addr ( reverse( @addrs ) )
2431 {
2432 113 splice( @{$program}, $addr, 1 );
2433 }
2434  
2435 #warn "-- Deleted:\n" . $self->dumpable_template();
2436  
2437 # Now we need to renumber any jump and loop targets affected.
2438 79 %renumbers = ();
2439 79 $lastnum = $lastoffset = 0;
2440 79 $numaddr = @addrs - 1;
2441 79 foreach my $line ( @{$program} )
2442 {
2443 435 ▶ next unless ( $instr = $line->[ 0 ] ) == JUMP or
Branch: 1 branch
BranchTrueFalseTotal
unless ($instr = $line->[0]) == 3 or $instr == 4 or $instr == 5 or $instr == 6 302 133 435
Condition100%9/9
($instr = $line->[0]) == 3 or $instr == 4 or $instr == 5 or $instr == 6
l!l&&r!l&&!r
11815302
($instr = $line->[0]) == 3 or $instr == 4 or $instr == 5
l!l&&r!l&&!r
10315317
($instr = $line->[0]) == 3 or $instr == 4
l!l&&r!l&&!r
9310332
MC/DC100%4/4
($instr = $line->[0]) == 3 or $instr == 4 or $instr == 5 or $instr == 6
($instr = $line->[0]) == 3 $instr == 4 $instr == 5 $instr == 6
Truth table100%5/5
A or B or C or D
A= ($instr = $line->[0]) == 3B= $instr == 4C= $instr == 5D= $instr == 6
ABCDresult
1XXX1
01XX1
001X1
00011
00000
2444 $instr == JUMP_IF or
2445 $instr == FOR or
2446 $instr == END_FOR;
2447  
2448 133 ▶ if( exists( $renumbers{ $num = $line->[ 2 ] } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $renumbers{$num = $line->[2]}) 16 117 133
2449 {
2450 16 $line->[ 2 ] = $renumbers{ $num };
2451 16 next;
2452 }
2453  
2454 # This contraption takes advantages of the fact that jumps
2455 # tend to have fairly local targets to other local jumps'
2456 # targets, rather than searching from the start of the
2457 # template each time.
2458 117 ▶ if( $lastnum <= $num )
Branch: 1 branch
BranchTrueFalseTotal
if ($lastnum <= $num) { } 90 27 117
2459 {
2460 #use Data::Dumper;
2461 #print "Jump target: $num.\nDeleted: ", Data::Dumper::Dumper( \@addrs ), "\n";
2462 # This jump is forwards from our last, search forwards.
2463 90 ▶ for( $offset = $lastoffset; $offset <= $numaddr; $offset++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($offset <= $numaddr) 95 55 150
2464 {
2465 #print " Offset is $offset, addrs[ $offset ] is $addrs[ $offset ]\n";
2466 95 ▶ last if $addrs[ $offset ] >= $num;
Branch: 1 branch
BranchTrueFalseTotal
if $addrs[$offset] >= $num 35 60 95
2467 }
2468 }
2469 else
2470 {
2471 # This jump is before our last, search backwards.
2472 27 ▶ for( $offset = $lastoffset; $offset > 0; $offset-- )
Branch: 1 branch
BranchTrueFalseTotal
if ($offset > 0) 26 11 37
2473 {
2474 26 ▶ last if $addrs[ $offset - 1 ] < $num;
Branch: 1 branch
BranchTrueFalseTotal
if $addrs[$offset - 1] < $num 16 10 26
2475 }
2476 }
2477 117 $lastnum = $num;
2478 117 $lastoffset = $offset;
2479  
2480 # Cache the result, if-elsif-else will have lots of the same targets.
2481 117 $renumbers{ $num } = ( $line->[ 2 ] -= $offset );
2482 }
2483  
2484 #warn "-- Renumbered:\n" . $self->dumpable_template();
2485 }
2486  
2487 sub _compile_expression
2488 3616 ▶{
Subroutine _compile_expression: called CC=10 SCAR=23.0
2489 3616 my ( $self, $expression ) = @_;
2490 3616 my ( @top_level, $highest_weight, $highest_pos );
2491  
2492 3616 $expression =~ s/^\s+//;
2493 3616 $expression =~ s/\s+$//;
2494  
2495 3616 ▶ $self->error( "Not a well-formed expression: $expression" )
Branch: 1 branch
BranchTrueFalseTotal
unless $expression =~ /^$expr_regexp$/so 6 3610 3616
2496 unless $expression =~ /^$expr_regexp$/so;
2497  
2498 3610 ▶ while( $expression =~ $capture_expr_op_remain_regexp )
Branch: 1 branch
BranchTrueFalseTotal
if ($expression =~ /$capture_expr_op_remain_regexp/) 320 3610 3930
2499 {
2500 # $lhs = $1;
2501 # $op = $2;
2502 # $rhs = $3;
2503 320 push @top_level, $1, $2;
2504 320 $expression = $3;
2505 }
2506  
2507 3610 ▶ return( $self->_build_op_tree( [ @top_level, $expression ] ) )
Branch: 1 branch
BranchTrueFalseTotal
if @top_level 319 3291 3610
2508 if @top_level;
2509  
2510 # Not a compound statement, must be atomic.
2511  
2512 # Is it a unary op?
2513 3291 ▶ if( my ( $op, $subexpr ) =
Branch: 1 branch
BranchTrueFalseTotal
if (() = $expression =~ /$capture_unary_operator_regexp/) 45 3246 3291
2514 $expression =~ $capture_unary_operator_regexp )
2515 {
2516 45 $subexpr = $self->_compile_expression( $subexpr );
2517  
2518 # Fold constant values.
2519 45 ▶ return( [ LITERAL, $expression,
Branch: 1 branch
BranchTrueFalseTotal
if $subexpr->[0] == 0 24 21 45
2520 $self->_eval_unary_op( $op, $subexpr ) ] )
2521 if $subexpr->[ 0 ] == LITERAL;
2522  
2523 21 return( [ UNARY_OP, $expression, $op, $subexpr ] );
2524 }
2525  
2526 # Is it a bracketed expression?
2527 # TODO: Do I care at this point if it's matching?
2528 # return( $self->_compile_expression( substr( $expression, 1, -1 ) ) )
2529 # if $expression =~ /^$matching_round_brackets_regexp$/so;
2530 3246 ▶ return( $self->_compile_expression( $1 ) )
Branch: 1 branch
BranchTrueFalseTotal
if $expression =~ /^\((.*)\)$/so 5 3241 3246
2531 if $expression =~ /^\((.*)\)$/so;
2532  
2533 # A literal number
2534 3241 ▶ return( [ LITERAL, $expression, $expression, 0 ] )
Branch: 1 branch
BranchTrueFalseTotal
if $expression =~ /^$literal_number_regexp$/so 413 2828 3241
2535 if $expression =~ /^$literal_number_regexp$/so;
2536  
2537 # A literal string
2538 2828 ▶ if( $expression =~ /^$single_quoted_text_regexp$/so )
Branch: 1 branch
BranchTrueFalseTotal
if ($expression =~ /^$Template::Sandbox::single_quoted_text_regexp$/so) 1012 1816 2828
2539 {
2540 1012 my ( $string );
2541  
2542 # Strip leading/trailing ' and unescape backslashed characters.
2543 1012 $string = substr( $expression, 1, -1 );
2544 1012 $string =~ s/\\(.)/$1/go;
2545 1012 return( [ LITERAL, $expression, $string, 0 ] );
2546 }
2547  
2548 # A variable or chained construct (including functions)
2549 1816 ▶ return( $self->_compile_chained_operation( $expression ) )
Branch: 1 branch
BranchTrueFalseTotal
if $expression =~ /^$chained_operation_regexp$/so 1816 0 1816
2550 if $expression =~ /^$chained_operation_regexp$/so;
2551  
2552 # "cannot happen".
2553 0 $self->error( "Unrecognised atomic expression element: $expression" );
2554 }
2555  
2556 # TODO: replace with "replace tightest-binding operator with subtree"
2557 # while-loop, rather than recursive divide-and-conquer by
2558 # loosest-binding operator.
2559 # will eleminate the depth*depth cartesian loops.
2560 sub _build_op_tree
2561 801 ▶{
Subroutine _build_op_tree: called CC=18 SCAR=29.0
2562 801 my ( $self, $arr ) = @_;
2563 801 my ( $highest_weight, $highest_pos, $op, $lhs, $rhs );
2564  
2565 #print "build_op_tree( ", Data::Dumper::Dumper( $arr ), "\n";
2566  
2567 # "cannot happen"
2568 801 ▶ $self->error( "Empty expression" ) unless @{$arr};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$arr;} 0 801 801
2569  
2570 # TODO: cache @{$arr} size.
2571 801 ▶ for( my $i = 0; $i < @{$arr}; $i += 2 )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$arr;}) 1122 801 1923
2572 {
2573 # TODO: this is a crappy hack to provide compat with recursion.
2574 1122 ▶ next if ref( $arr->[ $i ] );
Branch: 1 branch
BranchTrueFalseTotal
if ref $arr->[$i] 483 639 1122
2575 639 $arr->[ $i ] = $self->_compile_expression( $arr->[ $i ] );
2576 }
2577  
2578 801 ▶ return( $arr->[ 0 ] ) if @{$arr} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @{$arr;} == 1 481 320 801
2579  
2580 # Look for literals to fold together.
2581 #print "Looking at: ", Data::Dumper::Dumper( $arr ), "\n";
2582 320 ▶ for( my $i = 1; $i < @{$arr} - 1; $i += 2 )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$arr;} - 1) 321 320 641
2583 {
2584 321 my ( $op, $weight );
2585  
2586 321 $op = $arr->[ $i ];
2587 321 $weight = $operators{ $op }->[ 0 ];
2588  
2589 321 $lhs = $arr->[ $i - 1 ];
2590 321 $rhs = $arr->[ $i + 1 ];
2591  
2592 #print " Looking at op $i: '$op'\n";
2593 # If we're higher or equal precedence to the operators either
2594 # side of us, and our lhs and rhs are literal values, we're
2595 # eligible for folding.
2596 321 ▶ if( ( ( $i < 3 ) or
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 3 || $weight <= $operators{$$arr[$i - 2]}[0] and $i >= @{$arr;} - 2 || $weight <= $operators{$$arr[$i + 2]}[0] and $lhs->[0] == 0 and $rhs->[0] == 0) 79 242 321
Condition80%12/15
$i < 3 || $weight <= $operators{$$arr[$i - 2]}[0] and $i >= @{$arr;} - 2 || $weight <= $operators{$$arr[$i + 2]}[0] and $lhs->[0] == 0 and $rhs->[0] == 0
!ll&&!rl&&r
1647879
$i < 3 || $weight <= $operators{$$arr[$i - 2]}[0] and $i >= @{$arr;} - 2 || $weight <= $operators{$$arr[$i + 2]}[0] and $lhs->[0] == 0
!ll&&!rl&&r
1163157
$i < 3 || $weight <= $operators{$$arr[$i - 2]}[0] and $i >= @{$arr;} - 2 || $weight <= $operators{$$arr[$i + 2]}[0]
!ll&&!rl&&r
01320
$i < 3 or $weight <= $operators{$$arr[$i - 2]}[0]
l!l&&r!l&&!r
32010
$i >= @{$arr;} - 2 or $weight <= $operators{$$arr[$i + 2]}[0]
l!l&&r!l&&!r
32001
MC/DC50%3/6
$i < 3 || $weight <= $operators{$$arr[$i - 2]}[0] and $i >= @{$arr;} - 2 || $weight <= $operators{$$arr[$i + 2]}[0] and $lhs->[0] == 0 and $rhs->[0] == 0
$i < 3 $weight <= $operators{$$arr[$i - 2]}[0] $i >= @{$arr;} - 2 $weight <= $operators{$$arr[$i + 2]}[0] $lhs->[0] == 0 $rhs->[0] == 0
Truth table33%5/15
A or B and C or D and E and F
A= $i < 3B= $weight <= $operators{$$arr[$i - 2]}[0]C= $i >= @{$arr;} - 2D= $weight <= $operators{$$arr[$i + 2]}[0]E= $lhs->[0] == 0F= $rhs->[0] == 0
ABCDEFresult
00XXXX0
1X00XX0
0100XX0
1X1X0X0
1X010X0
011X0X0
01010X0
1X1X100
1X01100
011X100
0101100
1X1X111
1X01111
011X111
0101111
2597 ( $weight <= $operators{ $arr->[ $i - 2 ] }->[ 0 ] ) ) and
2598 321 ( ( $i >= @{$arr} - 2 ) or
2599 ( $weight <= $operators{ $arr->[ $i + 2 ] }->[ 0 ] ) ) and
2600 ( $lhs->[ 0 ] == LITERAL ) and ( $rhs->[ 0 ] == LITERAL ) )
2601 {
2602 79 my ( $original );
2603  
2604 # Rebuild of "original" is surely hackery of the finest order. :(
2605 79 ▶ $original = ( $lhs->[ 3 ] ? "( $lhs->[ 1 ] )" : $lhs->[ 1 ] ) .
Branch: 2 branches
BranchTrueFalseTotal
$lhs->[3] ? : 0 79 79
$rhs->[3] ? : 0 79 79
2606 " $op " .
2607 ( $rhs->[ 3 ] ? "( $rhs->[ 1 ] )" : $rhs->[ 1 ] );
2608  
2609 79 splice( @{$arr}, $i - 1, 3,
2610 [ LITERAL, $original,
2611 $self->_eval_op( $op, $lhs, $rhs ), 1 ] );
2612 79 ▶ $i = ( $i <= 3 ) ? 1 : $i - 4;
Branch: 1 branch
BranchTrueFalseTotal
$i <= 3 ? : 79 0 79
2613 #print " Folding, arr becomes: ", Data::Dumper::Dumper( $arr ), ", i = $i\n";
2614 }
2615 }
2616  
2617 320 ▶ return( $arr->[ 0 ] ) if @{$arr} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @{$arr;} == 1 79 241 320
2618  
2619 241 $highest_weight = 0;
2620 241 ▶ for( my $i = 1; $i < @{$arr} - 1; $i += 2 )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$arr;} - 1) 242 241 483
2621 {
2622 242 my ( $op );
2623  
2624 242 $op = $arr->[ $i ];
2625 #print "looking at op $i: $op\n";
2626 242 ▶ if( $operators{ $op }->[ 0 ] > $highest_weight )
Branch: 1 branch
BranchTrueFalseTotal
if ($operators{$op}[0] > $highest_weight) 241 1 242
2627 {
2628 241 $highest_weight = $operators{ $op }->[ 0 ];
2629 241 $highest_pos = $i;
2630 }
2631 }
2632 #print "highest_pos = $highest_pos, highest_op = $highest_op\n";
2633  
2634 241 $op = $arr->[ $highest_pos ];
2635 241 $lhs = $self->_build_op_tree( [ @{$arr}[ 0..$highest_pos - 1 ] ] );
2636 241 $rhs = $self->_build_op_tree( [ @{$arr}[ $highest_pos + 1..@{$arr} - 1 ] ] );
2637  
2638 241 return( [ OP_TREE, '', $op, $lhs, $rhs ] );
2639 }
2640  
2641 sub _build_var
2642 2095 ▶{
Subroutine _build_var: called CC=4 SCAR=13.9
2643 2095 my ( $self, $segments, $originals, $original ) = @_;
2644 2095 my @segments = @{$segments};
2645 2095 my @originals = @{$originals};
2646  
2647 # If we're just a subexpression with no subscripts, just return
2648 # the subexpression.
2649 2095 ▶ return( $segments[ 0 ] )
Branch: 1 branch
BranchTrueFalseTotal
if @segments == 1 and ref $segments[0] 277 1818 2095
Condition100%3/3
@segments == 1 and ref $segments[0]
!ll&&!rl&&r
5131305277
MC/DC100%2/2
@segments == 1 and ref $segments[0]
@segments == 1 ref $segments[0]
Truth table100%3/3
A and B
A= @segments == 1B= ref $segments[0]
ABresult
0X0
100
111
2650 if @segments == 1 and ref( $segments[ 0 ] );
2651  
2652 1818 ▶ if( $segments[ @segments - 1 ] eq '__size__' )
Branch: 1 branch
BranchTrueFalseTotal
if ($segments[@segments - 1] eq "__size__") 4 1814 1818
2653 {
2654 4 pop @segments;
2655 4 pop @originals;
2656 4 return( [ FUNC, $original, 'size',
2657 [ $self->_build_var( \@segments, \@originals, $original ) ],
2658 ] );
2659 }
2660  
2661 1814 return( [ VAR, $original, \@segments, \@originals, @segments - 1 ] );
2662 }
2663  
2664 sub _compile_chained_operation
2665 1816 ▶{
Subroutine _compile_chained_operation: called CC=15 SCAR=27.3
2666 1816 my ( $self, $chain ) = @_;
2667 1816 my ( $original, $original_so_far, @segments, @originals, $segment,
2668 $subscript );
2669  
2670 #print "compile_chained_operation( $chain )\n";
2671  
2672 return( $symbolic_literals{ $chain } )
2673 1816 ▶ if exists( $symbolic_literals{ $chain } );
Branch: 1 branch
BranchTrueFalseTotal
if exists $symbolic_literals{$chain} 3 1813 1816
2674  
2675  
2676 1813 $original = $chain;
2677  
2678 1813 @segments = @originals = ();
2679 1813 ▶ if( ( $segment, $chain ) =
Branch: 1 branch
BranchTrueFalseTotal
if (($segment, $chain) = $chain =~ /$capture_chained_operation_top_regexp/) { } 1813 0 1813
2680 $chain =~ $capture_chained_operation_top_regexp )
2681 {
2682 # $segment = $1;
2683 # $chain = $2 || '';
2684 1813 $original_so_far = $segment;
2685  
2686 #print "Capture top on '$original', segment '$segment', chain '$chain'\n";
2687  
2688 1813 ▶ if( $segment =~ $capture_function_regexp )
Branch: 1 branch
BranchTrueFalseTotal
if ($segment =~ /$capture_function_regexp/) { } 323 1490 1813
2689 {
2690 323 push @originals, $segment;
2691 323 $segment = $self->_compile_function( $1, $2, $original );
2692 282 ▶ return( $segment ) unless $chain;
Branch: 1 branch
BranchTrueFalseTotal
unless $chain 120 162 282
2693 162 ▶ $segment = $segment->[ 2 ] if $segment->[ 0 ] == LITERAL;
Branch: 1 branch
BranchTrueFalseTotal
if $segment->[0] == 0 0 162 162
2694 162 push @segments, $segment;
2695 }
2696 else
2697 {
2698 1490 push @segments, $segment;
2699 1490 push @originals, $segment;
2700 }
2701 }
2702 else
2703 {
2704 0 $self->error( "Malformed chained operator: '$chain'" );
2705 }
2706  
2707 1652 ▶ while( $chain and
Branch: 1 branch
BranchTrueFalseTotal
if ($chain and ($segment, $chain) = $chain =~ /$capture_chained_operation_subscript_regexp/) 1390 1652 3042
Condition66%2/3
$chain and ($segment, $chain) = $chain =~ /$capture_chained_operation_subscript_regexp/
!ll&&!rl&&r
165201390
MC/DC50%1/2
$chain and ($segment, $chain) = $chain =~ /$capture_chained_operation_subscript_regexp/
$chain ($segment, $chain) = $chain =~ /$capture_chained_operation_subscript_regexp/
Truth table66%2/3
A and B
A= $chainB= ($segment, $chain) = $chain =~ /$capture_chained_operation_subscript_regexp/
ABresult
0X0
100
111
2708 ( $segment, $chain ) =
2709 $chain =~ $capture_chained_operation_subscript_regexp )
2710 {
2711 # $segment = $1;
2712 # $chain = $2 || '';
2713 #print "Segment: $segment\nRest: $chain\n";
2714  
2715 # TODO: use a capture rather than m// and s///
2716 1390 ▶ if( $segment =~ $capture_literal_subscript_regexp )
Branch: 1 branch
BranchTrueFalseTotal
if ($segment =~ /$capture_literal_subscript_regexp/) 486 904 1390
2717 {
2718 #print " Literal\n";
2719 486 $original_so_far .= $segment;
2720 486 push @segments, $1;
2721 486 push @originals, $1;
2722 486 next;
2723 }
2724 904 ▶ if( ( $subscript ) = $segment =~ $capture_expr_subscript_regexp )
Branch: 1 branch
BranchTrueFalseTotal
if (($subscript) = $segment =~ /$capture_expr_subscript_regexp/) 465 439 904
2725 {
2726 #print " Expr\n";
2727 # var[ ... ] expression subscript notation.
2728  
2729 465 $original_so_far .= $segment;
2730 # $subscript = $1;
2731 465 my $index = $self->_compile_expression( $subscript );
2732  
2733 # If it's a constant push it up as if it
2734 # was a dotted literal index.
2735 465 ▶ if( $index->[ 0 ] == LITERAL )
Branch: 1 branch
BranchTrueFalseTotal
if ($index->[0] == 0) { } 451 14 465
2736 {
2737 451 push @segments, $index->[ 2 ];
2738 }
2739 else
2740 {
2741 14 push @segments, $index;
2742 }
2743 # $subscript =~ s/^\s+//o;
2744 # $subscript =~ s/\s+$//o;
2745 465 push @originals, $subscript;
2746 465 next;
2747 }
2748 # TODO: use a capture rather than m// and s///
2749 439 ▶ if( my ( $method, $args ) =
Branch: 1 branch
BranchTrueFalseTotal
if (() = $segment =~ /$capture_method_subscript_regexp/) 439 0 439
2750 $segment =~ $capture_method_subscript_regexp )
2751 {
2752 #print " Method\n";
2753  
2754 439 my $var = $self->_build_var( \@segments, \@originals,
2755 $original_so_far );
2756  
2757 439 ▶ if( $self->{ vmethods } )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'vmethods'}) { } 1 438 439
2758 {
2759 # We convert "vmethods" into our normal function style.
2760  
2761 1 my $func = $self->_compile_function(
2762 $method, $args, $original, [ $var ] );
2763  
2764 # Fold if it's a literal.
2765 1 ▶ $func = $func->[ 2 ] if $func->[ 0 ] == LITERAL;
Branch: 1 branch
BranchTrueFalseTotal
if $func->[0] == 0 0 1 1
2766 1 @segments = ( $func );
2767 }
2768 else
2769 {
2770 438 $args = $self->_compile_function_args( $args );
2771 438 @segments = ( [ METHOD, $original, $var, $method, $args ] );
2772 }
2773 439 @originals = ( $original_so_far );
2774 439 $original_so_far .= $segment;
2775 439 next;
2776 }
2777 0 $self->error( "Unknown chained operator subsection: '$segment' in '$original'" );
2778 }
2779  
2780 1652 ▶ $self->error( "Malformed variable segment: '$chain' in '$original'" )
Branch: 1 branch
BranchTrueFalseTotal
if $chain 0 1652 1652
2781 if $chain;
2782  
2783 1652 return( $self->_build_var( \@segments, \@originals, $original ) );
2784 }
2785  
2786 sub _compile_function
2787 324 ▶{
Subroutine _compile_function: called CC=15 SCAR=27.1
2788 324 my ( $self, $func, $args, $expression, $prepend_args ) = @_;
2789 324 my ( $numargs, $func_def );
2790  
2791 # $args = length( $args) > 2 ? substr( $args, 1, -2 ) : '';
2792  
2793 324 ▶ $func_def = $functions{ $func } if $functions{ $func };
Branch: 1 branch
BranchTrueFalseTotal
if $functions{$func} 78 246 324
2794 $func_def = $self->{ local_functions }->{ $func }
2795 if $self->{ local_functions } and
2796 324 ▶ $self->{ local_functions }->{ $func };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"local_functions"} and $self->{"local_functions"}{$func} 208 116 324
Condition100%3/3
$self->{"local_functions"} and $self->{"local_functions"}{$func}
!ll&&!rl&&r
10214208
MC/DC100%2/2
$self->{"local_functions"} and $self->{"local_functions"}{$func}
$self->{"local_functions"} $self->{"local_functions"}{$func}
Truth table100%3/3
A and B
A= $self->{"local_functions"}B= $self->{"local_functions"}{$func}
ABresult
0X0
100
111
2797  
2798 324 ▶ $self->error( "Unknown function: $func" ) unless $func_def;
Branch: 1 branch
BranchTrueFalseTotal
unless $func_def 39 285 324
2799  
2800 285 $args = $self->_compile_function_args( $args );
2801 285 ▶ unshift @{$args}, @{$prepend_args} if $prepend_args;
Branch: 1 branch
BranchTrueFalseTotal
if $prepend_args 1 284 285
2802  
2803 # Check the number of args.
2804 285 ▶ if( ( $numargs = $func_def->[ FUNC_ARG_NUM ] ) >= 0 )
Branch: 1 branch
BranchTrueFalseTotal
if (($numargs = $func_def->[1]) >= 0) 284 1 285
2805 {
2806 $self->error( "too few args to $func(), expected $numargs " .
2807 1 "and got " . @{$args} . " in $expression" )
2808 284 ▶ if @{$args} < $numargs;
Branch: 1 branch
BranchTrueFalseTotal
if @{$args;} < $numargs 1 283 284
2809 $self->error( "too many args to $func(), expected $numargs " .
2810 1 "and got " . @{$args} . " in $expression" )
2811 283 ▶ if @{$args} > $numargs;
Branch: 1 branch
BranchTrueFalseTotal
if @{$args;} > $numargs 1 282 283
2812 }
2813  
2814 283 ▶ unless( $func_def->[ FUNC_INCONST ] )
Branch: 1 branch
BranchTrueFalseTotal
unless ($func_def->[3]) 282 1 283
2815 {
2816 282 my ( $nonliteral );
2817  
2818 282 foreach my $arg ( @{$args} )
2819 {
2820 245 ▶ next if $arg->[ 0 ] == LITERAL;
Branch: 1 branch
BranchTrueFalseTotal
if $arg->[0] == 0 67 178 245
2821 178 $nonliteral = 1;
2822 178 last;
2823 }
2824  
2825 #CORE::warn( "$expression has " . ( $nonliteral ? "nonliteral" : "literal" ) . " args" );
2826 282 ▶ unless( $nonliteral )
Branch: 1 branch
BranchTrueFalseTotal
unless ($nonliteral) 104 178 282
2827 {
2828 104 my ( $ret );
2829  
2830 104 $ret = $self->_eval_function( $func, $args );
2831  
2832 return( [ LITERAL, $expression,
2833 104 ▶ ( ( ref( $ret ) eq 'SCALAR' ) ? ${$ret} : $ret ), 1 ] );
Branch: 1 branch
BranchTrueFalseTotal
ref $ret eq 'SCALAR' ? : 1 103 104
2834 }
2835 }
2836  
2837 179 ▶ unshift @{$args}, [ TEMPLATE ]
Branch: 1 branch
BranchTrueFalseTotal
if $func_def->[2] 1 178 179
2838 if $func_def->[ FUNC_NEEDS_TEMPLATE ];
2839  
2840 179 return( [ FUNC, $expression, $func, $args ] );
2841 }
2842  
2843 sub _compile_function_args
2844 723 ▶{
Subroutine _compile_function_args: called CC=5 SCAR=16.1
2845 723 my ( $self, $arglist ) = @_;
2846 723 my ( $original, @args, $nextarg );
2847  
2848 723 $arglist =~ s/^\s+//;
2849 723 $arglist =~ s/\s+$//;
2850  
2851 723 $original = $arglist;
2852  
2853 723 @args = ();
2854 723 ▶ while( defined( $arglist ) and length( $arglist ) and
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arglist and length $arglist and ($nextarg, $arglist) = $arglist =~ /$capture_expr_comma_remain_regexp/) 687 723 1410
Condition83%5/6
defined $arglist and length $arglist and ($nextarg, $arglist) = $arglist =~ /$capture_expr_comma_remain_regexp/
!ll&&!rl&&r
7230687
defined $arglist and length $arglist
!ll&&!rl&&r
66657687
MC/DC66%2/3
defined $arglist and length $arglist and ($nextarg, $arglist) = $arglist =~ /$capture_expr_comma_remain_regexp/
defined $arglist length $arglist ($nextarg, $arglist) = $arglist =~ /$capture_expr_comma_remain_regexp/
Truth table75%3/4
A and B and C
A= defined $arglistB= length $arglistC= ($nextarg, $arglist) = $arglist =~ /$capture_expr_comma_remain_regexp/
ABCresult
0XX0
10X0
1100
1111
2855 ( $nextarg, $arglist ) =
2856 ( $arglist =~ $capture_expr_comma_remain_regexp ) )
2857 {
2858 # $nextarg = $1;
2859 # $arglist = $2;
2860 687 push @args, $self->_compile_expression( $nextarg );
2861 }
2862 $self->error(
2863 723 ▶ "Malformed function arguments list: '$arglist' in '$original'" )
Branch: 1 branch
BranchTrueFalseTotal
if $arglist 0 723 723
2864 if $arglist;
2865 723 return( \@args );
2866 }
2867  
2868 sub _eval_expression
2869 3408 ▶{
Subroutine _eval_expression: called CC=17 SCAR=28.4
2870 3408 my ( $self, $expr, $undef_ok ) = @_;
2871 3408 my ( $type, $val );
2872  
2873 # "can't happen" in normal use, will error on next line anyway.
2874 # $self->error( "Bad arg to _eval_expression(): $expr" )
2875 # unless ref( $expr );
2876  
2877 #$self->{ exprcount }->{ $type }++;
2878 #my $exprstart = Time::HiRes::time();
2879 3408 ▶ if( ( $type = $expr->[ 0 ] ) == LITERAL )
Branch: 7 branches
BranchTrueFalseTotal
if (($type = $expr->[0]) == 0) { } 958 2450 3408
elsif ($type == 104) { } 1499 951 2450
elsif ($type == 100) { } 245 706 951
elsif ($type == 101) { } 57 649 706
elsif ($type == 102) { } 206 443 649
elsif ($type == 103) { } 442 1 443
elsif ($type == 105) { } 1 0 1
2880 {
2881 958 $val = $expr->[ 2 ];
2882 }
2883 elsif( $type == VAR )
2884 {
2885 1499 $val = $self->_eval_var( @{$expr}, $undef_ok );
2886 }
2887 elsif( $type == OP_TREE )
2888 {
2889 # $val = $self->_eval_op( $expr->[ 2 ], $expr->[ 3 ], $expr->[ 4 ] );
2890 # WARNING: this is unrolled below from _eval_op: keep in sync.
2891 #eval
2892 #{
2893 245 $val = $operators{ $expr->[ 2 ] };
2894 # Do we defer evaluation or not?
2895 245 ▶ if( $val->[ 2 ] )
Branch: 1 branch
BranchTrueFalseTotal
if ($val->[2]) { } 49 196 245
2896 {
2897 49 $val = $val->[ 1 ]->( $self, $expr->[ 3 ], $expr->[ 4 ] );
2898 }
2899 else
2900 {
2901 196 $val = $val->[ 1 ]->( $self,
2902 $self->_eval_expression( $expr->[ 3 ] ),
2903 $self->_eval_expression( $expr->[ 4 ] ) );
2904 }
2905 #};
2906 #$self->error( "$@" ) if $@;
2907 }
2908 elsif( $type == UNARY_OP )
2909 {
2910 # TODO: unroll? common enough to bother?
2911 57 $val = $self->_eval_unary_op( $expr->[ 2 ], $expr->[ 3 ] );
2912 }
2913 elsif( $type == FUNC )
2914 {
2915 # $val = $self->_eval_function( $expr->[ 2 ], $expr->[ 3 ] );
2916 # WARNING: this is unrolled below from _eval_function: keep in sync.
2917  
2918 #warn "Eval func $expr->[ 2 ] against " . _tinydump( [ @function_table ] );
2919 # $val = $function_table[ $expr->[ 2 ] ];
2920  
2921 # TODO: should copy_global_functions block class-function lookup?
2922 206 ▶ $val = $functions{ $expr->[ 2 ] } if $functions{ $expr->[ 2 ] };
Branch: 1 branch
BranchTrueFalseTotal
if $functions{$expr->[2]} 29 177 206
2923 $val = $self->{ local_functions }->{ $expr->[ 2 ] }
2924 if $self->{ local_functions } and
2925 206 ▶ $self->{ local_functions }->{ $expr->[ 2 ] };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"local_functions"} and $self->{"local_functions"}{$expr->[2]} 175 31 206
Condition100%3/3
$self->{"local_functions"} and $self->{"local_functions"}{$expr->[2]}
!ll&&!rl&&r
301175
MC/DC100%2/2
$self->{"local_functions"} and $self->{"local_functions"}{$expr->[2]}
$self->{"local_functions"} $self->{"local_functions"}{$expr->[2]}
Truth table100%3/3
A and B
A= $self->{"local_functions"}B= $self->{"local_functions"}{$expr->[2]}
ABresult
0X0
100
111
2926 206 ▶ $self->error( "Unknown function: $expr->[ 2 ]" ) unless $val;
Branch: 1 branch
BranchTrueFalseTotal
unless $val 2 204 206
2927 204 ▶ if( $val->[ FUNC_UNDEF_OK ] )
Branch: 1 branch
BranchTrueFalseTotal
if ($val->[4]) { } 26 178 204
2928 {
2929 $val = $val->[ FUNC_FUNC ]->(
2930 26 map { $self->_eval_expression( $_, 1 ) } @{$expr->[ 3 ]} );
2931 }
2932 else
2933 {
2934 $val = $val->[ FUNC_FUNC ]->(
2935 178 map { $self->_eval_expression( $_ ) } @{$expr->[ 3 ]} );
2936 }
2937 }
2938 elsif( $type == METHOD )
2939 {
2940 442 $val = $self->_eval_method( $expr->[ 2 ], $expr->[ 3 ], $expr->[ 4 ] );
2941 }
2942 elsif( $type == TEMPLATE )
2943 {
2944 1 return( $self );
2945 }
2946 else
2947 {
2948 0 $self->error( "Unknown expression opcode: $type" );
2949 }
2950 #$self->{ exprprofile }->{ $type } += Time::HiRes::time() - $exprstart;
2951  
2952 # Undef warning.
2953 3394 ▶ $self->warning( "undefined template value '$expr->[ 1 ]'" )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val or $undef_ok or $expr->[1] eq "undef" 10 3384 3394
Condition83%5/6
defined $val or $undef_ok or $expr->[1] eq "undef"
l!l&&r!l&&!r
3384010
defined $val or $undef_ok
l!l&&r!l&&!r
3376810
MC/DC66%2/3
defined $val or $undef_ok or $expr->[1] eq "undef"
defined $val $undef_ok $expr->[1] eq "undef"
Truth table75%3/4
A or B or C
A= defined $valB= $undef_okC= $expr->[1] eq "undef"
ABCresult
1XX1
01X1
0011
0000
2954 unless defined( $val ) or $undef_ok or $expr->[ 1 ] eq 'undef';
2955  
2956 3394 return( $val );
2957 }
2958  
2959 sub _eval_op
2960 79 ▶{
Subroutine _eval_op: called CC=2 SCAR=6.9
2961 79 my ( $self, $op, $lhs, $rhs ) = @_;
2962  
2963 #my $ret;
2964 #$self->{ opcount }->{ $op }++;
2965 #my $opstart = Time::HiRes::time();
2966 #$ret = $operators{ $op }->[ 1 ]->( $self, $lhs, $rhs );
2967 #$self->{ opprofile }->{ $op } += Time::HiRes::time() - $opstart;
2968 #return( $ret );
2969  
2970 # WARNING: this function is unrolled above in _eval_expr: keep in sync.
2971  
2972 79 $op = $operators{ $op };
2973  
2974 # Do we defer evaluation or not?
2975 79 ▶ return( $op->[ 1 ]->( $self,
Branch: 1 branch
BranchTrueFalseTotal
unless $op->[2] 63 16 79
2976 $self->_eval_expression( $lhs ),
2977 $self->_eval_expression( $rhs ) ) )
2978 unless $op->[ 2 ];
2979  
2980 16 return( $op->[ 1 ]->( $self, $lhs, $rhs ) );
2981 }
2982  
2983 sub _eval_unary_op
2984 81 ▶{
Subroutine _eval_unary_op: called CC=6 SCAR=18.1
2985 81 my ( $self, $op, $expr ) = @_;
2986  
2987 # "|| 0" is there because !1 in perl is '' but we want 0.
2988 # !'' gives 1, so seems reasonable !'whatever' should be 0 too not ''.
2989 81 ▶ return( !$self->_eval_expression( $expr, 1 ) || 0 )
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "!" 16 65 81
Condition100%2/2
!$self->_eval_expression($expr, 1) || 0
l!l
88
MC/DC100%1/1
!$self->_eval_expression($expr, 1) || 0
!$self->_eval_expression($expr, 1)
Truth table100%2/2
not(A)
A= !$self->_eval_expression($expr, 1)
Aresult
11
00
2990 if $op eq '!';
2991 65 ▶ return( ( not $self->_eval_expression( $expr, 1 ) ) || 0 )
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "not" 48 17 65
Condition100%2/2
!$self->_eval_expression($expr, 1) || 0
l!l
2424
MC/DC100%1/1
!$self->_eval_expression($expr, 1) || 0
!$self->_eval_expression($expr, 1)
Truth table100%2/2
not(A)
A= !$self->_eval_expression($expr, 1)
Aresult
11
00
2992 if $op eq 'not';
2993 # TODO: This is odd for strings, probably should error or warn.
2994 17 ▶ return( -$self->_eval_expression( $expr ) )
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "-" 17 0 17
2995 if $op eq '-';
2996  
2997 0 $self->error( "Unknown unary operator: '$op'" );
2998 }
2999  
3000 sub _assign_var
3001 1 ▶{
Subroutine _assign_var: called CC=5 SCAR=17.8
3002 1 my ( $self, $lhs, $rhs ) = @_;
3003 1 my ( $var_stack, $counter, $sz, $var );
3004  
3005 # TODO: this should be compile-time ideally.
3006 1 ▶ $self->error( "Invalid LHS to assignment: $lhs->[ 1 ]" )
Branch: 1 branch
BranchTrueFalseTotal
if $lhs->[0] != 104 0 1 1
3007 if $lhs->[ 0 ] != VAR;
3008  
3009 # TODO: this should be compile-time ideally.
3010 $self->error( "Can only assign to top-level variables: $lhs->[ 1 ]" )
3011 1 ▶ if @{$lhs->[ 2 ]} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @{$lhs->[2];} > 1 0 1 1
3012  
3013 1 $var = $lhs->[ 2 ]->[ 0 ];
3014  
3015 1 $var_stack = $self->{ var_stack };
3016 1 $var_stack->[ 0 ]->{ $var } = $rhs;
3017 1 $sz = @{$var_stack};
3018 1 $counter = 1;
3019 1 ▶ while( $counter < $sz )
Branch: 1 branch
BranchTrueFalseTotal
if ($counter < $sz) 0 1 1
3020 {
3021 0 ▶ return( $rhs ) unless exists( $var_stack->[ $counter ]->{ $var } );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $var_stack->[$counter]{$var} 0 0 0
3022 0 $var_stack->[ $counter ]->{ $var } = $rhs;
3023 0 $counter++;
3024 }
3025  
3026 1 return( $rhs );
3027 }
3028  
3029 sub _eval_var
3030 1499 ▶{
Subroutine _eval_var: called CC=21 SCAR=30.6
3031 # The stem value _is_ the value if there's no other segments.
3032 # This is pulled above the sub's argument extraction for speed, I
3033 # will rot in hell for this, but it _is_ performance-critical.
3034 1499 ▶ return( $_[ 0 ]->{ var_stack_top }->{ $_[ 3 ]->[ 0 ] } )
Branch: 1 branch
BranchTrueFalseTotal
unless $_[5] 855 644 1499
3035 unless $_[ 5 ];
3036  
3037 644 my ( $self, $instr, $original, $segments, $originals, $last, $undef_ok ) = @_;
3038 644 my ( $val, $stem, $i, $special_values, $leaf, $type );
3039  
3040 644 $stem = $segments->[ 0 ];
3041 644 $special_values = $self->{ special_values };
3042  
3043 # Check to see if it's a special loop variable or something.
3044 644 ▶ if( $last >= 1 and
Branch: 1 branch
BranchTrueFalseTotal
if ($last >= 1 and $special_values->{$stem} and exists $special_values_names{$segments->[1]}) { } 160 484 644
Condition83%5/6
$last >= 1 and $special_values->{$stem} and exists $special_values_names{$segments->[1]}
!ll&&!rl&&r
4795160
$last >= 1 and $special_values->{$stem}
!ll&&!rl&&r
0479165
MC/DC66%2/3
$last >= 1 and $special_values->{$stem} and exists $special_values_names{$segments->[1]}
$last >= 1 $special_values->{$stem} exists $special_values_names{$segments->[1]}
Truth table75%3/4
A and B and C
A= $last >= 1B= $special_values->{$stem}C= exists $special_values_names{$segments->[1]}
ABCresult
0XX0
10X0
1100
1111
3045 $special_values->{ $stem } and
3046 exists( $special_values_names{ $segments->[ 1 ] } ) )
3047 {
3048 # Don't bother checking that the leaf isn't a ref, it won't
3049 # match a key and saves on a ref() call when it isn't.
3050 $val = $special_values->{ $stem }->[
3051 160 $special_values_names{ $segments->[ 1 ] } ];
3052 160 $i = 2;
3053 }
3054 else
3055 {
3056 484 $i = 1;
3057 # Determine the stem (top-level) value
3058 484 ▶ if( ref( $stem ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $stem) { } 324 160 484
3059 {
3060 # Top level is an expression not a var.
3061 324 $val = $self->_eval_expression( $stem );
3062 }
3063 else
3064 {
3065 160 $val = $self->{ var_stack_top }->{ $stem };
3066 }
3067 }
3068  
3069 # Navigate our way down the remaining segments.
3070 644 ▶ for( ; $i <= $last; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $last) 926 636 1562
3071 {
3072 926 ▶ if( ref( $leaf = $segments->[ $i ] ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref($leaf = $segments->[$i])) 22 904 926
3073 {
3074 # It's an index expression of the style var[index]
3075 22 ▶ unless( defined( $leaf = $self->_eval_expression( $leaf ) ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($leaf = $self->_eval_expression($leaf))) 2 20 22
3076 {
3077 2 ▶ return( undef ) if $undef_ok;
Branch: 1 branch
BranchTrueFalseTotal
if $undef_ok 0 2 2
3078 2 $self->error(
3079 "Undefined index '$originals->[ $i ]' in " .
3080 "'$original'" );
3081 }
3082  
3083 # Check to see if it's a special loop variable or something.
3084 # Only need to do this if we're an EXPR subscript, constant
3085 # ones will have been checked outside the loop.
3086 20 ▶ if( $i == 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 1) 15 5 20
3087 {
3088 15 ▶ if( $special_values->{ $stem } and
Branch: 1 branch
BranchTrueFalseTotal
if ($special_values->{$stem} and exists $special_values_names{$leaf}) 5 10 15
Condition66%2/3
$special_values->{$stem} and exists $special_values_names{$leaf}
!ll&&!rl&&r
1005
MC/DC50%1/2
$special_values->{$stem} and exists $special_values_names{$leaf}
$special_values->{$stem} exists $special_values_names{$leaf}
Truth table66%2/3
A and B
A= $special_values->{$stem}B= exists $special_values_names{$leaf}
ABresult
0X0
100
111
3089 exists( $special_values_names{ $leaf } ) )
3090 {
3091 $val = $special_values->{ $stem }->[
3092 5 $special_values_names{ $leaf } ];
3093 5 next;
3094 }
3095 }
3096 }
3097  
3098 919 ▶ unless( defined( $val ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 2 917 919
3099 {
3100 2 ▶ return( undef ) if $undef_ok;
Branch: 1 branch
BranchTrueFalseTotal
if $undef_ok 0 2 2
3101 2 ▶ $self->error(
Branch: 1 branch
BranchTrueFalseTotal
$originals->[$i] ne $leaf ? : 2 0 2
3102 "Can't get key '$leaf' " .
3103 ( $originals->[ $i ] ne $leaf ?
3104 "(from '$originals->[ $i ]') " : "" ) .
3105 #"(with segments " . Data::Dumper::Dumper( $segments ) . ") " .
3106 "of undefined parent in '$original'" );
3107 }
3108  
3109 917 ▶ if( not ( $type = ref( $val ) ) )
Branch: 2 branches
BranchTrueFalseTotal
if (not $type = ref $val) { } 2 915 917
elsif ($type eq 'ARRAY') { } 9 906 915
3110 {
3111 #use Data::Dumper;
3112 #warn "originals = " . Data::Dumper::Dumper( $originals ) . "\ni = $i\nleaf = $leaf\noriginal = $original\nsegments = " . Data::Dumper::Dumper( $segments ) . "\n";
3113  
3114 2 ▶ $self->error(
Branch: 1 branch
BranchTrueFalseTotal
$originals->[$i] ne $leaf ? : 2 0 2
3115 "Can't get key '$leaf' " .
3116 ( $originals->[ $i ] ne $leaf ?
3117 "(from '$originals->[ $i ]') " : "" ) .
3118 "of non-reference parent in '$original'" );
3119 }
3120 elsif( $type eq 'ARRAY' )
3121 {
3122 9 ▶ $self->error(
Branch: 2 branches
BranchTrueFalseTotal
unless $leaf =~ /^\d+$/o 2 7 9
$originals->[$i] ne $leaf ? : 2 0 2
3123 "Can't index array-reference with string '$leaf' " .
3124 ( $originals->[ $i ] ne $leaf ?
3125 "(from '$originals->[ $i ]') " : "" ) .
3126 "in '$original'" )
3127 unless $leaf =~ /^\d+$/o;
3128 7 $val = $val->[ $leaf ];
3129 }
3130 else
3131 {
3132 906 $val = $val->{ $leaf };
3133 }
3134 }
3135  
3136 636 return( $val );
3137 }
3138  
3139 sub _eval_function
3140 104 ▶{
Subroutine _eval_function: called CC=7 SCAR=19.5
3141 104 my ( $self, $func, $args ) = @_;
3142 104 my ( $val );
3143  
3144 # WARNING: this function is unrolled above in _eval_expr: keep in sync.
3145  
3146 # TODO: should copy_global_functions block class-function lookup?
3147 104 ▶ $val = $functions{ $func } if $functions{ $func };
Branch: 1 branch
BranchTrueFalseTotal
if $functions{$func} 72 32 104
3148 $val = $self->{ local_functions }->{ $func }
3149 if $self->{ local_functions } and
3150 104 ▶ $self->{ local_functions }->{ $func };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"local_functions"} and $self->{"local_functions"}{$func} 33 71 104
Condition66%2/3
$self->{"local_functions"} and $self->{"local_functions"}{$func}
!ll&&!rl&&r
71033
MC/DC50%1/2
$self->{"local_functions"} and $self->{"local_functions"}{$func}
$self->{"local_functions"} $self->{"local_functions"}{$func}
Truth table66%2/3
A and B
A= $self->{"local_functions"}B= $self->{"local_functions"}{$func}
ABresult
0X0
100
111
3151 104 ▶ $self->error( "Unknown function: $func" ) unless $val;
Branch: 1 branch
BranchTrueFalseTotal
unless $val 0 104 104
3152  
3153 104 ▶ if( $val->[ FUNC_UNDEF_OK ] )
Branch: 1 branch
BranchTrueFalseTotal
if ($val->[4]) { } 2 102 104
3154 {
3155 2 $args = [ map { $self->_eval_expression( $_, 1 ) } @{$args} ];
3156 }
3157 else
3158 {
3159 102 $args = [ map { $self->_eval_expression( $_ ) } @{$args} ];
3160 }
3161  
3162 #$self->{ funccount }->{ $func }++;
3163 #my $ret;
3164 #my $start_time = Time::HiRes::time();
3165 # $ret = $functions{ $func }->[ 1 ]->( $self, @{$args} );
3166 #$self->{ funcprofile }->{ $func } += Time::HiRes::time() - $start_time;
3167 # return( $ret );
3168  
3169 104 ▶ if( $val->[ FUNC_NEEDS_TEMPLATE ] )
Branch: 1 branch
BranchTrueFalseTotal
if ($val->[2]) { } 1 103 104
3170 {
3171 1 return( $val->[ FUNC_FUNC ]->( $self, @{$args} ) );
3172 }
3173 else
3174 {
3175 103 return( $val->[ FUNC_FUNC ]->( @{$args} ) );
3176 }
3177 }
3178  
3179 sub _eval_method
3180 442 ▶{
Subroutine _eval_method: called CC=4 SCAR=13.9
3181 442 my ( $self, $expr, $method, $args ) = @_;
3182 442 my ( $exprdesc, $ret );
3183  
3184 442 $exprdesc = $expr->[ 1 ];
3185 442 $expr = $self->_eval_expression( $expr );
3186  
3187 442 ▶ $self->error( "Can't call method on undefined value $exprdesc" )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expr 1 441 442
3188 unless defined $expr;
3189 441 ▶ $self->error( "Can't call method on non-reference value $exprdesc: $expr" )
Branch: 1 branch
BranchTrueFalseTotal
unless ref $expr 1 440 441
3190 unless ref( $expr );
3191  
3192 # For security reasons we don't want to allow calling
3193 # just any old method on any old object from within a
3194 # potentially user-defined template.
3195 440 ▶ $self->error( 'Invalid method to call from within a template: ' .
Branch: 1 branch
BranchTrueFalseTotal
unless $expr->valid_template_method($method) 1 439 440
3196 ref( $expr ) . "->$method" )
3197 unless $expr->valid_template_method( $method );
3198  
3199 439 $args = [ map { $self->_eval_expression( $_ ) } @{$args} ];
3200  
3201 439 $ret = $expr->$method( @{$args} );
3202  
3203 439 return( $ret );
3204 }
3205  
3206 sub run
3207 1117 ▶{
Subroutine run: called CC=38 SCAR=36.4
Pod documented
3208 1117 my $self = $_[ 0 ];
3209 # $line, $instr, $value lexically belong inside the loop,
3210 # but in such a tight loop it's a performance hit, they're
3211 # initialized at the start of each use anyway.
3212 # If oddness ensues, move this line into the head of the loop and
3213 # see if oddness abates.
3214 1117 my ( $lineno, $ret, @var_stack, @for_stack, $run_start,
3215 $program, $last_instr, $special_values, $line, $instr, $value );
3216  
3217 # For large templates this tricks perl's memory handling into
3218 # giving us a big chunk of contiguous memory so that $ret .= $whatever
3219 # doesn't have to keep incrementally adding more memory, this can
3220 # give a minor-but-not-insignificant speed boost of ~2-3%.
3221 # This may be highly sensitive to perl version and OS, and I'm
3222 # not sure it does nice things to the memory profile of the process
3223 # either...
3224 1117 $ret = ' ' x 80_000;
3225 1117 $ret = '';
3226 1117 $lineno = 0;
3227  
3228 1117 @var_stack = ( $self->{ vars } );
3229 1117 @for_stack = ();
3230  
3231 1117 $self->{ var_stack } = \@var_stack;
3232 1117 $self->{ var_stack_top } = $var_stack[ 0 ];
3233  
3234 1117 $self->{ phase } = 'runtime';
3235  
3236 #my $total_instr = 0;
3237  
3238 #foreach my $prof ( qw/instr expr func op/ )
3239 #{
3240 # $self->{ "${prof}count" } = {};
3241 # $self->{ "${prof}profile" } = {};
3242 #}
3243  
3244 # Local unroll of some of our properties
3245 1117 $program = $self->{ template }->{ program };
3246 1117 $last_instr = $self->{ template }->{ last_instr };
3247 # @function_table =
3248 # map { $functions{ $_ } } @{$self->{ template }->{ function_table }};
3249 1117 $special_values = $self->{ special_values };
3250  
3251 1117 ▶ while( $lineno <= $last_instr )
Branch: 1 branch
BranchTrueFalseTotal
if ($lineno <= $last_instr) 2103 1104 3207
3252 {
3253 2103 $line = $program->[ $lineno++ ];
3254 2103 $self->{ current_pos } = $line->[ 1 ];
3255  
3256 # TODO: look at $pos->[ 0 ] to determine file and recreate
3257 # the "stack" for error traces if neccessary.
3258  
3259 #$self->{ instrcount }->{ $line->[ 0 ] }++;
3260 #my $instrstart = Time::HiRes::time();
3261 2103 ▶ if( ( $instr = $line->[ 0 ] ) == LITERAL )
Branch: 11 branches
BranchTrueFalseTotal
if (($instr = $line->[0]) == 0) { } 654 1449 2103
elsif ($instr == 2) { } 952 497 1449
elsif ($instr == 3) { } 110 387 497
elsif ($instr == 4) { } 102 285 387
elsif ($instr == 5) { } 55 230 285
elsif ($instr == 6) { } 205 25 230
elsif ($instr == 7) { } 6 19 25
elsif ($instr == 8) { } 6 13 19
elsif ($self->{'local_syntaxes'}{'.instr'}{$instr}) { } 11 2 13
elsif ($syntaxes{'.instr'}{$instr}) { } 2 0 2
elsif ($instr == 1) { } 0 0 0
3262 {
3263 654 $ret .= $line->[ 2 ];
3264 }
3265 elsif( $instr == EXPR )
3266 {
3267 952 $value = $self->_eval_expression( $line->[ 2 ] );
3268 939 ▶ $ret .= ( ( ref( $value ) eq 'SCALAR' ) ? ${$value} : $value )
Branch: 2 branches
BranchTrueFalseTotal
unless $line->[3] 938 1 939
ref $value eq 'SCALAR' ? : 1 937 938
3269 unless $line->[ 3 ];
3270 }
3271 elsif( $instr == JUMP )
3272 {
3273 #$ret .= "[jump]";
3274 110 $lineno = $line->[ 2 ];
3275 }
3276 elsif( $instr == JUMP_IF )
3277 {
3278 #$ret .= "[jump if/unless $line->[3]]";
3279 102 $value = $self->_eval_expression( $line->[ 3 ], 1 );
3280 # $value = not $value if $line->[ 4 ];
3281 102 ▶ $lineno = $line->[ 2 ] unless $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 45 57 102
3282 }
3283 elsif( $instr == FOR )
3284 {
3285 55 my ( $iterator, $set, $set_value, $hash, $last, $specials_needed );
3286  
3287 55 $iterator = $line->[ 3 ];
3288 55 $set = $line->[ 4 ];
3289 55 $specials_needed = $line->[ 5 ];
3290  
3291 55 $set_value = $self->_eval_expression( $set, 1 );
3292 55 ▶ $set_value = [] unless defined $set_value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $set_value 1 54 55
3293  
3294 55 ▶ if( ref( $set_value ) eq 'HASH' )
Branch: 2 branches
BranchTrueFalseTotal
if (ref $set_value eq 'HASH') { } 16 39 55
elsif (not ref $set_value) { } 30 9 39
3295 {
3296 16 $hash = $set_value;
3297 16 $set_value = [ sort( keys( %{$set_value} ) ) ];
3298 }
3299 elsif( not ref( $set_value ) )
3300 {
3301 # If it's a number make it into a loop of 0..number.
3302 # If they want 1..number they can <: if x != 1 :> inside it.
3303 30 $set_value = [ 0..int( $set_value ) ];
3304 }
3305  
3306 # TODO: assign and compare
3307 55 $last = @{$set_value} - 1;
3308 55 ▶ if( $last == -1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($last == -1) { } 3 52 55
3309 {
3310 3 $lineno = $line->[ 2 ];
3311 }
3312 else
3313 {
3314 52 my ( $context );
3315  
3316 52 $value = $set_value->[ 0 ];
3317 $special_values->{ $iterator } =
3318 [
3319 0,
3320 1,
3321 0,
3322 1,
3323 0,
3324 $last == 0 ? 1 : 0,
3325 undef,
3326 $last == 0 ?
3327 undef : $set_value->[ 1 ],
3328 52 ▶ $hash ? $hash->{ $value } : undef,
Branch: 4 branches
BranchTrueFalseTotal
if $specials_needed 34 18 52
$last == 0 ? : 1 33 34
$last == 0 ? : 1 33 34
$hash ? : 11 23 34
3329 ]
3330 if $specials_needed;
3331 # Optimization: only create a new context if needed.
3332 52 ▶ if( $var_stack[ 0 ]->{ $iterator } )
Branch: 1 branch
BranchTrueFalseTotal
if ($var_stack[0]{$iterator}) { } 3 49 52
3333 {
3334 3 $context = { %{$var_stack[ 0 ]} };
3335 3 $context->{ $iterator } = $value;
3336 3 unshift @var_stack, $context;
3337 3 $self->{ var_stack_top } = $context;
3338 }
3339 else
3340 {
3341 49 $var_stack[ 0 ]->{ $iterator } = $value;
3342 }
3343 52 ▶ unshift @for_stack, [
Branch: 1 branch
BranchTrueFalseTotal
$context ? : 3 49 52
3344 0, $last, $set_value, $hash, $context ? 1 : 0,
3345 $specials_needed,
3346 ];
3347 }
3348 }
3349 elsif( $instr == END_FOR )
3350 {
3351 205 my ( $iterator, $set_value, $counter, $hash, $last,
3352 $specials_needed );
3353  
3354 205 $iterator = $line->[ 3 ];
3355  
3356 205 $counter = ++$for_stack[ 0 ]->[ LOOP_STACK_COUNTER ];
3357 205 $last = $for_stack[ 0 ]->[ LOOP_STACK_LAST ];
3358  
3359 205 ▶ if( $counter <= $last )
Branch: 1 branch
BranchTrueFalseTotal
if ($counter <= $last) { } 153 52 205
3360 {
3361 153 $set_value = $for_stack[ 0 ]->[ LOOP_STACK_SET ];
3362 153 $hash = $for_stack[ 0 ]->[ LOOP_STACK_HASH ];
3363 153 $specials_needed = $for_stack[ 0 ]->[ LOOP_STACK_SPECIALS ];
3364  
3365 153 $var_stack[ 0 ]->{ $iterator } = $set_value->[ $counter ];
3366 $special_values->{ $iterator } =
3367 [
3368 $counter,
3369 ( $counter % 2 ) ? 0 : 1,
3370 $counter % 2,
3371 0,
3372 $counter == $last ? 0 : 1,
3373 $counter == $last ? 1 : 0,
3374 $set_value->[ $counter - 1 ],
3375 $counter == $last ?
3376 undef :
3377 $set_value->[ $counter + 1 ],
3378 153 ▶ $hash ? $hash->{ $set_value->[ $counter ] } : undef,
Branch: 6 branches
BranchTrueFalseTotal
if $specials_needed 110 43 153
$counter % 2 ? : 57 53 110
$counter == $last ? : 33 77 110
$counter == $last ? : 33 77 110
$counter == $last ? : 33 77 110
$hash ? : 20 90 110
3379 ]
3380 if $specials_needed;
3381  
3382 153 $lineno = $line->[ 2 ];
3383 }
3384 else
3385 {
3386 52 ▶ if( $for_stack[ 0 ]->[ LOOP_STACK_CONTEXT ] )
Branch: 1 branch
BranchTrueFalseTotal
if ($for_stack[0][4]) { } 3 49 52
3387 {
3388 3 shift @var_stack;
3389 3 $self->{ var_stack_top } = $var_stack[ 0 ];
3390 }
3391 else
3392 {
3393 49 delete $var_stack[ 0 ]->{ $iterator };
3394 }
3395 52 shift @for_stack;
3396 52 delete $special_values->{ $iterator };
3397 }
3398 }
3399 elsif( $instr == CONTEXT_PUSH )
3400 {
3401 6 my ( $context, $new_context );
3402  
3403 # TODO: needed ||? empty contexts should be optimized away now.
3404 6 ▶ $new_context = $line->[ 2 ] || {};
Condition50%1/2
$line->[2] || {}
l!l
60
MC/DC0%0/1
$line->[2] || {}
$line->[2]
Truth table50%1/2
A
A= $line->[2]
Aresult
11
00
3405 6 $context = { %{$var_stack[ 0 ]} };
3406 6 foreach my $var ( keys( %{$new_context} ) )
3407 {
3408 $context->{ $var } = $self->_eval_expression(
3409 8 $new_context->{ $var }, 1 )
3410 }
3411 6 unshift @var_stack, $context;
3412 6 $self->{ var_stack_top } = $context;
3413 }
3414 elsif( $instr == CONTEXT_POP )
3415 {
3416 #$ret .= "[context_pop]";
3417 6 shift @var_stack;
3418 6 $self->{ var_stack_top } = $var_stack[ 0 ];
3419 }
3420 # TODO: ick, hate cut-n-paste code.
3421 # TODO: unroll constant parts of hash lookups to local var
3422 elsif( $self->{ local_syntaxes }->{ '.instr' }->{ $instr } )
3423 {
3424 11 my ( $executor, $token );
3425  
3426 11 $token = $self->{ local_syntaxes }->{ '.instr' }->{ $instr };
3427 11 $executor = $self->{ local_syntaxes }->{ $token }->{ run };
3428 11 $value = $executor->( $self, $token, $line->[ 2 ] );
3429 11 ▶ $ret .= $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 10 1 11
3430 }
3431 # TODO: ick, hate cut-n-paste code.
3432 # TODO: unroll constant parts of hash lookups to local var
3433 elsif( $syntaxes{ '.instr' }->{ $instr } )
3434 {
3435 2 my ( $executor, $token );
3436  
3437 2 $token = $syntaxes{ '.instr' }->{ $instr };
3438 2 $executor = $syntaxes{ $token }->{ run };
3439 2 $value = $executor->( $self, $token, $line->[ 2 ] );
3440 2 ▶ $ret .= $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 2 0 2
3441 }
3442 elsif( $instr == DEBUG )
3443 {
3444 $self->{ debug }->{ $line->[ 2 ]->{ type } } =
3445 0 ( $line->[ 2 ]->{ state } eq 'on' );
3446 }
3447 #$self->{ instrprofile }->{ $instr } += Time::HiRes::time() - $instrstart;
3448 }
3449  
3450 1104 delete $self->{ current_pos };
3451 1104 delete $self->{ var_stack };
3452 1104 delete $self->{ var_stack_top };
3453 1104 delete $self->{ phase };
3454  
3455 1104 return( \$ret );
3456 }
3457  
3458 sub _tersedump
3459 1 ▶{
Subroutine _tersedump: called CC=1 SCAR=0.0
3460 1 return( Data::Dumper->new( [ @_ ] )->Terse(1)->Useqq(1)->Dump() );
3461 }
3462  
3463 sub _tinydump
3464 28 ▶{
Subroutine _tinydump: called CC=1 SCAR=0.0
3465 28 return( Data::Dumper->new( [ @_ ] )->Indent(0)->Quotekeys(0)->Pair('=>')->Terse(1)->Useqq(1)->Dump() );
3466 }
3467  
3468 sub dumpable_template
3469 3 ▶{
Subroutine dumpable_template: called CC=14 SCAR=27.9
Pod documented
3470 3 my ( $self ) = @_;
3471 3 my ( $lineno, $ret, %instr_names );
3472  
3473 3 $ret = '';
3474 3 $lineno = 0;
3475 3 %instr_names = (
3476 (LITERAL) => 'literal',
3477 (EXPR) => 'expr',
3478 (JUMP) => 'jump',
3479 (JUMP_IF) => 'jump_if',
3480 (FOR) => 'for',
3481 (END_FOR) => 'end_for',
3482 (CONTEXT_PUSH) => 'context_push',
3483 (CONTEXT_POP) => 'context_pop',
3484 );
3485  
3486 3 foreach my $line ( @{$self->{ template }->{ program }} )
3487 {
3488 29 my ( $instr, $file );
3489  
3490 29 $file = $self->{ template }->{ files }->[ $line->[ 1 ][ 0 ] ];
3491 29 ▶ $file = 'template-string' if $file =~ m{^string:///};
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ m[^string:///] 29 0 29
3492 $ret .= sprintf( "%04d: [%-20s %3d %3d][%-12s] ", $lineno++,
3493 $file, $line->[ 1 ][ 1 ], $line->[ 1 ][ 2 ],
3494 29 ▶ $instr_names{ $line->[ 0 ] } || $line->[ 0 ] );
Condition33%1/3
$instr_names{$line->[0]} || $line->[0]
l!l&&r!l&&!r
2900
MC/DC0%0/2
$instr_names{$line->[0]} || $line->[0]
$instr_names{$line->[0]} $line->[0]
Truth table33%1/3
A || B
A= $instr_names{$line->[0]}B= $line->[0]
ABresult
1X1
011
000
3495  
3496 29 $instr = $line->[ 0 ];
3497 29 ▶ if( $instr == LITERAL )
Branch: 8 branches
BranchTrueFalseTotal
if ($instr == 0) { } 17 12 29
elsif ($instr == 2) { } 5 7 12
elsif ($instr == 3) { } 2 5 7
elsif ($instr == 4) { } 1 4 5
elsif ($instr == 5) { } 2 2 4
elsif ($instr == 6) { } 2 0 2
elsif ($instr == 7) { } 0 0 0
elsif ($instr == 8) { } 0 0 0
3498 {
3499 # $ret .= "\"$line->[2]\"\n";
3500 17 $ret .= _tinydump( $line->[ 2 ] ) . "\n";
3501 }
3502 elsif( $instr == EXPR )
3503 {
3504 5 ▶ $ret .= _tinydump( $line->[ 2 ] ) .
Branch: 1 branch
BranchTrueFalseTotal
$line->[3] ? : 0 5 5
3505 ( $line->[ 3 ] ? " (void)" : "" ). "\n";
3506 }
3507 elsif( $instr == JUMP )
3508 {
3509 2 $ret .= "$line->[2]\n";
3510 }
3511 elsif( $instr == JUMP_IF )
3512 {
3513 1 $ret .= $line->[ 2 ] . ' unless ' .
3514 _tinydump( $line->[ 3 ] ) . "\n";
3515 }
3516 elsif( $instr == FOR )
3517 {
3518 2 $ret .= "$line->[ 3 ] in " . _tinydump( $line->[ 4 ] ) .
3519 " then $line->[ 2 ]";
3520 2 ▶ $ret .= " (no special-vars)" unless $line->[ 5 ];
Branch: 1 branch
BranchTrueFalseTotal
unless $line->[5] 1 1 2
3521 2 $ret .= "\n";
3522 }
3523 elsif( $instr == END_FOR )
3524 {
3525 2 $ret .= "$line->[ 3 ] in " . _tinydump( $line->[ 4 ] ) .
3526 " repeat $line->[ 2 ]\n";
3527 }
3528 elsif( $instr == CONTEXT_PUSH )
3529 {
3530 0 $ret .= "context push of " . _tinydump( $line->[ 2 ] ) . "\n";
3531 }
3532 elsif( $instr == CONTEXT_POP )
3533 {
3534 0 $ret .= "context pop\n";
3535 }
3536 # TODO: local syntax support.
3537 }
3538  
3539 3 return( $ret );
3540 }
3541  
3542 #sub _decompile_template
3543 #{
3544 # my ( $self ) = @_;
3545 # my ( $lineno, $ret );
3546 #
3547 # $ret = '';
3548 # $lineno = 0;
3549 #
3550 # foreach my $line ( @{$self->{ template }->{ program }} )
3551 # {
3552 # my ( $instr );
3553 #
3554 # $instr = $line->[ 0 ];
3555 # if( $instr == LITERAL )
3556 # {
3557 # $ret .= ( $line->[ 2 ] =~ /^$/ ) ?
3558 # "<: empty literal :>" : $line->[ 2 ];
3559 # next;
3560 # }
3561 # $ret .= "<: $instr ";
3562 # if( $instr == EXPR )
3563 # {
3564 # my ( $dump );
3565 #
3566 # $dump = Data::Dumper::Dumper( $line->[ 2 ] );
3567 # $dump =~ s/^\$VAR1 = //;
3568 # $dump =~ s/;\n$//;
3569 # $ret .= $line->[ 2 ]->[ 1 ] . " ($dump)";
3570 # }
3571 # elsif( $instr == JUMP )
3572 # {
3573 # $ret .= "$line->[2]";
3574 # }
3575 # elsif( $instr == JUMP_IF )
3576 # {
3577 # $ret .= $line->[ 2 ] .
3578 # ( $line->[ 4 ] ? ' unless ' : ' if ' ) .
3579 # "$line->[3]";
3580 # }
3581 # elsif( $instr == FOR )
3582 # {
3583 # $ret .= "$line->[ 3 ] in $line->[ 4 ] then $line->[ 2 ]";
3584 # }
3585 # elsif( $instr == END_FOR )
3586 # {
3587 # $ret .= "$line->[ 3 ] in $line->[ 4 ] repeat $line->[ 2 ]";
3588 # }
3589 # elsif( $instr == CONTEXT_PUSH )
3590 # {
3591 # my ( $dump );
3592 #
3593 # $dump = defined( $line->[ 2 ] ) ? Data::Dumper::Dumper( $line->[ 2 ] ) : 'undef';
3594 # $dump =~ s/^\$VAR1 = //;
3595 # $dump =~ s/;\n$//;
3596 # $dump =~ s/\s+/ /g;
3597 # $ret .= "context push of $dump";
3598 # }
3599 # elsif( $instr == CONTEXT_POP )
3600 # {
3601 # $ret = substr( $ret, 0, -1 );
3602 # }
3603 ## TODO: support for local syntax
3604 # else
3605 # {
3606 # $ret .= "(unhandled by decompile)";
3607 # }
3608 # $ret .= " :>";
3609 # }
3610 #
3611 # return( $ret );
3612 #}
3613  
3614 32 1;
3615  
3616 __END__
↑ Summary Sandbox/Library.pm »