@intentius/chant 0.49.0 → 0.50.0

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.
Files changed (247) hide show
  1. package/dist/audit/catalog.d.ts +13 -3
  2. package/dist/audit/catalog.d.ts.map +1 -1
  3. package/dist/audit/core.d.ts +9 -0
  4. package/dist/audit/core.d.ts.map +1 -1
  5. package/dist/audit/discover.d.ts +6 -0
  6. package/dist/audit/discover.d.ts.map +1 -1
  7. package/dist/audit/fetch.d.ts.map +1 -1
  8. package/dist/audit/report-html.d.ts.map +1 -1
  9. package/dist/audit/report-model.d.ts +6 -0
  10. package/dist/audit/report-model.d.ts.map +1 -1
  11. package/dist/audit/report.d.ts.map +1 -1
  12. package/dist/audit/rules-doc.d.ts.map +1 -1
  13. package/dist/audit/secrets.d.ts +95 -0
  14. package/dist/audit/secrets.d.ts.map +1 -0
  15. package/dist/audit/wrangler.d.ts +33 -0
  16. package/dist/audit/wrangler.d.ts.map +1 -0
  17. package/dist/build.d.ts.map +1 -1
  18. package/dist/cli/commands/audit.d.ts +7 -0
  19. package/dist/cli/commands/audit.d.ts.map +1 -1
  20. package/dist/cli/commands/build.d.ts +23 -0
  21. package/dist/cli/commands/build.d.ts.map +1 -1
  22. package/dist/cli/handlers/build.d.ts.map +1 -1
  23. package/dist/cli/handlers/components.d.ts +31 -0
  24. package/dist/cli/handlers/components.d.ts.map +1 -1
  25. package/dist/cli/handlers/lifecycle.d.ts +11 -0
  26. package/dist/cli/handlers/lifecycle.d.ts.map +1 -1
  27. package/dist/cli/handlers/operator.d.ts +32 -0
  28. package/dist/cli/handlers/operator.d.ts.map +1 -0
  29. package/dist/cli/handlers/scenario.d.ts +39 -0
  30. package/dist/cli/handlers/scenario.d.ts.map +1 -0
  31. package/dist/cli/main.d.ts.map +1 -1
  32. package/dist/cli/mcp/server.d.ts +35 -2
  33. package/dist/cli/mcp/server.d.ts.map +1 -1
  34. package/dist/cli/mcp/types.d.ts +29 -1
  35. package/dist/cli/mcp/types.d.ts.map +1 -1
  36. package/dist/cli/registry.d.ts +14 -2
  37. package/dist/cli/registry.d.ts.map +1 -1
  38. package/dist/codegen/docs-rule-scanning.d.ts.map +1 -1
  39. package/dist/components/capability.d.ts +17 -2
  40. package/dist/components/capability.d.ts.map +1 -1
  41. package/dist/components/cli-support.d.ts +7 -0
  42. package/dist/components/cli-support.d.ts.map +1 -1
  43. package/dist/components/component.d.ts +15 -0
  44. package/dist/components/component.d.ts.map +1 -1
  45. package/dist/components/driver.d.ts.map +1 -1
  46. package/dist/components/verbs/index.d.ts +6 -1
  47. package/dist/components/verbs/index.d.ts.map +1 -1
  48. package/dist/components/verbs/run-agent.d.ts +499 -0
  49. package/dist/components/verbs/run-agent.d.ts.map +1 -0
  50. package/dist/components/verbs/sign.d.ts +30 -0
  51. package/dist/components/verbs/sign.d.ts.map +1 -1
  52. package/dist/composite.d.ts +6 -1
  53. package/dist/composite.d.ts.map +1 -1
  54. package/dist/discovery/collect.d.ts.map +1 -1
  55. package/dist/discovery/fold-import.d.ts +15 -1
  56. package/dist/discovery/fold-import.d.ts.map +1 -1
  57. package/dist/discovery/fold-rank.d.ts +66 -0
  58. package/dist/discovery/fold-rank.d.ts.map +1 -0
  59. package/dist/discovery/index.d.ts +15 -0
  60. package/dist/discovery/index.d.ts.map +1 -1
  61. package/dist/discovery/param-deps.d.ts +17 -0
  62. package/dist/discovery/param-deps.d.ts.map +1 -0
  63. package/dist/fold/fold.d.ts +55 -2
  64. package/dist/fold/fold.d.ts.map +1 -1
  65. package/dist/fold/subset.d.ts +21 -14
  66. package/dist/fold/subset.d.ts.map +1 -1
  67. package/dist/lexicon-schema.d.ts +2 -0
  68. package/dist/lexicon-schema.d.ts.map +1 -1
  69. package/dist/lexicon.d.ts +93 -0
  70. package/dist/lexicon.d.ts.map +1 -1
  71. package/dist/lifecycle/converge-ledger.d.ts +90 -0
  72. package/dist/lifecycle/converge-ledger.d.ts.map +1 -0
  73. package/dist/lifecycle/deep-diff.d.ts +18 -0
  74. package/dist/lifecycle/deep-diff.d.ts.map +1 -1
  75. package/dist/lifecycle/deep-observe.d.ts +9 -1
  76. package/dist/lifecycle/deep-observe.d.ts.map +1 -1
  77. package/dist/lifecycle/gate-ledger.d.ts +33 -0
  78. package/dist/lifecycle/gate-ledger.d.ts.map +1 -0
  79. package/dist/lifecycle/git.d.ts +145 -21
  80. package/dist/lifecycle/git.d.ts.map +1 -1
  81. package/dist/lifecycle/index.d.ts +4 -0
  82. package/dist/lifecycle/index.d.ts.map +1 -1
  83. package/dist/lifecycle/lease.d.ts +113 -0
  84. package/dist/lifecycle/lease.d.ts.map +1 -0
  85. package/dist/lifecycle/scenario-eval.d.ts +42 -0
  86. package/dist/lifecycle/scenario-eval.d.ts.map +1 -0
  87. package/dist/lifecycle/scenario.d.ts +163 -0
  88. package/dist/lifecycle/scenario.d.ts.map +1 -0
  89. package/dist/lifecycle/symptoms.d.ts +63 -0
  90. package/dist/lifecycle/symptoms.d.ts.map +1 -0
  91. package/dist/lint/output-docs.d.ts +94 -0
  92. package/dist/lint/output-docs.d.ts.map +1 -0
  93. package/dist/lint/post-synth.d.ts +29 -0
  94. package/dist/lint/post-synth.d.ts.map +1 -1
  95. package/dist/lint/rules/__fixtures__/comp/comp003/pass/agent-turn.component.d.ts +11 -0
  96. package/dist/lint/rules/__fixtures__/comp/comp003/pass/agent-turn.component.d.ts.map +1 -0
  97. package/dist/lsp/lexicon-providers.d.ts +7 -0
  98. package/dist/lsp/lexicon-providers.d.ts.map +1 -1
  99. package/dist/op/activity-contract.d.ts +139 -0
  100. package/dist/op/activity-contract.d.ts.map +1 -0
  101. package/dist/op/builders.d.ts +42 -2
  102. package/dist/op/builders.d.ts.map +1 -1
  103. package/dist/op/converge-rule.d.ts +161 -0
  104. package/dist/op/converge-rule.d.ts.map +1 -0
  105. package/dist/op/generate-pipeline.d.ts +39 -0
  106. package/dist/op/generate-pipeline.d.ts.map +1 -0
  107. package/dist/op/index.d.ts +14 -0
  108. package/dist/op/index.d.ts.map +1 -1
  109. package/dist/op/local-executor.d.ts.map +1 -1
  110. package/dist/op/op-verb-class.d.ts +42 -0
  111. package/dist/op/op-verb-class.d.ts.map +1 -0
  112. package/dist/op/operator.d.ts +128 -0
  113. package/dist/op/operator.d.ts.map +1 -0
  114. package/dist/op/step-output-ref.d.ts +187 -0
  115. package/dist/op/step-output-ref.d.ts.map +1 -0
  116. package/dist/op/types.d.ts +18 -1
  117. package/dist/op/types.d.ts.map +1 -1
  118. package/dist/provenance.d.ts +73 -3
  119. package/dist/provenance.d.ts.map +1 -1
  120. package/dist/runtime-adapter.d.ts +7 -1
  121. package/dist/runtime-adapter.d.ts.map +1 -1
  122. package/dist/serializer.d.ts +18 -0
  123. package/dist/serializer.d.ts.map +1 -1
  124. package/dist/toml.d.ts +40 -5
  125. package/dist/toml.d.ts.map +1 -1
  126. package/package.json +1 -1
  127. package/src/audit/catalog.test.ts +1 -1
  128. package/src/audit/catalog.ts +75 -3
  129. package/src/audit/core.ts +9 -0
  130. package/src/audit/discover.ts +29 -2
  131. package/src/audit/fetch.test.ts +216 -3
  132. package/src/audit/fetch.ts +270 -59
  133. package/src/audit/report-html.ts +5 -2
  134. package/src/audit/report-model.ts +9 -0
  135. package/src/audit/report.test.ts +22 -0
  136. package/src/audit/report.ts +3 -2
  137. package/src/audit/rules-doc.ts +2 -0
  138. package/src/audit/secrets.test.ts +303 -0
  139. package/src/audit/secrets.ts +406 -0
  140. package/src/audit/wrangler.test.ts +230 -0
  141. package/src/audit/wrangler.ts +290 -0
  142. package/src/build.ts +8 -3
  143. package/src/cli/command-group.ts +1 -1
  144. package/src/cli/commands/__fixtures__/schemas/sarif-2.1.0.schema.json +2882 -0
  145. package/src/cli/commands/audit.test.ts +215 -1
  146. package/src/cli/commands/audit.ts +86 -17
  147. package/src/cli/commands/build.test.ts +167 -2
  148. package/src/cli/commands/build.ts +114 -23
  149. package/src/cli/handlers/build.ts +2 -0
  150. package/src/cli/handlers/components.test.ts +199 -1
  151. package/src/cli/handlers/components.ts +160 -3
  152. package/src/cli/handlers/graph.test.ts +20 -0
  153. package/src/cli/handlers/graph.ts +10 -1
  154. package/src/cli/handlers/lifecycle.ts +12 -4
  155. package/src/cli/handlers/operator.test.ts +255 -0
  156. package/src/cli/handlers/operator.ts +240 -0
  157. package/src/cli/handlers/scenario.test.ts +456 -0
  158. package/src/cli/handlers/scenario.ts +330 -0
  159. package/src/cli/main.test.ts +23 -0
  160. package/src/cli/main.ts +72 -1
  161. package/src/cli/mcp/server.test.ts +265 -2
  162. package/src/cli/mcp/server.ts +84 -7
  163. package/src/cli/mcp/types.ts +27 -1
  164. package/src/cli/registry.ts +14 -2
  165. package/src/codegen/docs-rule-scanning.test.ts +42 -0
  166. package/src/codegen/docs-rule-scanning.ts +25 -2
  167. package/src/components/README.md +7 -0
  168. package/src/components/capability.ts +17 -2
  169. package/src/components/cli-support.test.ts +17 -0
  170. package/src/components/cli-support.ts +13 -1
  171. package/src/components/component-schema.test.ts +32 -0
  172. package/src/components/component.schema.json +6 -0
  173. package/src/components/component.test.ts +21 -0
  174. package/src/components/component.ts +15 -0
  175. package/src/components/driver.ts +12 -4
  176. package/src/components/verbs/index.ts +6 -1
  177. package/src/components/verbs/run-agent.test.ts +683 -0
  178. package/src/components/verbs/run-agent.ts +786 -0
  179. package/src/components/verbs/sign.test.ts +19 -0
  180. package/src/components/verbs/sign.ts +34 -2
  181. package/src/composite.ts +31 -2
  182. package/src/discovery/collect.ts +11 -2
  183. package/src/discovery/fold-import.test.ts +54 -0
  184. package/src/discovery/fold-import.ts +178 -38
  185. package/src/discovery/fold-rank.test.ts +197 -0
  186. package/src/discovery/fold-rank.ts +346 -0
  187. package/src/discovery/index.ts +16 -1
  188. package/src/discovery/param-deps.test.ts +118 -0
  189. package/src/discovery/param-deps.ts +170 -0
  190. package/src/fold/fold.test.ts +6 -2
  191. package/src/fold/fold.ts +184 -3
  192. package/src/fold/subset.test.ts +82 -19
  193. package/src/fold/subset.ts +79 -41
  194. package/src/lexicon-schema.ts +3 -0
  195. package/src/lexicon.ts +103 -2
  196. package/src/lifecycle/converge-ledger.test.ts +199 -0
  197. package/src/lifecycle/converge-ledger.ts +179 -0
  198. package/src/lifecycle/deep-diff.test.ts +79 -1
  199. package/src/lifecycle/deep-diff.ts +23 -0
  200. package/src/lifecycle/deep-observe.ts +13 -2
  201. package/src/lifecycle/gate-ledger.test.ts +103 -0
  202. package/src/lifecycle/gate-ledger.ts +140 -0
  203. package/src/lifecycle/git.test.ts +430 -0
  204. package/src/lifecycle/git.ts +446 -84
  205. package/src/lifecycle/index.ts +4 -0
  206. package/src/lifecycle/lease.test.ts +343 -0
  207. package/src/lifecycle/lease.ts +270 -0
  208. package/src/lifecycle/scenario-eval.test.ts +199 -0
  209. package/src/lifecycle/scenario-eval.ts +158 -0
  210. package/src/lifecycle/scenario.test.ts +195 -0
  211. package/src/lifecycle/scenario.ts +321 -0
  212. package/src/lifecycle/symptoms.test.ts +116 -0
  213. package/src/lifecycle/symptoms.ts +126 -0
  214. package/src/lint/output-docs.test.ts +220 -0
  215. package/src/lint/output-docs.ts +204 -0
  216. package/src/lint/post-synth.test.ts +97 -0
  217. package/src/lint/post-synth.ts +45 -0
  218. package/src/lint/rules/__fixtures__/comp/comp003/pass/agent-turn.component.ts +26 -0
  219. package/src/lint/rules/comp/comp.test.ts +49 -1
  220. package/src/lint/rules/evl001-non-literal-expression.test.ts +8 -3
  221. package/src/lint/rules/evl001-non-literal-expression.ts +6 -6
  222. package/src/lsp/lexicon-providers.test.ts +44 -0
  223. package/src/lsp/lexicon-providers.ts +11 -1
  224. package/src/op/activity-contract.test.ts +180 -0
  225. package/src/op/activity-contract.ts +278 -0
  226. package/src/op/builders-exports.test.ts +17 -1
  227. package/src/op/builders.ts +59 -5
  228. package/src/op/converge-rule.test.ts +179 -0
  229. package/src/op/converge-rule.ts +311 -0
  230. package/src/op/generate-pipeline.test.ts +53 -0
  231. package/src/op/generate-pipeline.ts +99 -0
  232. package/src/op/index.ts +30 -0
  233. package/src/op/local-executor.test.ts +92 -0
  234. package/src/op/local-executor.ts +45 -9
  235. package/src/op/op-verb-class.test.ts +126 -0
  236. package/src/op/op-verb-class.ts +115 -0
  237. package/src/op/operator.test.ts +346 -0
  238. package/src/op/operator.ts +213 -0
  239. package/src/op/step-output-ref.test.ts +334 -0
  240. package/src/op/step-output-ref.ts +453 -0
  241. package/src/op/types.ts +18 -1
  242. package/src/provenance.test.ts +151 -4
  243. package/src/provenance.ts +118 -4
  244. package/src/runtime-adapter.ts +31 -10
  245. package/src/serializer.ts +18 -0
  246. package/src/toml.test.ts +157 -384
  247. package/src/toml.ts +371 -5
