@intentius/chant 0.0.11 → 0.0.13
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/package.json +1 -1
- package/src/cli/commands/__fixtures__/init-lexicon-output/docs/astro.config.mjs +3 -0
- package/src/cli/commands/__fixtures__/init-lexicon-output/docs/src/content/docs/getting-started.mdx +6 -0
- package/src/cli/commands/__fixtures__/init-lexicon-output/docs/src/content/docs/lint-rules.mdx +6 -0
- package/src/cli/commands/__fixtures__/init-lexicon-output/docs/src/content/docs/serialization.mdx +6 -0
- package/src/cli/commands/__fixtures__/init-lexicon-output/examples/getting-started/package.json +9 -0
- package/src/cli/commands/__fixtures__/init-lexicon-output/examples/getting-started/src/infra.ts +12 -0
- package/src/cli/commands/__fixtures__/init-lexicon-output/src/composites/.gitkeep +0 -0
- package/src/cli/commands/__fixtures__/init-lexicon-output/src/lint/post-synth/.gitkeep +0 -0
- package/src/cli/commands/__fixtures__/init-lexicon-output/src/plugin.ts +37 -42
- package/src/cli/commands/__snapshots__/init-lexicon.test.ts.snap +37 -42
- package/src/cli/commands/build.ts +12 -7
- package/src/cli/commands/check-lexicon.ts +385 -0
- package/src/cli/commands/import.ts +6 -3
- package/src/cli/commands/init-lexicon.test.ts +22 -1
- package/src/cli/commands/init-lexicon.ts +194 -43
- package/src/cli/commands/init.ts +3 -3
- package/src/cli/commands/onboard.test.ts +295 -0
- package/src/cli/commands/onboard.ts +313 -0
- package/src/cli/handlers/dev.ts +26 -1
- package/src/cli/main.ts +5 -1
- package/src/codegen/docs.ts +11 -5
- package/src/codegen/generate-registry.ts +3 -2
- package/src/codegen/typecheck.ts +44 -2
- package/src/detectLexicon.test.ts +24 -0
- package/src/detectLexicon.ts +4 -2
- package/src/lsp/lexicon-providers.ts +1 -0
- package/src/runtime.ts +4 -0
- package/src/serializer-walker.test.ts +8 -0
- package/src/toml.test.ts +388 -0
- package/src/toml.ts +606 -0
- package/src/yaml.test.ts +192 -0
- package/src/yaml.ts +308 -0
- /package/src/cli/commands/__fixtures__/init-lexicon-output/{examples/getting-started → src/actions}/.gitkeep +0 -0
package/src/toml.ts
ADDED
|
@@ -0,0 +1,606 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lightweight TOML emitter and parser.
|
|
3
|
+
*
|
|
4
|
+
* Covers the subset of TOML used by Chant lexicons (scalars, tables,
|
|
5
|
+
* arrays, array of tables, inline tables, comments). Not a full TOML
|
|
6
|
+
* implementation — uses `smol-toml` for parsing.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
// Emitter
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
|
|
13
|
+
export interface EmitTOMLOptions {
|
|
14
|
+
/** Comment to prepend at the top of the document. */
|
|
15
|
+
header?: string;
|
|
16
|
+
/** Key ordering hint: keys matching earlier entries appear first. */
|
|
17
|
+
keyOrder?: string[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Emit a JavaScript object as a TOML document string.
|
|
22
|
+
*
|
|
23
|
+
* - Top-level scalars, arrays of scalars → bare key-value pairs.
|
|
24
|
+
* - Nested objects → `[section]` tables.
|
|
25
|
+
* - Arrays of objects → `[[section]]` array of tables.
|
|
26
|
+
* - Deeply nested objects → `[parent.child]` dotted sections.
|
|
27
|
+
*/
|
|
28
|
+
export function emitTOML(value: unknown, options?: EmitTOMLOptions): string {
|
|
29
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
30
|
+
throw new Error("emitTOML expects a plain object at the top level");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const lines: string[] = [];
|
|
34
|
+
|
|
35
|
+
if (options?.header) {
|
|
36
|
+
for (const line of options.header.split("\n")) {
|
|
37
|
+
lines.push(`# ${line}`);
|
|
38
|
+
}
|
|
39
|
+
lines.push("");
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const obj = value as Record<string, unknown>;
|
|
43
|
+
emitTable(obj, [], lines, options?.keyOrder);
|
|
44
|
+
|
|
45
|
+
// Trim trailing blank lines, ensure single trailing newline
|
|
46
|
+
while (lines.length > 0 && lines[lines.length - 1] === "") {
|
|
47
|
+
lines.pop();
|
|
48
|
+
}
|
|
49
|
+
return lines.join("\n") + "\n";
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Emit a TOML table (recursively handles nested tables and array of tables).
|
|
54
|
+
*/
|
|
55
|
+
function emitTable(
|
|
56
|
+
obj: Record<string, unknown>,
|
|
57
|
+
path: string[],
|
|
58
|
+
lines: string[],
|
|
59
|
+
keyOrder?: string[],
|
|
60
|
+
): void {
|
|
61
|
+
const keys = sortKeys(Object.keys(obj), keyOrder);
|
|
62
|
+
|
|
63
|
+
// First pass: emit all scalar / inline values
|
|
64
|
+
for (const key of keys) {
|
|
65
|
+
const val = obj[key];
|
|
66
|
+
if (val === undefined) continue;
|
|
67
|
+
if (isTableValue(val)) continue; // handled in second pass
|
|
68
|
+
if (isArrayOfTables(val)) continue; // handled in second pass
|
|
69
|
+
|
|
70
|
+
lines.push(`${escapeKey(key)} = ${emitValue(val)}`);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Second pass: emit nested tables
|
|
74
|
+
for (const key of keys) {
|
|
75
|
+
const val = obj[key];
|
|
76
|
+
if (val === undefined) continue;
|
|
77
|
+
|
|
78
|
+
if (isArrayOfTables(val)) {
|
|
79
|
+
const arr = val as Record<string, unknown>[];
|
|
80
|
+
for (const item of arr) {
|
|
81
|
+
lines.push("");
|
|
82
|
+
const sectionPath = [...path, key];
|
|
83
|
+
lines.push(`[[${sectionPath.map(escapeKey).join(".")}]]`);
|
|
84
|
+
emitTable(item, sectionPath, lines, keyOrder);
|
|
85
|
+
}
|
|
86
|
+
} else if (isTableValue(val)) {
|
|
87
|
+
lines.push("");
|
|
88
|
+
const sectionPath = [...path, key];
|
|
89
|
+
lines.push(`[${sectionPath.map(escapeKey).join(".")}]`);
|
|
90
|
+
emitTable(val as Record<string, unknown>, sectionPath, lines, keyOrder);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Check if a value should be emitted as a `[table]` section.
|
|
97
|
+
*/
|
|
98
|
+
function isTableValue(val: unknown): val is Record<string, unknown> {
|
|
99
|
+
return (
|
|
100
|
+
typeof val === "object" &&
|
|
101
|
+
val !== null &&
|
|
102
|
+
!Array.isArray(val) &&
|
|
103
|
+
!(val instanceof Date)
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Check if a value is an array of tables (`[[section]]`).
|
|
109
|
+
*/
|
|
110
|
+
function isArrayOfTables(val: unknown): boolean {
|
|
111
|
+
if (!Array.isArray(val) || val.length === 0) return false;
|
|
112
|
+
return val.every(
|
|
113
|
+
(item) => typeof item === "object" && item !== null && !Array.isArray(item) && !(item instanceof Date),
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Emit a TOML value (scalar, array of scalars, inline table).
|
|
119
|
+
*/
|
|
120
|
+
function emitValue(val: unknown): string {
|
|
121
|
+
if (val === null || val === undefined) {
|
|
122
|
+
return '""'; // TOML has no null — emit empty string
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (typeof val === "boolean") {
|
|
126
|
+
return val ? "true" : "false";
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (typeof val === "number") {
|
|
130
|
+
if (Number.isInteger(val)) return String(val);
|
|
131
|
+
return String(val);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (typeof val === "string") {
|
|
135
|
+
return emitString(val);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (val instanceof Date) {
|
|
139
|
+
return val.toISOString();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (Array.isArray(val)) {
|
|
143
|
+
if (val.length === 0) return "[]";
|
|
144
|
+
// Array of scalars → inline array
|
|
145
|
+
const items = val.map((item) => emitValue(item));
|
|
146
|
+
const inline = `[${items.join(", ")}]`;
|
|
147
|
+
if (inline.length <= 80) return inline;
|
|
148
|
+
// Multi-line array for long content
|
|
149
|
+
const multiLines = val.map((item) => ` ${emitValue(item)},`);
|
|
150
|
+
return "[\n" + multiLines.join("\n") + "\n]";
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Inline table for non-table contexts (shouldn't normally reach here
|
|
154
|
+
// because isTableValue is checked first, but handles edge cases)
|
|
155
|
+
if (typeof val === "object") {
|
|
156
|
+
const entries = Object.entries(val as Record<string, unknown>);
|
|
157
|
+
if (entries.length === 0) return "{}";
|
|
158
|
+
const pairs = entries.map(([k, v]) => `${escapeKey(k)} = ${emitValue(v)}`);
|
|
159
|
+
return `{ ${pairs.join(", ")} }`;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return String(val);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Emit a TOML string with proper quoting.
|
|
167
|
+
*/
|
|
168
|
+
function emitString(val: string): string {
|
|
169
|
+
// Use basic strings with escaping for most values
|
|
170
|
+
if (val.includes("\n") || val.includes("\r")) {
|
|
171
|
+
// Multi-line basic string
|
|
172
|
+
const escaped = val
|
|
173
|
+
.replace(/\\/g, "\\\\")
|
|
174
|
+
.replace(/"""/g, '\\"""');
|
|
175
|
+
return `"""\n${escaped}"""`;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Regular basic string
|
|
179
|
+
const escaped = val
|
|
180
|
+
.replace(/\\/g, "\\\\")
|
|
181
|
+
.replace(/"/g, '\\"')
|
|
182
|
+
.replace(/\t/g, "\\t")
|
|
183
|
+
.replace(/\r/g, "\\r");
|
|
184
|
+
return `"${escaped}"`;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Escape a TOML key if it contains special characters.
|
|
189
|
+
*/
|
|
190
|
+
function escapeKey(key: string): string {
|
|
191
|
+
if (/^[A-Za-z0-9_-]+$/.test(key)) return key;
|
|
192
|
+
return `"${key.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Sort keys according to a preferred order, with unmatched keys at the end.
|
|
197
|
+
*/
|
|
198
|
+
function sortKeys(keys: string[], keyOrder?: string[]): string[] {
|
|
199
|
+
if (!keyOrder || keyOrder.length === 0) return keys;
|
|
200
|
+
const orderMap = new Map(keyOrder.map((k, i) => [k, i]));
|
|
201
|
+
return [...keys].sort((a, b) => {
|
|
202
|
+
const ai = orderMap.get(a) ?? Infinity;
|
|
203
|
+
const bi = orderMap.get(b) ?? Infinity;
|
|
204
|
+
if (ai !== bi) return ai - bi;
|
|
205
|
+
return 0; // preserve original order for unmatched
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// ---------------------------------------------------------------------------
|
|
210
|
+
// Parser
|
|
211
|
+
// ---------------------------------------------------------------------------
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Parse a TOML document string into a plain object.
|
|
215
|
+
*
|
|
216
|
+
* Uses a built-in parser that handles the TOML subset used by Flyway
|
|
217
|
+
* configuration files.
|
|
218
|
+
*/
|
|
219
|
+
export function parseTOML(content: string): Record<string, unknown> {
|
|
220
|
+
const result: Record<string, unknown> = {};
|
|
221
|
+
const lines = content.split("\n");
|
|
222
|
+
let currentPath: string[] = [];
|
|
223
|
+
let isArrayOfTables = false;
|
|
224
|
+
|
|
225
|
+
for (let i = 0; i < lines.length; i++) {
|
|
226
|
+
const line = lines[i].trim();
|
|
227
|
+
|
|
228
|
+
// Skip empty lines and comments
|
|
229
|
+
if (line === "" || line.startsWith("#")) continue;
|
|
230
|
+
|
|
231
|
+
// Array of tables: [[section.path]]
|
|
232
|
+
const aotMatch = line.match(/^\[\[([^\]]+)\]\]\s*(?:#.*)?$/);
|
|
233
|
+
if (aotMatch) {
|
|
234
|
+
currentPath = parseDottedKey(aotMatch[1].trim());
|
|
235
|
+
isArrayOfTables = true;
|
|
236
|
+
// Ensure the array exists and add a new entry
|
|
237
|
+
const arr = ensureArrayAt(result, currentPath);
|
|
238
|
+
arr.push({});
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Table header: [section.path]
|
|
243
|
+
const tableMatch = line.match(/^\[([^\]]+)\]\s*(?:#.*)?$/);
|
|
244
|
+
if (tableMatch) {
|
|
245
|
+
currentPath = parseDottedKey(tableMatch[1].trim());
|
|
246
|
+
isArrayOfTables = false;
|
|
247
|
+
ensureTableAt(result, currentPath);
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// Key-value pair
|
|
252
|
+
const kvResult = parseKeyValue(line);
|
|
253
|
+
if (kvResult) {
|
|
254
|
+
const target = isArrayOfTables
|
|
255
|
+
? getLastArrayEntry(result, currentPath)
|
|
256
|
+
: getTableAt(result, currentPath);
|
|
257
|
+
if (target) {
|
|
258
|
+
setNestedValue(target, kvResult.key, kvResult.value);
|
|
259
|
+
}
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
return result;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
interface KeyValueResult {
|
|
268
|
+
key: string[];
|
|
269
|
+
value: unknown;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Parse a key = value line.
|
|
274
|
+
*/
|
|
275
|
+
function parseKeyValue(line: string): KeyValueResult | null {
|
|
276
|
+
// Find the = sign (not inside quotes)
|
|
277
|
+
const eqIndex = findEquals(line);
|
|
278
|
+
if (eqIndex === -1) return null;
|
|
279
|
+
|
|
280
|
+
const rawKey = line.slice(0, eqIndex).trim();
|
|
281
|
+
const rawValue = line.slice(eqIndex + 1).trim();
|
|
282
|
+
|
|
283
|
+
const key = parseDottedKey(rawKey);
|
|
284
|
+
const value = parseTOMLValue(rawValue);
|
|
285
|
+
|
|
286
|
+
return { key, value };
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Find the index of the first `=` not inside quotes.
|
|
291
|
+
*/
|
|
292
|
+
function findEquals(line: string): number {
|
|
293
|
+
let inSingleQuote = false;
|
|
294
|
+
let inDoubleQuote = false;
|
|
295
|
+
|
|
296
|
+
for (let i = 0; i < line.length; i++) {
|
|
297
|
+
const ch = line[i];
|
|
298
|
+
if (ch === "'" && !inDoubleQuote) {
|
|
299
|
+
inSingleQuote = !inSingleQuote;
|
|
300
|
+
} else if (ch === '"' && !inSingleQuote && (i === 0 || line[i - 1] !== "\\")) {
|
|
301
|
+
inDoubleQuote = !inDoubleQuote;
|
|
302
|
+
} else if (ch === "=" && !inSingleQuote && !inDoubleQuote) {
|
|
303
|
+
return i;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return -1;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Parse a dotted key like `flyway.placeholders.name` into path segments.
|
|
311
|
+
*/
|
|
312
|
+
function parseDottedKey(raw: string): string[] {
|
|
313
|
+
const parts: string[] = [];
|
|
314
|
+
let current = "";
|
|
315
|
+
let inQuote = false;
|
|
316
|
+
let quoteChar = "";
|
|
317
|
+
|
|
318
|
+
for (let i = 0; i < raw.length; i++) {
|
|
319
|
+
const ch = raw[i];
|
|
320
|
+
if (inQuote) {
|
|
321
|
+
if (ch === quoteChar) {
|
|
322
|
+
inQuote = false;
|
|
323
|
+
} else {
|
|
324
|
+
current += ch;
|
|
325
|
+
}
|
|
326
|
+
} else if (ch === '"' || ch === "'") {
|
|
327
|
+
inQuote = true;
|
|
328
|
+
quoteChar = ch;
|
|
329
|
+
} else if (ch === ".") {
|
|
330
|
+
parts.push(current.trim());
|
|
331
|
+
current = "";
|
|
332
|
+
} else {
|
|
333
|
+
current += ch;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
if (current.trim()) parts.push(current.trim());
|
|
337
|
+
return parts;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Parse a TOML value string.
|
|
342
|
+
*/
|
|
343
|
+
function parseTOMLValue(raw: string): unknown {
|
|
344
|
+
// Strip inline comments (not inside strings)
|
|
345
|
+
const value = stripInlineComment(raw);
|
|
346
|
+
|
|
347
|
+
if (value === "true") return true;
|
|
348
|
+
if (value === "false") return false;
|
|
349
|
+
|
|
350
|
+
// String values
|
|
351
|
+
if (value.startsWith('"""')) {
|
|
352
|
+
const end = value.indexOf('"""', 3);
|
|
353
|
+
return end !== -1 ? value.slice(3, end) : value.slice(3);
|
|
354
|
+
}
|
|
355
|
+
if (value.startsWith("'''")) {
|
|
356
|
+
const end = value.indexOf("'''", 3);
|
|
357
|
+
return end !== -1 ? value.slice(3, end) : value.slice(3);
|
|
358
|
+
}
|
|
359
|
+
if (value.startsWith('"') && value.endsWith('"')) {
|
|
360
|
+
return unescapeString(value.slice(1, -1));
|
|
361
|
+
}
|
|
362
|
+
if (value.startsWith("'") && value.endsWith("'")) {
|
|
363
|
+
return value.slice(1, -1); // Literal string, no escaping
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// Array
|
|
367
|
+
if (value.startsWith("[")) {
|
|
368
|
+
return parseTOMLArray(value);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// Inline table
|
|
372
|
+
if (value.startsWith("{")) {
|
|
373
|
+
return parseTOMLInlineTable(value);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
// Number
|
|
377
|
+
const num = Number(value);
|
|
378
|
+
if (!isNaN(num) && value !== "") return num;
|
|
379
|
+
|
|
380
|
+
// Bare string / date (return as string)
|
|
381
|
+
return value;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Strip inline comment from a value string.
|
|
386
|
+
*/
|
|
387
|
+
function stripInlineComment(raw: string): string {
|
|
388
|
+
let inString = false;
|
|
389
|
+
let stringChar = "";
|
|
390
|
+
|
|
391
|
+
for (let i = 0; i < raw.length; i++) {
|
|
392
|
+
const ch = raw[i];
|
|
393
|
+
if (inString) {
|
|
394
|
+
if (ch === stringChar && raw[i - 1] !== "\\") {
|
|
395
|
+
inString = false;
|
|
396
|
+
}
|
|
397
|
+
} else if (ch === '"' || ch === "'") {
|
|
398
|
+
inString = true;
|
|
399
|
+
stringChar = ch;
|
|
400
|
+
} else if (ch === "#") {
|
|
401
|
+
return raw.slice(0, i).trim();
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
return raw.trim();
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* Unescape a TOML basic string.
|
|
409
|
+
*/
|
|
410
|
+
function unescapeString(val: string): string {
|
|
411
|
+
return val
|
|
412
|
+
.replace(/\\n/g, "\n")
|
|
413
|
+
.replace(/\\t/g, "\t")
|
|
414
|
+
.replace(/\\r/g, "\r")
|
|
415
|
+
.replace(/\\"/g, '"')
|
|
416
|
+
.replace(/\\\\/g, "\\");
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Parse a TOML inline array.
|
|
421
|
+
*/
|
|
422
|
+
function parseTOMLArray(raw: string): unknown[] {
|
|
423
|
+
// Remove outer brackets
|
|
424
|
+
const inner = raw.slice(1, raw.lastIndexOf("]")).trim();
|
|
425
|
+
if (inner === "") return [];
|
|
426
|
+
|
|
427
|
+
const items: unknown[] = [];
|
|
428
|
+
let current = "";
|
|
429
|
+
let depth = 0;
|
|
430
|
+
let inString = false;
|
|
431
|
+
let stringChar = "";
|
|
432
|
+
|
|
433
|
+
for (let i = 0; i < inner.length; i++) {
|
|
434
|
+
const ch = inner[i];
|
|
435
|
+
if (inString) {
|
|
436
|
+
current += ch;
|
|
437
|
+
if (ch === stringChar && inner[i - 1] !== "\\") {
|
|
438
|
+
inString = false;
|
|
439
|
+
}
|
|
440
|
+
} else if (ch === '"' || ch === "'") {
|
|
441
|
+
inString = true;
|
|
442
|
+
stringChar = ch;
|
|
443
|
+
current += ch;
|
|
444
|
+
} else if (ch === "[" || ch === "{") {
|
|
445
|
+
depth++;
|
|
446
|
+
current += ch;
|
|
447
|
+
} else if (ch === "]" || ch === "}") {
|
|
448
|
+
depth--;
|
|
449
|
+
current += ch;
|
|
450
|
+
} else if (ch === "," && depth === 0) {
|
|
451
|
+
const trimmed = current.trim();
|
|
452
|
+
if (trimmed !== "") items.push(parseTOMLValue(trimmed));
|
|
453
|
+
current = "";
|
|
454
|
+
} else {
|
|
455
|
+
current += ch;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
const trimmed = current.trim();
|
|
460
|
+
if (trimmed !== "") items.push(parseTOMLValue(trimmed));
|
|
461
|
+
|
|
462
|
+
return items;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* Parse a TOML inline table.
|
|
467
|
+
*/
|
|
468
|
+
function parseTOMLInlineTable(raw: string): Record<string, unknown> {
|
|
469
|
+
const inner = raw.slice(1, raw.lastIndexOf("}")).trim();
|
|
470
|
+
if (inner === "") return {};
|
|
471
|
+
|
|
472
|
+
const result: Record<string, unknown> = {};
|
|
473
|
+
let current = "";
|
|
474
|
+
let depth = 0;
|
|
475
|
+
let inString = false;
|
|
476
|
+
let stringChar = "";
|
|
477
|
+
|
|
478
|
+
for (let i = 0; i < inner.length; i++) {
|
|
479
|
+
const ch = inner[i];
|
|
480
|
+
if (inString) {
|
|
481
|
+
current += ch;
|
|
482
|
+
if (ch === stringChar && inner[i - 1] !== "\\") {
|
|
483
|
+
inString = false;
|
|
484
|
+
}
|
|
485
|
+
} else if (ch === '"' || ch === "'") {
|
|
486
|
+
inString = true;
|
|
487
|
+
stringChar = ch;
|
|
488
|
+
current += ch;
|
|
489
|
+
} else if (ch === "[" || ch === "{") {
|
|
490
|
+
depth++;
|
|
491
|
+
current += ch;
|
|
492
|
+
} else if (ch === "]" || ch === "}") {
|
|
493
|
+
depth--;
|
|
494
|
+
current += ch;
|
|
495
|
+
} else if (ch === "," && depth === 0) {
|
|
496
|
+
parseInlineTableEntry(current.trim(), result);
|
|
497
|
+
current = "";
|
|
498
|
+
} else {
|
|
499
|
+
current += ch;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
if (current.trim()) parseInlineTableEntry(current.trim(), result);
|
|
504
|
+
return result;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
function parseInlineTableEntry(entry: string, target: Record<string, unknown>): void {
|
|
508
|
+
const kv = parseKeyValue(entry);
|
|
509
|
+
if (kv) {
|
|
510
|
+
setNestedValue(target, kv.key, kv.value);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
// ---------------------------------------------------------------------------
|
|
515
|
+
// Object path helpers
|
|
516
|
+
// ---------------------------------------------------------------------------
|
|
517
|
+
|
|
518
|
+
function ensureTableAt(root: Record<string, unknown>, path: string[]): Record<string, unknown> {
|
|
519
|
+
let current = root;
|
|
520
|
+
for (const segment of path) {
|
|
521
|
+
if (!(segment in current)) {
|
|
522
|
+
current[segment] = {};
|
|
523
|
+
}
|
|
524
|
+
const next = current[segment];
|
|
525
|
+
if (Array.isArray(next)) {
|
|
526
|
+
// Navigate into last array entry
|
|
527
|
+
current = next[next.length - 1] as Record<string, unknown>;
|
|
528
|
+
} else if (typeof next === "object" && next !== null) {
|
|
529
|
+
current = next as Record<string, unknown>;
|
|
530
|
+
} else {
|
|
531
|
+
const obj: Record<string, unknown> = {};
|
|
532
|
+
current[segment] = obj;
|
|
533
|
+
current = obj;
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
return current;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
function ensureArrayAt(root: Record<string, unknown>, path: string[]): unknown[] {
|
|
540
|
+
let current = root;
|
|
541
|
+
for (let i = 0; i < path.length - 1; i++) {
|
|
542
|
+
const segment = path[i];
|
|
543
|
+
if (!(segment in current)) {
|
|
544
|
+
current[segment] = {};
|
|
545
|
+
}
|
|
546
|
+
const next = current[segment];
|
|
547
|
+
if (Array.isArray(next)) {
|
|
548
|
+
current = next[next.length - 1] as Record<string, unknown>;
|
|
549
|
+
} else if (typeof next === "object" && next !== null) {
|
|
550
|
+
current = next as Record<string, unknown>;
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
const lastKey = path[path.length - 1];
|
|
555
|
+
if (!(lastKey in current) || !Array.isArray(current[lastKey])) {
|
|
556
|
+
current[lastKey] = [];
|
|
557
|
+
}
|
|
558
|
+
return current[lastKey] as unknown[];
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
function getTableAt(root: Record<string, unknown>, path: string[]): Record<string, unknown> {
|
|
562
|
+
let current = root;
|
|
563
|
+
for (const segment of path) {
|
|
564
|
+
const next = current[segment];
|
|
565
|
+
if (Array.isArray(next)) {
|
|
566
|
+
current = next[next.length - 1] as Record<string, unknown>;
|
|
567
|
+
} else if (typeof next === "object" && next !== null) {
|
|
568
|
+
current = next as Record<string, unknown>;
|
|
569
|
+
} else {
|
|
570
|
+
return current;
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
return current;
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
function getLastArrayEntry(root: Record<string, unknown>, path: string[]): Record<string, unknown> | null {
|
|
577
|
+
let current = root;
|
|
578
|
+
for (let i = 0; i < path.length - 1; i++) {
|
|
579
|
+
const next = current[path[i]];
|
|
580
|
+
if (Array.isArray(next)) {
|
|
581
|
+
current = next[next.length - 1] as Record<string, unknown>;
|
|
582
|
+
} else if (typeof next === "object" && next !== null) {
|
|
583
|
+
current = next as Record<string, unknown>;
|
|
584
|
+
} else {
|
|
585
|
+
return null;
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
const lastKey = path[path.length - 1];
|
|
590
|
+
const arr = current[lastKey];
|
|
591
|
+
if (Array.isArray(arr) && arr.length > 0) {
|
|
592
|
+
return arr[arr.length - 1] as Record<string, unknown>;
|
|
593
|
+
}
|
|
594
|
+
return null;
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
function setNestedValue(target: Record<string, unknown>, key: string[], value: unknown): void {
|
|
598
|
+
let current = target;
|
|
599
|
+
for (let i = 0; i < key.length - 1; i++) {
|
|
600
|
+
if (!(key[i] in current)) {
|
|
601
|
+
current[key[i]] = {};
|
|
602
|
+
}
|
|
603
|
+
current = current[key[i]] as Record<string, unknown>;
|
|
604
|
+
}
|
|
605
|
+
current[key[key.length - 1]] = value;
|
|
606
|
+
}
|