paseo-plugin-genui 0.0.0-stage → 0.1.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 (375) hide show
  1. package/LICENSE +202 -0
  2. package/OVERVIEW.md +21 -0
  3. package/README.md +77 -2
  4. package/assets/fonts/Excalifont-OFL.txt +109 -0
  5. package/assets/fonts/Excalifont-Regular-latin.woff2 +0 -0
  6. package/assets/icon.png +0 -0
  7. package/assets/icon.svg +1 -0
  8. package/assets/screenshot-chat.png +0 -0
  9. package/assets/screenshot-gallery.png +0 -0
  10. package/assets/screenshot-playground.png +0 -0
  11. package/client/components/content.tsx +468 -0
  12. package/client/components/data.tsx +967 -0
  13. package/client/components/dev.tsx +998 -0
  14. package/client/components/diagram.tsx +513 -0
  15. package/client/components/inputs.tsx +994 -0
  16. package/client/components/layout.tsx +239 -0
  17. package/client/components/media.tsx +428 -0
  18. package/client/components/project.tsx +641 -0
  19. package/client/components/widgets.tsx +836 -0
  20. package/client/gallery.tsx +449 -0
  21. package/client/live-ui.tsx +525 -0
  22. package/client/loaders.ts +342 -0
  23. package/client/markdown.tsx +172 -0
  24. package/client/node-renderer.tsx +149 -0
  25. package/client/render.tsx +186 -0
  26. package/client/runtime.ts +103 -0
  27. package/client/settings.tsx +57 -0
  28. package/client/web.ts +186 -0
  29. package/gallery/Acceptance-baseline.ui +1 -0
  30. package/gallery/Acceptance-checklist.ui +14 -0
  31. package/gallery/Acceptance-evidence.ui +9 -0
  32. package/gallery/Acceptance.ui +1 -0
  33. package/gallery/Badge.ui +3 -0
  34. package/gallery/Benchmark-context.ui +8 -0
  35. package/gallery/Benchmark-hyperfine.ui +1 -0
  36. package/gallery/Benchmark-runs-list.ui +9 -0
  37. package/gallery/Benchmark-runs.ui +1 -0
  38. package/gallery/Benchmark-vitest.ui +1 -0
  39. package/gallery/Benchmark.ui +9 -0
  40. package/gallery/Board-bb-tasks.ui +1 -0
  41. package/gallery/Board-gh-issues.ui +1 -0
  42. package/gallery/Board-gh-prs.ui +1 -0
  43. package/gallery/Board.ui +12 -0
  44. package/gallery/Button-destructive.ui +4 -0
  45. package/gallery/Button.ui +5 -0
  46. package/gallery/Calendar.ui +15 -0
  47. package/gallery/CalendarHeatmap-short.ui +11 -0
  48. package/gallery/CalendarHeatmap.ui +4 -0
  49. package/gallery/Callout.ui +4 -0
  50. package/gallery/Card.ui +4 -0
  51. package/gallery/Carousel.ui +8 -0
  52. package/gallery/Changes-paths.ui +1 -0
  53. package/gallery/Changes-uncommitted.ui +1 -0
  54. package/gallery/Changes.ui +1 -0
  55. package/gallery/Chart-area.ui +3 -0
  56. package/gallery/Chart-funnel.ui +3 -0
  57. package/gallery/Chart-heatmap.ui +11 -0
  58. package/gallery/Chart-pie.ui +3 -0
  59. package/gallery/Chart-radar.ui +3 -0
  60. package/gallery/Chart-scatter.ui +4 -0
  61. package/gallery/Chart-stacked.ui +5 -0
  62. package/gallery/Chart.ui +3 -0
  63. package/gallery/Checklist.ui +6 -0
  64. package/gallery/Choice.ui +8 -0
  65. package/gallery/Code.ui +3 -0
  66. package/gallery/Commits-limit.ui +1 -0
  67. package/gallery/Commits.ui +1 -0
  68. package/gallery/Compare-conditions.ui +4 -0
  69. package/gallery/Compare-three.ui +7 -0
  70. package/gallery/Compare.ui +4 -0
  71. package/gallery/Coverage-go.ui +1 -0
  72. package/gallery/Coverage-lcov.ui +1 -0
  73. package/gallery/Coverage-python.ui +1 -0
  74. package/gallery/Coverage.ui +1 -0
  75. package/gallery/DateInput.ui +5 -0
  76. package/gallery/Decision-percent.ui +10 -0
  77. package/gallery/Decision.ui +12 -0
  78. package/gallery/Dependencies-audit.ui +1 -0
  79. package/gallery/Dependencies-both.ui +1 -0
  80. package/gallery/Dependencies-list.ui +7 -0
  81. package/gallery/Dependencies-pip.ui +1 -0
  82. package/gallery/Dependencies-pnpm.ui +1 -0
  83. package/gallery/Dependencies.ui +1 -0
  84. package/gallery/Details.ui +4 -0
  85. package/gallery/Diagram-flow.ui +16 -0
  86. package/gallery/Diagram-sequence.ui +15 -0
  87. package/gallery/Diagram.ui +16 -0
  88. package/gallery/Diff.ui +3 -0
  89. package/gallery/Divider.ui +5 -0
  90. package/gallery/EditableTable.ui +9 -0
  91. package/gallery/Entity-list.ui +6 -0
  92. package/gallery/Entity.ui +8 -0
  93. package/gallery/Environments.ui +1 -0
  94. package/gallery/Estimate-points.ui +6 -0
  95. package/gallery/Estimate.ui +8 -0
  96. package/gallery/File.ui +4 -0
  97. package/gallery/Findings-biome.ui +1 -0
  98. package/gallery/Findings-eslint.ui +1 -0
  99. package/gallery/Findings-oxlint.ui +1 -0
  100. package/gallery/Findings-review.ui +1 -0
  101. package/gallery/Findings-ruff.ui +1 -0
  102. package/gallery/Findings-sarif.ui +1 -0
  103. package/gallery/Findings-ship.ui +11 -0
  104. package/gallery/Findings-tsc.ui +1 -0
  105. package/gallery/Findings.ui +7 -0
  106. package/gallery/Flashcards.ui +3 -0
  107. package/gallery/FollowUps.ui +4 -0
  108. package/gallery/Form.ui +8 -0
  109. package/gallery/Gallery-links.ui +8 -0
  110. package/gallery/Gallery-strip.ui +14 -0
  111. package/gallery/Gallery.ui +10 -0
  112. package/gallery/Gauge.ui +7 -0
  113. package/gallery/Grid.ui +8 -0
  114. package/gallery/Hotspots-photo.ui +8 -0
  115. package/gallery/Hotspots.ui +11 -0
  116. package/gallery/Icon.ui +5 -0
  117. package/gallery/Image.ui +4 -0
  118. package/gallery/ImageSearch-aspects.ui +6 -0
  119. package/gallery/ImageSearch.ui +4 -0
  120. package/gallery/Input.ui +5 -0
  121. package/gallery/Json-api.ui +1 -0
  122. package/gallery/Json.ui +8 -0
  123. package/gallery/KeyValue.ui +3 -0
  124. package/gallery/List-dots.ui +8 -0
  125. package/gallery/List.ui +8 -0
  126. package/gallery/ListEditor.ui +5 -0
  127. package/gallery/Logs-jsonl.ui +1 -0
  128. package/gallery/Logs-lines.ui +11 -0
  129. package/gallery/Logs-python.ui +1 -0
  130. package/gallery/Logs.ui +1 -0
  131. package/gallery/Map-city.ui +8 -0
  132. package/gallery/Map.ui +8 -0
  133. package/gallery/Metric.ui +3 -0
  134. package/gallery/OptionCards-multi.ui +10 -0
  135. package/gallery/OptionCards.ui +10 -0
  136. package/gallery/Pipeline-checks.ui +1 -0
  137. package/gallery/Pipeline-passed.ui +1 -0
  138. package/gallery/Pipeline-running.ui +1 -0
  139. package/gallery/Pipeline-stages.ui +13 -0
  140. package/gallery/Pipeline.ui +1 -0
  141. package/gallery/Plan.ui +9 -0
  142. package/gallery/Progress.ui +4 -0
  143. package/gallery/Provenance-deploy.ui +6 -0
  144. package/gallery/Provenance-no-receipt.ui +1 -0
  145. package/gallery/Provenance.ui +1 -0
  146. package/gallery/PullRequest.ui +1 -0
  147. package/gallery/Quiz.ui +6 -0
  148. package/gallery/Radio.ui +5 -0
  149. package/gallery/Rating.ui +5 -0
  150. package/gallery/Roadmap-sprint.ui +8 -0
  151. package/gallery/Roadmap-year.ui +9 -0
  152. package/gallery/Roadmap.ui +12 -0
  153. package/gallery/Row.ui +3 -0
  154. package/gallery/SegmentedBar-status.ui +8 -0
  155. package/gallery/SegmentedBar.ui +8 -0
  156. package/gallery/Select.ui +5 -0
  157. package/gallery/Slider.ui +7 -0
  158. package/gallery/Snippet.ui +3 -0
  159. package/gallery/Sources-chips.ui +4 -0
  160. package/gallery/Sources.ui +8 -0
  161. package/gallery/Sparkline.ui +6 -0
  162. package/gallery/Stack.ui +7 -0
  163. package/gallery/StackTrace-aggregate.ui +1 -0
  164. package/gallery/StackTrace-browser.ui +1 -0
  165. package/gallery/StackTrace-go.ui +1 -0
  166. package/gallery/StackTrace-java.ui +1 -0
  167. package/gallery/StackTrace-python.ui +1 -0
  168. package/gallery/StackTrace-rust.ui +1 -0
  169. package/gallery/StackTrace.ui +1 -0
  170. package/gallery/Status.ui +9 -0
  171. package/gallery/Stepper.ui +5 -0
  172. package/gallery/Steps.ui +8 -0
  173. package/gallery/Swatches-long-names.ui +4 -0
  174. package/gallery/Swatches-picker.ui +5 -0
  175. package/gallery/Swatches.ui +10 -0
  176. package/gallery/Table.ui +7 -0
  177. package/gallery/Tabs.ui +8 -0
  178. package/gallery/Tests-junit.ui +1 -0
  179. package/gallery/Tests-vitest-junit.ui +1 -0
  180. package/gallery/Tests-vitest.ui +1 -0
  181. package/gallery/Tests.ui +9 -0
  182. package/gallery/Text.ui +4 -0
  183. package/gallery/TextArea.ui +5 -0
  184. package/gallery/Threads-ids.ui +1 -0
  185. package/gallery/Threads.ui +1 -0
  186. package/gallery/Timeline.ui +8 -0
  187. package/gallery/Timer.ui +4 -0
  188. package/gallery/Toggle.ui +5 -0
  189. package/gallery/Transcript-text.ui +1 -0
  190. package/gallery/Transcript-untimed.ui +1 -0
  191. package/gallery/Transcript-whisper.ui +1 -0
  192. package/gallery/Transcript.ui +1 -0
  193. package/gallery/Tree.ui +3 -0
  194. package/gallery/Treemap-du.ui +1 -0
  195. package/gallery/Treemap-esbuild.ui +1 -0
  196. package/gallery/Treemap-tree.ui +7 -0
  197. package/gallery/Treemap.ui +18 -0
  198. package/gallery/Walkthrough.ui +9 -0
  199. package/gallery/Waterfall-har.ui +1 -0
  200. package/gallery/Waterfall-trace.ui +1 -0
  201. package/gallery/Waterfall.ui +11 -0
  202. package/gallery/data/acceptance/coil/viewport-lifecycle.json +25 -0
  203. package/gallery/data/acceptance/coil-gate.json +153 -0
  204. package/gallery/data/acceptance/orbit-baseline.json +163 -0
  205. package/gallery/data/acceptance/shots/gate-cell-opened.png +0 -0
  206. package/gallery/data/acceptance/shots/gate-light.png +0 -0
  207. package/gallery/data/acceptance/shots/gate-phone-dark.png +0 -0
  208. package/gallery/data/activity.json +1 -0
  209. package/gallery/data/bb-tasks-orb.json +420 -0
  210. package/gallery/data/biome.json +229 -0
  211. package/gallery/data/compare/status-after.png +0 -0
  212. package/gallery/data/compare/status-before.png +0 -0
  213. package/gallery/data/compare/status-dark-2x.png +0 -0
  214. package/gallery/data/compare/status-rerender.png +0 -0
  215. package/gallery/data/coverage-py.json +1 -0
  216. package/gallery/data/coverage-summary.json +1 -0
  217. package/gallery/data/coverage.out +23 -0
  218. package/gallery/data/deploys.json +1 -0
  219. package/gallery/data/device-runs.json +285 -0
  220. package/gallery/data/du-node-modules.txt +359 -0
  221. package/gallery/data/esbuild-meta.json +292 -0
  222. package/gallery/data/eslint.json +266 -0
  223. package/gallery/data/eslint.sarif +343 -0
  224. package/gallery/data/findings-review.json +76 -0
  225. package/gallery/data/gh-issues-nextjs.json +293 -0
  226. package/gallery/data/gh-pr-checks.json +272 -0
  227. package/gallery/data/gh-prs-nextjs.json +419 -0
  228. package/gallery/data/gh-run-failed.json +1 -0
  229. package/gallery/data/gh-run-passed.json +1 -0
  230. package/gallery/data/gh-run-running.json +1 -0
  231. package/gallery/data/github-release.json +57 -0
  232. package/gallery/data/hyperfine-startup.json +187 -0
  233. package/gallery/data/lcov.info +234 -0
  234. package/gallery/data/logs/media-sync.log +127 -0
  235. package/gallery/data/logs/thumbnailer.jsonl +17 -0
  236. package/gallery/data/logs/vitest-run.log +89 -0
  237. package/gallery/data/npm-audit.json +672 -0
  238. package/gallery/data/npm-outdated.json +179 -0
  239. package/gallery/data/oxlint.json +156 -0
  240. package/gallery/data/pip-outdated.json +68 -0
  241. package/gallery/data/pnpm-outdated.json +58 -0
  242. package/gallery/data/provenance/devicectl-install.json +35 -0
  243. package/gallery/data/provenance/install-timeout.log +4 -0
  244. package/gallery/data/provenance/iphone-install.json +72 -0
  245. package/gallery/data/provenance/iphone-no-receipt.json +57 -0
  246. package/gallery/data/provenance/live-home.jpg +0 -0
  247. package/gallery/data/provenance/live-probe.txt +10 -0
  248. package/gallery/data/provenance/xcodebuild-2811.log +19 -0
  249. package/gallery/data/provenance/xcodebuild-4712.log +19 -0
  250. package/gallery/data/pytest-junit.xml +64 -0
  251. package/gallery/data/python-org-trace.json +35 -0
  252. package/gallery/data/python-org.har +31 -0
  253. package/gallery/data/retry.ts +13 -0
  254. package/gallery/data/ruff.json +261 -0
  255. package/gallery/data/src/durations.py +63 -0
  256. package/gallery/data/src/json-logic.ts +156 -0
  257. package/gallery/data/src/mapMath.ts +135 -0
  258. package/gallery/data/src/semver/bump.go +73 -0
  259. package/gallery/data/traces/browser-error.json +5 -0
  260. package/gallery/data/traces/go/main.go +27 -0
  261. package/gallery/data/traces/go-panic.txt +16 -0
  262. package/gallery/data/traces/java-caused-by.txt +15 -0
  263. package/gallery/data/traces/node-aggregate.txt +30 -0
  264. package/gallery/data/traces/node-cause.txt +21 -0
  265. package/gallery/data/traces/py/deploy.json +5 -0
  266. package/gallery/data/traces/py/deploy.py +17 -0
  267. package/gallery/data/traces/py/settings.py +19 -0
  268. package/gallery/data/traces/python-chain.txt +30 -0
  269. package/gallery/data/traces/rust/main.rs +11 -0
  270. package/gallery/data/traces/rust-panic.txt +16 -0
  271. package/gallery/data/traces/shop/cart.mjs +19 -0
  272. package/gallery/data/traces/shop/pricing.mjs +12 -0
  273. package/gallery/data/traces/shop/status.mjs +9 -0
  274. package/gallery/data/traces/web/app.js +11 -0
  275. package/gallery/data/traces/web/index.html +1 -0
  276. package/gallery/data/traces/web/search.js +10 -0
  277. package/gallery/data/transcript/call-noisy.wav +0 -0
  278. package/gallery/data/transcript/call.json +362 -0
  279. package/gallery/data/transcript/call.wav +0 -0
  280. package/gallery/data/transcript/mlx-whisper-tiny.en-noisy.json +1 -0
  281. package/gallery/data/transcript/whisper-cpp-base.en-noisy.json +658 -0
  282. package/gallery/data/transcript/whisper-cpp-base.en.json +658 -0
  283. package/gallery/data/transcript/whisper-cpp-base.en.srt +16 -0
  284. package/gallery/data/transcript/whisper-cpp-tiny.en-noisy.json +619 -0
  285. package/gallery/data/tsc.txt +4 -0
  286. package/gallery/data/vitest-bench.json +119 -0
  287. package/gallery/data/vitest-junit.xml +133 -0
  288. package/gallery/data/vitest-report.json +612 -0
  289. package/gallery/images/bell.svg +10 -0
  290. package/gallery/images/bike.svg +47 -0
  291. package/gallery/images/footer-planet.webp +0 -0
  292. package/gallery/images/front-light.svg +13 -0
  293. package/gallery/images/golden-hour.webp +0 -0
  294. package/gallery/images/option-flowers.webp +0 -0
  295. package/gallery/images/option-hilltop.webp +0 -0
  296. package/gallery/images/option-meadow.webp +0 -0
  297. package/gallery/images/option-sky.webp +0 -0
  298. package/gallery/images/u-lock.svg +10 -0
  299. package/index.client.tsx +164 -0
  300. package/index.server.ts +216 -0
  301. package/package.json +50 -4
  302. package/paseo-plugin.json +14 -0
  303. package/server/data-source.ts +407 -0
  304. package/server/project.ts +981 -0
  305. package/server/web/address.ts +57 -0
  306. package/server/web/cache.ts +85 -0
  307. package/server/web/commons.ts +126 -0
  308. package/server/web/geocode.ts +40 -0
  309. package/server/web/image.ts +35 -0
  310. package/server/web/lookups.ts +248 -0
  311. package/server/web/net.ts +210 -0
  312. package/shared/core/acceptance.ts +744 -0
  313. package/shared/core/ast.ts +76 -0
  314. package/shared/core/benchmark.ts +743 -0
  315. package/shared/core/catalog.ts +733 -0
  316. package/shared/core/compare.ts +225 -0
  317. package/shared/core/coverage.ts +456 -0
  318. package/shared/core/data.ts +334 -0
  319. package/shared/core/decisions.ts +527 -0
  320. package/shared/core/dependencies.ts +604 -0
  321. package/shared/core/diagnostics.ts +24 -0
  322. package/shared/core/environments.ts +212 -0
  323. package/shared/core/evaluate.ts +737 -0
  324. package/shared/core/fields.ts +66 -0
  325. package/shared/core/findings.ts +1172 -0
  326. package/shared/core/form.ts +69 -0
  327. package/shared/core/index.ts +44 -0
  328. package/shared/core/junit.ts +252 -0
  329. package/shared/core/lexer.ts +275 -0
  330. package/shared/core/logs.ts +496 -0
  331. package/shared/core/markdown.ts +2374 -0
  332. package/shared/core/parser.ts +355 -0
  333. package/shared/core/perf-units.ts +228 -0
  334. package/shared/core/pipeline.ts +679 -0
  335. package/shared/core/project.ts +472 -0
  336. package/shared/core/provenance.ts +725 -0
  337. package/shared/core/stack-trace.ts +718 -0
  338. package/shared/core/streaming.ts +136 -0
  339. package/shared/core/test-results.ts +562 -0
  340. package/shared/core/thread-activity.ts +203 -0
  341. package/shared/core/tracking.ts +887 -0
  342. package/shared/core/transcript.ts +1364 -0
  343. package/shared/core/treemap.ts +446 -0
  344. package/shared/core/types.ts +70 -0
  345. package/shared/core/validate.ts +574 -0
  346. package/shared/core/waterfall.ts +536 -0
  347. package/shared/diagram/flowchart.ts +205 -0
  348. package/shared/diagram/layout.ts +440 -0
  349. package/shared/diagram/markdown.ts +135 -0
  350. package/shared/diagram/sequence.ts +122 -0
  351. package/shared/diagram/zoom.ts +114 -0
  352. package/shared/fallback.ts +120 -0
  353. package/shared/genui.test.ts +122 -0
  354. package/shared/instructions.ts +144 -0
  355. package/shared/logic/calendarMath.ts +130 -0
  356. package/shared/logic/chartMath.ts +289 -0
  357. package/shared/logic/choose-logic.ts +196 -0
  358. package/shared/logic/compare-logic.ts +270 -0
  359. package/shared/logic/content-model.ts +227 -0
  360. package/shared/logic/environments-logic.ts +53 -0
  361. package/shared/logic/explain-logic.ts +315 -0
  362. package/shared/logic/fieldLogic.ts +138 -0
  363. package/shared/logic/gallery-search.ts +71 -0
  364. package/shared/logic/icons.ts +322 -0
  365. package/shared/logic/json-logic.ts +153 -0
  366. package/shared/logic/mapMath.ts +133 -0
  367. package/shared/logic/project-logic.ts +39 -0
  368. package/shared/logic/references-model.ts +201 -0
  369. package/shared/logic/sketch.ts +165 -0
  370. package/shared/logic/supersession.ts +77 -0
  371. package/shared/logic/values.ts +133 -0
  372. package/shared/logic/widget-logic.ts +163 -0
  373. package/shared/rpc.ts +252 -0
  374. package/shared/segment.ts +81 -0
  375. package/shared/settings.ts +12 -0