package/src/toml.ts CHANGED
@@ -1,9 +1,375 @@
1
1
  /**
2
- * TOML emitter and parser — re-exports from split modules.
2
+ * Lightweight TOML parser (#446).
3
3
  *
4
- * Import from here for the public API:
5
- * import { emitTOML, parseTOML } from "@intentius/chant/toml";
4
+ * Written for the audit engine's spike: is there a pure, dependency-free way
5
+ * to parse a config format the engine can't read today? TOML fits — it is a
6
+ * fully-specified, static (no macros/interpolation/imports) grammar, so a
7
+ * hand-rolled tokenizer + recursive-descent parser covers it without any
8
+ * runtime surface a Workers sandbox would reject (no `eval`, no native
9
+ * modules, no filesystem/network).
10
+ *
11
+ * Covers the subset of TOML v1.0 that real-world deploy configs
12
+ * (`wrangler.toml`, `Cargo.toml`, `pyproject.toml`, `netlify.toml`) actually
13
+ * use: basic/literal strings (single-line), integers, floats, booleans,
14
+ * arrays (including multi-line), inline tables, dotted keys, standard tables
15
+ * (`[a.b]`), and arrays of tables (`[[a.b]]`).
16
+ *
17
+ * Deliberately NOT a full spec implementation:
18
+ * - Multi-line strings (`"""..."""`, `'''...'''`) are read as a single
19
+ * opaque string body (no line-ending backslash trimming); good enough to
20
+ * not crash, not spec-perfect for continuation whitespace.
21
+ * - Dates/times are returned as their raw source text (a string), not a
22
+ * `Date` — none of the checks built on this parser need calendar math.
23
+ * - A dotted-key path element must be a bare word or a plain quoted string;
24
+ * mixing exotic key shapes mid-path is not supported.
25
+ * These gaps only matter for exotic TOML; they don't affect the shapes the
26
+ * audit checks read (`[vars]`, `[[kv_namespaces]]`, `[env.NAME]`, …).
6
27
  */
