TOML: the keys before the first table, each `[table]` with its keys, each `[[table]]` as a list * item. An array may run over several lines ("dependencies = [" with one item per line).
(text: string)
| 60 | /** TOML: the keys before the first table, each `[table]` with its keys, each `[[table]]` as a list |
| 61 | * item. An array may run over several lines ("dependencies = [" with one item per line). */ |
| 62 | function tomlTree(text: string): YamlValue | undefined { |
| 63 | const root: [string, YamlValue][] = []; |
| 64 | let current = root, quoted: string | undefined; |
| 65 | const unquote = (item: string) => item.replace(/^(["'])(.*)\1$/, '$2'); |
| 66 | const value = (raw: string): YamlValue => { |
| 67 | const plain = raw.replace(/\s+#[^"']*$/, '').trim(); |
| 68 | if (/^\[[\s\S]*\]$/.test(plain)) return { kind: 'list', items: arrayItems(plain.slice(1, -1)).map(item => ({ kind: 'scalar', value: unquote(item) })) }; |
| 69 | return { kind: 'scalar', value: unquote(plain) }; |
| 70 | }; |
| 71 | const lines = text.split(/\r?\n/); |
| 72 | for (let at = 0; at < lines.length; at++) { |
| 73 | const line = lines[at].trim(); |
| 74 | if (quoted) { if (line.split(quoted).length % 2 === 0) quoted = undefined; continue; } |
| 75 | if (!line || line.startsWith('#')) continue; |
| 76 | const table = /^(\[\[?)\s*([^\]]+?)\s*\]\]?\s*(?:#.*)?$/.exec(line); |
| 77 | if (table) { |
| 78 | const entries: [string, YamlValue][] = []; |
| 79 | if (table[1] === '[[') { |
| 80 | const name = `[[${table[2]}]]`; |
| 81 | const list = root.find(([key]) => key === name)?.[1]; |
| 82 | if (list?.kind === 'list') list.items.push({ kind: 'map', entries }); else root.push([name, { kind: 'list', items: [{ kind: 'map', entries }] }]); |
| 83 | } else root.push([`[${table[2]}]`, { kind: 'map', entries }]); |
| 84 | current = entries; |
| 85 | continue; |
| 86 | } |
| 87 | const key = /^([A-Za-z0-9_\-."']+)\s*=\s*(.*)$/.exec(line); |
| 88 | if (!key) continue; |
| 89 | const delimiter = ['"""', "'''"].find(mark => line.split(mark).length === 2); |
| 90 | if (delimiter) quoted = delimiter; |
| 91 | let raw = key[2]; |
| 92 | // The lines an open array or inline table runs over, without their comments. |
| 93 | while (!delimiter && openBrackets(raw) > 0 && at + 1 < lines.length) raw += ` ${lines[++at].trim().replace(/\s+#[^"']*$/, '')}`; |
| 94 | current.push([key[1], delimiter ? { kind: 'scalar', value: '' } : value(raw)]); |
| 95 | } |
| 96 | return root.length ? { kind: 'map', entries: root } : undefined; |
| 97 | } |
| 98 | |
| 99 | /** INI: the keys before the first section, then each `[section]` with its keys. A value |
| 100 | * continued on indented lines is a list of those lines (`deps =` with one per line, B6). */ |
no test coverage detected