FormulaEngine/Parser/ContextUtil.pm

filter: Statementstmt 47.1% 25 / 53 Branchbran 12.5% 2 / 16 Conditioncond 12.0% 3 / 25 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 83.3% 5 / 6 Podpod 100% 3 / 3 totaltotal 32.7% 38 / 116 CC 20 SCAR 49.8
CC
20
Cov
32%
format_context_multiline
240.0
calc_text_coordinates
4.6
format_context_string
3.2
CRAP 145.2
↑ Summary FormulaEngine/Namespace.pm »
1 package Language::FormulaEngine::Parser::ContextUtil;
2 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 our @EXPORT_OK= qw( calc_text_coordinates format_context_string format_context_multiline );
6  
7 # ABSTRACT: utility methods for parsers
8 9 our $VERSION = '0.08'; # VERSION
9  
10  
11 2 ▶sub calc_text_coordinates {
Subroutine calc_text_coordinates: called CC=4 SCAR=15.2
Pod documented
12 2 my ($buf, $pos, $line, $col)= @_;
13 2 ▶ $line ||= 0;
Condition50%1/2
$line ||= 0
l!l
02
MC/DC0%0/1
$line ||= 0
$line
Truth table50%1/2
A
A= $line
Aresult
11
00
14 2 ▶ $col ||= 0;
Condition50%1/2
$col ||= 0
l!l
02
MC/DC0%0/1
$col ||= 0
$col
Truth table50%1/2
A
A= $col
Aresult
11
00
15 # If there are any newlines from the start of the buffer to the given position...
16 2 my $line_end= rindex($buf, "\n", $pos-1);
17 2 ▶ if ($line_end >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_end >= 0) { } 0 2 2
18 # ...then add up the number of newlines and re-calculate the column
19 0 $line+= (substr($buf, 0, $line_end+1) =~ /\n/);
20 0 $col= $pos - ($line_end+1);
21 }
22 else {
23 2 $col += $pos;
24 }
25 2 return ($line, $col);
26 }
27  
28  
29 2 ▶sub format_context_string {
Subroutine format_context_string: called CC=3 SCAR=11.6
Pod documented
30 2 my ($buf, $start, $limit, $line, $col)= @_;
31 # If we don't have a buffer, there's nothing to show, so print "end of input".
32 2 ▶ defined $buf and length $buf > $start
Branch: 1 branch
BranchTrueFalseTotal
defined $buf and length $buf > $start or return "(end of input)" 2 0 2
Condition33%1/3
defined $buf and length $buf > $start
!ll&&!rl&&r
002
MC/DC0%0/2
defined $buf and length $buf > $start
defined $buf length $buf > $start
Truth table33%1/3
A and B
A= defined $bufB= length $buf > $start
ABresult
0X0
100
111
33 or return '(end of input)';
34 2 my $context= substr($buf, $start, 20);
35 2 $context =~ s/\n.*//s; # remove subsequent lines
36 2 ($line, $col)= calc_text_coordinates($buf, $start, $line, $col);
37 2 return sprintf '"%s" at line %d char %d', $context, $line+1, $col+1;
38 }
39  
40  
41 0 ▶sub format_context_multiline {
Subroutine format_context_multiline: not called CC=15 SCAR=54.8
Pod documented
42 0 my ($self, $buf, $start, $limit, %args)= @_;
43 0 my ($prefix, $token, $suffix)= ('','','');
44 0 ▶ my $line= $args{buffer_line} || 0;
Condition0%0/2
$args{'buffer_line'} || 0
l!l
00
MC/DC0%0/1
$args{'buffer_line'} || 0
$args{'buffer_line'}
Truth table0%0/2
A
A= $args{'buffer_line'}
Aresult
11
00
45 0 ▶ my $col= $args{buffer_col} || 0;
Condition0%0/2
$args{'buffer_col'} || 0
l!l
00
MC/DC0%0/1
$args{'buffer_col'} || 0
$args{'buffer_col'}
Truth table0%0/2
A
A= $args{'buffer_col'}
Aresult
11
00
46 0 ▶ my $max_width= $args{max_width} || 78;
Condition0%0/2
$args{'max_width'} || 78
l!l
00
MC/DC0%0/1
$args{'max_width'} || 78
$args{'max_width'}
Truth table0%0/2
A
A= $args{'max_width'}
Aresult
11
00
47 0 ▶ my $min_token= $args{min_token} || 30;
Condition0%0/2
$args{'min_token'} || 30
l!l
00
MC/DC0%0/1
$args{'min_token'} || 30
$args{'min_token'}
Truth table0%0/2
A
A= $args{'min_token'}
Aresult
11
00
48
49 # Make sure both start and limit are defined, defaulting to equal
50 0 ▶ $start ||= $limit || 0;
Condition0%0/5
$start ||= $limit || 0
l!l&&r!l&&!r
000
$limit || 0
l!l
00
MC/DC0%0/2
$start ||= $limit || 0
$start $limit
Truth table0%0/3
A ||= B
A= $startB= $limit
ABresult
1X1
011
000
51 0 ▶ $limit ||= $start;
Condition0%0/3
$limit ||= $start
l!l&&r!l&&!r
000
MC/DC0%0/2
$limit ||= $start
$limit $start
Truth table0%0/3
A ||= B
A= $limitB= $start
ABresult
1X1
011
000
52 # If they are identical, move limit over one
53 0 ▶ $limit++ if $start == $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $start == $limit 0 0 0
54 # If we don't have a buffer, there's nothing to show, so print "end of input".
55 0 ▶ if (!length($buf)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not length $buf) { } 0 0 0
56 0 $suffix= '(end of input)';
57 }
58 else {
59 0 $prefix= substr($buf, 0, $start);
60 0 $token= substr($buf, $start, $limit-$start);
61 0 $suffix= substr($buf, $limit);
62 }
63
64 # Truncate prefix and suffix at line breaks
65 0 $prefix =~ s/.*\n//s;
66 0 $suffix =~ s/\n.*//s;
67 # Limit lengths of prefix and suffix and token
68 0 ▶ if (length($prefix) + length($token) > $max_width) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($prefix) + length($token) > $max_width) 0 0 0
69 0 $min_token= min(length($token), $min_token);
70 # truncate prefix, or token, or both
71 0 ▶ if (length($prefix) > $max_width - $min_token) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $prefix > $max_width - $min_token) 0 0 0
72 0 substr($prefix, 0, -($max_width - $min_token))= '';
73 }
74 0 ▶ if (length($prefix) + length($token) > $max_width) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($prefix) + length($token) > $max_width) 0 0 0
75 0 substr($token, -($max_width - length($prefix) - length($token)))= '';
76 }
77 }
78 0 ▶ if (length($prefix) + length($token) + length($suffix) > $max_width) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($prefix) + length($token) + length($suffix) > $max_width) 0 0 0
79 0 substr($suffix, -($max_width - length($prefix) - length($token)))= '';
80 }
81 0 ($line, $col)= calc_text_coordinates($buf, $start, $line, $col);
82 0 ▶ return sprintf "%s%s%s\n%s%s\n (line %d char %d)\n",
Condition0%0/2
length $token || 1
l!l
00
MC/DC0%0/1
length $token || 1
length $token
Truth table0%0/2
A
A= length $token
Aresult
11
00
83 $prefix, $token, $suffix,
84 ' ' x length($prefix), '^' x (length($token) || 1),
85 $line+1, $col+1;
86 }
87  
88 9 1;
89  
90 __END__
↑ Summary FormulaEngine/Namespace.pm »