| 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
MC/DC0%0/1
$line ||= 0
$line
Truth table50%1/2
A
A= $line
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | $col ||= 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$col ||= 0
MC/DC0%0/1
$col ||= 0
$col
Truth table50%1/2
A
A= $col
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $buf and length $buf > $start
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
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$args{'buffer_line'} || 0
$args{'buffer_line'}
Truth table0%0/2
A
A= $args{'buffer_line'}
| |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | my $col= $args{buffer_col} || 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args{'buffer_col'} || 0
MC/DC0%0/1
$args{'buffer_col'} || 0
$args{'buffer_col'}
Truth table0%0/2
A
A= $args{'buffer_col'}
| |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | my $max_width= $args{max_width} || 78; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args{'max_width'} || 78
MC/DC0%0/1
$args{'max_width'} || 78
$args{'max_width'}
Truth table0%0/2
A
A= $args{'max_width'}
| |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | my $min_token= $args{min_token} || 30; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args{'min_token'} || 30
MC/DC0%0/1
$args{'min_token'} || 30
$args{'min_token'}
Truth table0%0/2
A
A= $args{'min_token'}
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # Make sure both start and limit are defined, defaulting to equal | ||||||||||||||||||||||||||||
| 50 | 0 | ▶ | $start ||= $limit || 0; | ||||||||||||||||||||||||||
|
Condition0%0/5
$start ||= $limit || 0
$limit || 0
MC/DC0%0/2
$start ||= $limit || 0
$start
$limit
Truth table0%0/3
A ||= B
A= $startB= $limit
| |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | $limit ||= $start; | ||||||||||||||||||||||||||
|
Condition0%0/3
$limit ||= $start
MC/DC0%0/2
$limit ||= $start
$limit
$start
Truth table0%0/3
A ||= B
A= $limitB= $start
| |||||||||||||||||||||||||||||
| 52 | # If they are identical, move limit over one | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | $limit++ if $start == $limit; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 72 | 0 | substr($prefix, 0, -($max_width - $min_token))= ''; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 0 | ▶ | if (length($prefix) + length($token) > $max_width) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
length $token || 1
length $token
Truth table0%0/2
A
A= length $token
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||