7
28
 
8
- export { emitTOML, type EmitTOMLOptions } from "./toml-emit";
9
- export { parseTOML } from "./toml-parse";
29
+ export class TomlParseError extends Error {}
30
+
31
+ type Token =
32
+ | { t: "["; }
33
+ | { t: "]"; }
34
+ | { t: "[["; }
35
+ | { t: "]]"; }
36
+ | { t: "{"; }
37
+ | { t: "}"; }
38
+ | { t: "="; }
39
+ | { t: ","; }
40
+ | { t: "."; }
41
+ | { t: "nl"; }
42
+ | { t: "str"; v: string; }
43
+ | { t: "bare"; v: string; }
44
+ | { t: "eof"; };
45
+
46
+ const STOP_CHARS = new Set([" ", "\t", "\r", "\n", "[", "]", "{", "}", "=", ",", ".", "#", '"', "'"]);
47
+ /** Same stop set, minus `.` — used for numeric-looking tokens so `3.14` lexes whole. */
48
+ const NUM_STOP_CHARS = new Set([...STOP_CHARS].filter((ch) => ch !== "."));
49
+
50
+ /** Read a quoted string (basic `"..."`/`'...'` or multi-line `"""..."""`/`'''...'''`) starting at `i` (the opening quote). */
51
+ function readString(s: string, i: number): { value: string; next: number } {
52
+ const q = s[i];
53
+ const literal = q === "'";
54
+ const triple = s[i + 1] === q && s[i + 2] === q;
55
+ const quoteLen = triple ? 3 : 1;
56
+ let j = i + quoteLen;
57
+ // A newline immediately after an opening multi-line delimiter is trimmed (TOML spec).
58
+ if (triple && s[j] === "\r") j++;
59
+ if (triple && s[j] === "\n") j++;
60
+ let out = "";
61
+ while (true) {
62
+ if (j >= s.length) throw new TomlParseError(`Unterminated string starting at offset ${i}`);
63
+ if (s.startsWith(q.repeat(quoteLen), j)) {
64
+ return { value: out, next: j + quoteLen };
65
+ }
66
+ const c = s[j];
67
+ if (!triple && (c === "\n" || c === "\r")) {
68
+ throw new TomlParseError(`Unterminated single-line string starting at offset ${i}`);
69
+ }
70
+ if (literal) {
71
+ out += c;
72
+ j++;
73
+ continue;
74
+ }
75
+ if (c === "\\") {
76
+ const esc = s[j + 1];
77
+ if (triple && (esc === "\n" || esc === "\r" || esc === " " || esc === "\t")) {
78
+ // Line-ending backslash: consume the backslash, trailing whitespace, the
79
+ // newline, and all leading whitespace on the next line (folds away).
80
+ let k = j + 1;
81
+ while (s[k] === " " || s[k] === "\t") k++;
82
+ if (s[k] === "\r") k++;
83
+ if (s[k] === "\n") k++;
84
+ while (s[k] === " " || s[k] === "\t" || s[k] === "\n" || s[k] === "\r") k++;
85
+ j = k;
86
+ continue;
87
+ }
88
+ const simple: Record<string, string> = { n: "\n", t: "\t", r: "\r", b: "\b", f: "\f", '"': '"', "\\": "\\" };
89
+ if (esc !== undefined && esc in simple) {
90
+ out += simple[esc];
91
+ j += 2;
92
+ continue;
93
+ }
94
+ if (esc === "u" || esc === "U") {
95
+ const width = esc === "u" ? 4 : 8;
96
+ const hex = s.slice(j + 2, j + 2 + width);
97
+ if (hex.length < width || !/^[0-9a-fA-F]+$/.test(hex)) {
98
+ throw new TomlParseError(`Invalid unicode escape at offset ${j}`);
99
+ }
100
+ out += String.fromCodePoint(parseInt(hex, 16));
101
+ j += 2 + width;
102
+ continue;
103
+ }
104
+ throw new TomlParseError(`Invalid escape \\${esc} at offset ${j}`);
105
+ }
106
+ out += c;
107
+ j++;
108
+ }
109
+ }
110
+
111
+ function lex(input: string): Token[] {
112
+ const s = input.replace(/\r\n/g, "\n");
113
+ const tokens: Token[] = [];
114
+ let i = 0;
115
+ let atLineStart = true;
116
+ const n = s.length;
117
+ while (i < n) {
118
+ const c = s[i];
119
+ if (c === "\n") {
120
+ tokens.push({ t: "nl" });
121
+ i++;
122
+ atLineStart = true;
123
+ continue;
124
+ }
125
+ if (c === " " || c === "\t") {
126
+ i++;
127
+ continue;
128
+ }
129
+ if (c === "#") {
130
+ while (i < n && s[i] !== "\n") i++;
131
+ continue;
132
+ }
133
+ if (c === "[") {
134
+ if (atLineStart && s[i + 1] === "[") {
135
+ tokens.push({ t: "[[" });
136
+ i += 2;
137
+ } else {
138
+ tokens.push({ t: "[" });
139
+ i += 1;
140
+ }
141
+ atLineStart = false;
142
+ continue;
143
+ }
144
+ if (c === "]") {
145
+ if (s[i + 1] === "]" && tokens.length > 0 && hasUnmatchedArrayTableOpen(tokens)) {
146
+ tokens.push({ t: "]]" });
147
+ i += 2;
148
+ } else {
149
+ tokens.push({ t: "]" });
150
+ i += 1;
151
+ }
152
+ atLineStart = false;
153
+ continue;
154
+ }
155
+ if (c === "{") { tokens.push({ t: "{" }); i++; atLineStart = false; continue; }
156
+ if (c === "}") { tokens.push({ t: "}" }); i++; atLineStart = false; continue; }
157
+ if (c === "=") { tokens.push({ t: "=" }); i++; atLineStart = false; continue; }
158
+ if (c === ",") { tokens.push({ t: "," }); i++; atLineStart = false; continue; }
159
+ if (c === ".") { tokens.push({ t: "." }); i++; atLineStart = false; continue; }
160
+ if (c === '"' || c === "'") {
161
+ const { value, next } = readString(s, i);
162
+ tokens.push({ t: "str", v: value });
163
+ i = next;
164
+ atLineStart = false;
165
+ continue;
166
+ }
167
+ // A dotted key (`a.b`) uses `.` as a segment separator, so a plain
168
+ // bareword stops there. A value that looks numeric (starts with a digit,
169
+ // or +/- followed by one) needs `.` to stay IN the token so floats
170
+ // (`3.14`) and dates (`2024-01-01`, already dash-based) lex as one piece.
171
+ const numericStart = /[0-9]/.test(c) || ((c === "+" || c === "-") && /[0-9]/.test(s[i + 1] ?? ""));
172
+ const stopSet = numericStart ? NUM_STOP_CHARS : STOP_CHARS;
173
+ let j = i;
174
+ while (j < n && !stopSet.has(s[j])) j++;
175
+ if (j === i) throw new TomlParseError(`Unexpected character ${JSON.stringify(c)} at offset ${i}`);
176
+ tokens.push({ t: "bare", v: s.slice(i, j) });
177
+ i = j;
178
+ atLineStart = false;
179
+ }
180
+ tokens.push({ t: "eof" });
181
+ return tokens;
182
+ }
183
+
184
+ /** Is the most recent unmatched `[[` in `tokens` still open (no `]]` seen since)? Used only to decide whether a trailing `]]` closes an array-of-tables header vs. two array closes. */
185
+ function hasUnmatchedArrayTableOpen(tokens: Token[]): boolean {
186
+ let depth = 0;
187
+ for (const tok of tokens) {
188
+ if (tok.t === "[[") depth++;
189
+ else if (tok.t === "]]") depth--;
190
+ }
191
+ return depth > 0;
192
+ }
193
+
194
+ type TomlValue = string | number | boolean | TomlValue[] | { [k: string]: TomlValue };
195
+ type TomlTable = { [k: string]: TomlValue };
196
+
197
+ class Cursor {
198
+ constructor(private tokens: Token[], private pos = 0) {}
199
+ peek(): Token { return this.tokens[this.pos]; }
200
+ next(): Token { return this.tokens[this.pos++]; }
201
+ skipNl(): void { while (this.peek().t === "nl") this.pos++; }
202
+ expect<T extends Token["t"]>(t: T): Extract<Token, { t: T }> {
203
+ const tok = this.next();
204
+ if (tok.t !== t) throw new TomlParseError(`Expected ${t}, got ${tok.t}`);
205
+ return tok as Extract<Token, { t: T }>;
206
+ }
207
+ }
208
+
209
+ /** Read a dotted key path (`a.b.c`) — bare words or quoted strings joined by `.`. */
210
+ function readPath(c: Cursor): string[] {
211
+ const parts: string[] = [];
212
+ while (true) {
213
+ const tok = c.next();
214
+ if (tok.t === "bare") parts.push(tok.v);
215
+ else if (tok.t === "str") parts.push(tok.v);
216
+ else throw new TomlParseError(`Expected a key segment, got ${tok.t}`);
217
+ if (c.peek().t === ".") { c.next(); continue; }
218
+ break;
219
+ }
220
+ return parts;
221
+ }
222
+
223
+ const INT_RE = /^[+-]?(0|[1-9](_?\d)*)$/;
224
+ const HEX_RE = /^0x[0-9A-Fa-f](_?[0-9A-Fa-f])*$/;
225
+ const OCT_RE = /^0o[0-7](_?[0-7])*$/;
226
+ const BIN_RE = /^0b[01](_?[01])*$/;
227
+ const FLOAT_RE = /^[+-]?(\d(_?\d)*)(\.\d(_?\d)*)?([eE][+-]?\d(_?\d)*)?$/;
228
+
229
+ /** Coerce a bare value token to boolean / number / raw string (dates fall through as strings). */
230
+ function coerceBare(raw: string): TomlValue {
231
+ if (raw === "true") return true;
232
+ if (raw === "false") return false;
233
+ if (raw === "nan" || raw === "+nan" || raw === "-nan") return NaN;
234
+ if (raw === "inf" || raw === "+inf") return Infinity;
235
+ if (raw === "-inf") return -Infinity;
236
+ if (HEX_RE.test(raw)) return parseInt(raw.replace(/_/g, ""), 16);
237
+ if (OCT_RE.test(raw)) return parseInt(raw.replace(/_/g, "").slice(2), 8);
238
+ if (BIN_RE.test(raw)) return parseInt(raw.replace(/_/g, "").slice(2), 2);
239
+ if (INT_RE.test(raw)) return parseInt(raw.replace(/_/g, ""), 10);
240
+ if (FLOAT_RE.test(raw) && /[.eE]/.test(raw)) return parseFloat(raw.replace(/_/g, ""));
241
+ // Not a recognized scalar shape — likely a date/time or something exotic.
242
+ // Kept as its raw source text rather than failing the whole document.
243
+ return raw;
244
+ }
245
+
246
+ function parseValue(c: Cursor): TomlValue {
247
+ const tok = c.peek();
248
+ if (tok.t === "str") { c.next(); return tok.v; }
249
+ if (tok.t === "bare") { c.next(); return coerceBare(tok.v); }
250
+ if (tok.t === "[") return parseArray(c);
251
+ if (tok.t === "{") return parseInlineTable(c);
252
+ throw new TomlParseError(`Unexpected token ${tok.t} where a value was expected`);
253
+ }
254
+
255
+ function parseArray(c: Cursor): TomlValue[] {
256
+ c.expect("[");
257
+ const out: TomlValue[] = [];
258
+ c.skipNl();
259
+ while (c.peek().t !== "]") {
260
+ out.push(parseValue(c));
261
+ c.skipNl();
262
+ if (c.peek().t === ",") { c.next(); c.skipNl(); continue; }
263
+ break;
264
+ }
265
+ c.skipNl();
266
+ c.expect("]");
267
+ return out;
268
+ }
269
+
270
+ function assignDotted(target: TomlTable, path: string[], value: TomlValue): void {
271
+ let cur: TomlTable = target;
272
+ for (let i = 0; i < path.length - 1; i++) {
273
+ const seg = path[i];
274
+ const existing = cur[seg];
275
+ if (existing && typeof existing === "object" && !Array.isArray(existing)) {
276
+ cur = existing as TomlTable;
277
+ } else {
278
+ const created: TomlTable = {};
279
+ cur[seg] = created;
280
+ cur = created;
281
+ }
282
+ }
283
+ cur[path[path.length - 1]] = value;
284
+ }
285
+
286
+ function parseInlineTable(c: Cursor): TomlTable {
287
+ c.expect("{");
288
+ const out: TomlTable = {};
289
+ // Inline tables are single-line per spec, but stray newlines are tolerated
290
+ // here rather than rejected — the audit must never crash on odd input.
291
+ c.skipNl();
292
+ while (c.peek().t !== "}") {
293
+ const path = readPath(c);
294
+ c.expect("=");
295
+ const value = parseValue(c);
296
+ assignDotted(out, path, value);
297
+ c.skipNl();
298
+ if (c.peek().t === ",") { c.next(); c.skipNl(); continue; }
299
+ break;
300
+ }
301
+ c.skipNl();
302
+ c.expect("}");
303
+ return out;
304
+ }
305
+
306
+ /** Navigate/create a nested table (or the last element of a nested array-of-tables) at `path`, per TOML header semantics. */
307
+ function navigateTable(root: TomlTable, path: string[]): TomlTable {
308
+ let cur: TomlTable = root;
309
+ for (const seg of path) {
310
+ const existing = cur[seg];
311
+ if (existing === undefined) {
312
+ const created: TomlTable = {};
313
+ cur[seg] = created;
314
+ cur = created;
315
+ } else if (Array.isArray(existing)) {
316
+ const last = existing[existing.length - 1];
317
+ if (!last || typeof last !== "object") throw new TomlParseError(`Cannot navigate into array segment "${seg}"`);
318
+ cur = last as TomlTable;
319
+ } else if (typeof existing === "object") {
320
+ cur = existing as TomlTable;
321
+ } else {
322
+ throw new TomlParseError(`Key "${seg}" is already a scalar value`);
323
+ }
324
+ }
325
+ return cur;
326
+ }
327
+
328
+ function navigateArrayTable(root: TomlTable, path: string[]): TomlTable {
329
+ const parent = navigateTable(root, path.slice(0, -1));
330
+ const last = path[path.length - 1];
331
+ const existing = parent[last];
332
+ const arr: TomlValue[] = Array.isArray(existing) ? existing : [];
333
+ if (!Array.isArray(existing)) parent[last] = arr;
334
+ const entry: TomlTable = {};
335
+ arr.push(entry);
336
+ return entry;
337
+ }
338
+
339
+ /**
340
+ * Parse a TOML document into a plain nested object. Throws {@link TomlParseError}
341
+ * on malformed input — callers that scan arbitrary repo files should wrap the
342
+ * call in a try/catch (matching the rest of the audit engine's tolerance for
343
+ * unparseable content).
344
+ */
345
+ export function parseTOML(input: string): TomlTable {
346
+ const tokens = lex(input);
347
+ const c = new Cursor(tokens);
348
+ const root: TomlTable = {};
349
+ let current: TomlTable = root;
350
+ c.skipNl();
351
+ while (c.peek().t !== "eof") {
352
+ const tok = c.peek();
353
+ if (tok.t === "[") {
354
+ c.next();
355
+ const path = readPath(c);
356
+ c.expect("]");
357
+ current = navigateTable(root, path);
358
+ } else if (tok.t === "[[") {
359
+ c.next();
360
+ const path = readPath(c);
361
+ c.expect("]]");
362
+ current = navigateArrayTable(root, path);
363
+ } else {
364
+ const path = readPath(c);
365
+ c.expect("=");
366
+ const value = parseValue(c);
367
+ assignDotted(current, path, value);
368
+ }
369
+ const end = c.next();
370
+ if (end.t === "eof") break;
371
+ if (end.t !== "nl") throw new TomlParseError(`Expected end of line, got ${end.t}`);
372
+ c.skipNl();
373
+ }
374
+ return root;
375
+ }