TOON/PP.pm

filter: Statementstmt 82.7% 149 / 180 Branchbran 54.0% 54 / 100 Conditioncond 64.2% 18 / 28 MC/DCmcdc 41.1% 7 / 17 Subroutinesub 100% 23 / 23 Podpod 100% 3 / 3 totaltotal 72.3% 254 / 351 CC 62 SCAR 32.7
CC
62
Cov
72%
_parse_string
122.5
_encode_value
11.3
_parse_value
9.9
CRAP 26.4
↑ Summary TOON/Error.pm »
1 package TOON::PP;
2  
3 5 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use Scalar::Util qw(looks_like_number blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use TOON::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 our $VERSION = '0.0.1';
10  
11 ▶sub new ($class, %opts) {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
12 return bless {
13 pretty => $opts{pretty} // 0,
14 canonical => $opts{canonical} // 0,
15 5 ▶ indent => $opts{indent} // 2,
Condition100%6/6
$opts{"pretty"} // 0
l!l
32
$opts{"canonical"} // 0
l!l
41
$opts{"indent"} // 2
l!l
32
MC/DC100%1/1
$opts{"pretty"} // 0
$opts{"pretty"}
MC/DC100%1/1
$opts{"canonical"} // 0
$opts{"canonical"}
MC/DC100%1/1
$opts{"indent"} // 2
$opts{"indent"}
Truth table100%2/2
A
A= $opts{"pretty"}
Aresult
11
00
Truth table100%2/2
A
A= $opts{"canonical"}
Aresult
11
00
Truth table100%2/2
A
A= $opts{"indent"}
Aresult
11
00
16 }, $class;
17 }
18  
19 ▶sub encode ($self, $data) {
Subroutine encode: called CC=1 SCAR=0.0
Pod documented
20 2 return $self->_encode_value($data, 0);
21 }
22  
23 ▶sub decode ($self, $text) {
Subroutine decode: called CC=2 SCAR=7.1
Pod documented
24 3 my $state = {
25 text => $text,
26 len => length($text),
27 pos => 0,
28 };
29  
30 3 $self->_skip_ws($state);
31 3 my $value = $self->_parse_value($state);
32 2 $self->_skip_ws($state);
33  
34 2 ▶ if ($state->{pos} < $state->{len}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{"pos"} < $state->{"len"}) 0 2 2
35 0 $self->_throw($state, 'Trailing characters after document');
36 }
37  
38 2 return $value;
39 }
40  
41 ▶sub _encode_value ($self, $value, $level) {
Subroutine _encode_value: called CC=10 SCAR=24.3
42 7 ▶ return 'null' if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 7 7
43  
44 7 ▶ if (blessed($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value)) 0 7 7
45 0 die TOON::Error->new(
46 message => 'Encoding blessed references is not supported',
47 line => 1,
48 column => 1,
49 offset => 0,
50 );
51 }
52  
53 7 my $ref = ref $value;
54  
55 7 ▶ if (!$ref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ref) 4 3 7
56 4 ▶ return 'true' if $value eq 'true';
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "true" 0 4 4
57 4 ▶ return 'false' if $value eq 'false';
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "false" 0 4 4
58  
59 4 ▶ if ($value =~ /\A(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\z/ || looks_like_number($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\A(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\z/u or &looks_like_number($value)) 3 1 4
Condition66%2/3
$value =~ /\A(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\z/u or &looks_like_number($value)
l!l&&r!l&&!r
301
MC/DC50%1/2
$value =~ /\A(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\z/u or &looks_like_number($value)
$value =~ /\A(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\z/u &looks_like_number($value)
Truth table66%2/3
A or B
A= $value =~ /\A(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\z/uB= &looks_like_number($value)
ABresult
1X1
011
000
60 3 return 0 + $value;
61 }
62  
63 1 return $self->_quote_string($value);
64 }
65  
66 3 ▶ if ($ref eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "ARRAY") 1 2 3
67 1 return $self->_encode_array($value, $level);
68 }
69  
70 2 ▶ if ($ref eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "HASH") 2 0 2
71 2 return $self->_encode_hash($value, $level);
72 }
73  
74 0 die TOON::Error->new(
75 message => "Encoding $ref references is not supported",
76 line => 1,
77 column => 1,
78 offset => 0,
79 );
80 }
81  
82 ▶sub _encode_array ($self, $array, $level) {
Subroutine _encode_array: called CC=3 SCAR=11.1
83 1 ▶ return '[]' unless @$array;
Branch: 1 branch
BranchTrueFalseTotal
unless @$array 0 1 1
84  
85 1 my @items = map { $self->_encode_value($_, $level + 1) } @$array;
86  
87 return '[' . join(', ', @items) . ']'
88 1 ▶ unless $self->{pretty};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"pretty"} 0 1 1
89  
90 1 my $pad = ' ' x ($self->{indent} * $level);
91 1 my $childpad = ' ' x ($self->{indent} * ($level + 1));
92  
93 return "[\n"
94 1 . join(",\n", map { $childpad . $_ } @items)
95 . "\n$pad]";
96 }
97  
98 ▶sub _encode_hash ($self, $hash, $level) {
Subroutine _encode_hash: called CC=5 SCAR=16.3
99 2 ▶ return '{}' unless %$hash;
Branch: 1 branch
BranchTrueFalseTotal
unless %$hash 0 2 2
100  
101 2 my @keys = keys %$hash;
102 2 ▶ @keys = sort @keys if $self->{canonical};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"canonical"} 2 0 2
103  
104 my @pairs = map {
105 2 ▶ my $key = $_ =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/u ? : 3 0 3
106 ? $_
107 : $self->_quote_string($_);
108 3 $key . ': ' . $self->_encode_value($hash->{$_}, $level + 1);
109 } @keys;
110  
111 return '{' . join(', ', @pairs) . '}'
112 2 ▶ unless $self->{pretty};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"pretty"} 1 1 2
113  
114 1 my $pad = ' ' x ($self->{indent} * $level);
115 1 my $childpad = ' ' x ($self->{indent} * ($level + 1));
116  
117 return "{\n"
118 1 . join(",\n", map { $childpad . $_ } @pairs)
119 . "\n$pad}";
120 }
121  
122 ▶sub _quote_string ($self, $string) {
Subroutine _quote_string: called CC=1 SCAR=0.0
123 1 $string =~ s/\\/\\\\/g;
124 1 $string =~ s/"/\\"/g;
125 1 $string =~ s/\n/\\n/g;
126 1 $string =~ s/\r/\\r/g;
127 1 $string =~ s/\t/\\t/g;
128 1 $string =~ s/\f/\\f/g;
129 1 $string =~ s/\x08/\\b/g;
130 1 return qq{"$string"};
131 }
132  
133 ▶sub _parse_value ($self, $state) {
Subroutine _parse_value: called CC=9 SCAR=23.0
134 16 $self->_skip_ws($state);
135  
136 16 my $ch = $self->_peek($state);
137 16 ▶ $self->_throw($state, 'Unexpected end of input') unless defined $ch;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ch 0 16 16
138  
139 16 ▶ return $self->_parse_object($state) if $ch eq '{';
Branch: 1 branch
BranchTrueFalseTotal
if $ch eq "{" 3 13 16
140 13 ▶ return $self->_parse_array($state) if $ch eq '[';
Branch: 1 branch
BranchTrueFalseTotal
if $ch eq "[" 3 10 13
141 10 ▶ return $self->_parse_string($state) if $ch eq '"';
Branch: 1 branch
BranchTrueFalseTotal
if $ch eq "\"" 1 9 10
142  
143 9 ▶ if ($self->_consume_literal($state, 'null')) { return undef }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_consume_literal($state, "null")) 0 9 9
144 9 ▶ if ($self->_consume_literal($state, 'true')) { return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_consume_literal($state, "true")) 1 8 9
145 8 ▶ if ($self->_consume_literal($state, 'false')) { return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_consume_literal($state, "false")) 0 8 8
146  
147 8 ▶ if ($ch =~ /[-0-9]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch =~ /[-0-9]/u) 8 0 8
148 8 return $self->_parse_number($state);
149 }
150  
151 0 $self->_throw($state, "Unexpected character '$ch'");
152 }
153  
154 ▶sub _parse_object ($self, $state) {
Subroutine _parse_object: called CC=5 SCAR=16.3
155 3 $self->_expect($state, '{');
156 3 $self->_skip_ws($state);
157  
158 3 my %hash;
159  
160 3 ▶ if (($self->_peek($state) // '') eq '}') {
Branch: 1 branch
BranchTrueFalseTotal
if (($self->_peek($state) // "") eq "}") 0 3 3
Condition50%1/2
$self->_peek($state) // ""
l!l
30
MC/DC0%0/1
$self->_peek($state) // ""
$self->_peek($state)
Truth table50%1/2
A
A= $self->_peek($state)
Aresult
11
00
161 0 $state->{pos}++;
162 0 return \%hash;
163 }
164  
165 3 while (1) {
166 6 $self->_skip_ws($state);
167 6 my $key = $self->_parse_key($state);
168 6 $self->_skip_ws($state);
169 6 $self->_expect($state, ':');
170 6 $self->_skip_ws($state);
171 6 $hash{$key} = $self->_parse_value($state);
172 5 $self->_skip_ws($state);
173  
174 5 my $ch = $self->_peek($state);
175 5 ▶ if (defined $ch && $ch eq ',') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ch and $ch eq ",") 3 2 5
Condition66%2/3
defined $ch and $ch eq ","
!ll&&!rl&&r
023
MC/DC50%1/2
defined $ch and $ch eq ","
defined $ch $ch eq ","
Truth table66%2/3
A and B
A= defined $chB= $ch eq ","
ABresult
0X0
100
111
176 3 $state->{pos}++;
177 3 next;
178 }
179 2 last;
180 }
181  
182 2 $self->_skip_ws($state);
183 2 $self->_expect($state, '}');
184 2 return \%hash;
185 }
186  
187 ▶sub _parse_array ($self, $state) {
Subroutine _parse_array: called CC=5 SCAR=16.4
188 3 $self->_expect($state, '[');
189 3 $self->_skip_ws($state);
190  
191 3 my @array;
192  
193 3 ▶ if (($self->_peek($state) // '') eq ']') {
Branch: 1 branch
BranchTrueFalseTotal
if (($self->_peek($state) // "") eq "]") 0 3 3
Condition50%1/2
$self->_peek($state) // ""
l!l
30
MC/DC0%0/1
$self->_peek($state) // ""
$self->_peek($state)
Truth table50%1/2
A
A= $self->_peek($state)
Aresult
11
00
194 0 $state->{pos}++;
195 0 return \@array;
196 }
197  
198 3 while (1) {
199 7 push @array, $self->_parse_value($state);
200 7 $self->_skip_ws($state);
201  
202 7 my $ch = $self->_peek($state);
203 7 ▶ if (defined $ch && $ch eq ',') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ch and $ch eq ",") 4 3 7
Condition66%2/3
defined $ch and $ch eq ","
!ll&&!rl&&r
034
MC/DC50%1/2
defined $ch and $ch eq ","
defined $ch $ch eq ","
Truth table66%2/3
A and B
A= defined $chB= $ch eq ","
ABresult
0X0
100
111
204 4 $state->{pos}++;
205 4 next;
206 }
207 3 last;
208 }
209  
210 3 $self->_skip_ws($state);
211 3 $self->_expect($state, ']');
212 2 return \@array;
213 }
214  
215 ▶sub _parse_key ($self, $state) {
Subroutine _parse_key: called CC=4 SCAR=15.5
216 6 my $ch = $self->_peek($state);
217 6 ▶ return $self->_parse_string($state) if defined $ch && $ch eq '"';
Branch: 1 branch
BranchTrueFalseTotal
if defined $ch and $ch eq "\"" 0 6 6
Condition33%1/3
defined $ch and $ch eq "\""
!ll&&!rl&&r
060
MC/DC0%0/2
defined $ch and $ch eq "\""
defined $ch $ch eq "\""
Truth table33%1/3
A and B
A= defined $chB= $ch eq "\""
ABresult
0X0
100
111
218  
219 6 my $remaining = substr($state->{text}, $state->{pos});
220 6 ▶ if ($remaining =~ /\A([A-Za-z_][A-Za-z0-9_-]*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining =~ /\A([A-Za-z_][A-Za-z0-9_-]*)/u) 6 0 6
221 6 $state->{pos} += length $1;
222 6 return $1;
223 }
224  
225 0 $self->_throw($state, 'Expected object key');
226 }
227  
228 ▶sub _parse_string ($self, $state) {
Subroutine _parse_string: called CC=15 SCAR=48.1
229 1 $self->_expect($state, '"');
230 1 my $out = '';
231  
232 1 ▶ while ($state->{pos} < $state->{len}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{"pos"} < $state->{"len"}) 4 0 4
233 4 my $ch = substr($state->{text}, $state->{pos}, 1);
234 4 $state->{pos}++;
235  
236 4 ▶ return $out if $ch eq '"';
Branch: 1 branch
BranchTrueFalseTotal
if $ch eq "\"" 1 3 4
237  
238 3 ▶ if ($ch eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\") 0 3 3
239 $self->_throw($state, 'Unexpected end of input in string escape')
240 0 ▶ if $state->{pos} >= $state->{len};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"pos"} >= $state->{"len"} 0 0 0
241  
242 0 my $esc = substr($state->{text}, $state->{pos}, 1);
243 0 $state->{pos}++;
244  
245 0 ▶ if ($esc eq '"') { $out .= '"' }
Branch: 9 branches
BranchTrueFalseTotal
if ($esc eq '"') { } 0 0 0
elsif ($esc eq '\\') { } 0 0 0
elsif ($esc eq '/') { } 0 0 0
elsif ($esc eq 'n') { } 0 0 0
elsif ($esc eq 'r') { } 0 0 0
elsif ($esc eq 't') { } 0 0 0
elsif ($esc eq 'f') { } 0 0 0
elsif ($esc eq 'b') { } 0 0 0
elsif ($esc eq 'u') { } 0 0 0
246 0 elsif ($esc eq '\\') { $out .= '\\' }
247 0 elsif ($esc eq '/') { $out .= '/' }
248 0 elsif ($esc eq 'n') { $out .= "\n" }
249 0 elsif ($esc eq 'r') { $out .= "\r" }
250 0 elsif ($esc eq 't') { $out .= "\t" }
251 0 elsif ($esc eq 'f') { $out .= "\f" }
252 0 elsif ($esc eq 'b') { $out .= "\b" }
253 elsif ($esc eq 'u') {
254 0 my $hex = substr($state->{text}, $state->{pos}, 4);
255 0 ▶ $self->_throw($state, 'Invalid unicode escape')
Branch: 1 branch
BranchTrueFalseTotal
unless $hex =~ /\A[0-9A-Fa-f]{4}\z/u 0 0 0
256 unless $hex =~ /\A[0-9A-Fa-f]{4}\z/;
257 0 $state->{pos} += 4;
258 0 $out .= chr(hex($hex));
259 }
260 else {
261 0 $self->_throw($state, "Unknown escape sequence \\$esc");
262 }
263  
264 0 next;
265 }
266  
267 3 $out .= $ch;
268 }
269  
270 0 $self->_throw($state, 'Unterminated string');
271 }
272  
273 ▶sub _parse_number ($self, $state) {
Subroutine _parse_number: called CC=2 SCAR=7.4
274 8 my $remaining = substr($state->{text}, $state->{pos});
275 8 ▶ if ($remaining =~ /\A(-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining =~ /\A(-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?)/u) 8 0 8
276 8 $state->{pos} += length $1;
277 8 return 0 + $1;
278 }
279  
280 0 $self->_throw($state, 'Invalid number');
281 }
282  
283 ▶sub _consume_literal ($self, $state, $literal) {
Subroutine _consume_literal: called CC=4 SCAR=14.5
284 26 ▶ return 0 unless substr($state->{text}, $state->{pos}, length($literal)) eq $literal;
Branch: 1 branch
BranchTrueFalseTotal
unless substr($state->{"text"}, $state->{"pos"}, length $literal) eq $literal 25 1 26
285  
286 1 my $next = substr($state->{text}, $state->{pos} + length($literal), 1);
287 1 ▶ return 0 if defined($next) && $next =~ /[A-Za-z0-9_-]/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $next and $next =~ /[A-Za-z0-9_-]/u 0 1 1
Condition33%1/3
defined $next and $next =~ /[A-Za-z0-9_-]/u
!ll&&!rl&&r
010
MC/DC0%0/2
defined $next and $next =~ /[A-Za-z0-9_-]/u
defined $next $next =~ /[A-Za-z0-9_-]/u
Truth table33%1/3
A and B
A= defined $nextB= $next =~ /[A-Za-z0-9_-]/u
ABresult
0X0
100
111
288  
289 1 $state->{pos} += length($literal);
290 1 return 1;
291 }
292  
293 ▶sub _expect ($self, $state, $char) {
Subroutine _expect: called CC=3 SCAR=11.0
294 18 my $got = $self->_peek($state);
295 18 ▶ $self->_throw($state, "Expected '$char'") unless defined $got && $got eq $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $got and $got eq $char 1 17 18
Condition66%2/3
defined $got and $got eq $char
!ll&&!rl&&r
0117
MC/DC50%1/2
defined $got and $got eq $char
defined $got $got eq $char
Truth table66%2/3
A and B
A= defined $gotB= $got eq $char
ABresult
0X0
100
111
296 17 $state->{pos}++;
297 }
298  
299 ▶sub _peek ($self, $state) {
Subroutine _peek: called CC=2 SCAR=7.2
300 58 ▶ return undef if $state->{pos} >= $state->{len};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"pos"} >= $state->{"len"} 0 58 58
301 58 return substr($state->{text}, $state->{pos}, 1);
302 }
303  
304 ▶sub _skip_ws ($self, $state) {
Subroutine _skip_ws: called CC=3 SCAR=11.0
305 62 ▶ while ($state->{pos} < $state->{len}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{"pos"} < $state->{"len"}) 91 2 93
306 91 my $ch = substr($state->{text}, $state->{pos}, 1);
307 91 ▶ if ($ch =~ /[\x20\x09\x0A\x0D]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch =~ /[\x20\x09\x0A\x0D]/u) 31 60 91
308 31 $state->{pos}++;
309 31 next;
310 }
311 60 last;
312 }
313 }
314  
315 ▶sub _throw ($self, $state, $message) {
Subroutine _throw: called CC=1 SCAR=0.0
316 1 my ($line, $column) = $self->_line_and_column($state->{text}, $state->{pos});
317 die TOON::Error->new(
318 message => $message,
319 line => $line,
320 column => $column,
321 offset => $state->{pos},
322 1 );
323 }
324  
325 ▶sub _line_and_column ($self, $text, $pos) {
Subroutine _line_and_column: called CC=1 SCAR=0.0
326 1 my $prefix = substr($text, 0, $pos);
327 1 my $line = 1 + ($prefix =~ tr/\n//);
328 1 my $last_nl = rindex($prefix, "\n");
329 1 my $column = $pos - $last_nl;
330 1 return ($line, $column);
331 }
332  
333 5 1;
334  
335 __END__
↑ Summary TOON/Error.pm »