| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
builtin::blessed($_[0]) and $_[0]->isa("Skilletor::Frontmatter::Float")
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")
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$t eq "" or substr($t, 0, 1) eq "#"
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 "#"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$text eq "-" or substr($text, 0, 2) eq "- "
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 "- "
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$pos < @$lines and Skilletor::Frontmatter::_skippable($lines->[$pos])
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])
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 813 | my $line = $lines->[$pos]; | |||||||||||||||||||||||||||||||||||||||||
| 110 | 813 | my $ind = Skilletor::Frontmatter::_indent_of($line); | |||||||||||||||||||||||||||||||||||||||||
| 111 | 813 | ▶ | last if $ind < $indent; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 803 | ▶ | Skilletor::Frontmatter::_fail( $line, 'unexpected indentation' ) if $ind > $indent; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 757 | ▶ | my @m = $text =~ $KEY_RE or Skilletor::Frontmatter::_fail( $line, 'expected `key: value`' ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 528 | ▶ | my $key = defined $m[0] ? Skilletor::Frontmatter::_unescape_double( $m[0], $line ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 526 | ▶ | my ( $value, $next ) = $self->value( $m[3] // '', $pos + 1, $indent, $line ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$m[3] // ""
MC/DC100%1/1
$m[3] // ""
$m[3]
Truth table100%2/2
A
A= $m[3]
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 494 | ▶ | if ( $r eq '' || substr( $r, 0, 1 ) eq '#' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$r eq "" or substr($r, 0, 1) eq "#"
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 "#"
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 61 | my $q = $self->next_content($pos); | |||||||||||||||||||||||||||||||||||||||||
| 135 | 61 | ▶ | return ( undef, $q ) if $q >= @$lines; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | 39 | ▶ | return Skilletor::Frontmatter::_is_seq_item($text) ? $self->sequence( $q, $ind ) : $self->mapping( $q, $ind ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 4 | ▶ | return $self->sequence( $q, $ind ) if $ind == $indent && Skilletor::Frontmatter::_is_seq_item($text); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$ind == $indent and Skilletor::Frontmatter::_is_seq_item($text)
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$q >= @$lines or Skilletor::Frontmatter::_indent_of($lines->[$q]) <= $indent
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | 16 | ▶ | return ( Skilletor::Frontmatter::_inline( join( ' ', @parts ), $line ), $p ) if $r =~ /\A["'\[{]/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 8 | ▶ | Skilletor::Frontmatter::_fail( $line, 'anchors, aliases and tags are not supported' ) if $r =~ /\A[&*!]/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | 40 | my $line = $lines->[$pos]; | |||||||||||||||||||||||||||||||||||||||||
| 169 | 40 | my $ind = Skilletor::Frontmatter::_indent_of($line); | |||||||||||||||||||||||||||||||||||||||||
| 170 | 40 | ▶ | last if $ind < $indent; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | 34 | ▶ | Skilletor::Frontmatter::_fail( $line, 'unexpected indentation in a sequence' ) if $ind > $indent; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | 32 | my $text = substr $line->{raw}, $ind; | |||||||||||||||||||||||||||||||||||||||||
| 173 | 32 | ▶ | last unless Skilletor::Frontmatter::_is_seq_item($text); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 174 | 30 | my $item = Skilletor::JSString::js_trim( substr $text, 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 175 | 30 | ▶ | if ( $item eq '' || substr( $item, 0, 1 ) eq '#' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$item eq "" or substr($item, 0, 1) eq "#"
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 "#"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 26 | ▶ | Skilletor::Frontmatter::_fail( $line, 'nested collections are not supported' ) if $item =~ /\A[\[{]/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | 30 | my @collected; | |||||||||||||||||||||||||||||||||||||||||
| 195 | 30 | my $p = $pos; | |||||||||||||||||||||||||||||||||||||||||
| 196 | 30 | ▶ | while ( $p < @$lines ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | 86 | my $l = $lines->[$p]; | |||||||||||||||||||||||||||||||||||||||||
| 198 | 86 | ▶ | if ( Skilletor::JSString::js_trim( $l->{raw} ) eq '' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 205 | 41 | ▶ | $content_indent = $ind if $content_indent == -1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 206 | 41 | ▶ | Skilletor::Frontmatter::_fail( $l, 'block scalar line is less indented than its first line' ) if $ind < $content_indent; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | 28 | my $trailing = 0; | |||||||||||||||||||||||||||||||||||||||||
| 212 | 28 | ▶ | while ( @texts && $texts[-1] eq '' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@texts and $texts[-1] eq ""
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 ""
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | 25 | pop @texts; | |||||||||||||||||||||||||||||||||||||||||
| 214 | 25 | $trailing++; | |||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||
| 216 | 28 | my $body; | |||||||||||||||||||||||||||||||||||||||||
| 217 | 28 | ▶ | if ( !$folded ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 225 | 2 | $pending++; | |||||||||||||||||||||||||||||||||||||||||
| 226 | 2 | next; | |||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||
| 228 | 14 | ▶ | my $more = $t =~ /\A[ \t]/ ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | 14 | ▶ | if ($first) { $body = $t } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC100%2/2
$more or $prev_more
$more
$prev_more
Truth table100%3/3
A or B
A= $moreB= $prev_more
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | 22 | ▶ | return ( '', $p ) if $body eq '' && $chomp ne '+'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$body eq "" and $chomp ne "+"
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 "+"
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | 18 | ▶ | return ( $body."\n".( $chomp eq '+' ? "\n" x $trailing : '' ), $p ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition83%5/6
$s eq "" or $s eq "~" or $s =~ /\A(?:null|Null|NULL)\z/
$s eq "" or $s eq "~"
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/
| |||||||||||||||||||||||||||||||||||||||||||
| 255 | 388 | ▶ | return JSON::MaybeXS::true() if $s =~ /\A(?:true|True|TRUE)\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | 375 | ▶ | return JSON::MaybeXS::false() if $s =~ /\A(?:false|False|FALSE)\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | 371 | ▶ | if ( $s =~ /\A[-+]?([0-9]+)\z/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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'
length $digits == 16 and $digits le "9007199254740991"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | 75 | ▶ | return $safe ? 0 + $s : $s; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||||||||||||
| 263 | 296 | ▶ | if ( $s =~ /\A[-+]?(?:\.[0-9]+|[0-9]+\.[0-9]*|[0-9]+(?=[eE]))(?:[eE][-+]?[0-9]+)?\z/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 275 | 515 | my $ch = substr $s, $i, 1; | |||||||||||||||||||||||||||||||||||||||||
| 276 | 515 | ▶ | if ( $ch ne '\\' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 284 | 36 | ▶ | if ($hex_len) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 311 | 12 | my $out = ''; | |||||||||||||||||||||||||||||||||||||||||
| 312 | 12 | for ( ;; ) { | |||||||||||||||||||||||||||||||||||||||||
| 313 | 134 | ▶ | _fail( $line, 'unterminated single-quoted string' ) if $j >= $n; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 314 | 134 | ▶ | if ( substr( $s, $j, 1 ) eq "'" ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 315 | 16 | ▶ | if ( $j + 1 < $n && substr( $s, $j + 1, 1 ) eq "'" ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$j + 1 < $n and substr($s, $j + 1, 1) eq "'"
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 "'"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 327 | 636 | my $c = substr $s, $j, 1; | |||||||||||||||||||||||||||||||||||||||||
| 328 | 636 | ▶ | if ( $c eq '\\' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 340 | 132 | ▶ | _fail( $line, 'unexpected text after a value: "'.$rest.'"' ) if $rest ne '' && substr( $rest, 0, 1 ) ne '#'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$rest ne "" and substr($rest, 0, 1) ne "#"
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 "#"
| |||||||||||||||||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||
| 343 | 2530 | ▶ | sub _char { $_[1] < length $_[0] ? substr( $_[0], $_[1], 1 ) : '' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_char: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
_char($s, $i) eq " " or _char($s, $i) eq "\t"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 349 | 146 | my $c = _char( $s, $i ); | |||||||||||||||||||||||||||||||||||||||||
| 350 | 146 | ▶ | return _quoted( $s, $i, $line ) if $c eq '"' || $c eq "'"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$c eq "\"" or $c eq "'"
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 "'"
| |||||||||||||||||||||||||||||||||||||||||||
| 351 | 119 | ▶ | _fail( $line, 'nested collections are not supported' ) if $c eq '[' || $c eq '{'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$c eq "[" or $c eq "{"
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 "{"
| |||||||||||||||||||||||||||||||||||||||||||
| 352 | 115 | my $j = $i; | |||||||||||||||||||||||||||||||||||||||||
| 353 | 115 | ▶ | $j++ while $j < length $s && _char( $s, $j ) ne ',' && _char( $s, $j ) ne $closer; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$j < length $s and _char($s, $j) ne "," and _char($s, $j) ne $closer
$j < length $s and _char($s, $j) ne ","
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
_char($s, $j) eq " " or _char($s, $j) eq "\t"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 360 | 142 | ▶ | return ( 0, $j + 1 ) if _char( $s, $j ) eq ','; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 361 | 102 | ▶ | return ( 1, $j + 1 ) if _char( $s, $j ) eq $closer; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 369 | 437 | my $c = _char( $s, 0 ); | |||||||||||||||||||||||||||||||||||||||||
| 370 | 437 | ▶ | if ( $c eq '"' || $c eq "'" ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$c eq "\"" or $c eq "'"
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 "'"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 376 | 97 | my @arr; | |||||||||||||||||||||||||||||||||||||||||
| 377 | 97 | ▶ | if ( $s =~ /\A\[$WS*\]/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||||||||||||||||
| 389 | 89 | _assert_end( $s, $j, $line ); | |||||||||||||||||||||||||||||||||||||||||
| 390 | 89 | return \@arr; | |||||||||||||||||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||||||||||||||||
| 392 | 302 | ▶ | if ( $c eq '{' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 393 | 18 | my $obj = js_ordered(); | |||||||||||||||||||||||||||||||||||||||||
| 394 | 18 | ▶ | if ( $s =~ /\A\{$WS*\}/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 401 | 23 | my $colon = index $s, ':', $j; | |||||||||||||||||||||||||||||||||||||||||
| 402 | 23 | ▶ | _fail( $line, 'expected `key: value` in a flow mapping' ) if $colon == -1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$key eq "" or $key =~ /[,{}\[\]]/
MC/DC50%1/2
$key eq "" or $key =~ /[,{}\[\]]/
$key eq ""
$key =~ /[,{}\[\]]/
Truth table66%2/3
A or B
A= $key eq ""B= $key =~ /[,{}\[\]]/
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||