@@ -0,0 +1,355 @@
1
+ import type { BinaryOperator, Expr, NamedArg, Statement } from "./ast";
2
+ import type { DiagnosticList, Pos } from "./diagnostics";
3
+ import type { Token } from "./lexer";
4
+
5
+ const PRECEDENCE: Record<string, number> = {
6
+ "??": 1,
7
+ "||": 2,
8
+ "&&": 3,
9
+ "==": 4, "!=": 4, "===": 4, "!==": 4,
10
+ "<": 5, "<=": 5, ">": 5, ">=": 5,
11
+ "+": 6, "-": 6,
12
+ "*": 7, "/": 7, "%": 7,
13
+ };
14
+
15
+ const KEYWORDS: Record<string, true | false | null> = { true: true, false: false, null: null };
16
+
17
+ /** AST depth beyond this is rejected so later tree walks cannot overflow the stack. */
18
+ const MAX_NESTING = 100;
19
+
20
+ class ParseError {
21
+ constructor(
22
+ readonly pos: Pos,
23
+ readonly message: string,
24
+ ) {}
25
+ }
26
+
27
+ function describe(token: Token): string {
28
+ switch (token.t) {
29
+ case "num":
30
+ return `number ${token.value}`;
31
+ case "str":
32
+ return "a string";
33
+ case "tpl":
34
+ return "a template string";
35
+ case "ident":
36
+ return `\`${token.value}\``;
37
+ case "state":
38
+ return `\`$${token.value}\``;
39
+ case "punct":
40
+ return `\`${token.value}\``;
41
+ case "sep":
42
+ return token.value === ";" ? "`;`" : "end of line";
43
+ case "eof":
44
+ return "end of input";
45
+ }
46
+ }
47
+
48
+ /** Parses all statements; syntax errors are reported and their statement skipped. */
49
+ export function parse(tokens: readonly Token[], diagnostics: DiagnosticList): Statement[] {
50
+ const parser = new Parser(tokens, 0);
51
+ const statements: Statement[] = [];
52
+ for (;;) {
53
+ while (parser.peek().t === "sep") parser.i++;
54
+ if (parser.peek().t === "eof") return statements;
55
+ try {
56
+ statements.push(parser.statement());
57
+ } catch (error) {
58
+ if (!(error instanceof ParseError)) throw error;
59
+ diagnostics.report(error.pos, error.message);
60
+ while (parser.peek().t !== "sep" && parser.peek().t !== "eof") parser.i++;
61
+ }
62
+ }
63
+ }
64
+
65
+ class Parser {
66
+ i = 0;
67
+
68
+ constructor(
69
+ private readonly tokens: readonly Token[],
70
+ private depth: number,
71
+ ) {}
72
+
73
+ peek(offset = 0): Token {
74
+ return this.tokens[Math.min(this.i + offset, this.tokens.length - 1)];
75
+ }
76
+
77
+ private isPunct(value: string, offset = 0): boolean {
78
+ const token = this.peek(offset);
79
+ return token.t === "punct" && token.value === value;
80
+ }
81
+
82
+ private eat(value: string): boolean {
83
+ if (!this.isPunct(value)) return false;
84
+ this.i++;
85
+ return true;
86
+ }
87
+
88
+ /** Consumes the closer of `open`, or fails naming what else was acceptable. */
89
+ private close(closer: string, open: Token, alternatives = ""): void {
90
+ if (this.eat(closer)) return;
91
+ const opener = open.t === "punct" ? open.value : "(";
92
+ throw new ParseError(
93
+ this.peek(),
94
+ `expected ${alternatives}\`${closer}\` to close \`${opener}\` from ${open.line}:${open.column}, found ${describe(this.peek())}`,
95
+ );
96
+ }
97
+
98
+ private ident(what: string): Pos & { readonly value: string } {
99
+ const token = this.peek();
100
+ if (token.t !== "ident") throw new ParseError(token, `expected ${what}, found ${describe(token)}`);
101
+ this.i++;
102
+ return token;
103
+ }
104
+
105
+ statement(): Statement {
106
+ this.depth = 0;
107
+ const first = this.peek();
108
+ let statement: Statement;
109
+ if ((first.t === "state" || first.t === "ident") && this.isPunct("=", 1)) {
110
+ if (first.t === "ident" && Object.hasOwn(KEYWORDS, first.value)) {
111
+ throw new ParseError(first, `\`${first.value}\` is a keyword and cannot be assigned`);
112
+ }
113
+ this.i += 2;
114
+ const kind = first.t === "state" ? "state" : "named";
115
+ statement = { kind, name: first.value, value: this.expr(), pos: first };
116
+ } else {
117
+ statement = { kind: "root", value: this.expr() };
118
+ }
119
+ const next = this.peek();
120
+ if (next.t !== "sep" && next.t !== "eof") {
121
+ throw new ParseError(
122
+ next,
123
+ `expected a new line or \`;\` after the statement, found ${describe(next)}; wrap multi-line expressions in brackets`,
124
+ );
125
+ }
126
+ return statement;
127
+ }
128
+
129
+ /** Counts one level of AST nesting; callers restore `depth` when their subtree is done. */
130
+ private nest(at: Token): void {
131
+ if (++this.depth > MAX_NESTING) throw new ParseError(at, "expression is nested too deeply; split it into named values");
132
+ }
133
+
134
+ private expr(): Expr {
135
+ const saved = this.depth;
136
+ this.nest(this.peek());
137
+ const expr = this.isArrowStart() ? this.arrow() : this.conditional();
138
+ this.depth = saved;
139
+ return expr;
140
+ }
141
+
142
+ private isArrowStart(): boolean {
143
+ if (this.peek().t === "ident") return this.isPunct("=>", 1);
144
+ if (!this.isPunct("(")) return false;
145
+ let offset = 1;
146
+ if (this.peek(offset).t === "ident") {
147
+ offset++;
148
+ while (this.isPunct(",", offset) && this.peek(offset + 1).t === "ident") offset += 2;
149
+ }
150
+ return this.isPunct(")", offset) && this.isPunct("=>", offset + 1);
151
+ }
152
+
153
+ private arrow(): Expr {
154
+ const start = this.peek();
155
+ const params: string[] = [];
156
+ if (this.eat("(")) {
157
+ while (!this.eat(")")) {
158
+ params.push(this.ident("a parameter name").value);
159
+ this.eat(",");
160
+ }
161
+ } else {
162
+ params.push(this.ident("a parameter name").value);
163
+ }
164
+ const keyword = params.find((name) => Object.hasOwn(KEYWORDS, name));
165
+ if (keyword !== undefined) throw new ParseError(start, `\`${keyword}\` cannot be a parameter name`);
166
+ this.i++; // `=>`, guaranteed by isArrowStart
167
+ return { type: "arrow", params, body: this.expr(), pos: start };
168
+ }
169
+
170
+ private conditional(): Expr {
171
+ const test = this.binary(1);
172
+ const question = this.peek();
173
+ if (!this.eat("?")) return test;
174
+ const then = this.expr();
175
+ if (!this.eat(":")) {
176
+ throw new ParseError(this.peek(), `expected \`:\` for the \`?\` at ${question.line}:${question.column}, found ${describe(this.peek())}`);
177
+ }
178
+ return { type: "conditional", test, then, else: this.expr(), pos: question };
179
+ }
180
+
181
+ private binary(minPrecedence: number): Expr {
182
+ const saved = this.depth;
183
+ let left = this.unary();
184
+ for (;;) {
185
+ const token = this.peek();
186
+ if (token.t !== "punct") break;
187
+ const precedence = PRECEDENCE[token.value];
188
+ if (precedence === undefined || precedence < minPrecedence) break;
189
+ this.nest(token);
190
+ this.i++;
191
+ const right = this.binary(precedence + 1);
192
+ left = { type: "binary", op: token.value as BinaryOperator, left, right, pos: token };
193
+ }
194
+ this.depth = saved;
195
+ return left;
196
+ }
197
+
198
+ private unary(): Expr {
199
+ const saved = this.depth;
200
+ const operators: Token[] = [];
201
+ while (this.isPunct("!") || this.isPunct("-")) {
202
+ this.nest(this.peek());
203
+ operators.push(this.tokens[this.i++]);
204
+ }
205
+ let expr = this.postfix();
206
+ // `**` binds tighter than a prefix operator and associates to the right,
207
+ // as in maths and Python: `-a ** b` is `-(a ** b)`, `a ** b ** c` is `a ** (b ** c)`.
208
+ if (this.isPunct("**")) {
209
+ const token = this.peek();
210
+ this.nest(token);
211
+ this.i++;
212
+ expr = { type: "binary", op: "**", left: expr, right: this.unary(), pos: token };
213
+ }
214
+ for (let k = operators.length - 1; k >= 0; k--) {
215
+ const token = operators[k];
216
+ expr = { type: "unary", op: token.t === "punct" && token.value === "!" ? "!" : "-", operand: expr, pos: token };
217
+ }
218
+ this.depth = saved;
219
+ return expr;
220
+ }
221
+
222
+ private postfix(): Expr {
223
+ let expr = this.primary();
224
+ for (;;) {
225
+ if (this.isPunct(".") || this.isPunct("[") || this.isPunct("(")) this.nest(this.peek());
226
+ if (this.eat(".")) {
227
+ const name = this.ident("a property or method name after `.`");
228
+ if (this.isPunct("(")) {
229
+ // `Math.round(x)` and `Object.entries(o)` are how models write the
230
+ // built-ins; they parse as the plain calls `round(x)` and `entries(o)`.
231
+ expr =
232
+ expr.type === "ident" && (expr.name === "Math" || expr.name === "Object")
233
+ ? { type: "call", name: name.value, ...this.args(), pos: name }
234
+ : { type: "method", object: expr, name: name.value, ...this.args(), pos: name };
235
+ } else {
236
+ expr = { type: "member", object: expr, name: name.value, pos: name };
237
+ }
238
+ } else if (this.isPunct("[")) {
239
+ const open = this.tokens[this.i++];
240
+ const index = this.expr();
241
+ this.close("]", open);
242
+ expr = { type: "index", object: expr, index, pos: open };
243
+ } else if (this.isPunct("(")) {
244
+ if (expr.type !== "ident") {
245
+ throw new ParseError(this.peek(), "only components, actions, functions and methods can be called");
246
+ }
247
+ expr = { type: "call", name: expr.name, ...this.args(), pos: expr.pos };
248
+ } else {
249
+ return expr;
250
+ }
251
+ }
252
+ }
253
+
254
+ private args(): { args: Expr[]; named: NamedArg[] } {
255
+ const open = this.tokens[this.i++];
256
+ const args: Expr[] = [];
257
+ const named: NamedArg[] = [];
258
+ while (!this.eat(")")) {
259
+ const token = this.peek();
260
+ if (token.t === "ident" && this.isPunct(":", 1)) {
261
+ this.i += 2;
262
+ named.push({ name: token.value, value: this.expr(), pos: token });
263
+ } else {
264
+ if (named.length > 0) {
265
+ throw new ParseError(token, "positional arguments must come before named ones");
266
+ }
267
+ args.push(this.expr());
268
+ }
269
+ if (!this.eat(",")) {
270
+ this.close(")", open, "`,` or ");
271
+ break;
272
+ }
273
+ }
274
+ return { args, named };
275
+ }
276
+
277
+ private primary(): Expr {
278
+ const token = this.peek();
279
+ switch (token.t) {
280
+ case "num":
281
+ case "str":
282
+ this.i++;
283
+ return { type: "literal", value: token.value, pos: token };
284
+ case "tpl": {
285
+ this.i++;
286
+ const parts = token.parts.map((tokens) => {
287
+ const sub = new Parser(tokens, this.depth);
288
+ const part = sub.expr();
289
+ const end = sub.peek();
290
+ if (end.t !== "eof") {
291
+ throw new ParseError(end, `expected \`}\` to end the template expression, found ${describe(end)}`);
292
+ }
293
+ return part;
294
+ });
295
+ return { type: "template", quasis: token.quasis, parts, pos: token };
296
+ }
297
+ case "ident":
298
+ this.i++;
299
+ if (Object.hasOwn(KEYWORDS, token.value)) return { type: "literal", value: KEYWORDS[token.value], pos: token };
300
+ return { type: "ident", name: token.value, pos: token };
301
+ case "state":
302
+ this.i++;
303
+ return { type: "state", name: token.value, pos: token };
304
+ case "punct":
305
+ if (token.value === "(") {
306
+ this.i++;
307
+ const inner = this.expr();
308
+ this.close(")", token);
309
+ return inner;
310
+ }
311
+ if (token.value === "[") {
312
+ this.i++;
313
+ const items: Expr[] = [];
314
+ while (!this.eat("]")) {
315
+ items.push(this.expr());
316
+ if (!this.eat(",")) {
317
+ this.close("]", token, "`,` or ");
318
+ break;
319
+ }
320
+ }
321
+ return { type: "list", items, pos: token };
322
+ }
323
+ if (token.value === "{") return this.object(token);
324
+ break;
325
+ }
326
+ throw new ParseError(token, `expected an expression, found ${describe(token)}`);
327
+ }
328
+
329
+ private object(open: Token): Expr {
330
+ this.i++;
331
+ const entries: { key: string; value: Expr; pos: Pos }[] = [];
332
+ while (!this.eat("}")) {
333
+ const key = this.peek();
334
+ if (key.t !== "ident" && key.t !== "str") {
335
+ throw new ParseError(key, `expected an object key (a name or a string), found ${describe(key)}`);
336
+ }
337
+ this.i++;
338
+ // `{Good for: …}`: models write labels as bare words; nothing else may follow a key, so read them as one.
339
+ let name = key.value;
340
+ if (key.t === "ident") {
341
+ for (let next = this.peek(); next.t === "ident" || next.t === "num"; next = this.peek()) {
342
+ name += ` ${next.value}`;
343
+ this.i++;
344
+ }
345
+ }
346
+ if (!this.eat(":")) throw new ParseError(this.peek(), `expected \`:\` after the object key, found ${describe(this.peek())}`);
347
+ entries.push({ key: name, value: this.expr(), pos: key });
348
+ if (!this.eat(",")) {
349
+ this.close("}", open, "`,` or ");
350
+ break;
351
+ }
352
+ }
353
+ return { type: "object", entries, pos: open };
354
+ }
355
+ }
@@ -0,0 +1,228 @@
1
+ // Units for performance data (Benchmark, Waterfall, Treemap): what a unit word
2
+ // measures, values auto-scaled to the unit a person would read them in
3
+ // (0.0277 ms → 27.7 µs, 349438 B → 341 KB), signed deltas, and axis ticks;
4
+ // and the one field reader their parsers share. Pure, shared by the renderers
5
+ // and the markdown fallback.
6
+ import { isPlainObject } from "./evaluate";
7
+ import type { Value } from "./types";
8
+
9
+ /** An object's fields by key. */
10
+ export type Fields = { readonly [key: string]: Value };
11
+
12
+ /**
13
+ * `value` as fields to read by key when it is a plain object, else null.
14
+ * Takes the `undefined` an absent field reads as, and widens past the Action
15
+ * shapes isPlainObject's narrowing keeps, like markdown.ts's `fields`.
16
+ */
17
+ export function objectFields(value: Value | undefined): Fields | null {
18
+ return value !== undefined && isPlainObject(value) ? value : null;
19
+ }
20
+
21
+ export type UnitKind = "time" | "bytes" | "rate" | "percent" | "count";
22
+
23
+ export interface UnitInfo {
24
+ readonly kind: UnitKind;
25
+ /** Multiplier from the unit to its kind's base: seconds, bytes, per second; 1 for percent and counts. */
26
+ readonly factor: number;
27
+ /** The unit as written, for counts (`"req"`, `"$"`); null when there is none. */
28
+ readonly label: string | null;
29
+ }
30
+
31
+ const TIME: Record<string, number> = {
32
+ ns: 1e-9,
33
+ nanosecond: 1e-9,
34
+ nanoseconds: 1e-9,
35
+ us: 1e-6,
36
+ "µs": 1e-6,
37
+ "μs": 1e-6,
38
+ microsecond: 1e-6,
39
+ microseconds: 1e-6,
40
+ ms: 1e-3,
41
+ millisecond: 1e-3,
42
+ milliseconds: 1e-3,
43
+ s: 1,
44
+ sec: 1,
45
+ secs: 1,
46
+ second: 1,
47
+ seconds: 1,
48
+ min: 60,
49
+ mins: 60,
50
+ minute: 60,
51
+ minutes: 60,
52
+ h: 3600,
53
+ hr: 3600,
54
+ hrs: 3600,
55
+ hour: 3600,
56
+ hours: 3600,
57
+ };
58
+
59
+ /** Bytes are counted in 1,024s, as `du`, esbuild and most dev tools do. */
60
+ const BYTES: Record<string, number> = {
61
+ b: 1,
62
+ byte: 1,
63
+ bytes: 1,
64
+ kb: 1024,
65
+ kib: 1024,
66
+ mb: 1024 ** 2,
67
+ mib: 1024 ** 2,
68
+ gb: 1024 ** 3,
69
+ gib: 1024 ** 3,
70
+ tb: 1024 ** 4,
71
+ tib: 1024 ** 4,
72
+ };
73
+
74
+ const RATE = /^(?:ops?|req|requests|queries|it|iterations|runs|frames|events)\s*\/\s*(?:s|sec|second)$|^(?:hz|rps|qps|fps)$/i;
75
+
76
+ /** What `unit` measures; anything unknown is a count labelled with the unit. */
77
+ export function unitInfo(unit: string | null | undefined): UnitInfo {
78
+ const raw = (unit ?? "").trim();
79
+ if (raw === "") return { kind: "count", factor: 1, label: null };
80
+ if (raw === "%") return { kind: "percent", factor: 1, label: "%" };
81
+ const lower = raw.toLowerCase();
82
+ if (Object.hasOwn(TIME, raw)) return { kind: "time", factor: TIME[raw], label: raw };
83
+ if (Object.hasOwn(TIME, lower) && lower !== "m") return { kind: "time", factor: TIME[lower], label: raw };
84
+ if (Object.hasOwn(BYTES, lower)) return { kind: "bytes", factor: BYTES[lower], label: raw };
85
+ if (RATE.test(raw)) return { kind: "rate", factor: 1, label: /^hz$/i.test(raw) ? "ops/s" : raw };
86
+ return { kind: "count", factor: 1, label: raw };
87
+ }
88
+
89
+ /** `value` to `digits` significant digits, grouped (12,345 · 18.4 · 1.84 · 0.000123). */
90
+ export function significant(value: number, digits = 3): string {
91
+ if (!Number.isFinite(value)) return "—";
92
+ if (value === 0) return "0";
93
+ const magnitude = Math.floor(Math.log10(Math.abs(value)));
94
+ const decimals = Math.min(6, Math.max(0, digits - 1 - magnitude));
95
+ return new Intl.NumberFormat("en-US", { maximumFractionDigits: decimals, minimumFractionDigits: 0 }).format(
96
+ Number(value.toFixed(decimals)),
97
+ );
98
+ }
99
+
100
+ /** Currency symbols lead (`$5`); other units trail (`5%`, `5 ms`). */
101
+ function attach(number: string, unit: string | null): string {
102
+ if (unit === null || unit === "") return number;
103
+ if (/^[$€£¥]$/.test(unit)) return number.startsWith("-") || number.startsWith("−") ? `${number[0]}${unit}${number.slice(1)}` : `${unit}${number}`;
104
+ if (unit === "%") return `${number}%`;
105
+ return `${number} ${unit}`;
106
+ }
107
+
108
+ /** The divisor and label that read a time of `seconds` best: ns, µs, ms, s, min or h. */
109
+ function timeScale(seconds: number): [number, string] {
110
+ const size = Math.abs(seconds);
111
+ if (size === 0) return [1e-3, "ms"];
112
+ if (size < 1e-6) return [1e-9, "ns"];
113
+ if (size < 1e-3) return [1e-6, "µs"];
114
+ if (size < 1) return [1e-3, "ms"];
115
+ if (size < 120) return [1, "s"];
116
+ if (size < 7200) return [60, "min"];
117
+ return [3600, "h"];
118
+ }
119
+
120
+ function byteScale(bytes: number): [number, string] {
121
+ const size = Math.abs(bytes);
122
+ if (size < 1024) return [1, "B"];
123
+ if (size < 1024 ** 2) return [1024, "KB"];
124
+ if (size < 1024 ** 3) return [1024 ** 2, "MB"];
125
+ if (size < 1024 ** 4) return [1024 ** 3, "GB"];
126
+ return [1024 ** 4, "TB"];
127
+ }
128
+
129
+ function countScale(value: number): [number, string] {
130
+ const size = Math.abs(value);
131
+ if (size < 1e4) return [1, ""];
132
+ if (size < 1e6) return [1e3, "K"];
133
+ if (size < 1e9) return [1e6, "M"];
134
+ return [1e9, "B"];
135
+ }
136
+
137
+ /**
138
+ * `value` (in `unit`) as a person reads it: 0.0277 ms → "27.7 µs", 349438 B →
139
+ * "341 KB", 36074 ops/s → "36.1K ops/s". `scaleBy` picks the scale instead of
140
+ * the value itself, so a spread reads in its mean's unit ("7.8 ms ± 0.75 ms").
141
+ */
142
+ export function formatAmount(value: number, unit: string | null | undefined, digits = 3, scaleBy = value): string {
143
+ if (!Number.isFinite(value)) return "—";
144
+ const info = unitInfo(unit);
145
+ switch (info.kind) {
146
+ case "time": {
147
+ const [divisor, label] = timeScale((Number.isFinite(scaleBy) ? scaleBy : value) * info.factor);
148
+ return `${significant((value * info.factor) / divisor, digits)} ${label}`;
149
+ }
150
+ case "bytes": {
151
+ const [divisor, label] = byteScale((Number.isFinite(scaleBy) ? scaleBy : value) * info.factor);
152
+ const scaled = (value * info.factor) / divisor;
153
+ return `${label === "B" ? significant(Math.round(scaled), Math.max(digits, 4)) : significant(scaled, digits)} ${label}`;
154
+ }
155
+ case "rate": {
156
+ const [divisor, suffix] = countScale(Number.isFinite(scaleBy) ? scaleBy : value);
157
+ return `${significant(value / divisor, digits)}${suffix} ${info.label}`;
158
+ }
159
+ case "percent":
160
+ return `${significant(value, digits)}%`;
161
+ case "count": {
162
+ // Plain counts keep every digit up to 10,000 (1,234 req), then compact.
163
+ const [divisor, suffix] = countScale(Number.isFinite(scaleBy) ? scaleBy : value);
164
+ const number = suffix === "" && Number.isInteger(value) ? significant(value, 12) : `${significant(value / divisor, digits)}${suffix}`;
165
+ return attach(number, info.label);
166
+ }
167
+ }
168
+ }
169
+
170
+ /** A signed amount: "+1.2 ms", "−340 KB"; zero is "±0". */
171
+ export function formatDelta(value: number, unit: string | null | undefined, digits = 3): string {
172
+ if (!Number.isFinite(value)) return "—";
173
+ if (value === 0) return `±${formatAmount(0, unit, digits)}`;
174
+ const body = formatAmount(Math.abs(value), unit, digits);
175
+ return `${value > 0 ? "+" : "−"}${body}`;
176
+ }
177
+
178
+ /** A signed percent: "+12.3%", "−0.4%", "±0%"; slivers read "<0.1%" with their sign. */
179
+ export function formatPercent(percent: number): string {
180
+ if (!Number.isFinite(percent)) return "—";
181
+ if (percent === 0) return "±0%";
182
+ const sign = percent > 0 ? "+" : "−";
183
+ const size = Math.abs(percent);
184
+ if (size < 0.1) return `${sign}<0.1%`;
185
+ return `${sign}${size >= 100 ? significant(size, 4) : size.toFixed(1)}%`;
186
+ }
187
+
188
+ /** A milliseconds duration, auto-scaled (Waterfall's internal unit). */
189
+ export function formatMs(ms: number, digits = 3): string {
190
+ return formatAmount(ms, "ms", digits);
191
+ }
192
+
193
+ /** A byte count, auto-scaled. */
194
+ export function formatBytes(bytes: number, digits = 3): string {
195
+ return formatAmount(bytes, "B", digits);
196
+ }
197
+
198
+ /** A share of a whole: "41%", "3.2%", "<0.1%". */
199
+ export function formatShare(part: number, whole: number): string {
200
+ if (!(whole > 0) || !(part >= 0)) return "";
201
+ const percent = (part / whole) * 100;
202
+ if (percent === 0) return "0%";
203
+ if (percent < 0.1) return "<0.1%";
204
+ return percent < 10 ? `${percent.toFixed(1)}%` : `${Math.round(percent)}%`;
205
+ }
206
+
207
+ /** The step of a nice axis (1, 2 or 5 × 10ⁿ) splitting `span` into about `count` parts. */
208
+ export function niceStep(span: number, count = 5): number {
209
+ if (!(span > 0)) return 1;
210
+ const raw = span / count;
211
+ const power = 10 ** Math.floor(Math.log10(raw));
212
+ const fraction = raw / power;
213
+ return (fraction <= 1 ? 1 : fraction <= 2 ? 2 : fraction <= 5 ? 5 : 10) * power;
214
+ }
215
+
216
+ /** Ticks from `min` to past `max` at a nice step; the axis then runs from the first to the last tick. */
217
+ export function niceTicks(min: number, max: number, count = 5): number[] {
218
+ if (!Number.isFinite(min) || !Number.isFinite(max)) return [0, 1];
219
+ if (max <= min) max = min + (min === 0 ? 1 : Math.abs(min));
220
+ const step = niceStep(max - min, count);
221
+ const start = Math.floor(min / step + 1e-9) * step;
222
+ const ticks: number[] = [];
223
+ for (let tick = start; ticks.length < 50; tick += step) {
224
+ ticks.push(Number(tick.toPrecision(12)));
225
+ if (tick >= max - step * 1e-9) break;
226
+ }
227
+ return ticks;
228
+ }