@skillstech/thunderlang 0.5.11 → 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 +437 -0
- package/dist/core.cjs +30431 -7604
- package/dist/index.cjs +28577 -5331
- package/dist/learn.cjs +22676 -3870
- 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 +48 -6
- package/src/meaning.d.mts +89 -0
- package/src/meaning.mjs +2544 -0
- package/src/model.d.mts +59 -12
- package/src/model.mjs +462 -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 +1015 -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
|
@@ -0,0 +1,509 @@
|
|
|
1
|
+
// The expression language shared by Terraform (HCL) and Bicep, read without any library: a tokenizer that walks the
|
|
2
|
+
// text one character at a time (strings, heredocs, and `${ … }` inside them are matched by counting, never by a
|
|
3
|
+
// regex that can backtrack), and a small precedence parser. Both readers get the same node shapes, so the words for
|
|
4
|
+
// a value ("worked out when it is applied", "the setting location") are decided once. Browser-safe, deterministic.
|
|
5
|
+
//
|
|
6
|
+
// Nodes: { t, s, e, line } plus
|
|
7
|
+
// str { v, template, parts } parts: strings and nodes (the `${ … }` inside a template)
|
|
8
|
+
// num { v } bool { v } null
|
|
9
|
+
// list { items } obj { entries: [{ key, keyNode?, value, line }] }
|
|
10
|
+
// ref { path: ['azurerm_storage_account', 'main', 'id'], index: [nodes] } (a.b[0].c, a.*.b, a::b)
|
|
11
|
+
// call { name, args } get { obj, path } (a call or a bracket followed by .x)
|
|
12
|
+
// unary { op, x } bin { op, a, b } cond { test, yes, no }
|
|
13
|
+
// for { kind: 'list' | 'obj', vars, coll, key?, value, when? } if { test, body } (Bicep)
|
|
14
|
+
// lambda { vars, body } spread { x } other { why }
|
|
15
|
+
|
|
16
|
+
export class ParseError extends Error {
|
|
17
|
+
constructor(message, line) { super(`${message} (line ${line})`); this.line = line; this.why = message; }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** A function from a character offset to its 1-based line. */
|
|
21
|
+
export function lineIndex(src) {
|
|
22
|
+
const starts = [0];
|
|
23
|
+
for (let i = 0; i < src.length; i++) if (src.charCodeAt(i) === 10) starts.push(i + 1);
|
|
24
|
+
return (off) => {
|
|
25
|
+
let lo = 0, hi = starts.length - 1;
|
|
26
|
+
while (lo < hi) { const mid = (lo + hi + 1) >> 1; if (starts[mid] <= off) lo = mid; else hi = mid - 1; }
|
|
27
|
+
return lo + 1;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const isIdStart = (c) => (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c === '_';
|
|
32
|
+
const isDigit = (c) => c >= '0' && c <= '9';
|
|
33
|
+
const isIdChar = (c, hcl) => isIdStart(c) || isDigit(c) || (hcl && c === '-');
|
|
34
|
+
const PUNCT3 = ['...'];
|
|
35
|
+
const PUNCT2 = ['==', '!=', '<=', '>=', '&&', '||', '=>', '::', '??', '=~', '!~'];
|
|
36
|
+
|
|
37
|
+
/** End offset of a quoted string that starts at `i` (HCL `"…"` or Bicep `'…'`), skipping `${ … }` inside it. */
|
|
38
|
+
export function scanString(src, i, dialect) {
|
|
39
|
+
const q = src[i];
|
|
40
|
+
let j = i + 1;
|
|
41
|
+
while (j < src.length) {
|
|
42
|
+
const c = src[j];
|
|
43
|
+
if (c === '\\') { j += 2; continue; }
|
|
44
|
+
if (c === q) return j + 1;
|
|
45
|
+
if (c === '\n' && dialect === 'hcl') return j; // an unclosed string ends at the line (HCL strings are one line)
|
|
46
|
+
if (dialect === 'hcl' && (c === '$' || c === '%') && src[j + 1] === c && src[j + 2] === '{') { j += 3; continue; }
|
|
47
|
+
if ((c === '$' || (c === '%' && dialect === 'hcl')) && src[j + 1] === '{') { j = scanInterp(src, j + 2, dialect); continue; }
|
|
48
|
+
j++;
|
|
49
|
+
}
|
|
50
|
+
return j;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Offset just past the `}` that closes an interpolation whose body starts at `j`. */
|
|
54
|
+
export function scanInterp(src, j, dialect) {
|
|
55
|
+
let depth = 1;
|
|
56
|
+
const q = dialect === 'hcl' ? '"' : "'";
|
|
57
|
+
while (j < src.length) {
|
|
58
|
+
const c = src[j];
|
|
59
|
+
if (c === q) { j = scanString(src, j, dialect); continue; }
|
|
60
|
+
if (c === '{') depth++;
|
|
61
|
+
else if (c === '}') { depth--; if (!depth) return j + 1; }
|
|
62
|
+
j++;
|
|
63
|
+
}
|
|
64
|
+
return j;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Tokens of `src` between `from` and `to`. Kinds: id, num, str, heredoc, p (punctuation), nl, and comments are kept
|
|
69
|
+
* aside in `comments` ([{ line, endLine, text }]).
|
|
70
|
+
*/
|
|
71
|
+
export function tokenize(src, dialect, from = 0, to = src.length, lineAt = lineIndex(src)) {
|
|
72
|
+
const hcl = dialect === 'hcl';
|
|
73
|
+
const toks = [], comments = [];
|
|
74
|
+
let i = from;
|
|
75
|
+
while (i < to) {
|
|
76
|
+
const c = src[i];
|
|
77
|
+
if (c === ' ' || c === '\t' || c === '\r' || c === '') { i++; continue; }
|
|
78
|
+
if (c === '\n') { toks.push({ k: 'nl', v: '\n', s: i, e: i + 1 }); i++; continue; }
|
|
79
|
+
// `#` is a comment in HCL; in Bicep it starts a pragma line (#disable-next-line), read the same way.
|
|
80
|
+
if (c === '#' || (c === '/' && src[i + 1] === '/')) {
|
|
81
|
+
let j = i;
|
|
82
|
+
while (j < to && src[j] !== '\n') j++;
|
|
83
|
+
comments.push({ line: lineAt(i), endLine: lineAt(i), text: src.slice(i, j).replace(/^(#|\/\/)\s?/, '').trim(), s: i });
|
|
84
|
+
i = j; continue;
|
|
85
|
+
}
|
|
86
|
+
if (c === '/' && src[i + 1] === '*') {
|
|
87
|
+
let j = src.indexOf('*/', i + 2);
|
|
88
|
+
if (j < 0 || j > to) j = to - 2;
|
|
89
|
+
comments.push({ line: lineAt(i), endLine: lineAt(j), text: src.slice(i + 2, j).replace(/^\s*\*+/gm, '').trim(), s: i });
|
|
90
|
+
i = j + 2; continue;
|
|
91
|
+
}
|
|
92
|
+
if (hcl && c === '"') { const e = scanString(src, i, dialect); toks.push({ k: 'str', s: i, e }); i = e; continue; }
|
|
93
|
+
if (!hcl && c === "'") {
|
|
94
|
+
if (src.startsWith("'''", i)) { let e = src.indexOf("'''", i + 3); e = e < 0 ? to : e + 3; toks.push({ k: 'str', multi: true, s: i, e }); i = e; continue; }
|
|
95
|
+
const e = scanString(src, i, dialect); toks.push({ k: 'str', s: i, e }); i = e; continue;
|
|
96
|
+
}
|
|
97
|
+
if (hcl && c === '<' && src[i + 1] === '<' && (src[i + 2] === '-' || isIdStart(src[i + 2] ?? ''))) {
|
|
98
|
+
// A heredoc: <<EOF or <<-EOF, its body until a line that is only the marker.
|
|
99
|
+
let j = i + 2;
|
|
100
|
+
const strip = src[j] === '-';
|
|
101
|
+
if (strip) j++;
|
|
102
|
+
const ms = j;
|
|
103
|
+
while (j < to && isIdChar(src[j], true)) j++;
|
|
104
|
+
const marker = src.slice(ms, j);
|
|
105
|
+
let nl = src.indexOf('\n', j);
|
|
106
|
+
if (nl < 0) nl = to;
|
|
107
|
+
const bodyStart = nl + 1;
|
|
108
|
+
let k = bodyStart, end = to, bodyEnd = to;
|
|
109
|
+
while (k < to) {
|
|
110
|
+
let le = src.indexOf('\n', k);
|
|
111
|
+
if (le < 0 || le > to) le = to;
|
|
112
|
+
if (src.slice(k, le).trim() === marker) { bodyEnd = k; end = le; break; }
|
|
113
|
+
k = le + 1;
|
|
114
|
+
}
|
|
115
|
+
toks.push({ k: 'heredoc', s: i, e: end, bodyStart, bodyEnd: Math.max(bodyStart, bodyEnd), strip });
|
|
116
|
+
i = end; continue;
|
|
117
|
+
}
|
|
118
|
+
if (isDigit(c)) {
|
|
119
|
+
let j = i;
|
|
120
|
+
while (j < to && (isDigit(src[j]) || src[j] === '.' || ((src[j] === 'e' || src[j] === 'E') && /[\d+-]/.test(src[j + 1] ?? '')) || ((src[j] === '+' || src[j] === '-') && /[eE]/.test(src[j - 1])))) {
|
|
121
|
+
if (src[j] === '.' && !isDigit(src[j + 1] ?? '')) break;
|
|
122
|
+
j++;
|
|
123
|
+
}
|
|
124
|
+
toks.push({ k: 'num', v: src.slice(i, j), s: i, e: j }); i = j; continue;
|
|
125
|
+
}
|
|
126
|
+
if (isIdStart(c)) {
|
|
127
|
+
let j = i + 1;
|
|
128
|
+
while (j < to && isIdChar(src[j], hcl)) j++;
|
|
129
|
+
toks.push({ k: 'id', v: src.slice(i, j), s: i, e: j }); i = j; continue;
|
|
130
|
+
}
|
|
131
|
+
const three = src.slice(i, i + 3), two = src.slice(i, i + 2);
|
|
132
|
+
const p = PUNCT3.includes(three) ? three : PUNCT2.includes(two) ? two : c;
|
|
133
|
+
toks.push({ k: 'p', v: p, s: i, e: i + p.length });
|
|
134
|
+
i += p.length;
|
|
135
|
+
}
|
|
136
|
+
toks.push({ k: 'eof', v: '', s: to, e: to });
|
|
137
|
+
return { toks, comments };
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const BIN = { '??': 1, '||': 2, '&&': 3, '==': 4, '!=': 4, '=~': 4, '!~': 4, '<': 5, '>': 5, '<=': 5, '>=': 5, '+': 6, '-': 6, '*': 7, '/': 7, '%': 7 };
|
|
141
|
+
|
|
142
|
+
/** A parser over tokens. `hooks.objectKey(parser, tok)` may read a keyword inside an object (Bicep's nested resources). */
|
|
143
|
+
export class Parser {
|
|
144
|
+
constructor(src, toks, dialect, lineAt, hooks = {}) {
|
|
145
|
+
this.src = src; this.toks = toks; this.i = 0; this.dialect = dialect; this.lineAt = lineAt; this.hooks = hooks;
|
|
146
|
+
this.skipNl = [false];
|
|
147
|
+
}
|
|
148
|
+
get hcl() { return this.dialect === 'hcl'; }
|
|
149
|
+
raw(t = this.toks[this.i]) { return t; }
|
|
150
|
+
peek(n = 0) {
|
|
151
|
+
let i = this.i;
|
|
152
|
+
const skip = this.skipNl.at(-1);
|
|
153
|
+
for (let k = 0; ; i++) {
|
|
154
|
+
const t = this.toks[i];
|
|
155
|
+
if (!t) return this.toks.at(-1);
|
|
156
|
+
if (t.k === 'nl' && skip) continue;
|
|
157
|
+
if (k === n) return t;
|
|
158
|
+
k++;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
next() {
|
|
162
|
+
const skip = this.skipNl.at(-1);
|
|
163
|
+
while (skip && this.toks[this.i]?.k === 'nl') this.i++;
|
|
164
|
+
const t = this.toks[this.i] ?? this.toks.at(-1);
|
|
165
|
+
if (t.k !== 'eof') this.i++;
|
|
166
|
+
return t;
|
|
167
|
+
}
|
|
168
|
+
is(v, n = 0) { const t = this.peek(n); return t.k === 'p' && t.v === v; }
|
|
169
|
+
isId(v, n = 0) { const t = this.peek(n); return t.k === 'id' && (v == null || t.v === v); }
|
|
170
|
+
expect(v) {
|
|
171
|
+
const t = this.next();
|
|
172
|
+
if (t.k !== 'p' || t.v !== v) throw new ParseError(`Expected "${v}" but found ${t.k === 'eof' ? 'the end of the file' : t.k === 'nl' ? 'a new line' : `"${this.src.slice(t.s, t.e).slice(0, 20)}"`}`, this.lineAt(t.s));
|
|
173
|
+
return t;
|
|
174
|
+
}
|
|
175
|
+
newlines() { while (this.toks[this.i]?.k === 'nl') this.i++; }
|
|
176
|
+
lineOf(t) { return this.lineAt(t.s); }
|
|
177
|
+
node(t, s, e, extra) { return { t, s, e, line: this.lineAt(s), ...extra }; }
|
|
178
|
+
withNl(skip, fn) { this.skipNl.push(skip); try { return fn(); } finally { this.skipNl.pop(); } }
|
|
179
|
+
last() { let i = this.i - 1; while (i > 0 && this.toks[i].k === 'nl') i--; return this.toks[Math.max(0, i)]; }
|
|
180
|
+
|
|
181
|
+
expr() {
|
|
182
|
+
const s = this.peek().s;
|
|
183
|
+
// Bicep lambdas: x => …, (x, y) => …
|
|
184
|
+
if (!this.hcl && this.isId() && this.is('=>', 1)) { const v = this.next().v; this.next(); const body = this.expr(); return this.node('lambda', s, body.e, { vars: [v], body }); }
|
|
185
|
+
if (!this.hcl && this.is('(') && this.lambdaAhead()) {
|
|
186
|
+
this.next(); const vars = [];
|
|
187
|
+
while (!this.is(')')) { vars.push(this.next().v); if (this.is(',')) this.next(); }
|
|
188
|
+
this.next(); this.expect('=>'); const body = this.expr();
|
|
189
|
+
return this.node('lambda', s, body.e, { vars, body });
|
|
190
|
+
}
|
|
191
|
+
const test = this.binary(0);
|
|
192
|
+
// Bicep lets a ternary break its line before `?` and `:`.
|
|
193
|
+
if (!this.hcl && this.withNl(true, () => this.is('?'))) this.newlines();
|
|
194
|
+
if (this.is('?')) {
|
|
195
|
+
this.next();
|
|
196
|
+
const yes = this.withNl(true, () => this.expr());
|
|
197
|
+
this.withNl(true, () => this.expect(':'));
|
|
198
|
+
const no = this.expr();
|
|
199
|
+
return this.node('cond', s, no.e, { test, yes, no });
|
|
200
|
+
}
|
|
201
|
+
return test;
|
|
202
|
+
}
|
|
203
|
+
lambdaAhead() {
|
|
204
|
+
let k = 1;
|
|
205
|
+
for (;;) {
|
|
206
|
+
const t = this.peek(k);
|
|
207
|
+
if (t.k === 'p' && t.v === ')') return this.is('=>', k + 1);
|
|
208
|
+
if (t.k !== 'id' && !(t.k === 'p' && t.v === ',')) return false;
|
|
209
|
+
k++;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
binary(min) {
|
|
213
|
+
let a = this.unary();
|
|
214
|
+
for (;;) {
|
|
215
|
+
const t = this.peek();
|
|
216
|
+
const prec = t.k === 'p' ? BIN[t.v] : undefined;
|
|
217
|
+
if (prec == null || prec <= min || (t.v === '??' && this.hcl)) return a;
|
|
218
|
+
this.next();
|
|
219
|
+
const b = this.binary(prec);
|
|
220
|
+
a = this.node('bin', a.s, b.e, { op: t.v, a, b });
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
unary() {
|
|
224
|
+
const t = this.peek();
|
|
225
|
+
if (t.k === 'p' && (t.v === '!' || t.v === '-')) { this.next(); const x = this.unary(); return this.node('unary', t.s, x.e, { op: t.v, x }); }
|
|
226
|
+
if (t.k === 'p' && t.v === '...') { this.next(); const x = this.unary(); return this.node('spread', t.s, x.e, { x }); }
|
|
227
|
+
return this.postfix(this.primary());
|
|
228
|
+
}
|
|
229
|
+
postfix(n) {
|
|
230
|
+
for (;;) {
|
|
231
|
+
const t = this.toks[this.i];
|
|
232
|
+
if (!t || t.k !== 'p') return n;
|
|
233
|
+
if (t.v === '.' || t.v === '::') {
|
|
234
|
+
this.next();
|
|
235
|
+
if (this.is('?')) this.next(); // Bicep safe access a.?b
|
|
236
|
+
const u = this.next();
|
|
237
|
+
const part = u.k === 'p' && u.v === '*' ? '*' : u.k === 'num' ? `[${u.v}]` : u.k === 'id' ? u.v : null;
|
|
238
|
+
if (part == null) throw new ParseError('Expected a name after "."', this.lineOf(u));
|
|
239
|
+
n = this.extend(n, part, null, u.e);
|
|
240
|
+
if (this.is('(') && this.toks[this.i].k === 'p' && this.toks[this.i].v === '(' && n.t === 'ref') n = this.call(n.path.join('.'), n.s);
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
if (t.v === '[') {
|
|
244
|
+
this.next();
|
|
245
|
+
const r = this.withNl(true, () => {
|
|
246
|
+
if (this.is('?')) this.next();
|
|
247
|
+
if (this.is('*')) { this.next(); this.expect(']'); return { part: '*', idx: null }; }
|
|
248
|
+
const idx = this.expr();
|
|
249
|
+
const close = this.expect(']');
|
|
250
|
+
const lit = idx.t === 'num' ? `[${idx.v}]` : idx.t === 'str' && !idx.template ? `[${JSON.stringify(idx.v)}]` : '[…]';
|
|
251
|
+
return { part: lit, idx, e: close.e };
|
|
252
|
+
});
|
|
253
|
+
n = this.extend(n, r.part, r.idx, this.toks[this.i - 1].e);
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
// Bicep's non-null assertion `x!` (a prefix `!` never follows a value).
|
|
257
|
+
if (t.v === '!' && !this.hcl) { this.next(); continue; }
|
|
258
|
+
return n;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
extend(n, part, idx, e) {
|
|
262
|
+
if (n.t === 'ref') return { ...n, e, path: [...n.path, part], index: idx ? [...n.index, idx] : n.index };
|
|
263
|
+
return this.node('get', n.s, e, { obj: n, path: [part], index: idx ? [idx] : [] });
|
|
264
|
+
}
|
|
265
|
+
call(name, s) {
|
|
266
|
+
this.next(); // (
|
|
267
|
+
const args = this.withNl(true, () => {
|
|
268
|
+
const out = [];
|
|
269
|
+
while (!this.is(')')) {
|
|
270
|
+
if (this.peek().k === 'eof') throw new ParseError(`The call to ${name} is not closed`, this.lineAt(s));
|
|
271
|
+
out.push(this.expr());
|
|
272
|
+
if (this.is(',')) this.next();
|
|
273
|
+
else if (this.is('...')) this.next();
|
|
274
|
+
else if (!this.is(')')) throw new ParseError(`Expected "," or ")" in the call to ${name}`, this.lineOf(this.peek()));
|
|
275
|
+
}
|
|
276
|
+
return { out, close: this.next() };
|
|
277
|
+
});
|
|
278
|
+
const { close } = args;
|
|
279
|
+
return this.node('call', s, close.e, { name, args: args.out });
|
|
280
|
+
}
|
|
281
|
+
primary() {
|
|
282
|
+
const t = this.next();
|
|
283
|
+
const s = t.s;
|
|
284
|
+
if (t.k === 'num') return this.node('num', s, t.e, { v: Number(t.v) });
|
|
285
|
+
if (t.k === 'str') return this.string(t);
|
|
286
|
+
if (t.k === 'heredoc') return this.heredoc(t);
|
|
287
|
+
if (t.k === 'id') {
|
|
288
|
+
if (t.v === 'true' || t.v === 'false') return this.node('bool', s, t.e, { v: t.v === 'true' });
|
|
289
|
+
if (t.v === 'null') return this.node('null', s, t.e, {});
|
|
290
|
+
if (!this.hcl && t.v === 'if' && this.is('(')) {
|
|
291
|
+
this.next(); const test = this.withNl(true, () => this.expr()); this.withNl(true, () => this.expect(')'));
|
|
292
|
+
const body = this.expr();
|
|
293
|
+
return this.node('if', s, body.e, { test, body });
|
|
294
|
+
}
|
|
295
|
+
const nt = this.toks[this.i];
|
|
296
|
+
if (nt && nt.k === 'p' && nt.v === '(') return this.call(t.v, s);
|
|
297
|
+
return this.node('ref', s, t.e, { path: [t.v], index: [] });
|
|
298
|
+
}
|
|
299
|
+
if (t.k === 'p') {
|
|
300
|
+
if (t.v === '(') {
|
|
301
|
+
const x = this.withNl(true, () => { const x = this.expr(); this.expect(')'); return x; });
|
|
302
|
+
return { ...x, s, e: this.toks[this.i - 1].e, paren: true };
|
|
303
|
+
}
|
|
304
|
+
if (t.v === '[') return this.list(t);
|
|
305
|
+
if (t.v === '{') return this.object(t);
|
|
306
|
+
}
|
|
307
|
+
throw new ParseError(t.k === 'eof' ? 'The file ends in the middle of a value' : t.k === 'nl' ? 'A value is missing before the end of the line' : `Unexpected "${this.src.slice(t.s, t.e).slice(0, 20)}"`, this.lineOf(t));
|
|
308
|
+
}
|
|
309
|
+
forHead() {
|
|
310
|
+
this.next(); // for
|
|
311
|
+
const vars = [];
|
|
312
|
+
if (this.is('(')) { this.next(); while (!this.is(')')) { const v = this.next(); if (v.k === 'id') vars.push(v.v); } this.next(); }
|
|
313
|
+
else { vars.push(this.next().v); while (this.is(',')) { this.next(); vars.push(this.next().v); } }
|
|
314
|
+
const kw = this.next();
|
|
315
|
+
if (kw.k !== 'id' || kw.v !== 'in') throw new ParseError('Expected "in" in a for expression', this.lineOf(kw));
|
|
316
|
+
const coll = this.expr();
|
|
317
|
+
this.expect(':');
|
|
318
|
+
return { vars, coll };
|
|
319
|
+
}
|
|
320
|
+
list(open) {
|
|
321
|
+
// HCL lists ignore new lines; Bicep arrays use them to separate items.
|
|
322
|
+
return this.withNl(this.hcl, () => {
|
|
323
|
+
if (this.hcl ? this.isId('for') : (this.newlines(), this.isId('for'))) {
|
|
324
|
+
const r = this.withNl(true, () => {
|
|
325
|
+
const head = this.forHead();
|
|
326
|
+
const value = this.expr();
|
|
327
|
+
let when = null;
|
|
328
|
+
if (this.isId('if')) { this.next(); when = this.expr(); }
|
|
329
|
+
const close = this.expect(']');
|
|
330
|
+
return { head, value, when, close };
|
|
331
|
+
});
|
|
332
|
+
return this.node('for', open.s, r.close.e, { kind: 'list', vars: r.head.vars, coll: r.head.coll, value: r.value, ...(r.when ? { when: r.when } : {}) });
|
|
333
|
+
}
|
|
334
|
+
const items = [];
|
|
335
|
+
for (;;) {
|
|
336
|
+
this.newlines();
|
|
337
|
+
if (this.is(']')) break;
|
|
338
|
+
if (this.peek().k === 'eof') throw new ParseError('A list is not closed', this.lineOf(open));
|
|
339
|
+
items.push(this.expr());
|
|
340
|
+
if (this.is(',')) this.next();
|
|
341
|
+
else if (!this.is(']') && this.toks[this.i]?.k !== 'nl') throw new ParseError('Expected "," or "]" in a list', this.lineOf(this.peek()));
|
|
342
|
+
}
|
|
343
|
+
const close = this.next();
|
|
344
|
+
return this.node('list', open.s, close.e, { items });
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
object(open) {
|
|
348
|
+
return this.withNl(false, () => {
|
|
349
|
+
this.newlines();
|
|
350
|
+
if (this.isId('for') && this.hcl) {
|
|
351
|
+
const r = this.withNl(true, () => {
|
|
352
|
+
const head = this.forHead();
|
|
353
|
+
const key = this.expr(); this.expect('=>');
|
|
354
|
+
const value = this.expr();
|
|
355
|
+
if (this.is('...')) this.next();
|
|
356
|
+
let when = null;
|
|
357
|
+
if (this.isId('if')) { this.next(); when = this.expr(); }
|
|
358
|
+
const close = this.expect('}');
|
|
359
|
+
return { head, key, value, when, close };
|
|
360
|
+
});
|
|
361
|
+
return this.node('for', open.s, r.close.e, { kind: 'obj', vars: r.head.vars, coll: r.head.coll, key: r.key, value: r.value, ...(r.when ? { when: r.when } : {}) });
|
|
362
|
+
}
|
|
363
|
+
const entries = [];
|
|
364
|
+
for (;;) {
|
|
365
|
+
this.newlines();
|
|
366
|
+
if (this.is('}')) break;
|
|
367
|
+
const kt = this.peek();
|
|
368
|
+
if (kt.k === 'eof') throw new ParseError('An object is not closed', this.lineOf(open));
|
|
369
|
+
if (kt.k === 'p' && kt.v === '...') { const x = this.expr(); entries.push({ key: '...', value: x, line: x.line, spread: true }); }
|
|
370
|
+
else {
|
|
371
|
+
const hooked = this.hooks.objectKey?.(this, kt);
|
|
372
|
+
if (hooked) { entries.push(hooked); continue; }
|
|
373
|
+
let key, keyNode = null;
|
|
374
|
+
if (kt.k === 'id' && (this.is('=', 1) || this.is(':', 1))) { this.next(); key = kt.v; }
|
|
375
|
+
else if (kt.k === 'str' && (this.is('=', 1) || this.is(':', 1))) { keyNode = this.string(this.next()); key = keyNode.template ? this.src.slice(kt.s, kt.e) : keyNode.v; }
|
|
376
|
+
else { keyNode = this.withNl(true, () => this.binary(0)); key = this.src.slice(keyNode.s, keyNode.e); }
|
|
377
|
+
const sep = this.next();
|
|
378
|
+
if (sep.k !== 'p' || (sep.v !== '=' && sep.v !== ':')) throw new ParseError(`Expected "=" or ":" after ${key}`, this.lineOf(sep));
|
|
379
|
+
const value = this.expr();
|
|
380
|
+
entries.push({ key, ...(keyNode && keyNode.t !== 'str' ? { keyNode } : {}), value, line: this.lineOf(kt) });
|
|
381
|
+
}
|
|
382
|
+
if (this.is(',')) this.next();
|
|
383
|
+
else if (!this.is('}') && this.toks[this.i]?.k !== 'nl') throw new ParseError('Expected a new line, "," or "}" in an object', this.lineOf(this.peek()));
|
|
384
|
+
}
|
|
385
|
+
const close = this.next();
|
|
386
|
+
return this.node('obj', open.s, close.e, { entries });
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
/** A quoted string, with its `${ … }` parts parsed (a template is worked out when it is applied). */
|
|
390
|
+
string(t) {
|
|
391
|
+
if (t.multi) return this.node('str', t.s, t.e, { v: this.src.slice(t.s + 3, Math.max(t.s + 3, t.e - 3)).replace(/^\r?\n/, ''), template: false, parts: [] });
|
|
392
|
+
const q = this.src[t.s];
|
|
393
|
+
const end = this.src[t.e - 1] === q && t.e - 1 > t.s ? t.e - 1 : t.e;
|
|
394
|
+
return this.template(t.s, t.s + 1, end, t.e, false);
|
|
395
|
+
}
|
|
396
|
+
heredoc(t) {
|
|
397
|
+
const n = this.template(t.s, t.bodyStart, t.bodyEnd, t.e, true);
|
|
398
|
+
if (t.strip && !n.template) {
|
|
399
|
+
const lines = n.v.split('\n');
|
|
400
|
+
const ind = Math.min(...lines.filter((l) => l.trim()).map((l) => l.length - l.trimStart().length));
|
|
401
|
+
n.v = lines.map((l) => l.slice(Number.isFinite(ind) ? ind : 0)).join('\n');
|
|
402
|
+
}
|
|
403
|
+
n.heredoc = true;
|
|
404
|
+
return n;
|
|
405
|
+
}
|
|
406
|
+
template(s, from, to, e, raw) {
|
|
407
|
+
const src = this.src, hcl = this.hcl;
|
|
408
|
+
const parts = [];
|
|
409
|
+
let lit = '', template = false;
|
|
410
|
+
for (let i = from; i < to;) {
|
|
411
|
+
const c = src[i];
|
|
412
|
+
if (!raw && c === '\\' && i + 1 < to) {
|
|
413
|
+
const n = src[i + 1];
|
|
414
|
+
if (n === 'u' && hcl) { lit += String.fromCharCode(parseInt(src.slice(i + 2, i + 6), 16) || 0); i += 6; continue; }
|
|
415
|
+
if (n === 'u' && src[i + 2] === '{') { const close = src.indexOf('}', i); lit += String.fromCodePoint(parseInt(src.slice(i + 3, close), 16) || 0); i = close + 1; continue; }
|
|
416
|
+
lit += { n: '\n', t: '\t', r: '\r' }[n] ?? n; i += 2; continue;
|
|
417
|
+
}
|
|
418
|
+
if (hcl && (c === '$' || c === '%') && src[i + 1] === c && src[i + 2] === '{') { lit += `${c}{`; i += 3; continue; }
|
|
419
|
+
if ((c === '$' || (hcl && c === '%')) && src[i + 1] === '{') {
|
|
420
|
+
const close = scanInterp(src, i + 2, this.dialect);
|
|
421
|
+
template = true;
|
|
422
|
+
if (lit) parts.push(lit);
|
|
423
|
+
lit = '';
|
|
424
|
+
if (c === '%') { parts.push(this.node('other', i, close, { why: 'a template directive' })); i = close; continue; }
|
|
425
|
+
let a = i + 2, b = close - 1;
|
|
426
|
+
if (src[a] === '~') a++;
|
|
427
|
+
if (src[b - 1] === '~') b--;
|
|
428
|
+
const { toks } = tokenize(src, this.dialect, a, b, this.lineAt);
|
|
429
|
+
const p = new Parser(src, toks, this.dialect, this.lineAt, this.hooks);
|
|
430
|
+
p.skipNl = [true];
|
|
431
|
+
try { parts.push(p.expr()); } catch { parts.push(this.node('other', a, b, { why: 'an expression Thunder cannot read yet' })); }
|
|
432
|
+
i = close; continue;
|
|
433
|
+
}
|
|
434
|
+
lit += c; i++;
|
|
435
|
+
}
|
|
436
|
+
if (lit) parts.push(lit);
|
|
437
|
+
return this.node('str', s, e, { v: template ? null : parts.join(''), template, parts });
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
// ── Walking nodes ──────────────────────────────────────────────────────────────────────────────────────────
|
|
442
|
+
|
|
443
|
+
/** Every child node of a node. */
|
|
444
|
+
export function childrenOf(n) {
|
|
445
|
+
if (!n || typeof n !== 'object') return [];
|
|
446
|
+
switch (n.t) {
|
|
447
|
+
case 'str': return n.parts.filter((p) => typeof p === 'object');
|
|
448
|
+
case 'list': return n.items;
|
|
449
|
+
case 'obj': return n.entries.flatMap((e) => [e.keyNode, e.value].filter(Boolean));
|
|
450
|
+
case 'ref': return n.index;
|
|
451
|
+
case 'get': return [n.obj, ...n.index];
|
|
452
|
+
case 'call': return n.args;
|
|
453
|
+
case 'unary': case 'spread': return [n.x];
|
|
454
|
+
case 'bin': return [n.a, n.b];
|
|
455
|
+
case 'cond': return [n.test, n.yes, n.no];
|
|
456
|
+
case 'for': return [n.coll, n.key, n.value, n.when].filter(Boolean);
|
|
457
|
+
case 'if': return [n.test, n.body];
|
|
458
|
+
case 'lambda': return [n.body];
|
|
459
|
+
default: return [];
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/** Every reference inside a node (a.b.c), with the variables a `for` or lambda introduces left out. */
|
|
464
|
+
export function refsIn(n, out = [], bound = new Set()) {
|
|
465
|
+
if (!n || typeof n !== 'object') return out;
|
|
466
|
+
if (n.t === 'ref') { if (!bound.has(n.path[0])) out.push(n); for (const x of n.index) refsIn(x, out, bound); return out; }
|
|
467
|
+
if (n.t === 'for' || n.t === 'lambda') {
|
|
468
|
+
const inner = new Set([...bound, ...n.vars]);
|
|
469
|
+
if (n.t === 'for') { refsIn(n.coll, out, bound); for (const x of [n.key, n.value, n.when]) refsIn(x, out, inner); }
|
|
470
|
+
else refsIn(n.body, out, inner);
|
|
471
|
+
return out;
|
|
472
|
+
}
|
|
473
|
+
for (const c of childrenOf(n)) refsIn(c, out, bound);
|
|
474
|
+
return out;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/** True when a node is written out in full in the file: no references, calls, or templates. */
|
|
478
|
+
export function isLiteral(n) {
|
|
479
|
+
if (!n) return false;
|
|
480
|
+
switch (n.t) {
|
|
481
|
+
case 'num': case 'bool': case 'null': return true;
|
|
482
|
+
case 'str': return !n.template;
|
|
483
|
+
case 'list': return n.items.every(isLiteral);
|
|
484
|
+
case 'obj': return n.entries.every((e) => !e.keyNode && !e.spread && isLiteral(e.value));
|
|
485
|
+
case 'unary': return n.op === '-' && n.x.t === 'num';
|
|
486
|
+
default: return false;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/** A literal node as a plain value; references and calls become { ref: 'source text' }. */
|
|
491
|
+
export function toValue(n, src) {
|
|
492
|
+
if (!n) return null;
|
|
493
|
+
switch (n.t) {
|
|
494
|
+
case 'num': case 'bool': return n.v;
|
|
495
|
+
case 'null': return null;
|
|
496
|
+
case 'str': return n.template ? { ref: src.slice(n.s, n.e) } : n.v;
|
|
497
|
+
case 'list': return n.items.map((x) => toValue(x, src));
|
|
498
|
+
case 'obj': return Object.fromEntries(n.entries.filter((e) => !e.spread).map((e) => [e.key, toValue(e.value, src)]));
|
|
499
|
+
case 'unary': if (n.op === '-' && n.x.t === 'num') return -n.x.v; break;
|
|
500
|
+
case 'call': if (/^(jsonencode|tolist|toset|tomap)$/.test(n.name) && n.args.length === 1) return toValue(n.args[0], src); break;
|
|
501
|
+
default: break;
|
|
502
|
+
}
|
|
503
|
+
return { ref: src.slice(n.s, n.e), node: n };
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/** An object node's entry by key (first match), or undefined. */
|
|
507
|
+
export function entryOf(obj, key) {
|
|
508
|
+
return obj?.t === 'obj' ? obj.entries.find((e) => e.key === key) : undefined;
|
|
509
|
+
}
|