blib/lib/DMS/Emitter.pm

filter: Statementstmt 49.6% 256 / 516 Branchbran 31.2% 90 / 288 Conditioncond 35.2% 43 / 122 MC/DCmcdc 17.4% 11 / 63 Subroutinesub 79.4% 27 / 34 Podpod 0.0% 0 / 4 totaltotal 41.5% 427 / 1027 CC 215 SCAR 46.6
CC
215
Cov
42%
_is_flow_safe
420.0
_emit_value_inline
242.7
_emit_table_block
167.9
CRAP 105.1
« blib/lib/DMS/XS/encoder.pl ↑ Summary blib/lib/DMS/XS/Parser.pm »
1 package DMS::Emitter;
2 # DMS encode emitter — re-emit a parsed Document as DMS source.
3 #
4 # Mirrors the Rust reference (language/rust/crates/dms/src/lib.rs::encode).
5 # Pure Perl; no parser dependency at parse time. Used by both
6 # DMS::Parser::encode and DMS::XS::Parser::encode — the document shape
7 # is identical between the two backends, so the emitter walks them the
8 # same way.
9 #
10 # Contract (SPEC §encode):
11 # decode(encode(decode(source))) is data-equivalent to decode(source),
12 # has the same comments at the same attached paths, and uses the same
13 # literal forms for values where preserved (integer base, string form).
14 #
15 # Round-trip stability:
16 # encode(decode(encode(decode(source)))) is byte-equal to encode(decode(source)).
17 #
18 # SPEC v0.14 renamed to_dms/to_dms_lite to encode/encode_lite. The old
19 # names remain as deprecated aliases for one release.
20 #
21 # A Document is a hashref:
22 # { meta => undef|tied-hash, body => value, comments => [...],
23 # original_forms => [ [path_aref, lit_href], ... ] }
24
25 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use POSIX qw(isnan isinf);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
31
32 1 our $VERSION = '0.5.0';
33
34 1 my $INDENT_STR = ' ';
35
36 # Public entry point. $doc is the decode_document return value.
37 # SPEC §encode (v0.14): renamed from to_dms() to encode().
38 19 ▶sub encode {
Subroutine encode: called CC=1 SCAR=0.0
Pod undocumented
39 19 my ($doc) = @_;
40 19 return _emit($doc, 0);
41 }
42
43 # Lite-mode entry point — emits canonical DMS source with no comments
44 # and no original-form preservation (decimal integers, basic-quoted
45 # strings). Mirrors the Rust reference's `encode_lite`. SPEC §encode.
46 #
47 # `decode(encode_lite(doc))` is data-equivalent to `doc`; it is
48 # *not* required to round-trip comments or literal forms.
49 0 ▶sub encode_lite {
Subroutine encode_lite: not called CC=1 SCAR=6.9
Pod undocumented
50 0 my ($doc) = @_;
51 0 return _emit($doc, 1);
52 }
53
54 # Deprecated aliases (SPEC v0.14 renamed to_dms/to_dms_lite to
55 # encode/encode_lite). Kept for one release. Carp once per process to
56 # avoid flooding callers that loop.
57 1 { my $warned;
58 0 ▶ sub to_dms {
Subroutine to_dms: not called CC=2 SCAR=17.9
Pod undocumented
59 0 ▶ unless ($warned++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($warned++) 0 0 0
60 0 Carp::carp(
61 'DMS::Emitter::to_dms() is deprecated; use encode() instead. '
62 . 'SPEC v0.14 renamed to_dms() to encode().');
63 }
64 0 goto &encode;
65 }
66 }
67 1 { my $warned;
68 0 ▶ sub to_dms_lite {
Subroutine to_dms_lite: not called CC=2 SCAR=17.9
Pod undocumented
69 0 ▶ unless ($warned++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($warned++) 0 0 0
70 0 Carp::carp(
71 'DMS::Emitter::to_dms_lite() is deprecated; use encode_lite() instead. '
72 . 'SPEC v0.14 renamed to_dms_lite() to encode_lite().');
73 }
74 0 goto &encode_lite;
75 }
76 }
77
78 1 ▶sub _emit {
Subroutine _emit: called CC=17 SCAR=31.0
79 19 my ($doc, $lite) = @_;
80 # SPEC §"Unordered tables": full-mode `encode` refuses to round-trip
81 # a Document that contains a DMS::UnorderedTable. The Document tree
82 # has no stable iteration order, so re-emission cannot be byte-stable.
83 # Lite mode is allowed (canonical emit, no order promise).
84 19 ▶ if (!$lite && _contains_unordered($doc->{body})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $lite and _contains_unordered($doc->{"body"})) 0 19 19
Condition33%1/3
not $lite and _contains_unordered($doc->{"body"})
!ll&&!rl&&r
0190
MC/DC0%0/2
not $lite and _contains_unordered($doc->{"body"})
not $lite _contains_unordered($doc->{"body"})
Truth table33%1/3
not(A) and B
A= not $liteB= _contains_unordered($doc->{"body"})
ABresult
0X0
100
111
85 0 die "encode (full-mode round-trip) refuses Document with DMS::UnorderedTable; "
86 . "use decode_document (ordered) or encode_lite. SPEC §Unordered tables.\n";
87 }
88 19 ▶ if (!$lite && defined($doc->{meta}) && _contains_unordered($doc->{meta})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $lite and defined $doc->{"meta"} and _contains_unordered($doc->{"meta"})) 0 19 19
Condition66%4/6
not $lite and defined $doc->{"meta"} and _contains_unordered($doc->{"meta"})
!ll&&!rl&&r
1630
not $lite and defined $doc->{"meta"}
!ll&&!rl&&r
0163
MC/DC0%0/3
not $lite and defined $doc->{"meta"} and _contains_unordered($doc->{"meta"})
not $lite defined $doc->{"meta"} _contains_unordered($doc->{"meta"})
Truth table50%2/4
not(A) and B and C
A= not $liteB= defined $doc->{"meta"}C= _contains_unordered($doc->{"meta"})
ABCresult
0XX0
10X0
1100
1111
89 0 die "encode (full-mode round-trip) refuses Document with DMS::UnorderedTable; "
90 . "use decode_document (ordered) or encode_lite. SPEC §Unordered tables.\n";
91 }
92 19 ▶ my $self = bless {
Branch: 1 branch
BranchTrueFalseTotal
$lite ? : 0 19 19
93 out => '',
94 comments_by_path => {}, # path-key -> { leading=>[], trailing=>scalar/undef, floating=>[] }
95 forms_by_path => {}, # path-key -> $lit
96 lite => $lite ? 1 : 0,
97 doc => $doc,
98 }, __PACKAGE__;
99 # In lite mode, the per-path comment + original-form maps stay
100 # empty: the walk then emits canonical form even when `doc.comments`
101 # / `doc.original_forms` are populated. Mirrors the Rust emitter's
102 # `new_lite` constructor.
103 19 ▶ if (!$self->{lite}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"lite"}) 19 0 19
104 # Bucket comments + original_forms by path-key (joined string).
105 19 ▶ for my $ac (@{ $doc->{comments} || [] }) {
Condition50%1/2
$doc->{'comments'} || []
l!l
190
MC/DC0%0/1
$doc->{'comments'} || []
$doc->{'comments'}
Truth table50%1/2
A
A= $doc->{'comments'}
Aresult
11
00
106 21 my $pk = _path_key($ac->{path});
107 21 ▶ $self->{comments_by_path}{$pk} ||= { leading => [], inner => [], trailing => [], floating => [] };
Condition100%2/2
$self->{"comments_by_path"}{$pk} ||= {"leading", [], "inner", [], "trailing", [], "floating", []}
l!l
1011
MC/DC100%1/1
$self->{"comments_by_path"}{$pk} ||= {"leading", [], "inner", [], "trailing", [], "floating", []}
$self->{"comments_by_path"}{$pk}
Truth table100%2/2
A
A= $self->{"comments_by_path"}{$pk}
Aresult
11
00
108 21 my $entry = $self->{comments_by_path}{$pk};
109 21 my $pos = $ac->{position};
110 21 ▶ if ($pos eq 'leading') {
Branch: 3 branches
BranchTrueFalseTotal
if ($pos eq 'leading') { } 10 11 21
elsif ($pos eq 'inner') { } 0 11 11
elsif ($pos eq 'trailing') { } 10 1 11
111 10 push @{ $entry->{leading} }, $ac->{comment};
112 } elsif ($pos eq 'inner') {
113 0 push @{ $entry->{inner} }, $ac->{comment};
114 } elsif ($pos eq 'trailing') {
115 10 push @{ $entry->{trailing} }, $ac->{comment};
116 } else {
117 1 push @{ $entry->{floating} }, $ac->{comment};
118 }
119 }
120 19 ▶ for my $pair (@{ $doc->{original_forms} || [] }) {
Condition50%1/2
$doc->{'original_forms'} || []
l!l
190
MC/DC0%0/1
$doc->{'original_forms'} || []
$doc->{'original_forms'}
Truth table50%1/2
A
A= $doc->{'original_forms'}
Aresult
11
00
121 7 my ($p, $lit) = @$pair;
122 7 my $pk = _path_key($p);
123 7 ▶ $self->{forms_by_path}{$pk} = $lit if !exists $self->{forms_by_path}{$pk};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"forms_by_path"}{$pk} 7 0 7
124 }
125 }
126
127 19 $self->_emit_document;
128 19 return $self->{out};
129 }
130
131 # --- path-key encoding ---
132 # A path is an arrayref of plain string segments (table keys) and
133 # DMS::Index objects (list indices). We encode each segment with a
134 # distinct prefix so the join is unambiguous (a string key "0" doesn't
135 # collide with index 0).
136 295 ▶sub _path_key {
Subroutine _path_key: called CC=2 SCAR=6.9
137 295 my ($p) = @_;
138 return join("\0", map {
139 295 ▶ ref($_) eq 'DMS::Index' ? "I:" . $$_ : "K:$_"
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'DMS::Index' ? : 30 316 346
140 } @$p);
141 }
142
143 19 ▶sub _emit_document {
Subroutine _emit_document: called CC=16 SCAR=39.4
144 19 my $self = shift;
145 19 my $doc = $self->{doc};
146
147 # Front matter: emit the `+++` block when meta is defined OR when
148 # any `__fm__`-prefixed comment exists. Spec §to_dms allows omitting
149 # an empty-meta block when no FM comments either, but emitting `+++\n+++\n`
150 # for an empty-meta doc with no FM comments would be wrong; the
151 # has_meta test below guards that.
152 # Lite mode emits no comments, so FM comments don't force a `+++`
153 # block — only an explicit `meta = Some(...)` does. Mirrors the
154 # Rust reference (lib.rs::emit_document `!self.lite && ...`).
155 19 my $has_fm_comments = 0;
156 19 ▶ if (!$self->{lite}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"lite"}) 19 0 19
157 19 ▶ for my $ac (@{ $doc->{comments} || [] }) {
Condition50%1/2
$doc->{'comments'} || []
l!l
190
MC/DC0%0/1
$doc->{'comments'} || []
$doc->{'comments'}
Truth table50%1/2
A
A= $doc->{'comments'}
Aresult
11
00
158 21 my $first = $ac->{path}[0];
159 21 ▶ if (defined($first) && !ref($first) && $first eq '__fm__') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $first and not ref $first and $first eq "__fm__") 0 21 21
Condition33%2/6
defined $first and not ref $first and $first eq "__fm__"
!ll&&!rl&&r
0210
defined $first and not ref $first
!ll&&!rl&&r
0021
MC/DC0%0/3
defined $first and not ref $first and $first eq "__fm__"
defined $first not ref $first $first eq "__fm__"
Truth table25%1/4
A and not(B) and C
A= defined $firstB= not ref $firstC= $first eq "__fm__"
ABCresult
0XX0
10X0
1100
1111
160 0 $has_fm_comments = 1; last;
161 }
162 }
163 }
164 19 my $fm_present = defined $doc->{meta};
165 19 ▶ if ($fm_present || $has_fm_comments) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fm_present or $has_fm_comments) 3 16 19
Condition66%2/3
$fm_present or $has_fm_comments
l!l&&r!l&&!r
3016
MC/DC50%1/2
$fm_present or $has_fm_comments
$fm_present $has_fm_comments
Truth table66%2/3
A or B
A= $fm_presentB= $has_fm_comments
ABresult
1X1
011
000
166 3 $self->{out} .= "+++\n";
167 3 my $fm_path = ['__fm__'];
168 3 ▶ if (defined $doc->{meta}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $doc->{'meta'}) { } 3 0 3
169 3 $self->_emit_table_block($doc->{meta}, $fm_path, 0);
170 } else {
171 0 $self->_emit_floating($fm_path, 0);
172 }
173 3 $self->{out} .= "+++\n\n";
174 }
175
176 19 my $body_path = [];
177 19 my $body = $doc->{body};
178 19 ▶ if (_is_table($body)) {
Branch: 2 branches
BranchTrueFalseTotal
if (_is_table($body)) { } 19 0 19
elsif (_is_list($body)) { } 0 0 0
179 19 $self->_emit_table_block($body, $body_path, 0);
180 } elsif (_is_list($body)) {
181 0 $self->_emit_list_block($body, $body_path, 0);
182 } else {
183 # Scalar root.
184 0 my $nc = $self->{comments_by_path}{ _path_key($body_path) };
185 0 ▶ if ($nc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nc) 0 0 0
186 0 for my $c (@{ $nc->{leading} }) {
187 0 $self->_emit_comment_line($c, 0);
188 }
189 }
190 0 $self->_emit_value_inline($body, $body_path);
191 0 $self->_emit_trailing_for($body_path);
192 0 $self->{out} .= "\n";
193 0 ▶ if ($nc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nc) 0 0 0
194 0 for my $c (@{ $nc->{floating} }) {
195 0 $self->_emit_comment_line($c, 0);
196 }
197 }
198 }
199 }
200
201 83 ▶sub _is_table {
Subroutine _is_table: called CC=3 SCAR=11.0
202 83 my ($v) = @_;
203 83 ▶ return 0 if !defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 83 83
204 # SPEC §"Unordered tables": DMS::UnorderedTable is a blessed hashref
205 # marker. Treat it as a table for emission purposes; the full-mode
206 # to_dms guard above prevents it from reaching here in round-trip.
207 83 ▶ if (blessed($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($v)) 38 45 83
208 38 return ref($v) eq 'DMS::UnorderedTable';
209 }
210 45 return ref($v) eq 'HASH';
211 }
212
213 56 ▶sub _is_list {
Subroutine _is_list: called CC=3 SCAR=11.0
214 56 my ($v) = @_;
215 56 ▶ return 0 if !defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 56 56
216 56 ▶ return 0 if blessed($v);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($v) 38 18 56
217 18 return ref($v) eq 'ARRAY';
218 }
219
220 28 ▶sub _table_keys {
Subroutine _table_keys: called CC=3 SCAR=12.3
221 28 my ($t) = @_;
222 # Fast path: when the table is a Tie::IxHash, bypass tie magic and
223 # read the keys AV directly. The tied object is a blessed arrayref
224 # `[idx_hv_rv, keys_av_rv, vals_av_rv, iter]` per Tie::IxHash's
225 # documented internal shape. `keys %$t` works too but goes through
226 # every entry's mg_find — measurably slower on a 5000-pair tree.
227 28 my $tied = tied(%$t);
228 28 ▶ if ($tied && ref($tied) eq 'Tie::IxHash') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tied and ref $tied eq "Tie::IxHash") 28 0 28
Condition33%1/3
$tied and ref $tied eq "Tie::IxHash"
!ll&&!rl&&r
0028
MC/DC0%0/2
$tied and ref $tied eq "Tie::IxHash"
$tied ref $tied eq "Tie::IxHash"
Truth table33%1/3
A and B
A= $tiedB= ref $tied eq "Tie::IxHash"
ABresult
0X0
100
111
229 28 return @{ $tied->[1] };
230 }
231 # DMS::UnorderedTable (plain blessed hash) or plain HV: arbitrary
232 # iteration order is the documented contract for lite mode.
233 0 return keys %$t;
234 }
235
236 # Recursive walk: returns true if any nested table (or the value itself)
237 # is a DMS::UnorderedTable. Used by `to_dms` (full mode) to refuse
238 # round-trip on unordered Documents per SPEC §"Unordered tables".
239 73 ▶sub _contains_unordered {
Subroutine _contains_unordered: called CC=11 SCAR=24.4
240 73 my ($v) = @_;
241 73 ▶ return 0 if !defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 73 73
242 73 ▶ if (blessed($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($v)) 38 35 73
243 # DMS::UnorderedTable is itself the marker — we don't need to
244 # walk into it because finding the outer one is enough; any
245 # nested UnorderedTable would still be detected via the body.
246 38 ▶ return 1 if ref($v) eq 'DMS::UnorderedTable';
Branch: 1 branch
BranchTrueFalseTotal
if ref $v eq "DMS::UnorderedTable" 0 38 38
247 # Other blessed sentinels (DMS::Integer, DMS::Bool, dates,
248 # DMS::Float) are leaves.
249 38 return 0;
250 }
251 35 ▶ if (ref($v) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq "HASH") 25 10 35
252 25 for my $k (keys %$v) {
253 45 ▶ next if $k eq "\0_keys";
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "\0_keys" 0 45 45
254 45 ▶ return 1 if _contains_unordered($v->{$k});
Branch: 1 branch
BranchTrueFalseTotal
if _contains_unordered($v->{$k}) 0 45 45
255 }
256 25 return 0;
257 }
258 10 ▶ if (ref($v) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq "ARRAY") 2 8 10
259 2 for my $item (@$v) {
260 6 ▶ return 1 if _contains_unordered($item);
Branch: 1 branch
BranchTrueFalseTotal
if _contains_unordered($item) 0 6 6
261 }
262 2 return 0;
263 }
264 8 return 0;
265 }
266
267 25 ▶sub _emit_table_block {
Subroutine _emit_table_block: called CC=28 SCAR=51.2
268 25 my ($self, $t, $path, $indent) = @_;
269 # Lite-mode hot path: no comments, no original_forms, so the per-kvpair
270 # path/path-key construction and comment-map lookups are pure overhead.
271 # Skipping them saves ~35% of emit wall time on a 5000-kvpair Helm
272 # chart values.yaml fixture (the path-building inner-array copy plus
273 # _path_key string-join was the dominant cost).
274 25 ▶ if ($self->{lite}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"lite"}) 0 25 25
275 0 my $pad = $INDENT_STR x $indent;
276 0 for my $k (_table_keys($t)) {
277 0 my $v = $t->{$k};
278 0 my $r = ref($v);
279 # Inline the common scalar cases. The realistic fixture is
280 # >90% plain-string + a few bool/int values; dispatching to
281 # _emit_value_inline + _emit_string per kvpair was the
282 # dominant cost after the path-key skip.
283 0 ▶ if (!$r) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r) 0 0 0
284 0 ▶ my $key_fmt = ($k =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/)
Branch: 1 branch
BranchTrueFalseTotal
$k =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/ ? : 0 0 0
285 ? $k : _format_key($k);
286 0 ▶ if ($v !~ /[\\"\x00-\x1F]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $v =~ /[\\"\x00-\x1F]/) { } 0 0 0
287 0 $self->{out} .= "${pad}${key_fmt}: \"${v}\"\n";
288 } else {
289 0 $self->{out} .= "${pad}${key_fmt}: \"" . _escape_basic($v) . "\"\n";
290 }
291 0 next;
292 }
293 0 ▶ if ($r eq 'DMS::Bool') {
Branch: 1 branch
BranchTrueFalseTotal
if ($r eq "DMS::Bool") 0 0 0
294 0 ▶ my $key_fmt = ($k =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/)
Branch: 1 branch
BranchTrueFalseTotal
$k =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/ ? : 0 0 0
295 ? $k : _format_key($k);
296 0 ▶ $self->{out} .= $pad . $key_fmt . ': '
Branch: 1 branch
BranchTrueFalseTotal
$$v ? : 0 0 0
297 . ($$v ? "true\n" : "false\n");
298 0 next;
299 }
300 0 ▶ if ($r eq 'DMS::Integer') {
Branch: 1 branch
BranchTrueFalseTotal
if ($r eq "DMS::Integer") 0 0 0
301 0 ▶ my $key_fmt = ($k =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/)
Branch: 1 branch
BranchTrueFalseTotal
$k =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/ ? : 0 0 0
302 ? $k : _format_key($k);
303 0 $self->{out} .= "${pad}${key_fmt}: ${$v}\n";
304 0 next;
305 }
306 0 ▶ my $can_block =
Condition0%0/7
_is_table($v) && scalar _table_keys($v) || _is_list($v) && scalar @$v
l!l&&r!l&&!r
000
_is_table($v) and scalar _table_keys($v)
!ll
00
_is_list($v) and scalar @$v
!ll
00
MC/DC0%0/2
_is_table($v) && scalar _table_keys($v) || _is_list($v) && scalar @$v
_is_table($v) _is_list($v)
Truth table0%0/3
A || B
A= _is_table($v)B= _is_list($v)
ABresult
1X1
011
000
307 (_is_table($v) && scalar(_table_keys($v))) ||
308 (_is_list($v) && scalar(@$v));
309 0 $self->{out} .= $pad;
310 0 $self->{out} .= _format_key($k);
311 0 $self->{out} .= ':';
312 0 ▶ if ($can_block) {
Branch: 1 branch
BranchTrueFalseTotal
if ($can_block) { } 0 0 0
313 0 $self->{out} .= "\n";
314 0 ▶ if (_is_table($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_table($v)) { } 0 0 0
315 0 $self->_emit_table_block($v, undef, $indent + 1);
316 } else {
317 0 $self->_emit_list_block($v, undef, $indent + 1);
318 }
319 } else {
320 0 $self->{out} .= ' ';
321 0 $self->_emit_value_inline($v, undef);
322 0 $self->{out} .= "\n";
323 }
324 }
325 0 return;
326 }
327 25 for my $k (_table_keys($t)) {
328 45 my $v = $t->{$k};
329 45 my $child_path = [ @$path, $k ];
330 45 my $child_pk = _path_key($child_path);
331 45 my $nc = $self->{comments_by_path}{$child_pk};
332 45 ▶ if ($nc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nc) 9 36 45
333 9 for my $c (@{ $nc->{leading} }) {
334 8 $self->_emit_comment_line($c, $indent);
335 }
336 }
337 45 ▶ my $has_trailing = $nc && @{ $nc->{trailing} };
Condition100%3/3
$nc && @{$nc->{'trailing'};}
!ll&&!rl&&r
3618
MC/DC100%2/2
$nc && @{$nc->{'trailing'};}
$nc @{$nc->{'trailing'};}
Truth table100%3/3
A && B
A= $ncB= @{$nc->{'trailing'};}
ABresult
0X0
100
111
338 45 my $has_inner = $self->_has_inner($child_path);
339 45 ▶ my $can_block =
Condition100%7/7
_is_table($v) && scalar _table_keys($v) || _is_list($v) && scalar @$v
l!l&&r!l&&!r
3240
_is_table($v) and scalar _table_keys($v)
!ll
423
_is_list($v) and scalar @$v
!ll
402
MC/DC100%2/2
_is_table($v) && scalar _table_keys($v) || _is_list($v) && scalar @$v
_is_table($v) _is_list($v)
Truth table100%3/3
A || B
A= _is_table($v)B= _is_list($v)
ABresult
1X1
011
000
340 (_is_table($v) && scalar(_table_keys($v))) ||
341 (_is_list($v) && scalar(@$v));
342 45 ▶ my $needs_block = $can_block && !($has_trailing && $self->_is_flow_safe($v, $child_path));
Condition50%3/6
$can_block && !($has_trailing && $self->_is_flow_safe($v, $child_path))
!ll&&!rl&&r
4005
$has_trailing && $self->_is_flow_safe($v, $child_path)
!ll&&!rl&&r
500
MC/DC33%1/3
$can_block && !($has_trailing && $self->_is_flow_safe($v, $child_path))
$can_block $has_trailing $self->_is_flow_safe($v, $child_path)
Truth table50%2/4
A && not(B && C)
A= $can_blockB= $has_trailingC= $self->_is_flow_safe($v, $child_path)
ABCresult
0XX0
1110
10X1
1101
343 45 $self->{out} .= $INDENT_STR x $indent;
344 45 $self->{out} .= _format_key($k);
345 45 $self->{out} .= ':';
346 45 ▶ if ($needs_block) {
Branch: 1 branch
BranchTrueFalseTotal
if ($needs_block) { } 5 40 45
347 5 ▶ if ($has_inner) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_inner) 0 5 5
348 0 $self->{out} .= ' ';
349 0 $self->_emit_inner_for($child_path);
350 # Trim trailing space left by _emit_inner_for.
351 0 $self->{out} =~ s/ \z//;
352 }
353 5 $self->{out} .= "\n";
354 5 ▶ if (_is_table($v)) { $self->_emit_table_block($v, $child_path, $indent + 1); }
Branch: 1 branch
BranchTrueFalseTotal
if (_is_table($v)) { } 3 2 5
355 2 else { $self->_emit_list_block($v, $child_path, $indent + 1); }
356 } else {
357 40 $self->{out} .= ' ';
358 40 $self->_emit_inner_for($child_path);
359 40 $self->_emit_value_inline($v, $child_path);
360 40 $self->_emit_trailing_for($child_path);
361 40 $self->{out} .= "\n";
362 }
363 }
364 25 $self->_emit_floating($path, $indent);
365 }
366
367 2 ▶sub _emit_list_block {
Subroutine _emit_list_block: called CC=16 SCAR=45.0
368 2 my ($self, $items, $path, $indent) = @_;
369 2 ▶ if ($self->{lite}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"lite"}) 0 2 2
370 0 my $pad = $INDENT_STR x $indent;
371 0 ▶ for (my $i = 0; $i < @$items; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$items) 0 0 0
372 0 my $v = $items->[$i];
373 0 $self->{out} .= $pad;
374 0 $self->{out} .= '+';
375 0 ▶ if (_is_table($v) && scalar(_table_keys($v))) {
Branch: 2 branches
BranchTrueFalseTotal
if (_is_table($v) and scalar _table_keys($v)) { } 0 0 0
elsif (_is_list($v) and scalar @$v) { } 0 0 0
Condition0%0/2
_is_table($v) and scalar _table_keys($v)
!ll
00
MC/DC0%0/1
_is_table($v) and scalar _table_keys($v)
_is_table($v)
Truth table0%0/2
A
A= _is_table($v)
Aresult
00
11
376 0 $self->{out} .= "\n";
377 0 $self->_emit_table_block($v, undef, $indent + 1);
378 ▶ } elsif (_is_list($v) && scalar(@$v)) {
Condition0%0/2
_is_list($v) and scalar @$v
!ll
00
MC/DC0%0/1
_is_list($v) and scalar @$v
_is_list($v)
Truth table0%0/2
A
A= _is_list($v)
Aresult
00
11
379 0 $self->{out} .= "\n";
380 0 $self->_emit_list_block($v, undef, $indent + 1);
381 } else {
382 0 $self->{out} .= ' ';
383 0 $self->_emit_value_inline($v, undef);
384 0 $self->{out} .= "\n";
385 }
386 }
387 0 return;
388 }
389 2 ▶ for (my $i = 0; $i < @$items; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$items) 6 2 8
390 6 my $v = $items->[$i];
391 6 my $child_path = [ @$path, DMS::Index->new($i) ];
392 6 my $child_pk = _path_key($child_path);
393 6 my $nc = $self->{comments_by_path}{$child_pk};
394 6 ▶ if ($nc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nc) 0 6 6
395 0 for my $c (@{ $nc->{leading} }) {
396 0 $self->_emit_comment_line($c, $indent);
397 }
398 }
399 6 $self->{out} .= $INDENT_STR x $indent;
400 6 $self->{out} .= '+';
401 6 my $has_inner = $self->_has_inner($child_path);
402 6 ▶ if (_is_table($v) && scalar(_table_keys($v))) {
Branch: 2 branches
BranchTrueFalseTotal
if (_is_table($v) and scalar _table_keys($v)) { } 0 6 6
elsif (_is_list($v) and scalar @$v) { } 0 6 6
Condition50%1/2
_is_table($v) and scalar _table_keys($v)
!ll
60
MC/DC0%0/1
_is_table($v) and scalar _table_keys($v)
_is_table($v)
Truth table50%1/2
A
A= _is_table($v)
Aresult
00
11
403 0 ▶ if ($has_inner) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_inner) 0 0 0
404 0 $self->{out} .= ' ';
405 0 $self->_emit_inner_for($child_path);
406 0 $self->{out} =~ s/ \z//;
407 }
408 0 $self->_emit_trailing_for($child_path);
409 0 $self->{out} .= "\n";
410 0 $self->_emit_table_block($v, $child_path, $indent + 1);
411 ▶ } elsif (_is_list($v) && scalar(@$v)) {
Condition50%1/2
_is_list($v) and scalar @$v
!ll
60
MC/DC0%0/1
_is_list($v) and scalar @$v
_is_list($v)
Truth table50%1/2
A
A= _is_list($v)
Aresult
00
11
412 0 ▶ if ($has_inner) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_inner) 0 0 0
413 0 $self->{out} .= ' ';
414 0 $self->_emit_inner_for($child_path);
415 0 $self->{out} =~ s/ \z//;
416 }
417 0 $self->_emit_trailing_for($child_path);
418 0 $self->{out} .= "\n";
419 0 $self->_emit_list_block($v, $child_path, $indent + 1);
420 } else {
421 6 $self->{out} .= ' ';
422 6 $self->_emit_inner_for($child_path);
423 6 $self->_emit_value_inline($v, $child_path);
424 6 $self->_emit_trailing_for($child_path);
425 6 $self->{out} .= "\n";
426 }
427 }
428 2 $self->_emit_floating($path, $indent);
429 }
430
431 46 ▶sub _emit_value_inline {
Subroutine _emit_value_inline: called CC=21 SCAR=54.9
432 46 my ($self, $v, $path) = @_;
433 46 ▶ if (blessed($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($v)) 38 8 46
434 38 my $cls = ref($v);
435 0 ▶ if ($cls eq 'DMS::Bool') { $self->{out} .= $v->value ? 'true' : 'false'; }
Branch: 5 branches
BranchTrueFalseTotal
if ($cls eq 'DMS::Bool') { } 0 38 38
elsif ($cls eq 'DMS::Integer') { } 38 0 38
elsif ($cls eq 'DMS::Float') { } 0 0 0
elsif ($cls eq 'DMS::OffsetDateTime' or $cls eq 'DMS::LocalDateTime' or $cls eq 'DMS::LocalDate' or $cls eq 'DMS::LocalTime') { } 0 0 0
$v->value ? : 0 0 0
436 38 elsif ($cls eq 'DMS::Integer') { $self->_emit_integer($v, $path); }
437 0 elsif ($cls eq 'DMS::Float') { $self->_emit_float($v->value); }
438 ▶ elsif ($cls eq 'DMS::OffsetDateTime'
Condition0%0/9
$cls eq "DMS::OffsetDateTime" or $cls eq "DMS::LocalDateTime" or $cls eq "DMS::LocalDate" or $cls eq "DMS::LocalTime"
l!l&&r!l&&!r
000
$cls eq "DMS::OffsetDateTime" or $cls eq "DMS::LocalDateTime" or $cls eq "DMS::LocalDate"
l!l&&r!l&&!r
000
$cls eq "DMS::OffsetDateTime" or $cls eq "DMS::LocalDateTime"
l!l&&r!l&&!r
000
MC/DC0%0/4
$cls eq "DMS::OffsetDateTime" or $cls eq "DMS::LocalDateTime" or $cls eq "DMS::LocalDate" or $cls eq "DMS::LocalTime"
$cls eq "DMS::OffsetDateTime" $cls eq "DMS::LocalDateTime" $cls eq "DMS::LocalDate" $cls eq "DMS::LocalTime"
Truth table0%0/5
A or B or C or D
A= $cls eq "DMS::OffsetDateTime"B= $cls eq "DMS::LocalDateTime"C= $cls eq "DMS::LocalDate"D= $cls eq "DMS::LocalTime"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
439 || $cls eq 'DMS::LocalDateTime'
440 || $cls eq 'DMS::LocalDate'
441 0 || $cls eq 'DMS::LocalTime') { $self->{out} .= $v->value; }
442 0 else { die "to_dms: unknown blessed class $cls"; }
443 38 return;
444 }
445 8 ▶ if (_is_list($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_list($v)) 0 8 8
446 0 ▶ if (!@$v) { $self->{out} .= '[]'; return; }
Branch: 1 branch
BranchTrueFalseTotal
unless (@$v) 0 0 0
447 0 $self->{out} .= '[';
448 0 my $lite = $self->{lite};
449 0 ▶ for (my $i = 0; $i < @$v; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$v) 0 0 0
450 0 ▶ $self->{out} .= ', ' if $i > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $i > 0 0 0 0
451 0 ▶ my $sub = $lite ? undef : [ @$path, DMS::Index->new($i) ];
Branch: 1 branch
BranchTrueFalseTotal
$lite ? : 0 0 0
452 0 $self->_emit_value_inline($v->[$i], $sub);
453 }
454 0 $self->{out} .= ']';
455 0 return;
456 }
457 8 ▶ if (_is_table($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_table($v)) 0 8 8
458 0 my @keys = _table_keys($v);
459 0 ▶ if (!@keys) { $self->{out} .= '{}'; return; }
Branch: 1 branch
BranchTrueFalseTotal
unless (@keys) 0 0 0
460 0 $self->{out} .= '{';
461 0 my $first = 1;
462 0 my $lite = $self->{lite};
463 0 for my $k (@keys) {
464 0 ▶ $self->{out} .= ', ' unless $first;
Branch: 1 branch
BranchTrueFalseTotal
unless $first 0 0 0
465 0 $first = 0;
466 0 $self->{out} .= _format_key($k);
467 0 $self->{out} .= ': ';
468 0 ▶ my $sub = $lite ? undef : [ @$path, $k ];
Branch: 1 branch
BranchTrueFalseTotal
$lite ? : 0 0 0
469 0 $self->_emit_value_inline($v->{$k}, $sub);
470 }
471 0 $self->{out} .= '}';
472 0 return;
473 }
474 # Plain scalar = string.
475 0 ▶ if (!defined $v) { die "to_dms: got undef value"; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $v) 0 8 8
476 8 $self->_emit_string("$v", $path);
477 }
478
479 38 ▶sub _emit_integer {
Subroutine _emit_integer: called CC=4 SCAR=14.8
480 38 my ($self, $iv, $path) = @_;
481 38 ▶ if ($self->{lite}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"lite"}) 0 38 38
482 # DMS::Integer is `bless \$v, 'DMS::Integer'` where $v is an IV.
483 # Direct deref + string concat skips the bstr() method dispatch.
484 0 $self->{out} .= "${$iv}";
485 0 return;
486 }
487 38 my $lit_ref = $self->{forms_by_path}{ _path_key($path) };
488 38 ▶ if ($lit_ref && exists $lit_ref->{integer_lit}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lit_ref and exists $lit_ref->{"integer_lit"}) 5 33 38
Condition66%2/3
$lit_ref and exists $lit_ref->{"integer_lit"}
!ll&&!rl&&r
3305
MC/DC50%1/2
$lit_ref and exists $lit_ref->{"integer_lit"}
$lit_ref exists $lit_ref->{"integer_lit"}
Truth table66%2/3
A and B
A= $lit_refB= exists $lit_ref->{"integer_lit"}
ABresult
0X0
100
111
489 5 $self->{out} .= $lit_ref->{integer_lit};
490 5 return;
491 }
492 # Default: canonical decimal. DMS::Integer's bstr stringifies the IV.
493 33 $self->{out} .= $iv->bstr;
494 }
495
496 0 ▶sub _emit_float {
Subroutine _emit_float: not called CC=7 SCAR=40.3
497 0 my ($self, $f) = @_;
498 0 ▶ if (isnan($f)) { $self->{out} .= 'nan'; return; }
Branch: 1 branch
BranchTrueFalseTotal
if (isnan($f)) 0 0 0
499 0 ▶ if (isinf($f)) { $self->{out} .= ($f > 0 ? 'inf' : '-inf'); return; }
Branch: 2 branches
BranchTrueFalseTotal
if (isinf($f)) 0 0 0
$f > 0 ? : 0 0 0
500 # ryu-shortest equivalent: try increasing %.Ng until round-trip works.
501 0 for my $p (1..17) {
502 0 my $s = sprintf("%.${p}g", $f);
503 0 ▶ if (0 + $s == $f) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 + $s == $f) 0 0 0
504 0 $s =~ s/e\+/e/;
505 0 $s =~ s/e-0+(\d)/e-$1/;
506 0 $s =~ s/e0+(\d)/e$1/;
507 0 ▶ if ($s !~ /[.eE]/) { $s .= '.0'; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($s =~ /[.eE]/) 0 0 0
508 0 $self->{out} .= $s;
509 0 return;
510 }
511 }
512 0 $self->{out} .= sprintf("%.17g", $f);
513 }
514
515 8 ▶sub _emit_string {
Subroutine _emit_string: called CC=13 SCAR=29.1
516 8 my ($self, $s, $path) = @_;
517 8 ▶ if ($self->{lite}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"lite"}) 0 8 8
518 0 $self->{out} .= '"';
519 0 $self->{out} .= _escape_basic($s);
520 0 $self->{out} .= '"';
521 0 return;
522 }
523 8 my $lit_ref = $self->{forms_by_path}{ _path_key($path) };
524 8 my $form;
525 8 ▶ if ($lit_ref && exists $lit_ref->{string_form}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lit_ref and exists $lit_ref->{"string_form"}) 2 6 8
Condition66%2/3
$lit_ref and exists $lit_ref->{"string_form"}
!ll&&!rl&&r
602
MC/DC50%1/2
$lit_ref and exists $lit_ref->{"string_form"}
$lit_ref exists $lit_ref->{"string_form"}
Truth table66%2/3
A and B
A= $lit_refB= exists $lit_ref->{"string_form"}
ABresult
0X0
100
111
526 2 $form = $lit_ref->{string_form};
527 }
528 8 ▶ if (!$form || $form->{kind} eq 'basic') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $form or $form->{"kind"} eq "basic") 6 2 8
Condition66%2/3
not $form or $form->{"kind"} eq "basic"
l!l&&r!l&&!r
602
MC/DC50%1/2
not $form or $form->{"kind"} eq "basic"
not $form $form->{"kind"} eq "basic"
Truth table66%2/3
not(A) or B
A= not $formB= $form->{"kind"} eq "basic"
ABresult
1X1
011
000
529 6 $self->{out} .= '"';
530 6 $self->{out} .= _escape_basic($s);
531 6 $self->{out} .= '"';
532 6 return;
533 }
534 2 ▶ if ($form->{kind} eq 'literal') {
Branch: 1 branch
BranchTrueFalseTotal
if ($form->{"kind"} eq "literal") 1 1 2
535 1 $self->{out} .= "'";
536 1 $self->{out} .= $s;
537 1 $self->{out} .= "'";
538 1 return;
539 }
540 1 ▶ if ($form->{kind} eq 'heredoc') {
Branch: 1 branch
BranchTrueFalseTotal
if ($form->{"kind"} eq "heredoc") 1 0 1
541 # The stored body is post-modifier. For idempotent modifiers this
542 # is fine, but `_fold_paragraphs` joins lines within a paragraph
543 # with spaces — so we pre-expand each `\n` to `\n\n` so the
544 # re-applied modifier preserves line boundaries on round-trip.
545 1 my $body = $s;
546 1 my $has_fold = 0;
547 1 ▶ for my $m (@{ $form->{modifiers} || [] }) {
Condition50%1/2
$form->{'modifiers'} || []
l!l
10
MC/DC0%0/1
$form->{'modifiers'} || []
$form->{'modifiers'}
Truth table50%1/2
A
A= $form->{'modifiers'}
Aresult
11
00
548 0 ▶ if ($m->{name} eq '_fold_paragraphs') { $has_fold = 1; last; }
Branch: 1 branch
BranchTrueFalseTotal
if ($m->{"name"} eq "_fold_paragraphs") 0 0 0
549 }
550 1 ▶ if ($has_fold) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_fold) 0 1 1
551 0 $body =~ s/\n/\n\n/g;
552 }
553 1 ▶ $self->_emit_heredoc($body, $form->{flavor}, $form->{label}, $form->{modifiers} || []);
Condition50%1/2
$form->{'modifiers'} || []
l!l
10
MC/DC0%0/1
$form->{'modifiers'} || []
$form->{'modifiers'}
Truth table50%1/2
A
A= $form->{'modifiers'}
Aresult
11
00
554 1 return;
555 }
556 0 die "to_dms: unknown string form: $form->{kind}";
557 }
558
559 1 ▶sub _emit_heredoc {
Subroutine _emit_heredoc: called CC=15 SCAR=37.0
560 1 my ($self, $body, $flavor, $label, $modifiers) = @_;
561 # Compute the kvpair's indent from the most recent newline in $self->{out}.
562 1 my $bytes = $self->{out};
563 1 my $last_nl = rindex($bytes, "\n");
564 1 ▶ my $line_start = $last_nl < 0 ? 0 : $last_nl + 1;
Branch: 1 branch
BranchTrueFalseTotal
$last_nl < 0 ? : 0 1 1
565 1 my $kv_indent_spaces = 0;
566 1 ▶ while ($line_start + $kv_indent_spaces < length($bytes)
Branch: 1 branch
BranchTrueFalseTotal
if ($line_start + $kv_indent_spaces < length $bytes and substr($bytes, $line_start + $kv_indent_spaces, 1) eq " ") 0 1 1
Condition33%1/3
$line_start + $kv_indent_spaces < length $bytes and substr($bytes, $line_start + $kv_indent_spaces, 1) eq " "
!ll&&!rl&&r
010
MC/DC0%0/2
$line_start + $kv_indent_spaces < length $bytes and substr($bytes, $line_start + $kv_indent_spaces, 1) eq " "
$line_start + $kv_indent_spaces < length $bytes substr($bytes, $line_start + $kv_indent_spaces, 1) eq " "
Truth table33%1/3
A and B
A= $line_start + $kv_indent_spaces < length $bytesB= substr($bytes, $line_start + $kv_indent_spaces, 1) eq " "
ABresult
0X0
100
111
567 && substr($bytes, $line_start + $kv_indent_spaces, 1) eq ' ') {
568 0 $kv_indent_spaces++;
569 }
570 1 my $body_indent_str = ' ' x ($kv_indent_spaces + length($INDENT_STR));
571 1 my $term_indent_str = $body_indent_str;
572 1 ▶ my $opener = ($flavor eq 'basic_triple') ? '"""' : "'''";
Branch: 1 branch
BranchTrueFalseTotal
$flavor eq 'basic_triple' ? : 1 0 1
573 1 $self->{out} .= $opener;
574 1 ▶ $self->{out} .= $label if defined $label;
Branch: 1 branch
BranchTrueFalseTotal
if defined $label 1 0 1
575 1 for my $m (@$modifiers) {
576 0 $self->{out} .= ' ';
577 0 $self->{out} .= $m->{name};
578 0 $self->{out} .= '(';
579 0 my $first = 1;
580 0 ▶ for my $a (@{ $m->{args} || [] }) {
Condition0%0/2
$m->{'args'} || []
l!l
00
MC/DC0%0/1
$m->{'args'} || []
$m->{'args'}
Truth table0%0/2
A
A= $m->{'args'}
Aresult
11
00
581 0 ▶ $self->{out} .= ', ' unless $first;
Branch: 1 branch
BranchTrueFalseTotal
unless $first 0 0 0
582 0 $first = 0;
583 0 $self->_emit_modifier_arg($a);
584 }
585 0 $self->{out} .= ')';
586 }
587 1 $self->{out} .= "\n";
588 1 ▶ if (length($body) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $body == 0) { } 0 1 1
589 # nothing — terminator on its own line follows
590 } else {
591 1 for my $line (split /\n/, $body, -1) {
592 1 ▶ if (length($line) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $line == 0) { } 0 1 1
593 0 $self->{out} .= "\n";
594 } else {
595 1 $self->{out} .= $body_indent_str;
596 1 $self->{out} .= $line;
597 1 $self->{out} .= "\n";
598 }
599 }
600 }
601 1 $self->{out} .= $term_indent_str;
602 1 ▶ if (defined $label) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $label) { } 1 0 1
603 1 $self->{out} .= $label;
604 } else {
605 0 ▶ $self->{out} .= ($flavor eq 'basic_triple') ? '"""' : "'''";
Branch: 1 branch
BranchTrueFalseTotal
$flavor eq 'basic_triple' ? : 0 0 0
606 }
607 }
608
609 0 ▶sub _emit_modifier_arg {
Subroutine _emit_modifier_arg: not called CC=12 SCAR=50.5
610 0 my ($self, $v) = @_;
611 0 ▶ if (blessed($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($v)) 0 0 0
612 0 my $cls = ref($v);
613 0 ▶ if ($cls eq 'DMS::Bool') { $self->{out} .= $v->value ? 'true' : 'false'; }
Branch: 5 branches
BranchTrueFalseTotal
if ($cls eq 'DMS::Bool') { } 0 0 0
elsif ($cls eq 'DMS::Integer') { } 0 0 0
elsif ($cls eq 'DMS::Float') { } 0 0 0
elsif ($cls eq 'DMS::OffsetDateTime' or $cls eq 'DMS::LocalDateTime' or $cls eq 'DMS::LocalDate' or $cls eq 'DMS::LocalTime') { } 0 0 0
$v->value ? : 0 0 0
614 0 elsif ($cls eq 'DMS::Integer') { $self->{out} .= $v->bstr; }
615 0 elsif ($cls eq 'DMS::Float') { $self->_emit_float($v->value); }
616 ▶ elsif ($cls eq 'DMS::OffsetDateTime'
Condition0%0/9
$cls eq "DMS::OffsetDateTime" or $cls eq "DMS::LocalDateTime" or $cls eq "DMS::LocalDate" or $cls eq "DMS::LocalTime"
l!l&&r!l&&!r
000
$cls eq "DMS::OffsetDateTime" or $cls eq "DMS::LocalDateTime" or $cls eq "DMS::LocalDate"
l!l&&r!l&&!r
000
$cls eq "DMS::OffsetDateTime" or $cls eq "DMS::LocalDateTime"
l!l&&r!l&&!r
000
MC/DC0%0/4
$cls eq "DMS::OffsetDateTime" or $cls eq "DMS::LocalDateTime" or $cls eq "DMS::LocalDate" or $cls eq "DMS::LocalTime"
$cls eq "DMS::OffsetDateTime" $cls eq "DMS::LocalDateTime" $cls eq "DMS::LocalDate" $cls eq "DMS::LocalTime"
Truth table0%0/5
A or B or C or D
A= $cls eq "DMS::OffsetDateTime"B= $cls eq "DMS::LocalDateTime"C= $cls eq "DMS::LocalDate"D= $cls eq "DMS::LocalTime"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
617 || $cls eq 'DMS::LocalDateTime'
618 || $cls eq 'DMS::LocalDate'
619 0 || $cls eq 'DMS::LocalTime') { $self->{out} .= $v->value; }
620 0 else { die "modifier arg: unknown blessed class $cls"; }
621 0 return;
622 }
623 0 ▶ if (_is_list($v)) { $self->{out} .= '[]'; return; }
Branch: 1 branch
BranchTrueFalseTotal
if (_is_list($v)) 0 0 0
624 0 ▶ if (_is_table($v)) { $self->{out} .= '{}'; return; }
Branch: 1 branch
BranchTrueFalseTotal
if (_is_table($v)) 0 0 0
625 # Plain scalar = string. Modifier args use basic-quoted always.
626 0 $self->{out} .= '"';
627 0 $self->{out} .= _escape_basic("$v");
628 0 $self->{out} .= '"';
629 }
630
631 9 ▶sub _emit_comment_line {
Subroutine _emit_comment_line: called CC=4 SCAR=19.9
632 9 my ($self, $c, $indent) = @_;
633 9 my $text = $c->{content};
634 9 my $prefix = $INDENT_STR x $indent;
635 9 ▶ if (index($text, "\n") < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($text, "\n") == -1) 9 0 9
636 9 $self->{out} .= $prefix;
637 9 $self->{out} .= $text;
638 9 $self->{out} .= "\n";
639 9 return;
640 }
641 # Multi-line: only the first line gets re-indented; subsequent body
642 # lines keep their original whitespace verbatim.
643 0 my @lines = split /\n/, $text, -1;
644 0 ▶ for (my $i = 0; $i < @lines; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @lines) 0 0 0
645 0 ▶ if ($i == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0) { } 0 0 0
646 0 $self->{out} .= $prefix;
647 0 $self->{out} .= $lines[$i];
648 } else {
649 0 $self->{out} .= "\n";
650 0 $self->{out} .= $lines[$i];
651 }
652 }
653 0 $self->{out} .= "\n";
654 }
655
656 46 ▶sub _emit_trailing_for {
Subroutine _emit_trailing_for: called CC=6 SCAR=18.1
657 46 my ($self, $path) = @_;
658 46 ▶ return if $self->{lite};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"lite"} 0 46 46
659 46 my $nc = $self->{comments_by_path}{ _path_key($path) };
660 46 ▶ return if !$nc || !@{ $nc->{trailing} };
Branch: 1 branch
BranchTrueFalseTotal
unless $nc and @{$nc->{"trailing"};} 38 8 46
Condition66%2/3
$nc and @{$nc->{"trailing"};}
!ll&&!rl&&r
3808
MC/DC50%1/2
$nc and @{$nc->{"trailing"};}
$nc @{$nc->{"trailing"};}
Truth table66%2/3
A and B
A= $ncB= @{$nc->{"trailing"};}
ABresult
0X0
100
111
661 8 my $first = 1;
662 8 for my $c (@{ $nc->{trailing} }) {
663 8 ▶ $self->{out} .= ($first ? ' ' : ' ');
Branch: 1 branch
BranchTrueFalseTotal
$first ? : 8 0 8
664 8 $first = 0;
665 8 $self->{out} .= $c->{content};
666 }
667 }
668
669 46 ▶sub _emit_inner_for {
Subroutine _emit_inner_for: called CC=4 SCAR=14.5
670 46 my ($self, $path) = @_;
671 46 ▶ return if $self->{lite};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"lite"} 0 46 46
672 46 my $nc = $self->{comments_by_path}{ _path_key($path) };
673 46 ▶ return if !$nc;
Branch: 1 branch
BranchTrueFalseTotal
unless $nc 38 8 46
674 8 for my $c (@{ $nc->{inner} }) {
675 0 $self->{out} .= $c->{content};
676 0 $self->{out} .= ' ';
677 }
678 }
679
680 51 ▶sub _has_inner {
Subroutine _has_inner: called CC=3 SCAR=11.2
681 51 my ($self, $path) = @_;
682 51 ▶ return 0 if $self->{lite};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"lite"} 0 51 51
683 51 my $nc = $self->{comments_by_path}{ _path_key($path) };
684 51 ▶ return $nc && @{ $nc->{inner} };
Condition66%2/3
$nc && @{$nc->{'inner'};}
!ll&&!rl&&r
4290
MC/DC0%0/2
$nc && @{$nc->{'inner'};}
$nc @{$nc->{'inner'};}
Truth table66%2/3
A && B
A= $ncB= @{$nc->{'inner'};}
ABresult
0X0
100
111
685 }
686
687 27 ▶sub _emit_floating {
Subroutine _emit_floating: called CC=4 SCAR=13.9
688 27 my ($self, $path, $indent) = @_;
689 27 ▶ return if $self->{lite};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"lite"} 0 27 27
690 27 my $nc = $self->{comments_by_path}{ _path_key($path) };
691 27 ▶ return if !$nc;
Branch: 1 branch
BranchTrueFalseTotal
unless $nc 26 1 27
692 1 for my $c (@{ $nc->{floating} }) {
693 1 $self->_emit_comment_line($c, $indent);
694 }
695 }
696
697 # Returns true if $v rooted at $path is safe to emit as a flow form: no
698 # heredoc strings (heredocs need their own line) and no descendant has
699 # an attached comment (flow has nowhere to put it). Used to decide
700 # flow-vs-block when a trailing comment forces flow form.
701 0 ▶sub _is_flow_safe {
Subroutine _is_flow_safe: not called CC=20 SCAR=60.4
702 0 my ($self, $v, $path) = @_;
703 0 my $pk_prefix = _path_key($path);
704 # Any descendant comment ⇒ unsafe. Skipped in lite mode (no
705 # comments are emitted anyway). Mirrors Rust lib.rs::is_flow_safe.
706 0 ▶ if (!$self->{lite}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"lite"}) 0 0 0
707 0 ▶ for my $ac (@{ $self->{doc}{comments} || [] }) {
Condition0%0/2
$self->{'doc'}{'comments'} || []
l!l
00
MC/DC0%0/1
$self->{'doc'}{'comments'} || []
$self->{'doc'}{'comments'}
Truth table0%0/2
A
A= $self->{'doc'}{'comments'}
Aresult
11
00
708 0 ▶ next if scalar(@{ $ac->{path} }) <= scalar(@$path);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$ac->{"path"};} <= scalar @$path 0 0 0
709 0 my $apk = _path_key($ac->{path});
710 0 ▶ my $prefix = $pk_prefix eq '' ? '' : "$pk_prefix\0";
Branch: 1 branch
BranchTrueFalseTotal
$pk_prefix eq '' ? : 0 0 0
711 0 ▶ if ($pk_prefix eq '') {
Branch: 2 branches
BranchTrueFalseTotal
if ($pk_prefix eq '') { } 0 0 0
elsif (substr($apk, 0, length $prefix) eq $prefix) { } 0 0 0
712 # any non-empty path is descendant of root
713 0 return 0;
714 } elsif (substr($apk, 0, length($prefix)) eq $prefix) {
715 0 return 0;
716 }
717 }
718 }
719 0 ▶ if (!ref($v) && !blessed($v)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $v or builtin::blessed($v)) 0 0 0
Condition0%0/3
ref $v or builtin::blessed($v)
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $v or builtin::blessed($v)
ref $v builtin::blessed($v)
Truth table0%0/3
A or B
A= ref $vB= builtin::blessed($v)
ABresult
1X1
011
000
720 # plain string: check heredoc form
721 0 my $lit = $self->{forms_by_path}{ _path_key($path) };
722 0 ▶ if ($lit && exists $lit->{string_form} && $lit->{string_form}{kind} eq 'heredoc') {
Branch: 1 branch
BranchTrueFalseTotal
if ($lit and exists $lit->{"string_form"} and $lit->{"string_form"}{"kind"} eq "heredoc") 0 0 0
Condition0%0/6
$lit and exists $lit->{"string_form"} and $lit->{"string_form"}{"kind"} eq "heredoc"
!ll&&!rl&&r
000
$lit and exists $lit->{"string_form"}
!ll&&!rl&&r
000
MC/DC0%0/3
$lit and exists $lit->{"string_form"} and $lit->{"string_form"}{"kind"} eq "heredoc"
$lit exists $lit->{"string_form"} $lit->{"string_form"}{"kind"} eq "heredoc"
Truth table0%0/4
A and B and C
A= $litB= exists $lit->{"string_form"}C= $lit->{"string_form"}{"kind"} eq "heredoc"
ABCresult
0XX0
10X0
1100
1111
723 0 return 0;
724 }
725 0 return 1;
726 }
727 0 ▶ if (blessed($v)) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($v)) 0 0 0
728 0 ▶ if (_is_list($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_list($v)) 0 0 0
729 0 ▶ for (my $i = 0; $i < @$v; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$v) 0 0 0
730 0 my $sub = [ @$path, DMS::Index->new($i) ];
731 0 ▶ return 0 if !$self->_is_flow_safe($v->[$i], $sub);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_flow_safe($v->[$i], $sub) 0 0 0
732 }
733 0 return 1;
734 }
735 0 ▶ if (_is_table($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_table($v)) 0 0 0
736 0 for my $k (_table_keys($v)) {
737 0 my $sub = [ @$path, $k ];
738 0 ▶ return 0 if !$self->_is_flow_safe($v->{$k}, $sub);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_flow_safe($v->{$k}, $sub) 0 0 0
739 }
740 0 return 1;
741 }
742 0 return 1;
743 }
744
745 6 ▶sub _escape_basic {
Subroutine _escape_basic: called CC=11 SCAR=46.3
746 6 my ($s) = @_;
747 # Fast path: most strings need no escaping. Skip the per-char split
748 # if the string contains no `\`, no `"`, and no control chars.
749 6 ▶ return $s if $s !~ /[\\"\x00-\x1F]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $s =~ /[\\"\x00-\x1F]/ 6 0 6
750 0 my $out = '';
751 0 for my $ch (split //, $s) {
752 0 my $code = ord($ch);
753 0 ▶ if ($ch eq '\\') { $out .= '\\\\'; }
Branch: 8 branches
BranchTrueFalseTotal
if ($ch eq '\\') { } 0 0 0
elsif ($ch eq '"') { } 0 0 0
elsif ($ch eq "\n") { } 0 0 0
elsif ($ch eq "\r") { } 0 0 0
elsif ($ch eq "\t") { } 0 0 0
elsif ($ch eq "\cH") { } 0 0 0
elsif ($ch eq "\f") { } 0 0 0
elsif ($code < 32) { } 0 0 0
754 0 elsif ($ch eq '"') { $out .= '\\"'; }
755 0 elsif ($ch eq "\n") { $out .= '\\n'; }
756 0 elsif ($ch eq "\r") { $out .= '\\r'; }
757 0 elsif ($ch eq "\t") { $out .= '\\t'; }
758 0 elsif ($ch eq "\b") { $out .= '\\b'; }
759 0 elsif ($ch eq "\f") { $out .= '\\f'; }
760 0 elsif ($code < 0x20) { $out .= sprintf('\\u%04X', $code); }
761 0 else { $out .= $ch; }
762 }
763 0 return $out;
764 }
765
766 0 ▶sub _is_bare_key_char_emit {
Subroutine _is_bare_key_char_emit: not called CC=4 SCAR=30.0
767 0 my ($c) = @_;
768 0 ▶ return 1 if $c eq '_' || $c eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "_" or $c eq "-" 0 0 0
Condition0%0/3
$c eq "_" or $c eq "-"
l!l&&r!l&&!r
000
MC/DC0%0/2
$c eq "_" or $c eq "-"
$c eq "_" $c eq "-"
Truth table0%0/3
A or B
A= $c eq "_"B= $c eq "-"
ABresult
1X1
011
000
769 0 my $o = ord($c);
770 0 ▶ if ($o < 128) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o < 128) 0 0 0
771 0 return $c =~ /[A-Za-z0-9]/;
772 }
773 # Match the parser's frozen Unicode 15.1 XID_Continue snapshot so that
774 # to_dms emits a key bare iff the parser would accept it bare. Avoids
775 # producing surface forms that drift with the host's Unicode tables.
776 0 require DMS::Parser;
777 0 return DMS::Parser::_is_xid_continue($o);
778 }
779
780 45 ▶sub _format_key {
Subroutine _format_key: called CC=9 SCAR=42.0
781 45 my ($k) = @_;
782 # ASCII fast path. Real-world keys are >99% plain ASCII identifiers;
783 # the regex bails out at the first non-bare char without splitting
784 # the string into single-char SVs. Saves per-key allocations.
785 45 ▶ return $k if $k =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/ 45 0 45
786 0 ▶ if (length($k) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $k > 0) 0 0 0
787 0 my $bare = 1;
788 0 for my $ch (split //, $k) {
789 0 ▶ if (!_is_bare_key_char_emit($ch)) { $bare = 0; last; }
Branch: 1 branch
BranchTrueFalseTotal
unless (_is_bare_key_char_emit($ch)) 0 0 0
790 }
791 0 ▶ return $k if $bare;
Branch: 1 branch
BranchTrueFalseTotal
if $bare 0 0 0
792 }
793 # Quoted: prefer literal if no single quote, no LF/CR.
794 0 ▶ if (index($k, "'") < 0 && index($k, "\n") < 0 && index($k, "\r") < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($k, "'") == -1 and index($k, "\n") == -1 and index($k, "\r") == -1) 0 0 0
Condition0%0/6
index($k, "'") == -1 and index($k, "\n") == -1 and index($k, "\r") == -1
!ll&&!rl&&r
000
index($k, "'") == -1 and index($k, "\n") == -1
!ll&&!rl&&r
000
MC/DC0%0/3
index($k, "'") == -1 and index($k, "\n") == -1 and index($k, "\r") == -1
index($k, "'") == -1 index($k, "\n") == -1 index($k, "\r") == -1
Truth table0%0/4
A and B and C
A= index($k, "'") == -1B= index($k, "\n") == -1C= index($k, "\r") == -1
ABCresult
0XX0
10X0
1100
1111
795 0 return "'$k'";
796 }
797 0 return '"' . _escape_basic($k) . '"';
798 }
799
800 1 1;
« blib/lib/DMS/XS/encoder.pl ↑ Summary blib/lib/DMS/XS/Parser.pm »