| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package DMS::Parser::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::Parser::XS::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
|
|
|
1
|
|
12
|
use strict; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
76
|
|
|
26
|
1
|
|
|
1
|
|
7
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
66
|
|
|
27
|
1
|
|
|
1
|
|
5
|
use utf8; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
7
|
|
|
28
|
1
|
|
|
1
|
|
38
|
use Scalar::Util qw(blessed); |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
79
|
|
|
29
|
1
|
|
|
1
|
|
724
|
use POSIX qw(isnan isinf); |
|
|
1
|
|
|
|
|
6937
|
|
|
|
1
|
|
|
|
|
21
|
|
|
30
|
1
|
|
|
1
|
|
2450
|
use Carp (); |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
8527
|
|
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
our $VERSION = '0.5.3'; |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
sub encode { |
|
39
|
21
|
|
|
21
|
1
|
62
|
my ($doc) = @_; |
|
40
|
21
|
|
|
|
|
61
|
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
|
|
|
|
|
|
|
sub encode_lite { |
|
50
|
2
|
|
|
2
|
1
|
5
|
my ($doc) = @_; |
|
51
|
2
|
|
|
|
|
8
|
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
|
|
|
|
|
|
|
{ my $warned; |
|
58
|
|
|
|
|
|
|
sub to_dms { |
|
59
|
0
|
0
|
|
0
|
1
|
0
|
unless ($warned++) { |
|
60
|
0
|
|
|
|
|
0
|
Carp::carp( |
|
61
|
|
|
|
|
|
|
'DMS::Parser::Emitter::to_dms() is deprecated; use encode() instead. ' |
|
62
|
|
|
|
|
|
|
. 'SPEC v0.14 renamed to_dms() to encode().'); |
|
63
|
|
|
|
|
|
|
} |
|
64
|
0
|
|
|
|
|
0
|
goto &encode; |
|
65
|
|
|
|
|
|
|
} |
|
66
|
|
|
|
|
|
|
} |
|
67
|
|
|
|
|
|
|
{ my $warned; |
|
68
|
|
|
|
|
|
|
sub to_dms_lite { |
|
69
|
0
|
0
|
|
0
|
1
|
0
|
unless ($warned++) { |
|
70
|
0
|
|
|
|
|
0
|
Carp::carp( |
|
71
|
|
|
|
|
|
|
'DMS::Parser::Emitter::to_dms_lite() is deprecated; use encode_lite() instead. ' |
|
72
|
|
|
|
|
|
|
. 'SPEC v0.14 renamed to_dms_lite() to encode_lite().'); |
|
73
|
|
|
|
|
|
|
} |
|
74
|
0
|
|
|
|
|
0
|
goto &encode_lite; |
|
75
|
|
|
|
|
|
|
} |
|
76
|
|
|
|
|
|
|
} |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub _emit { |
|
79
|
23
|
|
|
23
|
|
49
|
my ($doc, $lite) = @_; |
|
80
|
|
|
|
|
|
|
# SPEC §"Unordered tables": full-mode `encode` refuses to round-trip |
|
81
|
|
|
|
|
|
|
# a Document that contains a DMS::Parser::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
|
23
|
50
|
66
|
|
|
102
|
if (!$lite && _contains_unordered($doc->{body})) { |
|
85
|
0
|
|
|
|
|
0
|
die "encode (full-mode round-trip) refuses Document with DMS::Parser::UnorderedTable; " |
|
86
|
|
|
|
|
|
|
. "use decode_document (ordered) or encode_lite. SPEC §Unordered tables.\n"; |
|
87
|
|
|
|
|
|
|
} |
|
88
|
23
|
50
|
100
|
|
|
130
|
if (!$lite && defined($doc->{meta}) && _contains_unordered($doc->{meta})) { |
|
|
|
|
66
|
|
|
|
|
|
89
|
0
|
|
|
|
|
0
|
die "encode (full-mode round-trip) refuses Document with DMS::Parser::UnorderedTable; " |
|
90
|
|
|
|
|
|
|
. "use decode_document (ordered) or encode_lite. SPEC §Unordered tables.\n"; |
|
91
|
|
|
|
|
|
|
} |
|
92
|
23
|
100
|
|
|
|
163
|
my $self = bless { |
|
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
|
23
|
100
|
|
|
|
83
|
if (!$self->{lite}) { |
|
104
|
|
|
|
|
|
|
# Bucket comments + original_forms by path-key (joined string). |
|
105
|
21
|
50
|
|
|
|
37
|
for my $ac (@{ $doc->{comments} || [] }) { |
|
|
21
|
|
|
|
|
75
|
|
|
106
|
21
|
|
|
|
|
46
|
my $pk = _path_key($ac->{path}); |
|
107
|
21
|
|
100
|
|
|
112
|
$self->{comments_by_path}{$pk} ||= { leading => [], inner => [], trailing => [], floating => [] }; |
|
108
|
21
|
|
|
|
|
44
|
my $entry = $self->{comments_by_path}{$pk}; |
|
109
|
21
|
|
|
|
|
40
|
my $pos = $ac->{position}; |
|
110
|
21
|
100
|
|
|
|
58
|
if ($pos eq 'leading') { |
|
|
|
50
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
111
|
10
|
|
|
|
|
17
|
push @{ $entry->{leading} }, $ac->{comment}; |
|
|
10
|
|
|
|
|
35
|
|
|
112
|
|
|
|
|
|
|
} elsif ($pos eq 'inner') { |
|
113
|
0
|
|
|
|
|
0
|
push @{ $entry->{inner} }, $ac->{comment}; |
|
|
0
|
|
|
|
|
0
|
|
|
114
|
|
|
|
|
|
|
} elsif ($pos eq 'trailing') { |
|
115
|
10
|
|
|
|
|
15
|
push @{ $entry->{trailing} }, $ac->{comment}; |
|
|
10
|
|
|
|
|
30
|
|
|
116
|
|
|
|
|
|
|
} else { |
|
117
|
1
|
|
|
|
|
3
|
push @{ $entry->{floating} }, $ac->{comment}; |
|
|
1
|
|
|
|
|
3
|
|
|
118
|
|
|
|
|
|
|
} |
|
119
|
|
|
|
|
|
|
} |
|
120
|
21
|
50
|
|
|
|
33
|
for my $pair (@{ $doc->{original_forms} || [] }) { |
|
|
21
|
|
|
|
|
65
|
|
|
121
|
19
|
|
|
|
|
42
|
my ($p, $lit) = @$pair; |
|
122
|
19
|
|
|
|
|
41
|
my $pk = _path_key($p); |
|
123
|
19
|
50
|
|
|
|
81
|
$self->{forms_by_path}{$pk} = $lit if !exists $self->{forms_by_path}{$pk}; |
|
124
|
|
|
|
|
|
|
} |
|
125
|
|
|
|
|
|
|
} |
|
126
|
|
|
|
|
|
|
|
|
127
|
23
|
|
|
|
|
75
|
$self->_emit_document; |
|
128
|
23
|
|
|
|
|
313
|
return $self->{out}; |
|
129
|
|
|
|
|
|
|
} |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# --- path-key encoding --- |
|
132
|
|
|
|
|
|
|
# A path is an arrayref of plain string segments (table keys) and |
|
133
|
|
|
|
|
|
|
# DMS::Parser::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
|
|
|
|
|
|
|
sub _path_key { |
|
137
|
354
|
|
|
354
|
|
716
|
my ($p) = @_; |
|
138
|
|
|
|
|
|
|
return join("\0", map { |
|
139
|
354
|
100
|
|
|
|
725
|
ref($_) eq 'DMS::Parser::Index' ? "I:" . $$_ : "K:$_" |
|
|
422
|
|
|
|
|
1614
|
|
|
140
|
|
|
|
|
|
|
} @$p); |
|
141
|
|
|
|
|
|
|
} |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub _emit_document { |
|
144
|
23
|
|
|
23
|
|
41
|
my $self = shift; |
|
145
|
23
|
|
|
|
|
44
|
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
|
23
|
|
|
|
|
39
|
my $has_fm_comments = 0; |
|
156
|
23
|
100
|
|
|
|
53
|
if (!$self->{lite}) { |
|
157
|
21
|
50
|
|
|
|
35
|
for my $ac (@{ $doc->{comments} || [] }) { |
|
|
21
|
|
|
|
|
59
|
|
|
158
|
21
|
|
|
|
|
40
|
my $first = $ac->{path}[0]; |
|
159
|
21
|
50
|
33
|
|
|
219
|
if (defined($first) && !ref($first) && $first eq '__fm__') { |
|
|
|
|
33
|
|
|
|
|
|
160
|
0
|
|
|
|
|
0
|
$has_fm_comments = 1; last; |
|
|
0
|
|
|
|
|
0
|
|
|
161
|
|
|
|
|
|
|
} |
|
162
|
|
|
|
|
|
|
} |
|
163
|
|
|
|
|
|
|
} |
|
164
|
23
|
|
|
|
|
52
|
my $fm_present = defined $doc->{meta}; |
|
165
|
23
|
100
|
66
|
|
|
86
|
if ($fm_present || $has_fm_comments) { |
|
166
|
2
|
|
|
|
|
5
|
$self->{out} .= "+++\n"; |
|
167
|
2
|
|
|
|
|
7
|
my $fm_path = ['__fm__']; |
|
168
|
2
|
50
|
|
|
|
6
|
if (defined $doc->{meta}) { |
|
169
|
2
|
|
|
|
|
22
|
$self->_emit_table_block($doc->{meta}, $fm_path, 0); |
|
170
|
|
|
|
|
|
|
} else { |
|
171
|
0
|
|
|
|
|
0
|
$self->_emit_floating($fm_path, 0); |
|
172
|
|
|
|
|
|
|
} |
|
173
|
2
|
|
|
|
|
7
|
$self->{out} .= "+++\n\n"; |
|
174
|
|
|
|
|
|
|
} |
|
175
|
|
|
|
|
|
|
|
|
176
|
23
|
|
|
|
|
57
|
my $body_path = []; |
|
177
|
23
|
|
|
|
|
43
|
my $body = $doc->{body}; |
|
178
|
23
|
50
|
|
|
|
49
|
if (_is_table($body)) { |
|
|
|
0
|
|
|
|
|
|
|
179
|
23
|
|
|
|
|
63
|
$self->_emit_table_block($body, $body_path, 0); |
|
180
|
|
|
|
|
|
|
} elsif (_is_list($body)) { |
|
181
|
0
|
|
|
|
|
0
|
$self->_emit_list_block($body, $body_path, 0); |
|
182
|
|
|
|
|
|
|
} else { |
|
183
|
|
|
|
|
|
|
# Scalar root. |
|
184
|
0
|
|
|
|
|
0
|
my $nc = $self->{comments_by_path}{ _path_key($body_path) }; |
|
185
|
0
|
0
|
|
|
|
0
|
if ($nc) { |
|
186
|
0
|
|
|
|
|
0
|
for my $c (@{ $nc->{leading} }) { |
|
|
0
|
|
|
|
|
0
|
|
|
187
|
0
|
|
|
|
|
0
|
$self->_emit_comment_line($c, 0); |
|
188
|
|
|
|
|
|
|
} |
|
189
|
|
|
|
|
|
|
} |
|
190
|
0
|
|
|
|
|
0
|
$self->_emit_value_inline($body, $body_path); |
|
191
|
0
|
|
|
|
|
0
|
$self->_emit_trailing_for($body_path); |
|
192
|
0
|
|
|
|
|
0
|
$self->{out} .= "\n"; |
|
193
|
0
|
0
|
|
|
|
0
|
if ($nc) { |
|
194
|
0
|
|
|
|
|
0
|
for my $c (@{ $nc->{floating} }) { |
|
|
0
|
|
|
|
|
0
|
|
|
195
|
0
|
|
|
|
|
0
|
$self->_emit_comment_line($c, 0); |
|
196
|
|
|
|
|
|
|
} |
|
197
|
|
|
|
|
|
|
} |
|
198
|
|
|
|
|
|
|
} |
|
199
|
|
|
|
|
|
|
} |
|
200
|
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
sub _is_table { |
|
202
|
106
|
|
|
106
|
|
207
|
my ($v) = @_; |
|
203
|
106
|
50
|
|
|
|
234
|
return 0 if !defined $v; |
|
204
|
|
|
|
|
|
|
# SPEC §"Unordered tables": DMS::Parser::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
|
106
|
100
|
|
|
|
274
|
if (blessed($v)) { |
|
208
|
41
|
|
|
|
|
195
|
return ref($v) eq 'DMS::Parser::UnorderedTable'; |
|
209
|
|
|
|
|
|
|
} |
|
210
|
65
|
|
|
|
|
221
|
return ref($v) eq 'HASH'; |
|
211
|
|
|
|
|
|
|
} |
|
212
|
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
sub _is_list { |
|
214
|
73
|
|
|
73
|
|
140
|
my ($v) = @_; |
|
215
|
73
|
50
|
|
|
|
152
|
return 0 if !defined $v; |
|
216
|
73
|
100
|
|
|
|
252
|
return 0 if blessed($v); |
|
217
|
32
|
|
|
|
|
173
|
return ref($v) eq 'ARRAY'; |
|
218
|
|
|
|
|
|
|
} |
|
219
|
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
sub _table_keys { |
|
221
|
31
|
|
|
31
|
|
55
|
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
|
31
|
|
|
|
|
67
|
my $tied = tied(%$t); |
|
228
|
31
|
50
|
33
|
|
|
149
|
if ($tied && ref($tied) eq 'Tie::IxHash') { |
|
229
|
31
|
|
|
|
|
47
|
return @{ $tied->[1] }; |
|
|
31
|
|
|
|
|
134
|
|
|
230
|
|
|
|
|
|
|
} |
|
231
|
|
|
|
|
|
|
# DMS::Parser::UnorderedTable (plain blessed hash) or plain HV: arbitrary |
|
232
|
|
|
|
|
|
|
# iteration order is the documented contract for lite mode. |
|
233
|
0
|
|
|
|
|
0
|
return keys %$t; |
|
234
|
|
|
|
|
|
|
} |
|
235
|
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
# Recursive walk: returns true if any nested table (or the value itself) |
|
237
|
|
|
|
|
|
|
# is a DMS::Parser::UnorderedTable. Used by `to_dms` (full mode) to refuse |
|
238
|
|
|
|
|
|
|
# round-trip on unordered Documents per SPEC §"Unordered tables". |
|
239
|
|
|
|
|
|
|
sub _contains_unordered { |
|
240
|
84
|
|
|
84
|
|
211
|
my ($v) = @_; |
|
241
|
84
|
50
|
|
|
|
468
|
return 0 if !defined $v; |
|
242
|
84
|
100
|
|
|
|
192
|
if (blessed($v)) { |
|
243
|
|
|
|
|
|
|
# DMS::Parser::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
|
41
|
50
|
|
|
|
103
|
return 1 if ref($v) eq 'DMS::Parser::UnorderedTable'; |
|
247
|
|
|
|
|
|
|
# Other blessed sentinels (DMS::Parser::Integer, DMS::Parser::Bool, dates, |
|
248
|
|
|
|
|
|
|
# DMS::Parser::Float) are leaves. |
|
249
|
41
|
|
|
|
|
136
|
return 0; |
|
250
|
|
|
|
|
|
|
} |
|
251
|
43
|
100
|
|
|
|
108
|
if (ref($v) eq 'HASH') { |
|
252
|
26
|
|
|
|
|
126
|
for my $k (keys %$v) { |
|
253
|
51
|
50
|
|
|
|
640
|
next if $k eq "\0_keys"; |
|
254
|
51
|
50
|
|
|
|
159
|
return 1 if _contains_unordered($v->{$k}); |
|
255
|
|
|
|
|
|
|
} |
|
256
|
26
|
|
|
|
|
143
|
return 0; |
|
257
|
|
|
|
|
|
|
} |
|
258
|
17
|
100
|
|
|
|
39
|
if (ref($v) eq 'ARRAY') { |
|
259
|
4
|
|
|
|
|
10
|
for my $item (@$v) { |
|
260
|
10
|
50
|
|
|
|
22
|
return 1 if _contains_unordered($item); |
|
261
|
|
|
|
|
|
|
} |
|
262
|
4
|
|
|
|
|
14
|
return 0; |
|
263
|
|
|
|
|
|
|
} |
|
264
|
13
|
|
|
|
|
69
|
return 0; |
|
265
|
|
|
|
|
|
|
} |
|
266
|
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
sub _emit_table_block { |
|
268
|
28
|
|
|
28
|
|
64
|
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
|
28
|
100
|
|
|
|
79
|
if ($self->{lite}) { |
|
275
|
2
|
|
|
|
|
6
|
my $pad = $INDENT_STR x $indent; |
|
276
|
2
|
|
|
|
|
6
|
for my $k (_table_keys($t)) { |
|
277
|
4
|
|
|
|
|
18
|
my $v = $t->{$k}; |
|
278
|
4
|
|
|
|
|
33
|
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
|
4
|
50
|
|
|
|
13
|
if (!$r) { |
|
284
|
0
|
0
|
|
|
|
0
|
my $key_fmt = ($k =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/) |
|
285
|
|
|
|
|
|
|
? $k : _format_key($k); |
|
286
|
0
|
0
|
|
|
|
0
|
if ($v !~ /[\\"\x00-\x1F]/) { |
|
287
|
0
|
|
|
|
|
0
|
$self->{out} .= "${pad}${key_fmt}: \"${v}\"\n"; |
|
288
|
|
|
|
|
|
|
} else { |
|
289
|
0
|
|
|
|
|
0
|
$self->{out} .= "${pad}${key_fmt}: \"" . _escape_basic($v) . "\"\n"; |
|
290
|
|
|
|
|
|
|
} |
|
291
|
0
|
|
|
|
|
0
|
next; |
|
292
|
|
|
|
|
|
|
} |
|
293
|
4
|
50
|
|
|
|
11
|
if ($r eq 'DMS::Parser::Bool') { |
|
294
|
0
|
0
|
|
|
|
0
|
my $key_fmt = ($k =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/) |
|
295
|
|
|
|
|
|
|
? $k : _format_key($k); |
|
296
|
0
|
0
|
|
|
|
0
|
$self->{out} .= $pad . $key_fmt . ': ' |
|
297
|
|
|
|
|
|
|
. ($$v ? "true\n" : "false\n"); |
|
298
|
0
|
|
|
|
|
0
|
next; |
|
299
|
|
|
|
|
|
|
} |
|
300
|
4
|
50
|
|
|
|
11
|
if ($r eq 'DMS::Parser::Integer') { |
|
301
|
4
|
50
|
|
|
|
20
|
my $key_fmt = ($k =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/) |
|
302
|
|
|
|
|
|
|
? $k : _format_key($k); |
|
303
|
4
|
|
|
|
|
9
|
$self->{out} .= "${pad}${key_fmt}: ${$v}\n"; |
|
|
4
|
|
|
|
|
11
|
|
|
304
|
4
|
|
|
|
|
12
|
next; |
|
305
|
|
|
|
|
|
|
} |
|
306
|
0
|
|
0
|
|
|
0
|
my $can_block = |
|
307
|
|
|
|
|
|
|
(_is_table($v) && scalar(_table_keys($v))) || |
|
308
|
|
|
|
|
|
|
(_is_list($v) && scalar(@$v)); |
|
309
|
0
|
|
|
|
|
0
|
$self->{out} .= $pad; |
|
310
|
0
|
|
|
|
|
0
|
$self->{out} .= _format_key($k); |
|
311
|
0
|
|
|
|
|
0
|
$self->{out} .= ':'; |
|
312
|
0
|
0
|
|
|
|
0
|
if ($can_block) { |
|
313
|
0
|
|
|
|
|
0
|
$self->{out} .= "\n"; |
|
314
|
0
|
0
|
|
|
|
0
|
if (_is_table($v)) { |
|
315
|
0
|
|
|
|
|
0
|
$self->_emit_table_block($v, undef, $indent + 1); |
|
316
|
|
|
|
|
|
|
} else { |
|
317
|
0
|
|
|
|
|
0
|
$self->_emit_list_block($v, undef, $indent + 1); |
|
318
|
|
|
|
|
|
|
} |
|
319
|
|
|
|
|
|
|
} else { |
|
320
|
0
|
|
|
|
|
0
|
$self->{out} .= ' '; |
|
321
|
0
|
|
|
|
|
0
|
$self->_emit_value_inline($v, undef); |
|
322
|
0
|
|
|
|
|
0
|
$self->{out} .= "\n"; |
|
323
|
|
|
|
|
|
|
} |
|
324
|
|
|
|
|
|
|
} |
|
325
|
2
|
|
|
|
|
7
|
return; |
|
326
|
|
|
|
|
|
|
} |
|
327
|
26
|
|
|
|
|
61
|
for my $k (_table_keys($t)) { |
|
328
|
51
|
|
|
|
|
239
|
my $v = $t->{$k}; |
|
329
|
51
|
|
|
|
|
444
|
my $child_path = [ @$path, $k ]; |
|
330
|
51
|
|
|
|
|
131
|
my $child_pk = _path_key($child_path); |
|
331
|
51
|
|
|
|
|
114
|
my $nc = $self->{comments_by_path}{$child_pk}; |
|
332
|
51
|
100
|
|
|
|
115
|
if ($nc) { |
|
333
|
9
|
|
|
|
|
15
|
for my $c (@{ $nc->{leading} }) { |
|
|
9
|
|
|
|
|
21
|
|
|
334
|
8
|
|
|
|
|
28
|
$self->_emit_comment_line($c, $indent); |
|
335
|
|
|
|
|
|
|
} |
|
336
|
|
|
|
|
|
|
} |
|
337
|
51
|
|
100
|
|
|
148
|
my $has_trailing = $nc && @{ $nc->{trailing} }; |
|
338
|
51
|
|
|
|
|
134
|
my $has_inner = $self->_has_inner($child_path); |
|
339
|
51
|
|
100
|
|
|
104
|
my $can_block = |
|
340
|
|
|
|
|
|
|
(_is_table($v) && scalar(_table_keys($v))) || |
|
341
|
|
|
|
|
|
|
(_is_list($v) && scalar(@$v)); |
|
342
|
51
|
|
66
|
|
|
178
|
my $needs_block = $can_block && !($has_trailing && $self->_is_flow_safe($v, $child_path)); |
|
343
|
51
|
|
|
|
|
127
|
$self->{out} .= $INDENT_STR x $indent; |
|
344
|
51
|
|
|
|
|
122
|
$self->{out} .= _format_key($k); |
|
345
|
51
|
|
|
|
|
121
|
$self->{out} .= ':'; |
|
346
|
51
|
100
|
|
|
|
106
|
if ($needs_block) { |
|
347
|
7
|
50
|
|
|
|
30
|
if ($has_inner) { |
|
348
|
0
|
|
|
|
|
0
|
$self->{out} .= ' '; |
|
349
|
0
|
|
|
|
|
0
|
$self->_emit_inner_for($child_path); |
|
350
|
|
|
|
|
|
|
# Trim trailing space left by _emit_inner_for. |
|
351
|
0
|
|
|
|
|
0
|
$self->{out} =~ s/ \z//; |
|
352
|
|
|
|
|
|
|
} |
|
353
|
7
|
|
|
|
|
15
|
$self->{out} .= "\n"; |
|
354
|
7
|
100
|
|
|
|
13
|
if (_is_table($v)) { $self->_emit_table_block($v, $child_path, $indent + 1); } |
|
|
3
|
|
|
|
|
22
|
|
|
355
|
4
|
|
|
|
|
17
|
else { $self->_emit_list_block($v, $child_path, $indent + 1); } |
|
356
|
|
|
|
|
|
|
} else { |
|
357
|
44
|
|
|
|
|
84
|
$self->{out} .= ' '; |
|
358
|
44
|
|
|
|
|
121
|
$self->_emit_inner_for($child_path); |
|
359
|
44
|
|
|
|
|
141
|
$self->_emit_value_inline($v, $child_path); |
|
360
|
44
|
|
|
|
|
130
|
$self->_emit_trailing_for($child_path); |
|
361
|
44
|
|
|
|
|
153
|
$self->{out} .= "\n"; |
|
362
|
|
|
|
|
|
|
} |
|
363
|
|
|
|
|
|
|
} |
|
364
|
26
|
|
|
|
|
78
|
$self->_emit_floating($path, $indent); |
|
365
|
|
|
|
|
|
|
} |
|
366
|
|
|
|
|
|
|
|
|
367
|
|
|
|
|
|
|
sub _emit_list_block { |
|
368
|
4
|
|
|
4
|
|
25
|
my ($self, $items, $path, $indent) = @_; |
|
369
|
4
|
50
|
|
|
|
27
|
if ($self->{lite}) { |
|
370
|
0
|
|
|
|
|
0
|
my $pad = $INDENT_STR x $indent; |
|
371
|
0
|
|
|
|
|
0
|
for (my $i = 0; $i < @$items; $i++) { |
|
372
|
0
|
|
|
|
|
0
|
my $v = $items->[$i]; |
|
373
|
0
|
|
|
|
|
0
|
$self->{out} .= $pad; |
|
374
|
0
|
|
|
|
|
0
|
$self->{out} .= '+'; |
|
375
|
0
|
0
|
0
|
|
|
0
|
if (_is_table($v) && scalar(_table_keys($v))) { |
|
|
|
0
|
0
|
|
|
|
|
|
376
|
0
|
|
|
|
|
0
|
$self->{out} .= "\n"; |
|
377
|
0
|
|
|
|
|
0
|
$self->_emit_table_block($v, undef, $indent + 1); |
|
378
|
|
|
|
|
|
|
} elsif (_is_list($v) && scalar(@$v)) { |
|
379
|
0
|
|
|
|
|
0
|
$self->{out} .= "\n"; |
|
380
|
0
|
|
|
|
|
0
|
$self->_emit_list_block($v, undef, $indent + 1); |
|
381
|
|
|
|
|
|
|
} else { |
|
382
|
0
|
|
|
|
|
0
|
$self->{out} .= ' '; |
|
383
|
0
|
|
|
|
|
0
|
$self->_emit_value_inline($v, undef); |
|
384
|
0
|
|
|
|
|
0
|
$self->{out} .= "\n"; |
|
385
|
|
|
|
|
|
|
} |
|
386
|
|
|
|
|
|
|
} |
|
387
|
0
|
|
|
|
|
0
|
return; |
|
388
|
|
|
|
|
|
|
} |
|
389
|
4
|
|
|
|
|
15
|
for (my $i = 0; $i < @$items; $i++) { |
|
390
|
10
|
|
|
|
|
21
|
my $v = $items->[$i]; |
|
391
|
10
|
|
|
|
|
34
|
my $child_path = [ @$path, DMS::Parser::Index->new($i) ]; |
|
392
|
10
|
|
|
|
|
22
|
my $child_pk = _path_key($child_path); |
|
393
|
10
|
|
|
|
|
21
|
my $nc = $self->{comments_by_path}{$child_pk}; |
|
394
|
10
|
50
|
|
|
|
54
|
if ($nc) { |
|
395
|
0
|
|
|
|
|
0
|
for my $c (@{ $nc->{leading} }) { |
|
|
0
|
|
|
|
|
0
|
|
|
396
|
0
|
|
|
|
|
0
|
$self->_emit_comment_line($c, $indent); |
|
397
|
|
|
|
|
|
|
} |
|
398
|
|
|
|
|
|
|
} |
|
399
|
10
|
|
|
|
|
27
|
$self->{out} .= $INDENT_STR x $indent; |
|
400
|
10
|
|
|
|
|
19
|
$self->{out} .= '+'; |
|
401
|
10
|
|
|
|
|
27
|
my $has_inner = $self->_has_inner($child_path); |
|
402
|
10
|
50
|
50
|
|
|
21
|
if (_is_table($v) && scalar(_table_keys($v))) { |
|
|
|
50
|
50
|
|
|
|
|
|
403
|
0
|
0
|
|
|
|
0
|
if ($has_inner) { |
|
404
|
0
|
|
|
|
|
0
|
$self->{out} .= ' '; |
|
405
|
0
|
|
|
|
|
0
|
$self->_emit_inner_for($child_path); |
|
406
|
0
|
|
|
|
|
0
|
$self->{out} =~ s/ \z//; |
|
407
|
|
|
|
|
|
|
} |
|
408
|
0
|
|
|
|
|
0
|
$self->_emit_trailing_for($child_path); |
|
409
|
0
|
|
|
|
|
0
|
$self->{out} .= "\n"; |
|
410
|
0
|
|
|
|
|
0
|
$self->_emit_table_block($v, $child_path, $indent + 1); |
|
411
|
|
|
|
|
|
|
} elsif (_is_list($v) && scalar(@$v)) { |
|
412
|
0
|
0
|
|
|
|
0
|
if ($has_inner) { |
|
413
|
0
|
|
|
|
|
0
|
$self->{out} .= ' '; |
|
414
|
0
|
|
|
|
|
0
|
$self->_emit_inner_for($child_path); |
|
415
|
0
|
|
|
|
|
0
|
$self->{out} =~ s/ \z//; |
|
416
|
|
|
|
|
|
|
} |
|
417
|
0
|
|
|
|
|
0
|
$self->_emit_trailing_for($child_path); |
|
418
|
0
|
|
|
|
|
0
|
$self->{out} .= "\n"; |
|
419
|
0
|
|
|
|
|
0
|
$self->_emit_list_block($v, $child_path, $indent + 1); |
|
420
|
|
|
|
|
|
|
} else { |
|
421
|
10
|
|
|
|
|
20
|
$self->{out} .= ' '; |
|
422
|
10
|
|
|
|
|
30
|
$self->_emit_inner_for($child_path); |
|
423
|
10
|
|
|
|
|
29
|
$self->_emit_value_inline($v, $child_path); |
|
424
|
10
|
|
|
|
|
26
|
$self->_emit_trailing_for($child_path); |
|
425
|
10
|
|
|
|
|
47
|
$self->{out} .= "\n"; |
|
426
|
|
|
|
|
|
|
} |
|
427
|
|
|
|
|
|
|
} |
|
428
|
4
|
|
|
|
|
12
|
$self->_emit_floating($path, $indent); |
|
429
|
|
|
|
|
|
|
} |
|
430
|
|
|
|
|
|
|
|
|
431
|
|
|
|
|
|
|
sub _emit_value_inline { |
|
432
|
54
|
|
|
54
|
|
115
|
my ($self, $v, $path) = @_; |
|
433
|
54
|
100
|
|
|
|
126
|
if (blessed($v)) { |
|
434
|
41
|
|
|
|
|
74
|
my $cls = ref($v); |
|
435
|
41
|
0
|
0
|
|
|
149
|
if ($cls eq 'DMS::Parser::Bool') { $self->{out} .= $v->value ? 'true' : 'false'; } |
|
|
0
|
50
|
0
|
|
|
0
|
|
|
|
|
50
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
436
|
41
|
|
|
|
|
109
|
elsif ($cls eq 'DMS::Parser::Integer') { $self->_emit_integer($v, $path); } |
|
437
|
0
|
|
|
|
|
0
|
elsif ($cls eq 'DMS::Parser::Float') { $self->_emit_float($v->value); } |
|
438
|
|
|
|
|
|
|
elsif ($cls eq 'DMS::Parser::OffsetDateTime' |
|
439
|
|
|
|
|
|
|
|| $cls eq 'DMS::Parser::LocalDateTime' |
|
440
|
|
|
|
|
|
|
|| $cls eq 'DMS::Parser::LocalDate' |
|
441
|
0
|
|
|
|
|
0
|
|| $cls eq 'DMS::Parser::LocalTime') { $self->{out} .= $v->value; } |
|
442
|
0
|
|
|
|
|
0
|
else { die "to_dms: unknown blessed class $cls"; } |
|
443
|
41
|
|
|
|
|
83
|
return; |
|
444
|
|
|
|
|
|
|
} |
|
445
|
13
|
50
|
|
|
|
28
|
if (_is_list($v)) { |
|
446
|
0
|
0
|
|
|
|
0
|
if (!@$v) { $self->{out} .= '[]'; return; } |
|
|
0
|
|
|
|
|
0
|
|
|
|
0
|
|
|
|
|
0
|
|
|
447
|
0
|
|
|
|
|
0
|
$self->{out} .= '['; |
|
448
|
0
|
|
|
|
|
0
|
my $lite = $self->{lite}; |
|
449
|
0
|
|
|
|
|
0
|
for (my $i = 0; $i < @$v; $i++) { |
|
450
|
0
|
0
|
|
|
|
0
|
$self->{out} .= ', ' if $i > 0; |
|
451
|
0
|
0
|
|
|
|
0
|
my $sub = $lite ? undef : [ @$path, DMS::Parser::Index->new($i) ]; |
|
452
|
0
|
|
|
|
|
0
|
$self->_emit_value_inline($v->[$i], $sub); |
|
453
|
|
|
|
|
|
|
} |
|
454
|
0
|
|
|
|
|
0
|
$self->{out} .= ']'; |
|
455
|
0
|
|
|
|
|
0
|
return; |
|
456
|
|
|
|
|
|
|
} |
|
457
|
13
|
50
|
|
|
|
31
|
if (_is_table($v)) { |
|
458
|
0
|
|
|
|
|
0
|
my @keys = _table_keys($v); |
|
459
|
0
|
0
|
|
|
|
0
|
if (!@keys) { $self->{out} .= '{}'; return; } |
|
|
0
|
|
|
|
|
0
|
|
|
|
0
|
|
|
|
|
0
|
|
|
460
|
0
|
|
|
|
|
0
|
$self->{out} .= '{'; |
|
461
|
0
|
|
|
|
|
0
|
my $first = 1; |
|
462
|
0
|
|
|
|
|
0
|
my $lite = $self->{lite}; |
|
463
|
0
|
|
|
|
|
0
|
for my $k (@keys) { |
|
464
|
0
|
0
|
|
|
|
0
|
$self->{out} .= ', ' unless $first; |
|
465
|
0
|
|
|
|
|
0
|
$first = 0; |
|
466
|
0
|
|
|
|
|
0
|
$self->{out} .= _format_key($k); |
|
467
|
0
|
|
|
|
|
0
|
$self->{out} .= ': '; |
|
468
|
0
|
0
|
|
|
|
0
|
my $sub = $lite ? undef : [ @$path, $k ]; |
|
469
|
0
|
|
|
|
|
0
|
$self->_emit_value_inline($v->{$k}, $sub); |
|
470
|
|
|
|
|
|
|
} |
|
471
|
0
|
|
|
|
|
0
|
$self->{out} .= '}'; |
|
472
|
0
|
|
|
|
|
0
|
return; |
|
473
|
|
|
|
|
|
|
} |
|
474
|
|
|
|
|
|
|
# Plain scalar = string. |
|
475
|
13
|
50
|
|
|
|
27
|
if (!defined $v) { die "to_dms: got undef value"; } |
|
|
0
|
|
|
|
|
0
|
|
|
476
|
13
|
|
|
|
|
45
|
$self->_emit_string("$v", $path); |
|
477
|
|
|
|
|
|
|
} |
|
478
|
|
|
|
|
|
|
|
|
479
|
|
|
|
|
|
|
sub _emit_integer { |
|
480
|
41
|
|
|
41
|
|
109
|
my ($self, $iv, $path) = @_; |
|
481
|
41
|
50
|
|
|
|
98
|
if ($self->{lite}) { |
|
482
|
|
|
|
|
|
|
# DMS::Parser::Integer is `bless \$v, 'DMS::Parser::Integer'` where $v is an IV. |
|
483
|
|
|
|
|
|
|
# Direct deref + string concat skips the bstr() method dispatch. |
|
484
|
0
|
|
|
|
|
0
|
$self->{out} .= "${$iv}"; |
|
|
0
|
|
|
|
|
0
|
|
|
485
|
0
|
|
|
|
|
0
|
return; |
|
486
|
|
|
|
|
|
|
} |
|
487
|
41
|
|
|
|
|
101
|
my $lit_ref = $self->{forms_by_path}{ _path_key($path) }; |
|
488
|
41
|
50
|
66
|
|
|
136
|
if ($lit_ref && exists $lit_ref->{integer_lit}) { |
|
489
|
11
|
|
|
|
|
27
|
$self->{out} .= $lit_ref->{integer_lit}; |
|
490
|
11
|
|
|
|
|
23
|
return; |
|
491
|
|
|
|
|
|
|
} |
|
492
|
|
|
|
|
|
|
# Default: canonical decimal. DMS::Parser::Integer's bstr stringifies the IV. |
|
493
|
30
|
|
|
|
|
104
|
$self->{out} .= $iv->bstr; |
|
494
|
|
|
|
|
|
|
} |
|
495
|
|
|
|
|
|
|
|
|
496
|
|
|
|
|
|
|
sub _emit_float { |
|
497
|
0
|
|
|
0
|
|
0
|
my ($self, $f) = @_; |
|
498
|
0
|
0
|
|
|
|
0
|
if (isnan($f)) { $self->{out} .= 'nan'; return; } |
|
|
0
|
|
|
|
|
0
|
|
|
|
0
|
|
|
|
|
0
|
|
|
499
|
0
|
0
|
|
|
|
0
|
if (isinf($f)) { $self->{out} .= ($f > 0 ? 'inf' : '-inf'); return; } |
|
|
0
|
0
|
|
|
|
0
|
|
|
|
0
|
|
|
|
|
0
|
|
|
500
|
|
|
|
|
|
|
# ryu-shortest equivalent: try increasing %.Ng until round-trip works. |
|
501
|
0
|
|
|
|
|
0
|
for my $p (1..17) { |
|
502
|
0
|
|
|
|
|
0
|
my $s = sprintf("%.${p}g", $f); |
|
503
|
0
|
0
|
|
|
|
0
|
if (0 + $s == $f) { |
|
504
|
0
|
|
|
|
|
0
|
$s =~ s/e\+/e/; |
|
505
|
0
|
|
|
|
|
0
|
$s =~ s/e-0+(\d)/e-$1/; |
|
506
|
0
|
|
|
|
|
0
|
$s =~ s/e0+(\d)/e$1/; |
|
507
|
0
|
0
|
|
|
|
0
|
if ($s !~ /[.eE]/) { $s .= '.0'; } |
|
|
0
|
|
|
|
|
0
|
|
|
508
|
0
|
|
|
|
|
0
|
$self->{out} .= $s; |
|
509
|
0
|
|
|
|
|
0
|
return; |
|
510
|
|
|
|
|
|
|
} |
|
511
|
|
|
|
|
|
|
} |
|
512
|
0
|
|
|
|
|
0
|
$self->{out} .= sprintf("%.17g", $f); |
|
513
|
|
|
|
|
|
|
} |
|
514
|
|
|
|
|
|
|
|
|
515
|
|
|
|
|
|
|
sub _emit_string { |
|
516
|
13
|
|
|
13
|
|
29
|
my ($self, $s, $path) = @_; |
|
517
|
13
|
50
|
|
|
|
32
|
if ($self->{lite}) { |
|
518
|
0
|
|
|
|
|
0
|
$self->{out} .= '"'; |
|
519
|
0
|
|
|
|
|
0
|
$self->{out} .= _escape_basic($s); |
|
520
|
0
|
|
|
|
|
0
|
$self->{out} .= '"'; |
|
521
|
0
|
|
|
|
|
0
|
return; |
|
522
|
|
|
|
|
|
|
} |
|
523
|
13
|
|
|
|
|
30
|
my $lit_ref = $self->{forms_by_path}{ _path_key($path) }; |
|
524
|
13
|
|
|
|
|
25
|
my $form; |
|
525
|
13
|
50
|
66
|
|
|
48
|
if ($lit_ref && exists $lit_ref->{string_form}) { |
|
526
|
8
|
|
|
|
|
15
|
$form = $lit_ref->{string_form}; |
|
527
|
|
|
|
|
|
|
} |
|
528
|
13
|
100
|
66
|
|
|
53
|
if (!$form || $form->{kind} eq 'basic') { |
|
529
|
5
|
|
|
|
|
12
|
$self->{out} .= '"'; |
|
530
|
5
|
|
|
|
|
15
|
$self->{out} .= _escape_basic($s); |
|
531
|
5
|
|
|
|
|
13
|
$self->{out} .= '"'; |
|
532
|
5
|
|
|
|
|
12
|
return; |
|
533
|
|
|
|
|
|
|
} |
|
534
|
8
|
100
|
|
|
|
22
|
if ($form->{kind} eq 'literal') { |
|
535
|
1
|
|
|
|
|
3
|
$self->{out} .= "'"; |
|
536
|
1
|
|
|
|
|
3
|
$self->{out} .= $s; |
|
537
|
1
|
|
|
|
|
13
|
$self->{out} .= "'"; |
|
538
|
1
|
|
|
|
|
4
|
return; |
|
539
|
|
|
|
|
|
|
} |
|
540
|
7
|
50
|
|
|
|
18
|
if ($form->{kind} eq 'heredoc') { |
|
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
|
7
|
|
|
|
|
13
|
my $body = $s; |
|
546
|
7
|
|
|
|
|
12
|
my $has_fold = 0; |
|
547
|
7
|
50
|
|
|
|
11
|
for my $m (@{ $form->{modifiers} || [] }) { |
|
|
7
|
|
|
|
|
25
|
|
|
548
|
1
|
50
|
|
|
|
6
|
if ($m->{name} eq '_fold_paragraphs') { $has_fold = 1; last; } |
|
|
0
|
|
|
|
|
0
|
|
|
|
0
|
|
|
|
|
0
|
|
|
549
|
|
|
|
|
|
|
} |
|
550
|
7
|
50
|
|
|
|
18
|
if ($has_fold) { |
|
551
|
0
|
|
|
|
|
0
|
$body =~ s/\n/\n\n/g; |
|
552
|
|
|
|
|
|
|
} |
|
553
|
7
|
|
50
|
|
|
33
|
$self->_emit_heredoc($body, $form->{flavor}, $form->{label}, $form->{modifiers} || []); |
|
554
|
7
|
|
|
|
|
18
|
return; |
|
555
|
|
|
|
|
|
|
} |
|
556
|
0
|
|
|
|
|
0
|
die "to_dms: unknown string form: $form->{kind}"; |
|
557
|
|
|
|
|
|
|
} |
|
558
|
|
|
|
|
|
|
|
|
559
|
|
|
|
|
|
|
sub _emit_heredoc { |
|
560
|
7
|
|
|
7
|
|
46
|
my ($self, $body, $flavor, $label, $modifiers) = @_; |
|
561
|
|
|
|
|
|
|
# Compute the kvpair's indent from the most recent newline in $self->{out}. |
|
562
|
7
|
|
|
|
|
15
|
my $bytes = $self->{out}; |
|
563
|
7
|
|
|
|
|
17
|
my $last_nl = rindex($bytes, "\n"); |
|
564
|
7
|
100
|
|
|
|
32
|
my $line_start = $last_nl < 0 ? 0 : $last_nl + 1; |
|
565
|
7
|
|
|
|
|
13
|
my $kv_indent_spaces = 0; |
|
566
|
7
|
|
33
|
|
|
50
|
while ($line_start + $kv_indent_spaces < length($bytes) |
|
567
|
|
|
|
|
|
|
&& substr($bytes, $line_start + $kv_indent_spaces, 1) eq ' ') { |
|
568
|
0
|
|
|
|
|
0
|
$kv_indent_spaces++; |
|
569
|
|
|
|
|
|
|
} |
|
570
|
7
|
|
|
|
|
80
|
my $body_indent_str = ' ' x ($kv_indent_spaces + length($INDENT_STR)); |
|
571
|
7
|
|
|
|
|
14
|
my $term_indent_str = $body_indent_str; |
|
572
|
7
|
100
|
|
|
|
18
|
my $opener = ($flavor eq 'basic_triple') ? '"""' : "'''"; |
|
573
|
7
|
|
|
|
|
16
|
$self->{out} .= $opener; |
|
574
|
7
|
100
|
|
|
|
23
|
$self->{out} .= $label if defined $label; |
|
575
|
7
|
|
|
|
|
15
|
for my $m (@$modifiers) { |
|
576
|
1
|
|
|
|
|
3
|
$self->{out} .= ' '; |
|
577
|
1
|
|
|
|
|
3
|
$self->{out} .= $m->{name}; |
|
578
|
1
|
|
|
|
|
3
|
$self->{out} .= '('; |
|
579
|
1
|
|
|
|
|
2
|
my $first = 1; |
|
580
|
1
|
50
|
|
|
|
3
|
for my $a (@{ $m->{args} || [] }) { |
|
|
1
|
|
|
|
|
4
|
|
|
581
|
2
|
100
|
|
|
|
6
|
$self->{out} .= ', ' unless $first; |
|
582
|
2
|
|
|
|
|
3
|
$first = 0; |
|
583
|
2
|
|
|
|
|
7
|
$self->_emit_modifier_arg($a); |
|
584
|
|
|
|
|
|
|
} |
|
585
|
1
|
|
|
|
|
3
|
$self->{out} .= ')'; |
|
586
|
|
|
|
|
|
|
} |
|
587
|
7
|
|
|
|
|
11
|
$self->{out} .= "\n"; |
|
588
|
7
|
50
|
|
|
|
16
|
if (length($body) == 0) { |
|
589
|
|
|
|
|
|
|
# nothing — terminator on its own line follows |
|
590
|
|
|
|
|
|
|
} else { |
|
591
|
7
|
|
|
|
|
25
|
for my $line (split /\n/, $body, -1) { |
|
592
|
7
|
50
|
|
|
|
15
|
if (length($line) == 0) { |
|
593
|
0
|
|
|
|
|
0
|
$self->{out} .= "\n"; |
|
594
|
|
|
|
|
|
|
} else { |
|
595
|
7
|
|
|
|
|
14
|
$self->{out} .= $body_indent_str; |
|
596
|
7
|
|
|
|
|
13
|
$self->{out} .= $line; |
|
597
|
7
|
|
|
|
|
16
|
$self->{out} .= "\n"; |
|
598
|
|
|
|
|
|
|
} |
|
599
|
|
|
|
|
|
|
} |
|
600
|
|
|
|
|
|
|
} |
|
601
|
7
|
|
|
|
|
16
|
$self->{out} .= $term_indent_str; |
|
602
|
7
|
100
|
|
|
|
16
|
if (defined $label) { |
|
603
|
5
|
|
|
|
|
15
|
$self->{out} .= $label; |
|
604
|
|
|
|
|
|
|
} else { |
|
605
|
2
|
100
|
|
|
|
8
|
$self->{out} .= ($flavor eq 'basic_triple') ? '"""' : "'''"; |
|
606
|
|
|
|
|
|
|
} |
|
607
|
|
|
|
|
|
|
} |
|
608
|
|
|
|
|
|
|
|
|
609
|
|
|
|
|
|
|
sub _emit_modifier_arg { |
|
610
|
2
|
|
|
2
|
|
5
|
my ($self, $v) = @_; |
|
611
|
2
|
50
|
|
|
|
22
|
if (blessed($v)) { |
|
612
|
0
|
|
|
|
|
0
|
my $cls = ref($v); |
|
613
|
0
|
0
|
0
|
|
|
0
|
if ($cls eq 'DMS::Parser::Bool') { $self->{out} .= $v->value ? 'true' : 'false'; } |
|
|
0
|
0
|
0
|
|
|
0
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
614
|
0
|
|
|
|
|
0
|
elsif ($cls eq 'DMS::Parser::Integer') { $self->{out} .= $v->bstr; } |
|
615
|
0
|
|
|
|
|
0
|
elsif ($cls eq 'DMS::Parser::Float') { $self->_emit_float($v->value); } |
|
616
|
|
|
|
|
|
|
elsif ($cls eq 'DMS::Parser::OffsetDateTime' |
|
617
|
|
|
|
|
|
|
|| $cls eq 'DMS::Parser::LocalDateTime' |
|
618
|
|
|
|
|
|
|
|| $cls eq 'DMS::Parser::LocalDate' |
|
619
|
0
|
|
|
|
|
0
|
|| $cls eq 'DMS::Parser::LocalTime') { $self->{out} .= $v->value; } |
|
620
|
0
|
|
|
|
|
0
|
else { die "modifier arg: unknown blessed class $cls"; } |
|
621
|
0
|
|
|
|
|
0
|
return; |
|
622
|
|
|
|
|
|
|
} |
|
623
|
2
|
50
|
|
|
|
12
|
if (_is_list($v)) { $self->{out} .= '[]'; return; } |
|
|
0
|
|
|
|
|
0
|
|
|
|
0
|
|
|
|
|
0
|
|
|
624
|
2
|
50
|
|
|
|
5
|
if (_is_table($v)) { $self->{out} .= '{}'; return; } |
|
|
0
|
|
|
|
|
0
|
|
|
|
0
|
|
|
|
|
0
|
|
|
625
|
|
|
|
|
|
|
# Plain scalar = string. Modifier args use basic-quoted always. |
|
626
|
2
|
|
|
|
|
6
|
$self->{out} .= '"'; |
|
627
|
2
|
|
|
|
|
8
|
$self->{out} .= _escape_basic("$v"); |
|
628
|
2
|
|
|
|
|
6
|
$self->{out} .= '"'; |
|
629
|
|
|
|
|
|
|
} |
|
630
|
|
|
|
|
|
|
|
|
631
|
|
|
|
|
|
|
sub _emit_comment_line { |
|
632
|
9
|
|
|
9
|
|
21
|
my ($self, $c, $indent) = @_; |
|
633
|
9
|
|
|
|
|
20
|
my $text = $c->{content}; |
|
634
|
9
|
|
|
|
|
19
|
my $prefix = $INDENT_STR x $indent; |
|
635
|
9
|
50
|
|
|
|
32
|
if (index($text, "\n") < 0) { |
|
636
|
9
|
|
|
|
|
22
|
$self->{out} .= $prefix; |
|
637
|
9
|
|
|
|
|
17
|
$self->{out} .= $text; |
|
638
|
9
|
|
|
|
|
20
|
$self->{out} .= "\n"; |
|
639
|
9
|
|
|
|
|
24
|
return; |
|
640
|
|
|
|
|
|
|
} |
|
641
|
|
|
|
|
|
|
# Multi-line: only the first line gets re-indented; subsequent body |
|
642
|
|
|
|
|
|
|
# lines keep their original whitespace verbatim. |
|
643
|
0
|
|
|
|
|
0
|
my @lines = split /\n/, $text, -1; |
|
644
|
0
|
|
|
|
|
0
|
for (my $i = 0; $i < @lines; $i++) { |
|
645
|
0
|
0
|
|
|
|
0
|
if ($i == 0) { |
|
646
|
0
|
|
|
|
|
0
|
$self->{out} .= $prefix; |
|
647
|
0
|
|
|
|
|
0
|
$self->{out} .= $lines[$i]; |
|
648
|
|
|
|
|
|
|
} else { |
|
649
|
0
|
|
|
|
|
0
|
$self->{out} .= "\n"; |
|
650
|
0
|
|
|
|
|
0
|
$self->{out} .= $lines[$i]; |
|
651
|
|
|
|
|
|
|
} |
|
652
|
|
|
|
|
|
|
} |
|
653
|
0
|
|
|
|
|
0
|
$self->{out} .= "\n"; |
|
654
|
|
|
|
|
|
|
} |
|
655
|
|
|
|
|
|
|
|
|
656
|
|
|
|
|
|
|
sub _emit_trailing_for { |
|
657
|
54
|
|
|
54
|
|
110
|
my ($self, $path) = @_; |
|
658
|
54
|
50
|
|
|
|
135
|
return if $self->{lite}; |
|
659
|
54
|
|
|
|
|
110
|
my $nc = $self->{comments_by_path}{ _path_key($path) }; |
|
660
|
54
|
100
|
66
|
|
|
174
|
return if !$nc || !@{ $nc->{trailing} }; |
|
|
8
|
|
|
|
|
29
|
|
|
661
|
8
|
|
|
|
|
14
|
my $first = 1; |
|
662
|
8
|
|
|
|
|
15
|
for my $c (@{ $nc->{trailing} }) { |
|
|
8
|
|
|
|
|
19
|
|
|
663
|
8
|
50
|
|
|
|
22
|
$self->{out} .= ($first ? ' ' : ' '); |
|
664
|
8
|
|
|
|
|
13
|
$first = 0; |
|
665
|
8
|
|
|
|
|
26
|
$self->{out} .= $c->{content}; |
|
666
|
|
|
|
|
|
|
} |
|
667
|
|
|
|
|
|
|
} |
|
668
|
|
|
|
|
|
|
|
|
669
|
|
|
|
|
|
|
sub _emit_inner_for { |
|
670
|
54
|
|
|
54
|
|
101
|
my ($self, $path) = @_; |
|
671
|
54
|
50
|
|
|
|
129
|
return if $self->{lite}; |
|
672
|
54
|
|
|
|
|
113
|
my $nc = $self->{comments_by_path}{ _path_key($path) }; |
|
673
|
54
|
100
|
|
|
|
148
|
return if !$nc; |
|
674
|
8
|
|
|
|
|
14
|
for my $c (@{ $nc->{inner} }) { |
|
|
8
|
|
|
|
|
23
|
|
|
675
|
0
|
|
|
|
|
0
|
$self->{out} .= $c->{content}; |
|
676
|
0
|
|
|
|
|
0
|
$self->{out} .= ' '; |
|
677
|
|
|
|
|
|
|
} |
|
678
|
|
|
|
|
|
|
} |
|
679
|
|
|
|
|
|
|
|
|
680
|
|
|
|
|
|
|
sub _has_inner { |
|
681
|
61
|
|
|
61
|
|
122
|
my ($self, $path) = @_; |
|
682
|
61
|
50
|
|
|
|
154
|
return 0 if $self->{lite}; |
|
683
|
61
|
|
|
|
|
135
|
my $nc = $self->{comments_by_path}{ _path_key($path) }; |
|
684
|
61
|
|
66
|
|
|
236
|
return $nc && @{ $nc->{inner} }; |
|
685
|
|
|
|
|
|
|
} |
|
686
|
|
|
|
|
|
|
|
|
687
|
|
|
|
|
|
|
sub _emit_floating { |
|
688
|
30
|
|
|
30
|
|
63
|
my ($self, $path, $indent) = @_; |
|
689
|
30
|
50
|
|
|
|
77
|
return if $self->{lite}; |
|
690
|
30
|
|
|
|
|
61
|
my $nc = $self->{comments_by_path}{ _path_key($path) }; |
|
691
|
30
|
100
|
|
|
|
110
|
return if !$nc; |
|
692
|
1
|
|
|
|
|
3
|
for my $c (@{ $nc->{floating} }) { |
|
|
1
|
|
|
|
|
3
|
|
|
693
|
1
|
|
|
|
|
3
|
$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
|
|
|
|
|
|
|
sub _is_flow_safe { |
|
702
|
0
|
|
|
0
|
|
0
|
my ($self, $v, $path) = @_; |
|
703
|
0
|
|
|
|
|
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
|
0
|
|
|
|
0
|
if (!$self->{lite}) { |
|
707
|
0
|
0
|
|
|
|
0
|
for my $ac (@{ $self->{doc}{comments} || [] }) { |
|
|
0
|
|
|
|
|
0
|
|
|
708
|
0
|
0
|
|
|
|
0
|
next if scalar(@{ $ac->{path} }) <= scalar(@$path); |
|
|
0
|
|
|
|
|
0
|
|
|
709
|
0
|
|
|
|
|
0
|
my $apk = _path_key($ac->{path}); |
|
710
|
0
|
0
|
|
|
|
0
|
my $prefix = $pk_prefix eq '' ? '' : "$pk_prefix\0"; |
|
711
|
0
|
0
|
|
|
|
0
|
if ($pk_prefix eq '') { |
|
|
|
0
|
|
|
|
|
|
|
712
|
|
|
|
|
|
|
# any non-empty path is descendant of root |
|
713
|
0
|
|
|
|
|
0
|
return 0; |
|
714
|
|
|
|
|
|
|
} elsif (substr($apk, 0, length($prefix)) eq $prefix) { |
|
715
|
0
|
|
|
|
|
0
|
return 0; |
|
716
|
|
|
|
|
|
|
} |
|
717
|
|
|
|
|
|
|
} |
|
718
|
|
|
|
|
|
|
} |
|
719
|
0
|
0
|
0
|
|
|
0
|
if (!ref($v) && !blessed($v)) { |
|
720
|
|
|
|
|
|
|
# plain string: check heredoc form |
|
721
|
0
|
|
|
|
|
0
|
my $lit = $self->{forms_by_path}{ _path_key($path) }; |
|
722
|
0
|
0
|
0
|
|
|
0
|
if ($lit && exists $lit->{string_form} && $lit->{string_form}{kind} eq 'heredoc') { |
|
|
|
|
0
|
|
|
|
|
|
723
|
0
|
|
|
|
|
0
|
return 0; |
|
724
|
|
|
|
|
|
|
} |
|
725
|
0
|
|
|
|
|
0
|
return 1; |
|
726
|
|
|
|
|
|
|
} |
|
727
|
0
|
0
|
|
|
|
0
|
if (blessed($v)) { return 1; } |
|
|
0
|
|
|
|
|
0
|
|
|
728
|
0
|
0
|
|
|
|
0
|
if (_is_list($v)) { |
|
729
|
0
|
|
|
|
|
0
|
for (my $i = 0; $i < @$v; $i++) { |
|
730
|
0
|
|
|
|
|
0
|
my $sub = [ @$path, DMS::Parser::Index->new($i) ]; |
|
731
|
0
|
0
|
|
|
|
0
|
return 0 if !$self->_is_flow_safe($v->[$i], $sub); |
|
732
|
|
|
|
|
|
|
} |
|
733
|
0
|
|
|
|
|
0
|
return 1; |
|
734
|
|
|
|
|
|
|
} |
|
735
|
0
|
0
|
|
|
|
0
|
if (_is_table($v)) { |
|
736
|
0
|
|
|
|
|
0
|
for my $k (_table_keys($v)) { |
|
737
|
0
|
|
|
|
|
0
|
my $sub = [ @$path, $k ]; |
|
738
|
0
|
0
|
|
|
|
0
|
return 0 if !$self->_is_flow_safe($v->{$k}, $sub); |
|
739
|
|
|
|
|
|
|
} |
|
740
|
0
|
|
|
|
|
0
|
return 1; |
|
741
|
|
|
|
|
|
|
} |
|
742
|
0
|
|
|
|
|
0
|
return 1; |
|
743
|
|
|
|
|
|
|
} |
|
744
|
|
|
|
|
|
|
|
|
745
|
|
|
|
|
|
|
sub _escape_basic { |
|
746
|
7
|
|
|
7
|
|
16
|
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
|
7
|
100
|
|
|
|
31
|
return $s if $s !~ /[\\"\x00-\x1F]/; |
|
750
|
1
|
|
|
|
|
3
|
my $out = ''; |
|
751
|
1
|
|
|
|
|
5
|
for my $ch (split //, $s) { |
|
752
|
1
|
|
|
|
|
2
|
my $code = ord($ch); |
|
753
|
1
|
50
|
|
|
|
7
|
if ($ch eq '\\') { $out .= '\\\\'; } |
|
|
0
|
50
|
|
|
|
0
|
|
|
|
|
50
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
754
|
0
|
|
|
|
|
0
|
elsif ($ch eq '"') { $out .= '\\"'; } |
|
755
|
1
|
|
|
|
|
3
|
elsif ($ch eq "\n") { $out .= '\\n'; } |
|
756
|
0
|
|
|
|
|
0
|
elsif ($ch eq "\r") { $out .= '\\r'; } |
|
757
|
0
|
|
|
|
|
0
|
elsif ($ch eq "\t") { $out .= '\\t'; } |
|
758
|
0
|
|
|
|
|
0
|
elsif ($ch eq "\b") { $out .= '\\b'; } |
|
759
|
0
|
|
|
|
|
0
|
elsif ($ch eq "\f") { $out .= '\\f'; } |
|
760
|
0
|
|
|
|
|
0
|
elsif ($code < 0x20) { $out .= sprintf('\\u%04X', $code); } |
|
761
|
0
|
|
|
|
|
0
|
else { $out .= $ch; } |
|
762
|
|
|
|
|
|
|
} |
|
763
|
1
|
|
|
|
|
4
|
return $out; |
|
764
|
|
|
|
|
|
|
} |
|
765
|
|
|
|
|
|
|
|
|
766
|
|
|
|
|
|
|
sub _is_bare_key_char_emit { |
|
767
|
0
|
|
|
0
|
|
0
|
my ($c) = @_; |
|
768
|
0
|
0
|
0
|
|
|
0
|
return 1 if $c eq '_' || $c eq '-'; |
|
769
|
0
|
|
|
|
|
0
|
my $o = ord($c); |
|
770
|
0
|
0
|
|
|
|
0
|
if ($o < 128) { |
|
771
|
0
|
|
|
|
|
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
|
|
|
|
|
0
|
require DMS::Parser; |
|
777
|
0
|
|
|
|
|
0
|
return DMS::Parser::_is_xid_continue($o); |
|
778
|
|
|
|
|
|
|
} |
|
779
|
|
|
|
|
|
|
|
|
780
|
|
|
|
|
|
|
sub _format_key { |
|
781
|
51
|
|
|
51
|
|
105
|
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
|
51
|
50
|
|
|
|
299
|
return $k if $k =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/; |
|
786
|
0
|
0
|
|
|
|
|
if (length($k) > 0) { |
|
787
|
0
|
|
|
|
|
|
my $bare = 1; |
|
788
|
0
|
|
|
|
|
|
for my $ch (split //, $k) { |
|
789
|
0
|
0
|
|
|
|
|
if (!_is_bare_key_char_emit($ch)) { $bare = 0; last; } |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
790
|
|
|
|
|
|
|
} |
|
791
|
0
|
0
|
|
|
|
|
return $k if $bare; |
|
792
|
|
|
|
|
|
|
} |
|
793
|
|
|
|
|
|
|
# Quoted: prefer literal if no single quote, no LF/CR. |
|
794
|
0
|
0
|
0
|
|
|
|
if (index($k, "'") < 0 && index($k, "\n") < 0 && index($k, "\r") < 0) { |
|
|
|
|
0
|
|
|
|
|
|
795
|
0
|
|
|
|
|
|
return "'$k'"; |
|
796
|
|
|
|
|
|
|
} |
|
797
|
0
|
|
|
|
|
|
return '"' . _escape_basic($k) . '"'; |
|
798
|
|
|
|
|
|
|
} |
|
799
|
|
|
|
|
|
|
|
|
800
|
|
|
|
|
|
|
1; |
|
801
|
|
|
|
|
|
|
|
|
802
|
|
|
|
|
|
|
__END__ |