@skillstech/thunderlang 0.5.10 → 0.5.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +450 -0
- package/dist/core.cjs +30434 -7577
- package/dist/index.cjs +28576 -5328
- package/dist/learn.cjs +24201 -5367
- package/package.json +12 -3
- package/src/bicep.mjs +167 -0
- package/src/cli.mjs +169 -3
- package/src/commands.mjs +464 -0
- package/src/core.d.ts +12 -4
- package/src/core.mjs +12 -4
- package/src/emit.mjs +1 -1
- package/src/executed.d.mts +53 -0
- package/src/executed.mjs +465 -0
- package/src/faithful.mjs +137 -0
- package/src/file-formats.mjs +35 -0
- package/src/hcl.mjs +94 -0
- package/src/index.d.ts +7 -1
- package/src/index.mjs +6 -1
- package/src/infra-bicep.mjs +312 -0
- package/src/infra-cloud.mjs +348 -0
- package/src/infra-compose.mjs +315 -0
- package/src/infra-containers.mjs +100 -0
- package/src/infra-docker.mjs +267 -0
- package/src/infra-expr.mjs +509 -0
- package/src/infra-gha.mjs +350 -0
- package/src/infra-k8s.mjs +657 -0
- package/src/infra-terraform.mjs +499 -0
- package/src/infra-words.mjs +811 -0
- package/src/libraries.mjs +202 -0
- package/src/lift.mjs +49 -6
- package/src/meaning.d.mts +89 -0
- package/src/meaning.mjs +2544 -0
- package/src/model.d.mts +65 -14
- package/src/model.mjs +496 -80
- package/src/powershell.mjs +1361 -0
- package/src/read-repo-fs.mjs +29 -7
- package/src/read-repo.d.mts +5 -1
- package/src/read-repo.mjs +33 -7
- package/src/read.d.mts +21 -1
- package/src/read.mjs +1017 -42
- package/src/review.d.mts +35 -0
- package/src/review.mjs +671 -0
- package/src/rlang.mjs +1084 -0
- package/src/round-trip.d.mts +12 -0
- package/src/round-trip.mjs +174 -0
- package/src/runners/thunder-generate.mjs +56 -0
- package/src/runners/thunder-trace.cjs +284 -0
- package/src/runners/thunder_trace.py +599 -0
- package/src/running.d.mts +104 -0
- package/src/running.mjs +787 -0
- package/src/same.d.mts +1 -0
- package/src/same.mjs +24 -3
- package/src/shell.mjs +1212 -0
- package/src/sql.mjs +3 -2
- package/src/steps.d.mts +6 -1
- package/src/steps.mjs +1448 -86
- package/src/system.d.mts +12 -0
- package/src/system.mjs +845 -0
- package/src/things-langs.mjs +275 -0
- package/src/thunderize.mjs +3 -3
- package/src/visual.d.mts +5 -5
- package/src/visual.mjs +24 -3
- package/src/write-steps.d.mts +64 -0
- package/src/write-steps.mjs +1608 -0
- package/src/write.mjs +2 -1
- package/src/yaml.mjs +403 -0
package/src/write.mjs
CHANGED
|
@@ -22,7 +22,7 @@ const category = (t) => (t.t === 'op' ? 'cmp' : t.t);
|
|
|
22
22
|
const sameTok = (a, b) => a.t === b.t && a.v === b.v;
|
|
23
23
|
const normWhen = (w) => tokenize(w).map((t) => (t.t === 'str' ? JSON.stringify(t.v) : String(t.v))).join(' ');
|
|
24
24
|
|
|
25
|
-
const COMMENT = { python: ['#'], ruby: ['#'], php: ['#', '//', '/*'] };
|
|
25
|
+
const COMMENT = { python: ['#'], ruby: ['#'], php: ['#', '//', '/*'], r: ['#'], shell: ['#'], powershell: ['<#', '#'] };
|
|
26
26
|
const commentStarts = (language) => COMMENT[language] || ['//', '/*'];
|
|
27
27
|
|
|
28
28
|
/** Numbers, comparison operators, and text literals in a slice of the original file, with exact offsets. */
|
|
@@ -34,6 +34,7 @@ function scanSource(source, from, to, language) {
|
|
|
34
34
|
const c = source[i];
|
|
35
35
|
if (starts.some((s) => source.startsWith(s, i))) {
|
|
36
36
|
if (source.startsWith('/*', i)) { const j = source.indexOf('*/', i + 2); i = j < 0 ? to : j + 2; }
|
|
37
|
+
else if (source.startsWith('<#', i)) { const j = source.indexOf('#>', i + 2); i = j < 0 ? to : j + 2; }
|
|
37
38
|
else { const j = source.indexOf('\n', i); i = j < 0 ? to : j; }
|
|
38
39
|
continue;
|
|
39
40
|
}
|
package/src/yaml.mjs
ADDED
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
// A small YAML reader for the files Thunder reads as infrastructure and delivery (GitHub Actions workflows first).
|
|
2
|
+
// Not a full YAML 1.2 implementation: block mappings and sequences, flow lists `[a, b]` and maps `{ a: b }`, plain,
|
|
3
|
+
// single- and double-quoted scalars, block scalars `|` and `>` with chomping (`-`, `+`) and an indentation digit,
|
|
4
|
+
// comments, anchors and aliases, and `${{ … }}` expressions kept exactly as text. Every node keeps its line, so a
|
|
5
|
+
// reading can always point at the original. Walks line by line; no regex here can backtrack on long runs.
|
|
6
|
+
// Zero dependencies, browser-safe.
|
|
7
|
+
//
|
|
8
|
+
// Nodes: { t: 'map', line, entries: [{ key, line, value }] }
|
|
9
|
+
// { t: 'seq', line, items: [node] }
|
|
10
|
+
// { t: 'str', line, v, plain, block? } (scalars stay text; `toJS` gives YAML's core types)
|
|
11
|
+
|
|
12
|
+
export class YamlError extends Error {
|
|
13
|
+
constructor(message, line) { super(`${message} (line ${line})`); this.line = line; }
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const indentOf = (s) => { let i = 0; while (i < s.length && s[i] === ' ') i++; return i; };
|
|
17
|
+
const isBlank = (s) => { for (let i = 0; i < s.length; i++) { const c = s[i]; if (c === '#') return true; if (c !== ' ' && c !== '\t' && c !== '\r') return false; } return true; };
|
|
18
|
+
|
|
19
|
+
/** Drop a trailing comment (` # …`) that sits outside quotes and outside `${{ … }}`. */
|
|
20
|
+
export function stripComment(s) {
|
|
21
|
+
let q = null;
|
|
22
|
+
for (let i = 0; i < s.length; i++) {
|
|
23
|
+
const c = s[i];
|
|
24
|
+
if (q) {
|
|
25
|
+
if (q === '"' && c === '\\') { i++; continue; }
|
|
26
|
+
if (c === q) { if (q === "'" && s[i + 1] === "'") { i++; continue; } q = null; }
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
if (c === '$' && s[i + 1] === '{' && s[i + 2] === '{') { const end = s.indexOf('}}', i + 3); if (end < 0) return s; i = end + 1; continue; }
|
|
30
|
+
if ((c === '"' || c === "'") && (i === 0 || /[\s[{,:-]/.test(s[i - 1]))) { q = c; continue; }
|
|
31
|
+
if (c === '#' && (i === 0 || s[i - 1] === ' ' || s[i - 1] === '\t')) return s.slice(0, i).replace(/[ \t]+$/, '');
|
|
32
|
+
}
|
|
33
|
+
return s.replace(/[ \t\r]+$/, '');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Where a mapping key ends: the index of its `:`, or -1 when the text is not `key: value`. */
|
|
37
|
+
function keyColon(s) {
|
|
38
|
+
if (!s || s[0] === '-' && (s[1] === ' ' || s.length === 1) || s[0] === '[' || s[0] === '{' || s[0] === '#' || s[0] === '|' || s[0] === '>') return -1;
|
|
39
|
+
if (s[0] === '"' || s[0] === "'") {
|
|
40
|
+
const end = quoteEnd(s, 0);
|
|
41
|
+
if (end < 0) return -1;
|
|
42
|
+
let j = end + 1;
|
|
43
|
+
while (s[j] === ' ') j++;
|
|
44
|
+
return s[j] === ':' && (j + 1 === s.length || s[j + 1] === ' ' || s[j + 1] === '\t') ? j : -1;
|
|
45
|
+
}
|
|
46
|
+
for (let i = 0; i < s.length; i++) {
|
|
47
|
+
const c = s[i];
|
|
48
|
+
if (c === '$' && s[i + 1] === '{' && s[i + 2] === '{') { const end = s.indexOf('}}', i + 3); if (end < 0) return -1; i = end + 1; continue; }
|
|
49
|
+
if (c === ' ' && s[i + 1] === '#') return -1;
|
|
50
|
+
if (c === ':' && (i + 1 === s.length || s[i + 1] === ' ' || s[i + 1] === '\t')) return i;
|
|
51
|
+
}
|
|
52
|
+
return -1;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Index of the closing quote for the quote at `at`, or -1. */
|
|
56
|
+
function quoteEnd(s, at) {
|
|
57
|
+
const q = s[at];
|
|
58
|
+
for (let i = at + 1; i < s.length; i++) {
|
|
59
|
+
if (q === '"' && s[i] === '\\') { i++; continue; }
|
|
60
|
+
if (s[i] === q) { if (q === "'" && s[i + 1] === "'") { i++; continue; } return i; }
|
|
61
|
+
}
|
|
62
|
+
return -1;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const ESC = { n: '\n', t: '\t', r: '\r', '0': '\0', '"': '"', '\\': '\\', '/': '/', ' ': ' ', e: '\x1b', b: '\b', f: '\f', a: '\x07', v: '\v', N: '\u0085', _: ' ' };
|
|
66
|
+
function unquote(s) {
|
|
67
|
+
if (s[0] === "'") return s.slice(1, -1).replace(/''/g, "'");
|
|
68
|
+
let out = '';
|
|
69
|
+
for (let i = 1; i < s.length - 1; i++) {
|
|
70
|
+
const c = s[i];
|
|
71
|
+
if (c !== '\\') { out += c; continue; }
|
|
72
|
+
const n = s[++i];
|
|
73
|
+
if (n === 'x' || n === 'u' || n === 'U') { const len = n === 'x' ? 2 : n === 'u' ? 4 : 8; out += String.fromCodePoint(parseInt(s.slice(i + 1, i + 1 + len), 16) || 0); i += len; continue; }
|
|
74
|
+
out += ESC[n] ?? n;
|
|
75
|
+
}
|
|
76
|
+
return out;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Fold the lines of a multi-line quoted or plain scalar: one newline becomes a space, a blank line a newline. */
|
|
80
|
+
function foldLines(parts) {
|
|
81
|
+
let out = '';
|
|
82
|
+
let blank = 0;
|
|
83
|
+
for (const p of parts) {
|
|
84
|
+
const t = p.trim();
|
|
85
|
+
if (!t) { blank++; continue; }
|
|
86
|
+
out += out ? (blank ? '\n'.repeat(blank) : ' ') + t : t;
|
|
87
|
+
blank = 0;
|
|
88
|
+
}
|
|
89
|
+
return out;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Parse YAML text. Returns the first document's node (null when empty); `parseYamlAll` gives every document. */
|
|
93
|
+
export function parseYaml(src) { return parseYamlAll(src)[0] ?? null; }
|
|
94
|
+
|
|
95
|
+
export function parseYamlAll(src) {
|
|
96
|
+
return parseYamlDocs(src).map((d) => { if (d.error) throw d.error; return d.node; });
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Every document of a multi-document file (`---` between them), each parsed on its own so one unreadable document
|
|
101
|
+
* does not hide the others: [{ node, line, endLine, error? }]. `line` is the document's first written line; documents
|
|
102
|
+
* that hold only comments are left out. Kubernetes files often hold many resources this way.
|
|
103
|
+
*/
|
|
104
|
+
export function parseYamlDocs(src) {
|
|
105
|
+
const raw = String(src ?? '').replace(/^/, '').split('\n').map((l) => l.replace(/\r$/, ''));
|
|
106
|
+
for (let i = 0; i < raw.length; i++) if (raw[i].includes('\t') && /^\s*\t/.test(raw[i]) && !isBlank(raw[i])) raw[i] = raw[i].replace(/^[ \t]+/, (w) => ' '.repeat(w.replace(/\t/g, ' ').length));
|
|
107
|
+
const docs = [];
|
|
108
|
+
const push = (lines, start) => {
|
|
109
|
+
let first = 0;
|
|
110
|
+
while (first < lines.length && (isBlank(lines[first]) || /^%/.test(lines[first]))) first++;
|
|
111
|
+
if (first === lines.length) return;
|
|
112
|
+
let last = lines.length - 1;
|
|
113
|
+
while (last > first && isBlank(lines[last])) last--;
|
|
114
|
+
const doc = { node: null, line: start + first + 1, endLine: start + last + 1 };
|
|
115
|
+
try { doc.node = parseDoc(lines, start); } catch (e) { doc.error = e instanceof YamlError ? e : new YamlError(e?.message ?? 'Unreadable YAML', doc.line); }
|
|
116
|
+
docs.push(doc);
|
|
117
|
+
};
|
|
118
|
+
let start = 0;
|
|
119
|
+
for (let i = 0; i <= raw.length; i++) {
|
|
120
|
+
const l = raw[i];
|
|
121
|
+
if (i === raw.length || /^(---|\.\.\.)(\s|$)/.test(l ?? '')) {
|
|
122
|
+
push(raw.slice(start, i), start);
|
|
123
|
+
// `--- value` on the marker line itself starts the next document on that line (a comment there does not).
|
|
124
|
+
if (i < raw.length && /^---\s+[^\s#]/.test(l)) { raw[i] = l.slice(3).replace(/^\s+/, ''); start = i; continue; }
|
|
125
|
+
start = i + 1;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return docs;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function parseDoc(lines, offset) {
|
|
132
|
+
const L = lines.map((l) => (/^%/.test(l) ? '' : l));
|
|
133
|
+
let pos = 0;
|
|
134
|
+
const anchors = new Map();
|
|
135
|
+
const lineNo = (i) => i + offset + 1;
|
|
136
|
+
const skip = () => { while (pos < L.length && isBlank(L[pos])) pos++; };
|
|
137
|
+
|
|
138
|
+
/** A value written on one line after `key:` or `- `, possibly continuing on more-indented lines. */
|
|
139
|
+
function inlineValue(text, at, parentIndent) {
|
|
140
|
+
let s = text;
|
|
141
|
+
let anchor = null;
|
|
142
|
+
let m;
|
|
143
|
+
// Tags (!!str, !Ref) and anchors (&name) before a value; an alias (*name) is the value.
|
|
144
|
+
let tag = null;
|
|
145
|
+
while ((m = /^(![^\s]*|&[^\s]+)\s*/.exec(s))) { if (m[1][0] === '&') anchor = m[1].slice(1); else tag = m[1]; s = s.slice(m[0].length); }
|
|
146
|
+
let node;
|
|
147
|
+
if (s[0] === '*' && /^\*[^\s,\]}]+$/.test(s)) {
|
|
148
|
+
const ref = anchors.get(s.slice(1));
|
|
149
|
+
if (!ref) throw new YamlError(`Unknown alias ${s}`, lineNo(at));
|
|
150
|
+
node = ref;
|
|
151
|
+
} else if (!s) {
|
|
152
|
+
node = block(parentIndent, at);
|
|
153
|
+
} else if (s[0] === '|' || s[0] === '>') {
|
|
154
|
+
node = blockScalar(s, at, parentIndent);
|
|
155
|
+
} else if (s[0] === '[' || s[0] === '{') {
|
|
156
|
+
// A flow collection may span several lines: gather until its brackets balance.
|
|
157
|
+
let textAll = s;
|
|
158
|
+
while (!balanced(textAll) && pos < L.length) { textAll += ` ${stripComment(L[pos]).trim()}`; pos++; }
|
|
159
|
+
const p = { s: textAll, i: 0, line: lineNo(at) };
|
|
160
|
+
node = flowValue(p);
|
|
161
|
+
ws(p);
|
|
162
|
+
if (p.i < p.s.length) throw new YamlError(`Unexpected text after a flow collection: ${p.s.slice(p.i, p.i + 20)}`, lineNo(at));
|
|
163
|
+
} else if (s[0] === '"' || s[0] === "'") {
|
|
164
|
+
// A quoted value may continue on the next lines; its line breaks fold into spaces.
|
|
165
|
+
let full = s;
|
|
166
|
+
while (quoteEnd(full, 0) < 0 && pos < L.length) { full += `\n${L[pos].trim()}`; pos++; }
|
|
167
|
+
const end = quoteEnd(full, 0);
|
|
168
|
+
if (end < 0) throw new YamlError('A quoted value is not closed', lineNo(at));
|
|
169
|
+
node = { t: 'str', line: lineNo(at), v: unquote(full.slice(0, end + 1).replace(/[ \t]*\n[ \t]*/g, ' ')), plain: false };
|
|
170
|
+
} else {
|
|
171
|
+
// A plain scalar, continued on following lines that are indented deeper than its parent.
|
|
172
|
+
const parts = [s];
|
|
173
|
+
while (pos < L.length) {
|
|
174
|
+
const n = L[pos];
|
|
175
|
+
if (isBlank(n)) { if (n.trim() === '' && pos + 1 < L.length && !isBlank(L[pos + 1]) && indentOf(L[pos + 1]) > parentIndent) { parts.push(''); pos++; continue; } break; }
|
|
176
|
+
if (indentOf(n) <= parentIndent) break;
|
|
177
|
+
const t = stripComment(n).trim();
|
|
178
|
+
if (keyColon(t) >= 0 || /^- |^-$/.test(t)) break;
|
|
179
|
+
parts.push(t);
|
|
180
|
+
pos++;
|
|
181
|
+
}
|
|
182
|
+
node = { t: 'str', line: lineNo(at), v: parts.length > 1 ? foldLines(parts) : s, plain: true };
|
|
183
|
+
}
|
|
184
|
+
if (tag === '!!str' && node.t === 'str') node = { ...node, plain: false };
|
|
185
|
+
if (anchor) anchors.set(anchor, node);
|
|
186
|
+
return node;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** A nested block (mapping or sequence) under a key or dash, or null when nothing is indented under it. */
|
|
190
|
+
function block(parentIndent, at, allowSameIndentSeq = true) {
|
|
191
|
+
skip();
|
|
192
|
+
if (pos >= L.length) return { t: 'str', line: lineNo(at), v: null, plain: true };
|
|
193
|
+
const ind = indentOf(L[pos]);
|
|
194
|
+
const t = L[pos].slice(ind);
|
|
195
|
+
// `key:` followed by a sequence at the same indentation is allowed (`steps:\n- run: x`).
|
|
196
|
+
if (ind > parentIndent || (allowSameIndentSeq && ind === parentIndent && /^-( |$)/.test(t))) return node(ind);
|
|
197
|
+
return { t: 'str', line: lineNo(at), v: null, plain: true };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function node(ind) {
|
|
201
|
+
skip();
|
|
202
|
+
const t = stripComment(L[pos]).slice(ind);
|
|
203
|
+
if (/^-( |$)/.test(t)) return seq(ind);
|
|
204
|
+
if (keyColon(t) >= 0) return map(ind);
|
|
205
|
+
const at = pos++;
|
|
206
|
+
return inlineValue(t.trim(), at, ind - 1);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function map(ind) {
|
|
210
|
+
const out = { t: 'map', line: lineNo(pos), entries: [] };
|
|
211
|
+
while (true) {
|
|
212
|
+
skip();
|
|
213
|
+
if (pos >= L.length) break;
|
|
214
|
+
const l = L[pos];
|
|
215
|
+
const li = indentOf(l);
|
|
216
|
+
if (li < ind) break;
|
|
217
|
+
if (li > ind) throw new YamlError('This line is indented more than the lines around it', lineNo(pos));
|
|
218
|
+
const t = stripComment(l).slice(ind);
|
|
219
|
+
const c = keyColon(t);
|
|
220
|
+
if (c < 0) { if (/^-( |$)/.test(t)) break; throw new YamlError(`Expected "key: value", found "${t.slice(0, 40)}"`, lineNo(pos)); }
|
|
221
|
+
const rawKey = t.slice(0, c).trim();
|
|
222
|
+
const key = rawKey[0] === '"' || rawKey[0] === "'" ? unquote(rawKey) : rawKey;
|
|
223
|
+
const at = pos++;
|
|
224
|
+
const rest = t.slice(c + 1).trim();
|
|
225
|
+
const value = rest ? inlineValue(rest, at, ind) : block(ind, at);
|
|
226
|
+
if (key === '<<') { // merge key: copy the referenced mapping's entries
|
|
227
|
+
for (const src of value.t === 'seq' ? value.items : [value]) if (src.t === 'map') for (const e of src.entries) if (!out.entries.some((x) => x.key === e.key)) out.entries.push(e);
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
out.entries.push({ key, line: lineNo(at), value });
|
|
231
|
+
}
|
|
232
|
+
return out;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function seq(ind) {
|
|
236
|
+
const out = { t: 'seq', line: lineNo(pos), items: [] };
|
|
237
|
+
while (true) {
|
|
238
|
+
skip();
|
|
239
|
+
if (pos >= L.length) break;
|
|
240
|
+
const l = L[pos];
|
|
241
|
+
const li = indentOf(l);
|
|
242
|
+
if (li !== ind || !/^-( |$)/.test(l.slice(ind))) break;
|
|
243
|
+
const after = l.slice(ind + 1);
|
|
244
|
+
const extra = indentOf(after);
|
|
245
|
+
const rest = stripComment(after).trim();
|
|
246
|
+
if (!rest) { const at = pos++; out.items.push(block(ind, at, false)); continue; }
|
|
247
|
+
// `- key: value` starts a mapping whose keys line up one column after the dash and its spaces.
|
|
248
|
+
const col = ind + 1 + extra;
|
|
249
|
+
if (keyColon(rest) >= 0 || /^-( |$)/.test(rest)) { L[pos] = ' '.repeat(col) + after.slice(extra); out.items.push(node(col)); continue; }
|
|
250
|
+
const at = pos++;
|
|
251
|
+
out.items.push(inlineValue(rest, at, ind));
|
|
252
|
+
}
|
|
253
|
+
return out;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function blockScalar(head, at, parentIndent) {
|
|
257
|
+
const m = /^([|>])([1-9]?)([-+]?)([1-9]?)\s*$/.exec(head);
|
|
258
|
+
if (!m) throw new YamlError(`Unreadable block scalar header "${head}"`, lineNo(at));
|
|
259
|
+
const folded = m[1] === '>';
|
|
260
|
+
const chomp = m[3] || '';
|
|
261
|
+
const explicit = Number(m[2] || m[4] || 0);
|
|
262
|
+
let contentIndent = explicit ? parentIndent + explicit : 0;
|
|
263
|
+
const body = [];
|
|
264
|
+
let first = null;
|
|
265
|
+
while (pos < L.length) {
|
|
266
|
+
const l = L[pos];
|
|
267
|
+
if (l.trim() === '') { body.push(''); pos++; continue; }
|
|
268
|
+
const li = indentOf(l);
|
|
269
|
+
if (!contentIndent) { if (li <= Math.max(parentIndent, -1)) break; contentIndent = li; }
|
|
270
|
+
if (li < contentIndent) break;
|
|
271
|
+
if (first == null) first = pos;
|
|
272
|
+
body.push(l.slice(contentIndent));
|
|
273
|
+
pos++;
|
|
274
|
+
}
|
|
275
|
+
// Trailing blank lines belong to the scalar only for keep chomping; give the rest back.
|
|
276
|
+
let trailing = 0;
|
|
277
|
+
while (body.length && body[body.length - 1] === '') { body.pop(); trailing++; }
|
|
278
|
+
let text;
|
|
279
|
+
if (!folded) text = body.join('\n');
|
|
280
|
+
else {
|
|
281
|
+
text = '';
|
|
282
|
+
for (let i = 0; i < body.length; i++) {
|
|
283
|
+
const cur = body[i];
|
|
284
|
+
if (i === 0) { text = cur; continue; }
|
|
285
|
+
const prev = body[i - 1];
|
|
286
|
+
const keepBreak = cur === '' || prev === '' || /^\s/.test(cur) || /^\s/.test(prev);
|
|
287
|
+
text += keepBreak ? `\n${cur}` : ` ${cur}`;
|
|
288
|
+
}
|
|
289
|
+
text = text.replace(/\n\n/g, '\n');
|
|
290
|
+
}
|
|
291
|
+
if (body.length) text += chomp === '-' ? '' : chomp === '+' ? '\n'.repeat(trailing + 1) : '\n';
|
|
292
|
+
const leading = first == null ? 0 : body.findIndex((b) => b !== '');
|
|
293
|
+
return { t: 'str', line: lineNo(at), v: text, plain: false, block: folded ? 'folded' : 'literal', valueLine: first == null ? lineNo(at) + 1 : lineNo(first) - Math.max(leading, 0) };
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// ── Flow collections: [a, b] and { a: b } ──────────────────────────────────────────────────────────────
|
|
297
|
+
function balanced(s) {
|
|
298
|
+
let d = 0;
|
|
299
|
+
for (let i = 0; i < s.length; i++) {
|
|
300
|
+
const c = s[i];
|
|
301
|
+
if (c === '"' || c === "'") { const e = quoteEnd(s, i); if (e < 0) return false; i = e; continue; }
|
|
302
|
+
if (c === '$' && s[i + 1] === '{' && s[i + 2] === '{') { const e = s.indexOf('}}', i + 3); if (e < 0) return false; i = e + 1; continue; }
|
|
303
|
+
if (c === '[' || c === '{') d++;
|
|
304
|
+
else if (c === ']' || c === '}') d--;
|
|
305
|
+
}
|
|
306
|
+
return d <= 0;
|
|
307
|
+
}
|
|
308
|
+
function ws(p) { while (p.i < p.s.length && (p.s[p.i] === ' ' || p.s[p.i] === '\t')) p.i++; }
|
|
309
|
+
function flowScalar(p, stops) {
|
|
310
|
+
ws(p);
|
|
311
|
+
const c = p.s[p.i];
|
|
312
|
+
if (c === '"' || c === "'") {
|
|
313
|
+
const e = quoteEnd(p.s, p.i);
|
|
314
|
+
if (e < 0) throw new YamlError('A quoted value is not closed', p.line);
|
|
315
|
+
const v = unquote(p.s.slice(p.i, e + 1));
|
|
316
|
+
p.i = e + 1;
|
|
317
|
+
return { t: 'str', line: p.line, v, plain: false };
|
|
318
|
+
}
|
|
319
|
+
let out = '';
|
|
320
|
+
while (p.i < p.s.length) {
|
|
321
|
+
const ch = p.s[p.i];
|
|
322
|
+
if (ch === '$' && p.s[p.i + 1] === '{' && p.s[p.i + 2] === '{') { const e = p.s.indexOf('}}', p.i + 3); const end = e < 0 ? p.s.length : e + 2; out += p.s.slice(p.i, end); p.i = end; continue; }
|
|
323
|
+
if (stops.includes(ch)) break;
|
|
324
|
+
if (ch === ':' && (p.s[p.i + 1] === ' ' || p.s[p.i + 1] === ',' || p.s[p.i + 1] === '}' || p.i + 1 === p.s.length) && stops.includes(':')) break;
|
|
325
|
+
out += ch; p.i++;
|
|
326
|
+
}
|
|
327
|
+
return { t: 'str', line: p.line, v: out.trim(), plain: true };
|
|
328
|
+
}
|
|
329
|
+
function flowValue(p) {
|
|
330
|
+
ws(p);
|
|
331
|
+
const c = p.s[p.i];
|
|
332
|
+
if (c === '[') {
|
|
333
|
+
p.i++;
|
|
334
|
+
const out = { t: 'seq', line: p.line, items: [] };
|
|
335
|
+
while (true) {
|
|
336
|
+
ws(p);
|
|
337
|
+
if (p.s[p.i] === ']') { p.i++; return out; }
|
|
338
|
+
if (p.i >= p.s.length) throw new YamlError('A [ list is not closed', p.line);
|
|
339
|
+
const item = p.s[p.i] === '[' || p.s[p.i] === '{' ? flowValue(p) : flowScalar(p, ',]:');
|
|
340
|
+
ws(p);
|
|
341
|
+
if (p.s[p.i] === ':') { p.i++; const v = flowValue(p); out.items.push({ t: 'map', line: p.line, entries: [{ key: item.v, line: p.line, value: v }] }); }
|
|
342
|
+
else out.items.push(item);
|
|
343
|
+
ws(p);
|
|
344
|
+
if (p.s[p.i] === ',') p.i++;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
if (c === '{') {
|
|
348
|
+
p.i++;
|
|
349
|
+
const out = { t: 'map', line: p.line, entries: [] };
|
|
350
|
+
while (true) {
|
|
351
|
+
ws(p);
|
|
352
|
+
if (p.s[p.i] === '}') { p.i++; return out; }
|
|
353
|
+
if (p.i >= p.s.length) throw new YamlError('A { map is not closed', p.line);
|
|
354
|
+
const k = flowScalar(p, ',}:');
|
|
355
|
+
ws(p);
|
|
356
|
+
let v = { t: 'str', line: p.line, v: null, plain: true };
|
|
357
|
+
if (p.s[p.i] === ':') { p.i++; ws(p); v = p.s[p.i] === ',' || p.s[p.i] === '}' ? v : flowValue(p); }
|
|
358
|
+
out.entries.push({ key: k.v, line: p.line, value: v });
|
|
359
|
+
ws(p);
|
|
360
|
+
if (p.s[p.i] === ',') p.i++;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
return flowScalar(p, ',]}');
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
skip();
|
|
367
|
+
if (pos >= L.length) return null;
|
|
368
|
+
const root = node(indentOf(L[pos]));
|
|
369
|
+
skip();
|
|
370
|
+
if (pos < L.length) throw new YamlError(`Unexpected text "${L[pos].trim().slice(0, 40)}"`, lineNo(pos));
|
|
371
|
+
return root;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// ── Helpers for readers ──────────────────────────────────────────────────────────────────────────────────
|
|
375
|
+
|
|
376
|
+
/** A scalar as YAML's core schema types it: true/false, null, numbers; quoted text stays text. */
|
|
377
|
+
export function scalar(n) {
|
|
378
|
+
if (!n || n.t !== 'str') return undefined;
|
|
379
|
+
if (!n.plain || n.v == null) return n.v;
|
|
380
|
+
const v = n.v;
|
|
381
|
+
if (v === '' || v === '~' || /^null$/i.test(v)) return null;
|
|
382
|
+
if (/^(true|false)$/i.test(v)) return /^true$/i.test(v);
|
|
383
|
+
if (/^[-+]?\d+$/.test(v)) return Number(v);
|
|
384
|
+
if (/^[-+]?(\d+\.\d*|\.\d+|\d+(\.\d*)?[eE][-+]?\d+)$/.test(v)) return Number(v);
|
|
385
|
+
return v;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/** The whole node as plain JavaScript values (lines dropped). */
|
|
389
|
+
export function toJS(n) {
|
|
390
|
+
if (!n) return null;
|
|
391
|
+
if (n.t === 'map') return Object.fromEntries(n.entries.map((e) => [e.key, toJS(e.value)]));
|
|
392
|
+
if (n.t === 'seq') return n.items.map(toJS);
|
|
393
|
+
return scalar(n);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/** A mapping's value node for `key`, or undefined. */
|
|
397
|
+
export const get = (n, key) => (n?.t === 'map' ? n.entries.findLast((e) => e.key === key)?.value : undefined);
|
|
398
|
+
/** A mapping's entry for `key` (with its line), or undefined. */
|
|
399
|
+
export const entry = (n, key) => (n?.t === 'map' ? n.entries.findLast((e) => e.key === key) : undefined);
|
|
400
|
+
/** A scalar's text, or undefined for collections and missing nodes. */
|
|
401
|
+
export const text = (n) => (n?.t === 'str' && n.v != null ? String(n.v) : undefined);
|
|
402
|
+
/** A sequence's items, a single scalar as a one-item list, or []. */
|
|
403
|
+
export const list = (n) => (n?.t === 'seq' ? n.items : n?.t === 'str' && n.v != null ? [n] : []);
|