Skilletor/JSON.pm

filter: Statementstmt 97.5% 318 / 326 Branchbran 89.0% 171 / 192 Conditioncond 76.0% 76 / 100 MC/DCmcdc 66.1% 39 / 59 Subroutinesub 100% 37 / 37 Podpod 100% 6 / 6 totaltotal 89.8% 647 / 720 CC 136 SCAR 28.3
CC
136
Cov
91%
_write
59.1
js_number
21.1
_number
17.0
CRAP 17.0
« Skilletor/Report.pm ↑ Summary Skilletor/State.pm »
1 package Skilletor::JSON;
2 # ABSTRACT: JSON read and written exactly as the reference implementation does
3  
4 248 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 248 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 248 ▶use B ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 248 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 248 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 248 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 248 ▶use Scalar::Util qw( blessed reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 248 ▶use Skilletor::Error::JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 248 ▶use Skilletor::JSON::OrderedHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 248 our $VERSION = '0.001';
16  
17 248 our @EXPORT_OK = qw( js_parse js_stringify js_ordered js_sort_keys js_number js_type );
18  
19 248 ▶use constant CORE_BOOL => defined &builtin::is_bool;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 #### Writing ###########################################################
22  
23 13710 ▶sub js_ordered {
Subroutine js_ordered: called CC=1 SCAR=0.0
Pod documented
24 13710 my ( @pairs ) = @_;
25 13710 tie my %h, 'Skilletor::JSON::OrderedHash', @pairs;
26 13710 return \%h;
27 }
28  
29 # Default Array.prototype.sort: by UTF-16 code units.
30 3920 ▶sub js_sort_keys {
Subroutine js_sort_keys: called CC=2 SCAR=6.9
Pod documented
31 3920 my ( @keys ) = @_;
32 3920 ▶ return sort @keys unless grep { /[^\x{0}-\x{FFFF}]/ } @keys;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {/[^\x{0}-\x{FFFF}]/;} @keys 3918 2 3920
33 2 my %unit = map { $_ => _utf16_units($_) } @keys;
34 2 return sort { $unit{$a} cmp $unit{$b} } @keys;
35 }
36  
37 5 ▶sub _utf16_units {
Subroutine _utf16_units: called CC=2 SCAR=6.9
38 5 my ( $s ) = @_;
39 return join '', map {
40 5 my $c = ord;
41 $c > 0xFFFF
42 5 ▶ ? do { my $v = $c - 0x10000; chr( 0xD800 + ( $v >> 10 ) ).chr( 0xDC00 + ( $v & 0x3FF ) ) }
Branch: 1 branch
BranchTrueFalseTotal
$c > 65535 ? : 2 3 5
43 : $_
44 } split //, $s;
45 }
46  
47 # Property order of a plain hash: written as the reference writes a sorted object –
48 # index keys ascending first (JavaScript reorders them), then the rest sorted.
49 3981 ▶sub _hash_keys {
Subroutine _hash_keys: called CC=3 SCAR=11.0
50 3981 my ( $h ) = @_;
51 3981 my $tied = tied %$h;
52 3981 ▶ return $tied->keys_in_order if blessed $tied && $tied->isa('Skilletor::JSON::OrderedHash');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($tied) and $tied->isa("Skilletor::JSON::OrderedHash") 896 3085 3981
Condition66%2/3
builtin::blessed($tied) and $tied->isa("Skilletor::JSON::OrderedHash")
!ll&&!rl&&r
30850896
MC/DC50%1/2
builtin::blessed($tied) and $tied->isa("Skilletor::JSON::OrderedHash")
builtin::blessed($tied) $tied->isa("Skilletor::JSON::OrderedHash")
Truth table66%2/3
A and B
A= builtin::blessed($tied)B= $tied->isa("Skilletor::JSON::OrderedHash")
ABresult
0X0
100
111
53 3085 my @keys = keys %$h;
54 3085 my @index = sort { $a <=> $b } grep { Skilletor::JSON::OrderedHash->is_index_key($_) } @keys;
55 3085 my @rest = js_sort_keys( grep { !Skilletor::JSON::OrderedHash->is_index_key($_) } @keys );
56 3085 return ( @index, @rest );
57 }
58  
59 6666 ▶sub js_stringify {
Subroutine js_stringify: called CC=6 SCAR=20.3
Pod documented
60 6666 my ( $value, $indent ) = @_;
61 # A number: that many spaces (at most 10); a string: its first 10 characters.
62 6666 ▶ my $gap = !defined $indent ? ''
Branch: 4 branches
BranchTrueFalseTotal
!defined($indent) ? : 6345 321 6666
!(ref $indent || _is_number($indent)) ? : 0 321 321
$indent > 0 ? : 321 0 321
$indent > 10 ? : 0 321 321
Condition33%1/3
ref $indent || _is_number($indent)
l!l&&r!l&&!r
03210
MC/DC0%0/2
ref $indent || _is_number($indent)
ref $indent _is_number($indent)
Truth table33%1/3
A || B
A= ref $indentB= _is_number($indent)
ABresult
1X1
011
000
63 : ( !ref $indent && !_is_number($indent) ) ? substr( $indent, 0, 10 )
64 : $indent > 0 ? ' ' x ( $indent > 10 ? 10 : $indent )
65 : '';
66 6666 return _write( $value, $gap, '' );
67 }
68  
69 20139 ▶sub _is_number {
Subroutine _is_number: called CC=3 SCAR=11.0
70 20139 my ( $value ) = @_;
71 20139 my $flags = B::svref_2object( \$value )->FLAGS;
72 20139 ▶ return ( $flags & ( B::SVp_IOK() | B::SVp_NOK() ) and !( $flags & B::SVp_POK() ) ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$flags & 12288 && !($flags & 16384) ? : 2117 18022 20139
Condition66%2/3
$flags & 12288 and not $flags & 16384
!ll&&!rl&&r
1802202117
MC/DC50%1/2
$flags & 12288 and not $flags & 16384
$flags & 12288 not $flags & 16384
Truth table66%2/3
A and not(B)
A= $flags & 12288B= not $flags & 16384
ABresult
0X0
100
111
73 }
74  
75 # typeof, with Array.isArray and null told apart: what a validator asks of a parsed value.
76 14819 ▶sub js_type {
Subroutine js_type: called CC=8 SCAR=20.8
Pod documented
77 14819 my ( $value ) = @_;
78 14819 ▶ return 'null' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 331 14488 14819
79 14488 ▶ return 'boolean' if JSON::MaybeXS::is_bool($value);
Branch: 1 branch
BranchTrueFalseTotal
if JSON::MaybeXS::is_bool($value) 66 14422 14488
80 14422 ▶ if ( ref $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 8521 5901 14422
81 8521 my $type = reftype $value;
82 8521 ▶ return $type eq 'ARRAY' ? 'array' : $type eq 'HASH' ? 'object' : 'other';
Branch: 2 branches
BranchTrueFalseTotal
$type eq 'ARRAY' ? : 1595 6926 8521
$type eq 'HASH' ? : 6926 0 6926
83 }
84 5901 if (CORE_BOOL) {
85 248 ▶ no warnings 'experimental::builtin';
Subroutine BEGIN: called CC=1 SCAR=0.0
86 5901 ▶ return 'boolean' if builtin::is_bool($value);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::is_bool($value) 0 5901 5901
87 }
88 5901 ▶ return _is_number($value) ? 'number' : 'string';
Branch: 1 branch
BranchTrueFalseTotal
_is_number($value) ? : 270 5631 5901
89 }
90  
91 21757 ▶sub _write {
Subroutine _write: called CC=25 SCAR=40.8
92 21757 my ( $value, $gap, $current ) = @_;
93 21757 ▶ return 'null' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 440 21317 21757
94 21317 ▶ if ( ref $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 7400 13917 21317
95 7400 ▶ return $value ? 'true' : 'false' if JSON::MaybeXS::is_bool($value);
Branch: 2 branches
BranchTrueFalseTotal
if JSON::MaybeXS::is_bool($value) 80 7320 7400
$value ? : 67 13 80
96 0 ▶ if ( blessed $value && !( tied( %{ $value } ) && reftype $value eq 'HASH' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and not tied %{$value;} && builtin::reftype($value) eq "HASH") 0 7320 7320
Condition16%1/6
builtin::blessed($value) and not tied %{$value;} && builtin::reftype($value) eq "HASH"
!ll&&!rl&&r
732000
tied %{$value;} && builtin::reftype($value) eq 'HASH'
!ll&&!rl&&r
000
MC/DC0%0/3
builtin::blessed($value) and not tied %{$value;} && builtin::reftype($value) eq "HASH"
builtin::blessed($value) tied %{$value;} builtin::reftype($value) eq 'HASH'
Truth table25%1/4
A and not(B && C)
A= builtin::blessed($value)B= tied %{$value;}C= builtin::reftype($value) eq 'HASH'
ABCresult
0XX0
1110
10X1
1101
97 0 ▶ return _write( $value->TO_JSON, $gap, $current ) if $value->can('TO_JSON');
Branch: 1 branch
BranchTrueFalseTotal
if $value->can("TO_JSON") 0 0 0
98 0 croak __PACKAGE__.'->js_stringify cannot write a '.ref($value).' object';
99 }
100 7320 my $type = reftype $value;
101 7320 my $inner = $current.$gap;
102 7320 ▶ if ( $type eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "ARRAY") 3339 3981 7320
103 3339 ▶ return '[]' unless @$value;
Branch: 1 branch
BranchTrueFalseTotal
unless @$value 364 2975 3339
104 2975 my @parts = map { _write( $_, $gap, $inner ) } @$value;
105 2975 ▶ return '['.join( ',', @parts ).']' if $gap eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $gap eq "" 2716 259 2975
106 259 return "[\n".$inner.join( ",\n".$inner, @parts )."\n".$current.']';
107 }
108 3981 ▶ if ( $type eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "HASH") 3981 0 3981
109 3981 my @keys = _hash_keys($value);
110 3981 ▶ return '{}' unless @keys;
Branch: 1 branch
BranchTrueFalseTotal
unless @keys 261 3720 3981
111 3720 ▶ my $colon = $gap eq '' ? ':' : ': ';
Branch: 1 branch
BranchTrueFalseTotal
$gap eq '' ? : 2764 956 3720
112 3720 my @parts = map { _quote($_).$colon._write( $value->{$_}, $gap, $inner ) } @keys;
113 3720 ▶ return '{'.join( ',', @parts ).'}' if $gap eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $gap eq "" 2764 956 3720
114 956 return "{\n".$inner.join( ",\n".$inner, @parts )."\n".$current.'}';
115 }
116 0 ▶ if ( $type eq 'SCALAR' && !blessed $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "SCALAR" and not builtin::blessed($value)) 0 0 0
Condition0%0/3
$type eq "SCALAR" and not builtin::blessed($value)
!ll&&!rl&&r
000
MC/DC0%0/2
$type eq "SCALAR" and not builtin::blessed($value)
$type eq "SCALAR" not builtin::blessed($value)
Truth table0%0/3
A and not(B)
A= $type eq "SCALAR"B= not builtin::blessed($value)
ABresult
0X0
100
111
117 0 ▶ return $$value ? 'true' : 'false' if !defined $$value || $$value eq '1' || $$value eq '0';
Branch: 2 branches
BranchTrueFalseTotal
if not defined $$value or $$value eq 1 or $$value eq 0 0 0 0
$$value ? : 0 0 0
Condition0%0/6
not defined $$value or $$value eq 1 or $$value eq 0
l!l&&r!l&&!r
000
not defined $$value or $$value eq 1
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $$value or $$value eq 1 or $$value eq 0
not defined $$value $$value eq 1 $$value eq 0
Truth table0%0/4
not(A) or B or C
A= not defined $$valueB= $$value eq 1C= $$value eq 0
ABCresult
1XX1
01X1
0011
0000
118 }
119 0 croak __PACKAGE__.'->js_stringify cannot write a '.$type.' reference';
120 }
121 13917 if ( CORE_BOOL ) {
122 248 ▶ no warnings 'experimental::builtin';
Subroutine BEGIN: called CC=1 SCAR=0.0
123 13917 ▶ return $value ? 'true' : 'false' if builtin::is_bool($value);
Branch: 2 branches
BranchTrueFalseTotal
if builtin::is_bool($value) 0 13917 13917
$value ? : 0 0 0
124 }
125 13917 ▶ return js_number($value) if _is_number($value);
Branch: 1 branch
BranchTrueFalseTotal
if _is_number($value) 1526 12391 13917
126 12391 return _quote($value);
127 }
128  
129 248 my %ESCAPE = ( '"' => '\\"', '\\' => '\\\\', "\b" => '\\b', "\f" => '\\f', "\n" => '\\n', "\r" => '\\r', "\t" => '\\t' );
130  
131 # QuoteJSONString: the short escapes, \u00xx for other controls and lone surrogates, nothing
132 # else – no "/" and no non-ASCII escaped.
133 21472 ▶sub _quote {
Subroutine _quote: called CC=2 SCAR=6.9
134 21472 my ( $s ) = @_;
135 21472 $s = "$s";
136 21472 ▶ $s =~ s{([\x{0}-\x{1F}"\\\x{D800}-\x{DFFF}])}{ $ESCAPE{$1} // sprintf( '\\u%04x', ord $1 ) }ge;
Condition100%2/2
$ESCAPE{$1} // sprintf("\\u%04x", ord $1)
l!l
197026
MC/DC100%1/1
$ESCAPE{$1} // sprintf("\\u%04x", ord $1)
$ESCAPE{$1}
Truth table100%2/2
A
A= $ESCAPE{$1}
Aresult
11
00
137 21472 return '"'.$s.'"';
138 }
139  
140 # Number::toString for a finite double; JSON.stringify writes NaN and the infinities as null.
141 1526 ▶sub js_number {
Subroutine js_number: called CC=21 SCAR=30.5
Pod documented
142 1526 my ( $n ) = @_;
143 1526 $n = 0 + $n;
144 1526 ▶ return 'null' if $n != $n || $n == 9**9**9 || $n == -9**9**9;
Branch: 1 branch
BranchTrueFalseTotal
if $n != $n or $n == "Inf" or $n == "-Inf" 8 1518 1526
Condition100%6/6
$n != $n or $n == "Inf" or $n == "-Inf"
l!l&&r!l&&!r
621518
$n != $n or $n == "Inf"
l!l&&r!l&&!r
151520
MC/DC100%3/3
$n != $n or $n == "Inf" or $n == "-Inf"
$n != $n $n == "Inf" $n == "-Inf"
Truth table100%4/4
A or B or C
A= $n != $nB= $n == "Inf"C= $n == "-Inf"
ABCresult
1XX1
01X1
0011
0000
145 1518 ▶ return '0' if $n == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 0 9 1509 1518
146 1509 my $flags = B::svref_2object( \$n )->FLAGS;
147 1509 ▶ if ( ( $flags & B::SVf_IOK() ) && !( $flags & B::SVf_NOK() ) && abs($n) <= 9007199254740992 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 256 and not $flags & 512 and abs $n <= '9007199254740992') 0 1509 1509
Condition83%5/6
$flags & 256 and not $flags & 512 and abs $n <= '9007199254740992'
!ll&&!rl&&r
150810
$flags & 256 and not $flags & 512
!ll&&!rl&&r
8014281
MC/DC0%0/3
$flags & 256 and not $flags & 512 and abs $n <= '9007199254740992'
$flags & 256 not $flags & 512 abs $n <= '9007199254740992'
Truth table75%3/4
A and not(B) and C
A= $flags & 256B= not $flags & 512C= abs $n <= '9007199254740992'
ABCresult
0XX0
10X0
1100
1111
148 0 return "$n";
149 }
150 1509 $n = unpack 'd', pack 'd', $n;
151 1509 ▶ my $neg = $n < 0 ? '-' : '';
Branch: 1 branch
BranchTrueFalseTotal
$n < 0 ? : 16 1493 1509
152 1509 my $abs = abs $n;
153 1509 my ( $digits, $exp );
154 1509 for my $p ( 1 .. 17 ) {
155 6855 my $s = sprintf '%.*e', $p - 1, $abs;
156 6855 ▶ next unless ( 0 + $s ) == $abs || $p == 17;
Branch: 1 branch
BranchTrueFalseTotal
unless 0 + $s == $abs or $p == 17 5346 1509 6855
Condition66%2/3
0 + $s == $abs or $p == 17
l!l&&r!l&&!r
150905346
MC/DC50%1/2
0 + $s == $abs or $p == 17
0 + $s == $abs $p == 17
Truth table66%2/3
A or B
A= 0 + $s == $absB= $p == 17
ABresult
1X1
011
000
157 1509 ( $digits, $exp ) = $s =~ /\A([0-9.]+)e([-+][0-9]+)\z/;
158 1509 $digits =~ s/\.//;
159 1509 ▶ $digits =~ s/0+\z// if length $digits > 1;
Branch: 1 branch
BranchTrueFalseTotal
if length $digits > 1 797 712 1509
160 1509 last;
161 }
162 1509 my $k = length $digits;
163 1509 my $e = $exp + 1; # the decimal point sits after $e digits
164 1509 my $out;
165 1509 ▶ if ( $k <= $e && $e <= 21 ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($k <= $e and $e <= 21) { } 1438 71 1509
elsif (0 < $e and $e <= 21) { } 36 35 71
elsif (-6 < $e and $e <= 0) { } 19 16 35
Condition100%3/3
$k <= $e and $e <= 21
!ll&&!rl&&r
6651438
MC/DC100%2/2
$k <= $e and $e <= 21
$k <= $e $e <= 21
Truth table100%3/3
A and B
A= $k <= $eB= $e <= 21
ABresult
0X0
100
111
166 1438 $out = $digits.( '0' x ( $e - $k ) );
167 }
168 ▶ elsif ( 0 < $e && $e <= 21 ) {
Condition100%3/3
0 < $e and $e <= 21
!ll&&!rl&&r
30536
MC/DC100%2/2
0 < $e and $e <= 21
0 < $e $e <= 21
Truth table100%3/3
A and B
A= 0 < $eB= $e <= 21
ABresult
0X0
100
111
169 36 $out = substr( $digits, 0, $e ).'.'.substr( $digits, $e );
170 }
171 ▶ elsif ( -6 < $e && $e <= 0 ) {
Condition100%3/3
-6 < $e and $e <= 0
!ll&&!rl&&r
11519
MC/DC100%2/2
-6 < $e and $e <= 0
-6 < $e $e <= 0
Truth table100%3/3
A and B
A= -6 < $eB= $e <= 0
ABresult
0X0
100
111
172 19 $out = '0.'.( '0' x -$e ).$digits;
173 }
174 else {
175 16 my $x = $e - 1;
176 16 ▶ my $mant = $k == 1 ? $digits : substr( $digits, 0, 1 ).'.'.substr( $digits, 1 );
Branch: 1 branch
BranchTrueFalseTotal
$k == 1 ? : 8 8 16
177 16 ▶ $out = $mant.'e'.( $x < 0 ? '-' : '+' ).abs($x);
Branch: 1 branch
BranchTrueFalseTotal
$x < 0 ? : 11 5 16
178 }
179 1509 return $neg.$out;
180 }
181  
182 #### Reading ###########################################################
183  
184 # A recursive-descent JSON.parse with V8's error messages. Objects come back as hashes tied
185 # to Skilletor::JSON::OrderedHash, so they keep the property order JavaScript gives them.
186  
187 1628 ▶sub js_parse {
Subroutine js_parse: called CC=3 SCAR=11.0
Pod documented
188 1628 my ( $text ) = @_;
189 1628 ▶ croak __PACKAGE__.'->js_parse needs a string' unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 1628 1628
190 1628 my $p = { s => "$text", i => 0, len => length "$text" };
191 1628 _ws($p);
192 1628 my $value = _value($p);
193 1502 _ws($p);
194 1502 ▶ _fail_at( $p, 'Unexpected non-whitespace character after JSON', undef, 1 ) if $p->{i} < $p->{len};
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"i"} < $p->{"len"} 5 1497 1502
195 1497 return $value;
196 }
197  
198 86347 ▶sub _ws {
Subroutine _ws: called CC=2 SCAR=6.9
199 86347 my ( $p ) = @_;
200 86347 pos( $p->{s} ) = $p->{i};
201 86347 ▶ $p->{i} = pos( $p->{s} ) if $p->{s} =~ /\G[ \t\n\r]+/gc;
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"s"} =~ /\G[ \t\n\r]+/cg 42197 44150 86347
202 }
203  
204 140123 ▶sub _peek { $_[0]{i} < $_[0]{len} ? substr( $_[0]{s}, $_[0]{i}, 1 ) : undef }
Subroutine _peek: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'i'} < $_[0]{'len'} ? : 139984 139 140123
205  
206 10 ▶sub _eos { Skilletor::Error::JSON->throw('Unexpected end of JSON input') }
Subroutine _eos: called CC=1 SCAR=0.0
207  
208 # The position as JavaScript counts it, in UTF-16 code units.
209 103 ▶sub _units {
Subroutine _units: called CC=1 SCAR=0.0
210 103 my ( $p, $i ) = @_;
211 103 my $before = substr( $p->{s}, 0, $i );
212 103 my $astral = () = $before =~ /[^\x{0}-\x{FFFF}]/g;
213 103 return $i + $astral;
214 }
215  
216 100 ▶sub _fail_at {
Subroutine _fail_at: called CC=5 SCAR=16.1
217 100 my ( $p, $what, $i, $bare ) = @_;
218 100 ▶ $i //= $p->{i};
Condition33%1/3
$i //= $p->{"i"}
l!l&&r!l&&!r
01000
MC/DC0%0/2
$i //= $p->{"i"}
$i $p->{"i"}
Truth table33%1/3
A //= B
A= $iB= $p->{"i"}
ABresult
1X1
011
000
219 100 my $before = substr( $p->{s}, 0, $i );
220 100 my $line = 1;
221 100 my $last = -1;
222 100 ▶ while ( $before =~ /\r\n|\r|\n/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($before =~ /\r\n|\r|\n/g) 6 100 106
223 6 $line++;
224 6 $last = pos($before) - 1;
225 }
226 100 my $pos = _units( $p, $i );
227 100 ▶ my $column = $last < 0 ? $pos + 1 : $pos - _units( $p, $last );
Branch: 1 branch
BranchTrueFalseTotal
$last < 0 ? : 97 3 100
228 100 ▶ Skilletor::Error::JSON->throw( $what.( $bare ? '' : ' in JSON' ).' at position '.$pos.' (line '.$line.' column '.$column.')' );
Branch: 1 branch
BranchTrueFalseTotal
$bare ? : 5 95 100
229 }
230  
231 21 ▶sub _unexpected_token {
Subroutine _unexpected_token: called CC=6 SCAR=17.9
232 21 my ( $p ) = @_;
233 21 my $i = $p->{i};
234 21 my $tok = substr( $p->{s}, $i, 1 );
235 21 my $len = $p->{len};
236 21 my $context;
237 21 ▶ if ( $len <= 20 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len <= 20) { } 16 5 21
238 16 $context = '"'.$p->{s}.'"';
239 }
240 else {
241 5 ▶ my $start = $i - 10 < 0 ? 0 : $i - 10;
Branch: 1 branch
BranchTrueFalseTotal
$i - 10 < 0 ? : 2 3 5
242 5 ▶ my $end = $i + 10 > $len ? $len : $i + 10;
Branch: 1 branch
BranchTrueFalseTotal
$i + 10 > $len ? : 2 3 5
243 5 ▶ $context = ( $start > 0 ? '...' : '' ).'"'.substr( $p->{s}, $start, $end - $start ).'"'.( $end < $len ? '...' : '' );
Branch: 2 branches
BranchTrueFalseTotal
$start > 0 ? : 3 2 5
$end < $len ? : 3 2 5
244 }
245 21 Skilletor::Error::JSON->throw( "Unexpected token '".$tok."', ".$context.' is not valid JSON' );
246 }
247  
248 23800 ▶sub _value {
Subroutine _value: called CC=10 SCAR=23.0
249 23800 my ( $p ) = @_;
250 23800 my $c = _peek($p);
251 23800 ▶ _eos() unless defined $c;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 6 23794 23800
252 23794 ▶ return _string($p) if $c eq '"';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "\"" 11182 12612 23794
253 12612 ▶ return _object($p) if $c eq '{';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "{" 7448 5164 12612
254 5164 ▶ return _array($p) if $c eq '[';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "[" 2707 2457 5164
255 2457 ▶ return _number($p) if $c eq '-' || $c =~ /[0-9]/;
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "-" or $c =~ /[0-9]/ 1444 1013 2457
Condition100%3/3
$c eq "-" or $c =~ /[0-9]/
l!l&&r!l&&!r
1514291013
MC/DC100%2/2
$c eq "-" or $c =~ /[0-9]/
$c eq "-" $c =~ /[0-9]/
Truth table100%3/3
A or B
A= $c eq "-"B= $c =~ /[0-9]/
ABresult
1X1
011
000
256 1013 ▶ return _literal( $p, 'true', JSON::MaybeXS::true() ) if $c eq 't';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "t" 267 746 1013
257 746 ▶ return _literal( $p, 'false', JSON::MaybeXS::false() ) if $c eq 'f';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "f" 184 562 746
258 562 ▶ return _literal( $p, 'null', undef ) if $c eq 'n';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "n" 547 15 562
259 15 _unexpected_token($p);
260 }
261  
262 998 ▶sub _literal {
Subroutine _literal: called CC=4 SCAR=13.9
263 998 my ( $p, $word, $value ) = @_;
264 998 for my $ch ( split //, $word ) {
265 4174 my $c = _peek($p);
266 4174 ▶ _eos() unless defined $c;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 2 4172 4174
267 4172 ▶ _unexpected_token($p) unless $c eq $ch;
Branch: 1 branch
BranchTrueFalseTotal
unless $c eq $ch 6 4166 4172
268 4166 $p->{i}++;
269 }
270 990 return $value;
271 }
272  
273 1444 ▶sub _number {
Subroutine _number: called CC=17 SCAR=28.3
274 1444 my ( $p ) = @_;
275 1444 my $start = $p->{i};
276 1444 my $s = $p->{s};
277 1444 ▶ if ( _peek($p) eq '-' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_peek($p) eq "-") 15 1429 1444
278 15 $p->{i}++;
279 15 my $c = _peek($p);
280 15 ▶ _fail_at( $p, 'No number after minus sign' ) unless defined $c && $c =~ /[0-9]/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c and $c =~ /[0-9]/ 3 12 15
Condition100%3/3
defined $c and $c =~ /[0-9]/
!ll&&!rl&&r
2112
MC/DC100%2/2
defined $c and $c =~ /[0-9]/
defined $c $c =~ /[0-9]/
Truth table100%3/3
A and B
A= defined $cB= $c =~ /[0-9]/
ABresult
0X0
100
111
281 }
282 1441 my $int_start = $p->{i};
283 1441 ▶ if ( _peek($p) eq '0' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_peek($p) eq '0') { } 32 1409 1441
284 32 $p->{i}++;
285 32 my $c = _peek($p);
286 32 ▶ _fail_at( $p, 'Unexpected number' ) if defined $c && $c =~ /[0-9]/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $c and $c =~ /[0-9]/ 4 28 32
Condition100%3/3
defined $c and $c =~ /[0-9]/
!ll&&!rl&&r
2264
MC/DC100%2/2
defined $c and $c =~ /[0-9]/
defined $c $c =~ /[0-9]/
Truth table100%3/3
A and B
A= defined $cB= $c =~ /[0-9]/
ABresult
0X0
100
111
287 }
288 else {
289 1409 pos($s) = $p->{i};
290 1409 $s =~ /\G[0-9]+/gc;
291 1409 $p->{i} = pos($s);
292 }
293 1437 my $simple = 1;
294 1437 ▶ if ( ( _peek($p) // '' ) eq '.' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((_peek($p) // "") eq ".") 39 1398 1437
Condition100%2/2
_peek($p) // ""
l!l
141126
MC/DC100%1/1
_peek($p) // ""
_peek($p)
Truth table100%2/2
A
A= _peek($p)
Aresult
11
00
295 39 $simple = 0;
296 39 $p->{i}++;
297 39 pos($s) = $p->{i};
298 39 ▶ _fail_at( $p, 'Unterminated fractional number' ) unless $s =~ /\G[0-9]+/gc;
Branch: 1 branch
BranchTrueFalseTotal
unless $s =~ /\G[0-9]+/cg 2 37 39
299 37 $p->{i} = pos($s);
300 }
301 1435 ▶ if ( ( _peek($p) // '' ) =~ /\A[eE]\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((_peek($p) // "") =~ /\A[eE]\z/) 17 1418 1435
Condition100%2/2
_peek($p) // ""
l!l
139936
MC/DC100%1/1
_peek($p) // ""
_peek($p)
Truth table100%2/2
A
A= _peek($p)
Aresult
11
00
302 17 $simple = 0;
303 17 $p->{i}++;
304 17 ▶ $p->{i}++ if ( _peek($p) // '' ) =~ /\A[-+]\z/;
Branch: 1 branch
BranchTrueFalseTotal
if (_peek($p) // "") =~ /\A[-+]\z/ 7 10 17
Condition100%2/2
_peek($p) // ""
l!l
161
MC/DC100%1/1
_peek($p) // ""
_peek($p)
Truth table100%2/2
A
A= _peek($p)
Aresult
11
00
305 17 pos($s) = $p->{i};
306 17 ▶ _fail_at( $p, 'Exponent part is missing a number' ) unless $s =~ /\G[0-9]+/gc;
Branch: 1 branch
BranchTrueFalseTotal
unless $s =~ /\G[0-9]+/cg 2 15 17
307 15 $p->{i} = pos($s);
308 }
309 1433 my $text = substr( $s, $start, $p->{i} - $start );
310 1433 my $n = 0 + $text;
311 # A JavaScript number is a double: an integer beyond 2**53 loses its low digits there.
312 1433 ▶ $n = unpack 'd', pack 'd', $n unless $simple && $p->{i} - $int_start <= 15;
Branch: 1 branch
BranchTrueFalseTotal
unless $simple and $p->{"i"} - $int_start <= 15 55 1378 1433
Condition100%3/3
$simple and $p->{"i"} - $int_start <= 15
!ll&&!rl&&r
4961378
MC/DC100%2/2
$simple and $p->{"i"} - $int_start <= 15
$simple $p->{"i"} - $int_start <= 15
Truth table100%3/3
A and B
A= $simpleB= $p->{"i"} - $int_start <= 15
ABresult
0X0
100
111
313 1433 return $n;
314 }
315  
316 248 my %UNESCAPE = ( '"' => '"', '\\' => '\\', '/' => '/', b => "\b", f => "\f", n => "\n", r => "\r", t => "\t" );
317  
318 29126 ▶sub _string {
Subroutine _string: called CC=13 SCAR=25.7
319 29126 my ( $p ) = @_;
320 29126 $p->{i}++; # the opening quote
321 29126 my $s = $p->{s};
322 29126 my $out = '';
323 29126 while (1) {
324 37150 pos($s) = $p->{i};
325 37150 ▶ if ( $s =~ /\G([^"\\\x{0}-\x{1F}]+)/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\G([^"\\\x{0}-\x{1F}]+)/cg) 35736 1414 37150
326 35736 $out .= $1;
327 35736 $p->{i} = pos($s);
328 }
329 37150 my $c = _peek($p);
330 37150 ▶ _fail_at( $p, 'Unterminated string' ) unless defined $c;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 1 37149 37150
331 37149 ▶ if ( $c eq '"' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\"") 29117 8032 37149
332 29117 $p->{i}++;
333 29117 return $out;
334 }
335 8032 ▶ _fail_at( $p, 'Bad control character in string literal' ) if ord($c) < 0x20;
Branch: 1 branch
BranchTrueFalseTotal
if ord $c < 32 2 8030 8032
336 # a backslash
337 8030 $p->{i}++;
338 8030 my $e = _peek($p);
339 8030 ▶ _eos() unless defined $e;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $e 2 8028 8030
340 8028 ▶ if ( exists $UNESCAPE{$e} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $UNESCAPE{$e}) 8002 26 8028
341 8002 $out .= $UNESCAPE{$e};
342 8002 $p->{i}++;
343 8002 next;
344 }
345 26 ▶ _fail_at( $p, 'Bad escaped character' ) unless $e eq 'u';
Branch: 1 branch
BranchTrueFalseTotal
unless $e eq "u" 1 25 26
346 25 $p->{i}++;
347 25 my $code = _hex4($p);
348 22 ▶ if ( $code >= 0xD800 && $code <= 0xDBFF && substr( $s, $p->{i}, 2 ) eq '\\u' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code >= 55296 and $code <= 56319 and substr($s, $p->{"i"}, 2) eq "\\u") 2 20 22
Condition100%6/6
$code >= 55296 and $code <= 56319 and substr($s, $p->{"i"}, 2) eq "\\u"
!ll&&!rl&&r
1822
$code >= 55296 and $code <= 56319
!ll&&!rl&&r
1714
MC/DC100%3/3
$code >= 55296 and $code <= 56319 and substr($s, $p->{"i"}, 2) eq "\\u"
$code >= 55296 $code <= 56319 substr($s, $p->{"i"}, 2) eq "\\u"
Truth table100%4/4
A and B and C
A= $code >= 55296B= $code <= 56319C= substr($s, $p->{"i"}, 2) eq "\\u"
ABCresult
0XX0
10X0
1100
1111
349 2 my $save = $p->{i};
350 2 $p->{i} += 2;
351 2 my $low = _hex4($p);
352 2 ▶ if ( $low >= 0xDC00 && $low <= 0xDFFF ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($low >= 56320 and $low <= 57343) 2 0 2
Condition33%1/3
$low >= 56320 and $low <= 57343
!ll&&!rl&&r
002
MC/DC0%0/2
$low >= 56320 and $low <= 57343
$low >= 56320 $low <= 57343
Truth table33%1/3
A and B
A= $low >= 56320B= $low <= 57343
ABresult
0X0
100
111
353 2 $out .= chr( 0x10000 + ( ( $code - 0xD800 ) << 10 ) + ( $low - 0xDC00 ) );
354 2 next;
355 }
356 0 $p->{i} = $save;
357 }
358 248 ▶ { no warnings qw( surrogate utf8 ); $out .= chr $code; }
Subroutine BEGIN: called CC=1 SCAR=0.0
359 }
360 }
361  
362 27 ▶sub _hex4 {
Subroutine _hex4: called CC=4 SCAR=13.9
363 27 my ( $p ) = @_;
364 27 my $v = 0;
365 27 for ( 1 .. 4 ) {
366 103 my $c = _peek($p);
367 103 ▶ _fail_at( $p, 'Bad Unicode escape' ) unless defined $c && $c =~ /\A[0-9a-fA-F]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c and $c =~ /\A[0-9a-fA-F]\z/ 3 100 103
Condition100%3/3
defined $c and $c =~ /\A[0-9a-fA-F]\z/
!ll&&!rl&&r
12100
MC/DC100%2/2
defined $c and $c =~ /\A[0-9a-fA-F]\z/
defined $c $c =~ /\A[0-9a-fA-F]\z/
Truth table100%3/3
A and B
A= defined $cB= $c =~ /\A[0-9a-fA-F]\z/
ABresult
0X0
100
111
368 100 $v = $v * 16 + hex $c;
369 100 $p->{i}++;
370 }
371 24 return $v;
372 }
373  
374 7448 ▶sub _object {
Subroutine _object: called CC=12 SCAR=24.8
375 7448 my ( $p ) = @_;
376 7448 $p->{i}++;
377 7448 my $obj = js_ordered();
378 7448 _ws($p);
379 7448 my $c = _peek($p);
380 7448 ▶ if ( defined $c && $c eq '}' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c and $c eq "}") 255 7193 7448
Condition100%3/3
defined $c and $c eq "}"
!ll&&!rl&&r
507143255
MC/DC100%2/2
defined $c and $c eq "}"
defined $c $c eq "}"
Truth table100%3/3
A and B
A= defined $cB= $c eq "}"
ABresult
0X0
100
111
381 255 $p->{i}++;
382 255 return $obj;
383 }
384 7193 ▶ _fail_at( $p, "Expected property name or '}'" ) unless defined $c && $c eq '"';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c and $c eq "\"" 54 7139 7193
Condition100%3/3
defined $c and $c eq "\""
!ll&&!rl&&r
5047139
MC/DC100%2/2
defined $c and $c eq "\""
defined $c $c eq "\""
Truth table100%3/3
A and B
A= defined $cB= $c eq "\""
ABresult
0X0
100
111
385 7139 while (1) {
386 17944 my $key = _string($p);
387 17944 _ws($p);
388 17944 ▶ _fail_at( $p, "Expected ':' after property name" ) unless ( _peek($p) // '' ) eq ':';
Branch: 1 branch
BranchTrueFalseTotal
unless (_peek($p) // "") eq ":" 7 17937 17944
Condition100%2/2
_peek($p) // ""
l!l
179413
MC/DC100%1/1
_peek($p) // ""
_peek($p)
Truth table100%2/2
A
A= _peek($p)
Aresult
11
00
389 17937 $p->{i}++;
390 17937 _ws($p);
391 17937 $obj->{$key} = _value($p);
392 17929 _ws($p);
393 17929 ▶ my $n = _peek($p) // '';
Condition100%2/2
_peek($p) // ""
l!l
179281
MC/DC100%1/1
_peek($p) // ""
_peek($p)
Truth table100%2/2
A
A= _peek($p)
Aresult
11
00
394 17929 ▶ if ( $n eq '}' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n eq "}") 7115 10814 17929
395 7115 $p->{i}++;
396 7115 return $obj;
397 }
398 10814 ▶ _fail_at( $p, "Expected ',' or '}' after property value" ) unless $n eq ',';
Branch: 1 branch
BranchTrueFalseTotal
unless $n eq "," 6 10808 10814
399 10808 $p->{i}++;
400 10808 _ws($p);
401 10808 ▶ _fail_at( $p, 'Expected double-quoted property name' ) unless ( _peek($p) // '' ) eq '"';
Branch: 1 branch
BranchTrueFalseTotal
unless (_peek($p) // "") eq "\"" 3 10805 10808
Condition100%2/2
_peek($p) // ""
l!l
108071
MC/DC100%1/1
_peek($p) // ""
_peek($p)
Truth table100%2/2
A
A= _peek($p)
Aresult
11
00
402 }
403 }
404  
405 2707 ▶sub _array {
Subroutine _array: called CC=6 SCAR=17.9
406 2707 my ( $p ) = @_;
407 2707 $p->{i}++;
408 2707 my @out;
409 2707 _ws($p);
410 2707 my $c = _peek($p);
411 2707 ▶ if ( defined $c && $c eq ']' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c and $c eq "]") 1011 1696 2707
Condition100%3/3
defined $c and $c eq "]"
!ll&&!rl&&r
316931011
MC/DC100%2/2
defined $c and $c eq "]"
defined $c $c eq "]"
Truth table100%3/3
A and B
A= defined $cB= $c eq "]"
ABresult
0X0
100
111
412 1011 $p->{i}++;
413 1011 return \@out;
414 }
415 1696 while (1) {
416 4235 _ws($p);
417 4235 push @out, _value($p);
418 4209 _ws($p);
419 4209 ▶ my $n = _peek($p) // '';
Condition100%2/2
_peek($p) // ""
l!l
42072
MC/DC100%1/1
_peek($p) // ""
_peek($p)
Truth table100%2/2
A
A= _peek($p)
Aresult
11
00
420 4209 ▶ if ( $n eq ']' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n eq "]") 1663 2546 4209
421 1663 $p->{i}++;
422 1663 return \@out;
423 }
424 2546 ▶ _fail_at( $p, "Expected ',' or ']' after array element" ) unless $n eq ',';
Branch: 1 branch
BranchTrueFalseTotal
unless $n eq "," 7 2539 2546
425 2539 $p->{i}++;
426 }
427 }
428  
429 248 1;
430  
431 __END__
« Skilletor/Report.pm ↑ Summary Skilletor/State.pm »