Skilletor/Frontmatter.pm

filter: Statementstmt 100% 312 / 312 Branchbran 96.4% 189 / 196 Conditioncond 87.0% 67 / 77 MC/DCmcdc 79.1% 38 / 48 Subroutinesub 100% 35 / 35 Podpod 100% 3 / 3 totaltotal 95.9% 644 / 671 CC 126 SCAR 23.4
CC
126
Cov
97%
block_scalar
23.0
value
14.0
_inline
14.0
CRAP 10.3
« Skilletor/Tar.pm ↑ Summary Skilletor/Source/Url.pm »
1 package Skilletor::Frontmatter;
2 # ABSTRACT: The reference's YAML subset – agent frontmatter and bundle files
3  
4 34 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 34 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 34 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 34 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 34 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 34 ▶use Skilletor::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 34 ▶use Skilletor::Error::Frontmatter;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 34 ▶use Skilletor::JSON qw( js_ordered );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 34 ▶use Skilletor::JSString qw( js_trim js_ws_re );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 34 our $VERSION = '0.001';
16  
17 34 our @EXPORT_OK = qw( split_frontmatter parse_yaml_document is_yaml_float );
18  
19 # JavaScript's `.` (no line terminator) and `\s`.
20 34 my $DOT = qr/[^\n\r\x{2028}\x{2029}]/;
21 34 my $WS = js_ws_re();
22 34 my $LT = qr/[\n\r\x{2028}\x{2029}]/;
23  
24 34 my $OPEN = qr/\A---[ \t]*\r?\n/;
25 # /^---[ \t]*(?:\r?\n|$)/m: JavaScript's multiline ^ and $ sit at any line terminator.
26 34 my $CLOSE = qr/(?:\A|(?<=$LT))---[ \t]*(?:\r?\n|(?=$LT)|\z)/;
27  
28 # "key": / 'key': / plain key, then `:` followed by whitespace or the end.
29 34 my $KEY_RE = qr/\A(?:"((?:[^"\\]|\\$DOT)*)"|'((?:[^']|'')*)'|((?!$WS)[^"'#&*!|>\[\]{},?:\-][^:#]*?))[ \t]*:(?:[ \t]+($DOT*))?\z/;
30  
31 34 my %ESCAPES = (
32 '0' => "\0", a => "\x07", b => "\b", t => "\t", "\t" => "\t", n => "\n", v => "\x0B", f => "\f", r => "\r",
33 e => "\x1b", ' ' => ' ', '"' => '"', '/' => '/', '\\' => '\\', N => "\x{85}", _ => "\x{A0}", L => "\x{2028}",
34 P => "\x{2029}",
35 );
36  
37 167 ▶sub split_frontmatter {
Subroutine split_frontmatter: called CC=3 SCAR=11.0
Pod documented
38 167 my ( $text ) = @_;
39 167 ▶ return { data => js_ordered(), body => $text } unless $text =~ $OPEN;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /$OPEN/ 3 164 167
40 164 my $start = $+[0];
41 164 pos($text) = $start;
42 164 ▶ Skilletor::Error::Frontmatter->throw('frontmatter: no closing --- line') unless $text =~ /$CLOSE/g;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /$CLOSE/g 1 163 164
43 163 my ( $close, $after ) = ( $-[0], $+[0] );
44 163 my $yaml = substr $text, $start, $close - $start;
45 163 ( my $body = substr $text, $after ) =~ s/\A(?:[ \t]*\r?\n)+//;
46 163 return { data => _parse_yaml( $yaml, 2, 'frontmatter line' ), body => $body };
47 }
48  
49 405 ▶sub parse_yaml_document {
Subroutine parse_yaml_document: called CC=1 SCAR=0.0
Pod documented
50 405 my ( $text ) = @_;
51 405 return _parse_yaml( $text, 1, 'line' );
52 }
53  
54 2654 ▶sub is_yaml_float { blessed $_[0] && $_[0]->isa('Skilletor::Frontmatter::Float') ? 1 : 0 }
Subroutine is_yaml_float: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) && $_[0]->isa('Skilletor::Frontmatter::Float') ? : 17 2637 2654
Condition100%3/3
builtin::blessed($_[0]) and $_[0]->isa("Skilletor::Frontmatter::Float")
!ll&&!rl&&r
26201717
MC/DC100%2/2
builtin::blessed($_[0]) and $_[0]->isa("Skilletor::Frontmatter::Float")
builtin::blessed($_[0]) $_[0]->isa("Skilletor::Frontmatter::Float")
Truth table100%3/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa("Skilletor::Frontmatter::Float")
ABresult
0X0
100
111
55  
56 #### The parser ########################################################
57  
58 328 ▶sub _fail {
Subroutine _fail: called CC=3 SCAR=12.0
59 328 my ( $line, $message ) = @_;
60 328 ▶ Skilletor::Error::Frontmatter->throw( ( $line ? $line->{label} : 'line' ).' '.( $line ? $line->{no} : '?' ).': '.$message );
Branch: 2 branches
BranchTrueFalseTotal
$line ? : 328 0 328
$line ? : 328 0 328
61 }
62  
63 1170 ▶sub _indent_of {
Subroutine _indent_of: called CC=2 SCAR=6.9
64 1170 my ( $line ) = @_;
65 1170 my ($m) = $line->{raw} =~ /\A([ \t]*)/;
66 1170 ▶ _fail( $line, 'tabs are not allowed in indentation' ) if index( $m, "\t" ) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($m, "\t") != -1 2 1168 1170
67 1168 return length $m;
68 }
69  
70 1448 ▶sub _skippable {
Subroutine _skippable: called CC=3 SCAR=11.0
71 1448 my ( $line ) = @_;
72 1448 my $t = js_trim( $line->{raw} );
73 1448 ▶ return ( $t eq '' || substr( $t, 0, 1 ) eq '#' ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$t eq '' || substr($t, 0, 1) eq '#' ? : 335 1113 1448
Condition100%3/3
$t eq "" or substr($t, 0, 1) eq "#"
l!l&&r!l&&!r
33141113
MC/DC100%2/2
$t eq "" or substr($t, 0, 1) eq "#"
$t eq "" substr($t, 0, 1) eq "#"
Truth table100%3/3
A or B
A= $t eq ""B= substr($t, 0, 1) eq "#"
ABresult
1X1
011
000
74 }
75  
76 838 ▶sub _is_seq_item {
Subroutine _is_seq_item: called CC=3 SCAR=11.0
77 838 my ( $text ) = @_;
78 838 ▶ return ( $text eq '-' || substr( $text, 0, 2 ) eq '- ' ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$text eq '-' || substr($text, 0, 2) eq '- ' ? : 54 784 838
Condition100%3/3
$text eq "-" or substr($text, 0, 2) eq "- "
l!l&&r!l&&!r
252784
MC/DC100%2/2
$text eq "-" or substr($text, 0, 2) eq "- "
$text eq "-" substr($text, 0, 2) eq "- "
Truth table100%3/3
A or B
A= $text eq "-"B= substr($text, 0, 2) eq "- "
ABresult
1X1
011
000
79 }
80  
81 568 ▶sub _parse_yaml {
Subroutine _parse_yaml: called CC=2 SCAR=7.0
82 568 my ( $yaml, $first_line, $label ) = @_;
83 568 my $i = 0;
84 568 my @lines = map { +{ no => $i++ + $first_line, raw => $_, label => $label } } split /\r?\n/, $yaml, -1;
85 568 my $p = bless { lines => \@lines }, 'Skilletor::Frontmatter::_Parser';
86 568 my ( $value, $pos ) = $p->mapping( 0, 0 );
87 240 ▶ _fail( $lines[$pos], 'unexpected content' ) if $pos < @lines;
Branch: 1 branch
BranchTrueFalseTotal
if $pos < @lines 0 240 240
88 240 return $value;
89 }
90  
91 package # hide from PAUSE
92 Skilletor::Frontmatter::_Parser;
93  
94 1620 ▶sub next_content {
Subroutine next_content: called CC=3 SCAR=11.0
95 1620 my ( $self, $pos ) = @_;
96 1620 my $lines = $self->{lines};
97 1620 ▶ $pos++ while $pos < @$lines && Skilletor::Frontmatter::_skippable( $lines->[$pos] );
Branch: 1 branch
BranchTrueFalseTotal
$pos < @$lines and Skilletor::Frontmatter::_skippable($lines->[$pos]) and do { ++$pos } 335 1620 1955
Condition100%3/3
$pos < @$lines and Skilletor::Frontmatter::_skippable($lines->[$pos])
!ll&&!rl&&r
5071113335
MC/DC100%2/2
$pos < @$lines and Skilletor::Frontmatter::_skippable($lines->[$pos])
$pos < @$lines Skilletor::Frontmatter::_skippable($lines->[$pos])
Truth table100%3/3
A and B
A= $pos < @$linesB= Skilletor::Frontmatter::_skippable($lines->[$pos])
ABresult
0X0
100
111
98 1620 return $pos;
99 }
100  
101 591 ▶sub mapping {
Subroutine mapping: called CC=10 SCAR=23.0
102 591 my ( $self, $start, $indent ) = @_;
103 591 my $lines = $self->{lines};
104 591 my $obj = Skilletor::JSON::js_ordered();
105 591 my $pos = $start;
106 591 for ( ;; ) {
107 1066 $pos = $self->next_content($pos);
108 1066 ▶ last if $pos >= @$lines;
Branch: 1 branch
BranchTrueFalseTotal
if $pos >= @$lines 253 813 1066
109 813 my $line = $lines->[$pos];
110 813 my $ind = Skilletor::Frontmatter::_indent_of($line);
111 813 ▶ last if $ind < $indent;
Branch: 1 branch
BranchTrueFalseTotal
if $ind < $indent 10 803 813
112 803 ▶ Skilletor::Frontmatter::_fail( $line, 'unexpected indentation' ) if $ind > $indent;
Branch: 1 branch
BranchTrueFalseTotal
if $ind > $indent 40 763 803
113 763 my $text = substr $line->{raw}, $ind;
114 763 ▶ Skilletor::Frontmatter::_fail( $line, 'unexpected sequence item' ) if Skilletor::Frontmatter::_is_seq_item($text);
Branch: 1 branch
BranchTrueFalseTotal
if Skilletor::Frontmatter::_is_seq_item($text) 6 757 763
115 757 ▶ my @m = $text =~ $KEY_RE or Skilletor::Frontmatter::_fail( $line, 'expected `key: value`' );
Branch: 1 branch
BranchTrueFalseTotal
my(@m) = $text =~ /$KEY_RE/ or Skilletor::Frontmatter::_fail($line, "expected `key: value`") 528 229 757
116 528 ▶ my $key = defined $m[0] ? Skilletor::Frontmatter::_unescape_double( $m[0], $line )
Branch: 2 branches
BranchTrueFalseTotal
defined $m[0] ? : 4 524 528
defined $m[1] ? : 4 520 524
117 : defined $m[1] ? $m[1] =~ s/''/'/gr
118 : Skilletor::JSString::js_trim( $m[2] );
119 528 ▶ Skilletor::Frontmatter::_fail( $line, 'duplicate key "'.$key.'"' ) if exists $obj->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $obj->{$key} 2 526 528
120 526 ▶ my ( $value, $next ) = $self->value( $m[3] // '', $pos + 1, $indent, $line );
Condition100%2/2
$m[3] // ""
l!l
46858
MC/DC100%1/1
$m[3] // ""
$m[3]
Truth table100%2/2
A
A= $m[3]
Aresult
11
00
121 475 $obj->{$key} = $value;
122 475 $pos = $next;
123 }
124 263 return ( $obj, $pos );
125 }
126  
127 # The value after `key:` on $line; $pos is the line after it.
128 526 ▶sub value {
Subroutine value: called CC=14 SCAR=26.4
129 526 my ( $self, $rest, $pos, $indent, $line ) = @_;
130 526 my $lines = $self->{lines};
131 526 my $r = Skilletor::JSString::js_trim($rest);
132 526 ▶ return $self->block_scalar( $r, $pos, $indent, $line ) if $r =~ /\A[|>]/;
Branch: 1 branch
BranchTrueFalseTotal
if $r =~ /\A[|>]/ 32 494 526
133 494 ▶ if ( $r eq '' || substr( $r, 0, 1 ) eq '#' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r eq "" or substr($r, 0, 1) eq "#") 61 433 494
Condition100%3/3
$r eq "" or substr($r, 0, 1) eq "#"
l!l&&r!l&&!r
592433
MC/DC100%2/2
$r eq "" or substr($r, 0, 1) eq "#"
$r eq "" substr($r, 0, 1) eq "#"
Truth table100%3/3
A or B
A= $r eq ""B= substr($r, 0, 1) eq "#"
ABresult
1X1
011
000
134 61 my $q = $self->next_content($pos);
135 61 ▶ return ( undef, $q ) if $q >= @$lines;
Branch: 1 branch
BranchTrueFalseTotal
if $q >= @$lines 16 45 61
136 45 my $next = $lines->[$q];
137 45 my $ind = Skilletor::Frontmatter::_indent_of($next);
138 43 my $text = substr $next->{raw}, $ind;
139 43 ▶ if ( $ind > $indent ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ind > $indent) 39 4 43
140 39 ▶ return Skilletor::Frontmatter::_is_seq_item($text) ? $self->sequence( $q, $ind ) : $self->mapping( $q, $ind );
Branch: 1 branch
BranchTrueFalseTotal
Skilletor::Frontmatter::_is_seq_item($text) ? : 16 23 39
141 }
142 4 ▶ return $self->sequence( $q, $ind ) if $ind == $indent && Skilletor::Frontmatter::_is_seq_item($text);
Branch: 1 branch
BranchTrueFalseTotal
if $ind == $indent and Skilletor::Frontmatter::_is_seq_item($text) 2 2 4
Condition66%2/3
$ind == $indent and Skilletor::Frontmatter::_is_seq_item($text)
!ll&&!rl&&r
022
MC/DC50%1/2
$ind == $indent and Skilletor::Frontmatter::_is_seq_item($text)
$ind == $indent Skilletor::Frontmatter::_is_seq_item($text)
Truth table66%2/3
A and B
A= $ind == $indentB= Skilletor::Frontmatter::_is_seq_item($text)
ABresult
0X0
100
111
143 2 return ( undef, $q );
144 }
145 # An inline value, possibly continued on more-indented lines (folded with spaces).
146 433 my @parts = ($r);
147 433 my $p = $pos;
148 433 for ( ;; ) {
149 451 my $q = $self->next_content($p);
150 451 ▶ last if $q >= @$lines || Skilletor::Frontmatter::_indent_of( $lines->[$q] ) <= $indent;
Branch: 1 branch
BranchTrueFalseTotal
if $q >= @$lines or Skilletor::Frontmatter::_indent_of($lines->[$q]) <= $indent 433 18 451
Condition100%3/3
$q >= @$lines or Skilletor::Frontmatter::_indent_of($lines->[$q]) <= $indent
l!l&&r!l&&!r
23619718
MC/DC100%2/2
$q >= @$lines or Skilletor::Frontmatter::_indent_of($lines->[$q]) <= $indent
$q >= @$lines Skilletor::Frontmatter::_indent_of($lines->[$q]) <= $indent
Truth table100%3/3
A or B
A= $q >= @$linesB= Skilletor::Frontmatter::_indent_of($lines->[$q]) <= $indent
ABresult
1X1
011
000
151 18 push @parts, Skilletor::JSString::js_trim( $lines->[$q]{raw} );
152 18 $p = $q + 1;
153 }
154 433 ▶ return ( Skilletor::Frontmatter::_inline( $r, $line ), $pos ) if @parts == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @parts == 1 417 16 433
155 16 ▶ return ( Skilletor::Frontmatter::_inline( join( ' ', @parts ), $line ), $p ) if $r =~ /\A["'\[{]/;
Branch: 1 branch
BranchTrueFalseTotal
if $r =~ /\A["'\[{]/ 8 8 16
156 8 ▶ Skilletor::Frontmatter::_fail( $line, 'anchors, aliases and tags are not supported' ) if $r =~ /\A[&*!]/;
Branch: 1 branch
BranchTrueFalseTotal
if $r =~ /\A[&*!]/ 0 8 8
157 8 return ( join( ' ', map { Skilletor::Frontmatter::_strip_comment($_) } @parts ), $p );
158 }
159  
160 18 ▶sub sequence {
Subroutine sequence: called CC=9 SCAR=22.0
161 18 my ( $self, $start, $indent ) = @_;
162 18 my $lines = $self->{lines};
163 18 my @arr;
164 18 my $pos = $start;
165 18 for ( ;; ) {
166 42 $pos = $self->next_content($pos);
167 42 ▶ last if $pos >= @$lines;
Branch: 1 branch
BranchTrueFalseTotal
if $pos >= @$lines 2 40 42
168 40 my $line = $lines->[$pos];
169 40 my $ind = Skilletor::Frontmatter::_indent_of($line);
170 40 ▶ last if $ind < $indent;
Branch: 1 branch
BranchTrueFalseTotal
if $ind < $indent 6 34 40
171 34 ▶ Skilletor::Frontmatter::_fail( $line, 'unexpected indentation in a sequence' ) if $ind > $indent;
Branch: 1 branch
BranchTrueFalseTotal
if $ind > $indent 2 32 34
172 32 my $text = substr $line->{raw}, $ind;
173 32 ▶ last unless Skilletor::Frontmatter::_is_seq_item($text);
Branch: 1 branch
BranchTrueFalseTotal
unless Skilletor::Frontmatter::_is_seq_item($text) 2 30 32
174 30 my $item = Skilletor::JSString::js_trim( substr $text, 1 );
175 30 ▶ if ( $item eq '' || substr( $item, 0, 1 ) eq '#' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item eq "" or substr($item, 0, 1) eq "#") 2 28 30
Condition66%2/3
$item eq "" or substr($item, 0, 1) eq "#"
l!l&&r!l&&!r
2028
MC/DC50%1/2
$item eq "" or substr($item, 0, 1) eq "#"
$item eq "" substr($item, 0, 1) eq "#"
Truth table66%2/3
A or B
A= $item eq ""B= substr($item, 0, 1) eq "#"
ABresult
1X1
011
000
176 2 Skilletor::Frontmatter::_fail( $line, 'nested blocks inside sequences are not supported' );
177 }
178 28 ▶ Skilletor::Frontmatter::_fail( $line, 'mappings inside sequences are not supported' ) if $item =~ $KEY_RE;
Branch: 1 branch
BranchTrueFalseTotal
if $item =~ /$KEY_RE/ 2 26 28
179 26 ▶ Skilletor::Frontmatter::_fail( $line, 'nested collections are not supported' ) if $item =~ /\A[\[{]/;
Branch: 1 branch
BranchTrueFalseTotal
if $item =~ /\A[\[{]/ 2 24 26
180 24 push @arr, Skilletor::Frontmatter::_inline( $item, $line );
181 24 $pos++;
182 }
183 10 return ( \@arr, $pos );
184 }
185  
186 32 ▶sub block_scalar {
Subroutine block_scalar: called CC=23 SCAR=31.4
187 32 my ( $self, $header, $pos, $indent, $line ) = @_;
188 32 my $lines = $self->{lines};
189 32 ▶ my @h = $header =~ /\A([|>])([+-]?)([1-9]?)[ \t]*(?:#$DOT*)?\z/
Branch: 1 branch
BranchTrueFalseTotal
my(@h) = $header =~ /\A([|>])([+-]?)([1-9]?)[ \t]*(?:#$DOT*)?\z/ or Skilletor::Frontmatter::_fail($line, 'unsupported block scalar header "' . $header . '"') 30 2 32
190 or Skilletor::Frontmatter::_fail( $line, 'unsupported block scalar header "'.$header.'"' );
191 30 my $folded = $h[0] eq '>';
192 30 my $chomp = $h[1];
193 30 ▶ my $content_indent = length $h[2] ? $indent + $h[2] : -1;
Branch: 1 branch
BranchTrueFalseTotal
length $h[2] ? : 4 26 30
194 30 my @collected;
195 30 my $p = $pos;
196 30 ▶ while ( $p < @$lines ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p < @$lines) 86 12 98
197 86 my $l = $lines->[$p];
198 86 ▶ if ( Skilletor::JSString::js_trim( $l->{raw} ) eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Skilletor::JSString::js_trim($l->{"raw"}) eq "") 29 57 86
199 29 push @collected, $l;
200 29 $p++;
201 29 next;
202 }
203 57 my $ind = Skilletor::Frontmatter::_indent_of($l);
204 57 ▶ last if $ind <= $indent;
Branch: 1 branch
BranchTrueFalseTotal
if $ind <= $indent 16 41 57
205 41 ▶ $content_indent = $ind if $content_indent == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $content_indent == -1 18 23 41
206 41 ▶ Skilletor::Frontmatter::_fail( $l, 'block scalar line is less indented than its first line' ) if $ind < $content_indent;
Branch: 1 branch
BranchTrueFalseTotal
if $ind < $content_indent 2 39 41
207 39 push @collected, $l;
208 39 $p++;
209 }
210 28 ▶ my @texts = map { Skilletor::JSString::js_trim( $_->{raw} ) eq '' ? '' : substr( $_->{raw}, $content_indent ) } @collected;
Branch: 1 branch
BranchTrueFalseTotal
Skilletor::JSString::js_trim($_->{'raw'}) eq '' ? : 29 37 66
211 28 my $trailing = 0;
212 28 ▶ while ( @texts && $texts[-1] eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@texts and $texts[-1] eq "") 25 28 53
Condition100%3/3
@texts and $texts[-1] eq ""
!ll&&!rl&&r
82025
MC/DC100%2/2
@texts and $texts[-1] eq ""
@texts $texts[-1] eq ""
Truth table100%3/3
A and B
A= @textsB= $texts[-1] eq ""
ABresult
0X0
100
111
213 25 pop @texts;
214 25 $trailing++;
215 }
216 28 my $body;
217 28 ▶ if ( !$folded ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $folded) { } 22 6 28
218 22 $body = join "\n", @texts;
219 }
220 else {
221 6 $body = '';
222 6 my ( $pending, $first, $prev_more ) = ( 0, 1, 0 );
223 6 for my $t (@texts) {
224 16 ▶ if ( $t eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq "") 2 14 16
225 2 $pending++;
226 2 next;
227 }
228 14 ▶ my $more = $t =~ /\A[ \t]/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$t =~ /\A[ \t]/ ? : 2 12 14
229 14 ▶ if ($first) { $body = $t }
Branch: 3 branches
BranchTrueFalseTotal
if ($first) { } 4 10 14
elsif ($pending > 0) { } 2 8 10
elsif ($more or $prev_more) { } 4 4 8
230 2 elsif ( $pending > 0 ) { $body .= ( "\n" x $pending ).$t }
231 4 ▶ elsif ( $more || $prev_more ) { $body .= "\n".$t }
Condition100%3/3
$more or $prev_more
l!l&&r!l&&!r
224
MC/DC100%2/2
$more or $prev_more
$more $prev_more
Truth table100%3/3
A or B
A= $moreB= $prev_more
ABresult
1X1
011
000
232 4 else { $body .= ' '.$t }
233 14 $first = 0;
234 14 $pending = 0;
235 14 $prev_more = $more;
236 }
237 }
238 28 ▶ return ( $body, $p ) if $chomp eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $chomp eq "-" 6 22 28
239 22 ▶ return ( '', $p ) if $body eq '' && $chomp ne '+';
Branch: 1 branch
BranchTrueFalseTotal
if $body eq "" and $chomp ne "+" 4 18 22
Condition100%3/3
$body eq "" and $chomp ne "+"
!ll&&!rl&&r
1624
MC/DC100%2/2
$body eq "" and $chomp ne "+"
$body eq "" $chomp ne "+"
Truth table100%3/3
A and B
A= $body eq ""B= $chomp ne "+"
ABresult
0X0
100
111
240 18 ▶ return ( $body."\n".( $chomp eq '+' ? "\n" x $trailing : '' ), $p );
Branch: 1 branch
BranchTrueFalseTotal
$chomp eq '+' ? : 6 12 18
241 }
242  
243 package Skilletor::Frontmatter;
244  
245 #### Inline values #####################################################
246  
247 302 ▶sub _strip_comment {
Subroutine _strip_comment: called CC=2 SCAR=6.9
248 302 my ( $s ) = @_;
249 302 ▶ return js_trim( $s =~ /(?:\A|[ \t])#/ ? substr( $s, 0, $-[0] ) : $s );
Branch: 1 branch
BranchTrueFalseTotal
$s =~ /(?:\A|[ \t])#/ ? : 10 292 302
250 }
251  
252 399 ▶sub _typed_plain {
Subroutine _typed_plain: called CC=11 SCAR=24.0
253 399 my ( $s ) = @_;
254 399 ▶ return undef if $s eq '' || $s eq '~' || $s =~ /\A(?:null|Null|NULL)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "" or $s eq "~" or $s =~ /\A(?:null|Null|NULL)\z/ 11 388 399
Condition83%5/6
$s eq "" or $s eq "~" or $s =~ /\A(?:null|Null|NULL)\z/
l!l&&r!l&&!r
47388
$s eq "" or $s eq "~"
l!l&&r!l&&!r
04395
MC/DC66%2/3
$s eq "" or $s eq "~" or $s =~ /\A(?:null|Null|NULL)\z/
$s eq "" $s eq "~" $s =~ /\A(?:null|Null|NULL)\z/
Truth table75%3/4
A or B or C
A= $s eq ""B= $s eq "~"C= $s =~ /\A(?:null|Null|NULL)\z/
ABCresult
1XX1
01X1
0011
0000
255 388 ▶ return JSON::MaybeXS::true() if $s =~ /\A(?:true|True|TRUE)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $s =~ /\A(?:true|True|TRUE)\z/ 13 375 388
256 375 ▶ return JSON::MaybeXS::false() if $s =~ /\A(?:false|False|FALSE)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $s =~ /\A(?:false|False|FALSE)\z/ 4 371 375
257 371 ▶ if ( $s =~ /\A[-+]?([0-9]+)\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\A[-+]?([0-9]+)\z/) 75 296 371
258 75 ( my $digits = $1 ) =~ s/\A0+(?=.)//;
259 # Number.isSafeInteger: |n| <= 2**53 - 1, else the text stays a string.
260 75 ▶ my $safe = length($digits) < 16 || ( length($digits) == 16 && $digits le '9007199254740991' );
Condition83%5/6
length $digits < 16 || length $digits == 16 && $digits le '9007199254740991'
l!l&&r!l&&!r
6924
length $digits == 16 and $digits le "9007199254740991"
!ll&&!rl&&r
042
MC/DC66%2/3
length $digits < 16 || length $digits == 16 && $digits le '9007199254740991'
length $digits < 16 length $digits == 16 $digits le "9007199254740991"
Truth table75%3/4
A || B and C
A= length $digits < 16B= length $digits == 16C= $digits le "9007199254740991"
ABCresult
1XX1
0111
00X0
0100
261 75 ▶ return $safe ? 0 + $s : $s;
Branch: 1 branch
BranchTrueFalseTotal
$safe ? : 71 4 75
262 }
263 296 ▶ if ( $s =~ /\A[-+]?(?:\.[0-9]+|[0-9]+\.[0-9]*|[0-9]+(?=[eE]))(?:[eE][-+]?[0-9]+)?\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\A[-+]?(?:\.[0-9]+|[0-9]+\.[0-9]*|[0-9]+(?=[eE]))(?:[eE][-+]?[0-9]+)?\z/) 18 278 296
264 34 ▶ no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
265 18 return Skilletor::Frontmatter::Float->new( 0 + $s );
266 }
267 278 return $s;
268 }
269  
270 53 ▶sub _unescape_double {
Subroutine _unescape_double: called CC=11 SCAR=24.0
271 53 my ( $s, $line ) = @_;
272 53 my $out = '';
273 53 my $n = length $s;
274 53 ▶ for ( my $i = 0; $i < $n; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 515 46 561
275 515 my $ch = substr $s, $i, 1;
276 515 ▶ if ( $ch ne '\\' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch ne "\\") 479 36 515
277 479 $out .= $ch;
278 479 next;
279 }
280 36 $i++;
281 36 ▶ _fail( $line, 'dangling backslash in a double-quoted string' ) if $i >= $n;
Branch: 1 branch
BranchTrueFalseTotal
if $i >= $n 0 36 36
282 36 my $e = substr $s, $i, 1;
283 36 ▶ my $hex_len = $e eq 'x' ? 2 : $e eq 'u' ? 4 : $e eq 'U' ? 8 : 0;
Branch: 3 branches
BranchTrueFalseTotal
$e eq 'x' ? : 4 32 36
$e eq 'u' ? : 4 28 32
$e eq 'U' ? : 6 22 28
284 36 ▶ if ($hex_len) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hex_len) { } 14 22 36
elsif (exists $ESCAPES{$e}) { } 20 2 22
285 14 my $hex = substr $s, $i + 1, $hex_len;
286 14 ▶ _fail( $line, 'bad \\'.$e.' escape' ) unless $hex =~ /\A[0-9a-fA-F]{$hex_len}\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $hex =~ /\A[0-9a-fA-F]{$hex_len}\z/ 2 12 14
287 12 my $cp = hex $hex;
288 # Above U+10FFFF the reference refuses the escape like any other bad one.
289 12 ▶ _fail( $line, 'bad \\'.$e.' escape' ) if $cp > 0x10FFFF;
Branch: 1 branch
BranchTrueFalseTotal
if $cp > 1114111 3 9 12
290 34 ▶ no warnings qw( surrogate nonchar non_unicode );
Subroutine BEGIN: called CC=1 SCAR=0.0
291 9 $out .= chr $cp;
292 9 $i += $hex_len;
293 }
294 elsif ( exists $ESCAPES{$e} ) {
295 20 $out .= $ESCAPES{$e};
296 }
297 else {
298 2 _fail( $line, 'unknown escape \\'.$e );
299 }
300 }
301 46 return $out;
302 }
303  
304 # One quoted scalar at substr($s, $i, 1) (a quote): the value and the index after it.
305 65 ▶sub _quoted {
Subroutine _quoted: called CC=9 SCAR=22.0
306 65 my ( $s, $i, $line ) = @_;
307 65 my $q = substr $s, $i, 1;
308 65 my $n = length $s;
309 65 my $j = $i + 1;
310 65 ▶ if ( $q eq "'" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q eq "'") 12 53 65
311 12 my $out = '';
312 12 for ( ;; ) {
313 134 ▶ _fail( $line, 'unterminated single-quoted string' ) if $j >= $n;
Branch: 1 branch
BranchTrueFalseTotal
if $j >= $n 0 134 134
314 134 ▶ if ( substr( $s, $j, 1 ) eq "'" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($s, $j, 1) eq "'") 16 118 134
315 16 ▶ if ( $j + 1 < $n && substr( $s, $j + 1, 1 ) eq "'" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j + 1 < $n and substr($s, $j + 1, 1) eq "'") 4 12 16
Condition100%3/3
$j + 1 < $n and substr($s, $j + 1, 1) eq "'"
!ll&&!rl&&r
844
MC/DC100%2/2
$j + 1 < $n and substr($s, $j + 1, 1) eq "'"
$j + 1 < $n substr($s, $j + 1, 1) eq "'"
Truth table100%3/3
A and B
A= $j + 1 < $nB= substr($s, $j + 1, 1) eq "'"
ABresult
0X0
100
111
316 4 $out .= "'";
317 4 $j += 2;
318 4 next;
319 }
320 12 return ( $out, $j + 1 );
321 }
322 118 $out .= substr $s, $j++, 1;
323 }
324 }
325 53 for ( ;; ) {
326 640 ▶ _fail( $line, 'unterminated double-quoted string' ) if $j >= $n;
Branch: 1 branch
BranchTrueFalseTotal
if $j >= $n 4 636 640
327 636 my $c = substr $s, $j, 1;
328 636 ▶ if ( $c eq '\\' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\\") 36 600 636
329 36 $j += 2;
330 36 next;
331 }
332 600 ▶ return ( _unescape_double( substr( $s, $i + 1, $j - $i - 1 ), $line ), $j + 1 ) if $c eq '"';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "\"" 49 551 600
333 551 $j++;
334 }
335 }
336  
337 132 ▶sub _assert_end {
Subroutine _assert_end: called CC=4 SCAR=13.9
338 132 my ( $s, $i, $line ) = @_;
339 132 ▶ my $rest = js_trim( $i < length $s ? substr( $s, $i ) : '' );
Branch: 1 branch
BranchTrueFalseTotal
$i < length $s ? : 8 124 132
340 132 ▶ _fail( $line, 'unexpected text after a value: "'.$rest.'"' ) if $rest ne '' && substr( $rest, 0, 1 ) ne '#';
Branch: 1 branch
BranchTrueFalseTotal
if $rest ne "" and substr($rest, 0, 1) ne "#" 4 128 132
Condition100%3/3
$rest ne "" and substr($rest, 0, 1) ne "#"
!ll&&!rl&&r
12444
MC/DC100%2/2
$rest ne "" and substr($rest, 0, 1) ne "#"
$rest ne "" substr($rest, 0, 1) ne "#"
Truth table100%3/3
A and B
A= $rest ne ""B= substr($rest, 0, 1) ne "#"
ABresult
0X0
100
111
341 }
342  
343 2530 ▶sub _char { $_[1] < length $_[0] ? substr( $_[0], $_[1], 1 ) : '' }
Subroutine _char: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] < length $_[0] ? : 2522 8 2530
344  
345 # A scalar inside a flow collection: quoted, or plain up to `,` or the closing bracket.
346 146 ▶sub _flow_scalar {
Subroutine _flow_scalar: called CC=10 SCAR=23.1
347 146 my ( $s, $i, $closer, $line ) = @_;
348 146 ▶ $i++ while _char( $s, $i ) eq ' ' || _char( $s, $i ) eq "\t";
Branch: 1 branch
BranchTrueFalseTotal
_char($s, $i) eq " " || _char($s, $i) eq "\t" and do { ++$i } 51 146 197
Condition66%2/3
_char($s, $i) eq " " or _char($s, $i) eq "\t"
l!l&&r!l&&!r
510146
MC/DC50%1/2
_char($s, $i) eq " " or _char($s, $i) eq "\t"
_char($s, $i) eq " " _char($s, $i) eq "\t"
Truth table66%2/3
A or B
A= _char($s, $i) eq " "B= _char($s, $i) eq "\t"
ABresult
1X1
011
000
349 146 my $c = _char( $s, $i );
350 146 ▶ return _quoted( $s, $i, $line ) if $c eq '"' || $c eq "'";
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "\"" or $c eq "'" 27 119 146
Condition66%2/3
$c eq "\"" or $c eq "'"
l!l&&r!l&&!r
270119
MC/DC50%1/2
$c eq "\"" or $c eq "'"
$c eq "\"" $c eq "'"
Truth table66%2/3
A or B
A= $c eq "\""B= $c eq "'"
ABresult
1X1
011
000
351 119 ▶ _fail( $line, 'nested collections are not supported' ) if $c eq '[' || $c eq '{';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "[" or $c eq "{" 4 115 119
Condition66%2/3
$c eq "[" or $c eq "{"
l!l&&r!l&&!r
40115
MC/DC50%1/2
$c eq "[" or $c eq "{"
$c eq "[" $c eq "{"
Truth table66%2/3
A or B
A= $c eq "["B= $c eq "{"
ABresult
1X1
011
000
352 115 my $j = $i;
353 115 ▶ $j++ while $j < length $s && _char( $s, $j ) ne ',' && _char( $s, $j ) ne $closer;
Branch: 1 branch
BranchTrueFalseTotal
$j < length $s and _char($s, $j) ne "," and _char($s, $j) ne $closer and do { ++$j } 428 115 543
Condition100%6/6
$j < length $s and _char($s, $j) ne "," and _char($s, $j) ne $closer
!ll&&!rl&&r
3976428
$j < length $s and _char($s, $j) ne ","
!ll&&!rl&&r
237504
MC/DC100%3/3
$j < length $s and _char($s, $j) ne "," and _char($s, $j) ne $closer
$j < length $s _char($s, $j) ne "," _char($s, $j) ne $closer
Truth table100%4/4
A and B and C
A= $j < length $sB= _char($s, $j) ne ","C= _char($s, $j) ne $closer
ABCresult
0XX0
10X0
1100
1111
354 115 return ( _typed_plain( js_trim( substr $s, $i, $j - $i ) ), $j );
355 }
356  
357 142 ▶sub _flow_separator {
Subroutine _flow_separator: called CC=5 SCAR=16.6
358 142 my ( $s, $j, $closer, $line ) = @_;
359 142 ▶ $j++ while _char( $s, $j ) eq ' ' || _char( $s, $j ) eq "\t";
Branch: 1 branch
BranchTrueFalseTotal
_char($s, $j) eq " " || _char($s, $j) eq "\t" and do { ++$j } 0 142 142
Condition33%1/3
_char($s, $j) eq " " or _char($s, $j) eq "\t"
l!l&&r!l&&!r
00142
MC/DC0%0/2
_char($s, $j) eq " " or _char($s, $j) eq "\t"
_char($s, $j) eq " " _char($s, $j) eq "\t"
Truth table33%1/3
A or B
A= _char($s, $j) eq " "B= _char($s, $j) eq "\t"
ABresult
1X1
011
000
360 142 ▶ return ( 0, $j + 1 ) if _char( $s, $j ) eq ',';
Branch: 1 branch
BranchTrueFalseTotal
if _char($s, $j) eq "," 40 102 142
361 102 ▶ return ( 1, $j + 1 ) if _char( $s, $j ) eq $closer;
Branch: 1 branch
BranchTrueFalseTotal
if _char($s, $j) eq $closer 100 2 102
362 2 _fail( $line, 'expected "," or "'.$closer.'"' );
363 }
364  
365 449 ▶sub _inline {
Subroutine _inline: called CC=14 SCAR=26.4
366 449 my ( $value, $line ) = @_;
367 449 my $s = js_trim($value);
368 449 ▶ _fail( $line, 'anchors, aliases and tags are not supported' ) if $s =~ /\A[&*!]/;
Branch: 1 branch
BranchTrueFalseTotal
if $s =~ /\A[&*!]/ 12 437 449
369 437 my $c = _char( $s, 0 );
370 437 ▶ if ( $c eq '"' || $c eq "'" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\"" or $c eq "'") 38 399 437
Condition100%3/3
$c eq "\"" or $c eq "'"
l!l&&r!l&&!r
2612399
MC/DC100%2/2
$c eq "\"" or $c eq "'"
$c eq "\"" $c eq "'"
Truth table100%3/3
A or B
A= $c eq "\""B= $c eq "'"
ABresult
1X1
011
000
371 38 my ( $v, $j ) = _quoted( $s, 0, $line );
372 27 _assert_end( $s, $j, $line );
373 25 return $v;
374 }
375 399 ▶ if ( $c eq '[' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "[") 97 302 399
376 97 my @arr;
377 97 ▶ if ( $s =~ /\A\[$WS*\]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\A\[$WS*\]/) 2 95 97
378 2 _assert_end( $s, index( $s, ']' ) + 1, $line );
379 2 return \@arr;
380 }
381 95 my $j = 1;
382 95 for ( ;; ) {
383 127 my ( $v, $k ) = _flow_scalar( $s, $j, ']', $line );
384 123 push @arr, $v;
385 123 my ( $done, $n ) = _flow_separator( $s, $k, ']', $line );
386 121 $j = $n;
387 121 ▶ last if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 89 32 121
388 }
389 89 _assert_end( $s, $j, $line );
390 89 return \@arr;
391 }
392 302 ▶ if ( $c eq '{' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "{") 18 284 302
393 18 my $obj = js_ordered();
394 18 ▶ if ( $s =~ /\A\{$WS*\}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\A\{$WS*\}/) 3 15 18
395 3 _assert_end( $s, index( $s, '}' ) + 1, $line );
396 1 return $obj;
397 }
398 15 my $j = 1;
399 15 for ( ;; ) {
400 23 ▶ $j++ while _char( $s, $j ) eq ' ';
Branch: 1 branch
BranchTrueFalseTotal
_char($s, $j) eq " " and do { ++$j } 8 23 31
401 23 my $colon = index $s, ':', $j;
402 23 ▶ _fail( $line, 'expected `key: value` in a flow mapping' ) if $colon == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $colon == -1 2 21 23
403 21 ( my $key = js_trim( substr $s, $j, $colon - $j ) ) =~ s/\A(["'])($DOT*)\1\z/$2/;
404 21 ▶ _fail( $line, 'bad key in a flow mapping' ) if $key eq '' || $key =~ /[,{}\[\]]/;
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "" or $key =~ /[,{}\[\]]/ 2 19 21
Condition66%2/3
$key eq "" or $key =~ /[,{}\[\]]/
l!l&&r!l&&!r
2019
MC/DC50%1/2
$key eq "" or $key =~ /[,{}\[\]]/
$key eq "" $key =~ /[,{}\[\]]/
Truth table66%2/3
A or B
A= $key eq ""B= $key =~ /[,{}\[\]]/
ABresult
1X1
011
000
405 19 my ( $v, $k ) = _flow_scalar( $s, $colon + 1, '}', $line );
406 19 $obj->{$key} = $v;
407 19 my ( $done, $n ) = _flow_separator( $s, $k, '}', $line );
408 19 $j = $n;
409 19 ▶ last if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 11 8 19
410 }
411 11 _assert_end( $s, $j, $line );
412 11 return $obj;
413 }
414 284 return _typed_plain( _strip_comment($s) );
415 }
416  
417 package Skilletor::Frontmatter::Float;
418  
419 # A YAML float: JavaScript cannot tell 1 from 1.0, TOML can (Codex agents).
420 18 ▶sub new { my ( $class, $value ) = @_; return bless { value => $value }, $class }
Subroutine new: called CC=1 SCAR=0.0
421 17 ▶sub value { $_[0]{value} }
Subroutine value: called CC=1 SCAR=0.0
422  
423 package Skilletor::Frontmatter;
424  
425 34 1;
426  
427 __END__
« Skilletor/Tar.pm ↑ Summary Skilletor/Source/Url.pm »