File Coverage

blib/lib/HTML/Blitz/ParseError.pm
Criterion Covered Total %
statement 55 55 100.0
branch 24 42 57.1
condition 5 9 55.5
subroutine 8 8 100.0
pod 0 4 0.0
total 92 118 77.9


line stmt bran cond sub pod time code
1             # This code can be redistributed and modified under the terms of the GNU
2             # General Public License as published by the Free Software Foundation, either
3             # version 3 of the License, or (at your option) any later version.
4             # See the "COPYING" file for details.
5             package HTML::Blitz::ParseError 0.1001;
6 11     11   85 use HTML::Blitz::pragma;
  11         23  
  11         124  
7 11 50   11   8223 use overload fallback => 1, '""' => method (@) { $self->to_string };
  11     56   26  
  11         169  
  56         19954  
  56         117  
  56         79  
  56         181  
8              
9             method new($class:
10             :$src_name,
11             :$src_ref,
12             :$pos,
13             :$msg,
14             :$width = 1,
15             :$alt_pos = undef,
16             :$alt_msg = undef,
17             :$alt_width = 1,
18 28 50 33 28 0 91 ) {
  28 50       147  
  28 50       59  
  28 50       216  
  28 50       97  
  28 50       97  
  28 50       110  
  28 50       77  
  28 50       111  
  28         55  
  28         52  
  28         67  
  28         84  
  28         60  
19 28         865 bless {@_}, $class
20             }
21              
22 35 50   35   90 fun _context($src_ref, $pos) {
  35 50       85  
  35         103  
  35         53  
23 35         137 my $n_line = substr($$src_ref, 0, $pos) =~ tr/\n// + 1;
24 35         111 my $line_start = rindex($$src_ref, "\n", $pos - 1) + 1;
25 35         86 my $line_end = index($$src_ref, "\n", $pos);
26 35 100       97 $line_end = length $$src_ref if $line_end == -1;
27 35         95 my $s_line = substr $$src_ref, $line_start, $line_end - $line_start;
28 35         125 my $lpos = $pos - $line_start;
29             +{
30 35         330 line_num => $n_line,
31             col_num => $lpos + 1,
32             line => $s_line,
33             m_prefix => substr($s_line, 0, $lpos) =~ tr/ \t/ /cr,
34             }
35             }
36              
37 56 50   56 0 223 method location() {
  56 50       249  
  56         89  
  56         71  
38             $self->{_location} //= _context $self->{src_ref}, $self->{pos}
39 56   66     244 }
40              
41 56 50   56 0 129 method alt_location() {
  56 50       115  
  56         85  
  56         79  
42 56         127 my $alt_pos = $self->{alt_pos};
43 56 100       155 return undef if !defined $alt_pos;
44 14   66     82 $self->{_alt_location} //= _context $self->{src_ref}, $alt_pos
45             }
46              
47 56 50   56 0 150 method to_string() {
  56 50       128  
  56         100  
  56         78  
48 56         150 my $loc = $self->location;
49 56         152 my $alt_loc = $self->alt_location;
50             "$self->{src_name}:$loc->{line_num}:$loc->{col_num}: error: $self->{msg}\n"
51             . " |\n"
52             . " | $loc->{line}\n"
53             . " | $loc->{m_prefix}" . '^' x $self->{width} . "\n"
54             . (!defined $alt_loc ? "" :
55             "$self->{src_name}:$alt_loc->{line_num}:$alt_loc->{col_num}: ... $self->{alt_msg}\n"
56             . " |\n"
57             . " | $alt_loc->{line}\n"
58 56 100       900 . " | $alt_loc->{m_prefix}" . '^' x $self->{alt_width} . "\n"
59             )
60             }
61              
62             1