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.
- package/LICENSE +202 -0
- package/OVERVIEW.md +21 -0
- package/README.md +77 -2
- package/assets/fonts/Excalifont-OFL.txt +109 -0
- package/assets/fonts/Excalifont-Regular-latin.woff2 +0 -0
- package/assets/icon.png +0 -0
- package/assets/icon.svg +1 -0
- package/assets/screenshot-chat.png +0 -0
- package/assets/screenshot-gallery.png +0 -0
- package/assets/screenshot-playground.png +0 -0
- package/client/components/content.tsx +468 -0
- package/client/components/data.tsx +967 -0
- package/client/components/dev.tsx +998 -0
- package/client/components/diagram.tsx +513 -0
- package/client/components/inputs.tsx +994 -0
- package/client/components/layout.tsx +239 -0
- package/client/components/media.tsx +428 -0
- package/client/components/project.tsx +641 -0
- package/client/components/widgets.tsx +836 -0
- package/client/gallery.tsx +449 -0
- package/client/live-ui.tsx +525 -0
- package/client/loaders.ts +342 -0
- package/client/markdown.tsx +172 -0
- package/client/node-renderer.tsx +149 -0
- package/client/render.tsx +186 -0
- package/client/runtime.ts +103 -0
- package/client/settings.tsx +57 -0
- package/client/web.ts +186 -0
- package/gallery/Acceptance-baseline.ui +1 -0
- package/gallery/Acceptance-checklist.ui +14 -0
- package/gallery/Acceptance-evidence.ui +9 -0
- package/gallery/Acceptance.ui +1 -0
- package/gallery/Badge.ui +3 -0
- package/gallery/Benchmark-context.ui +8 -0
- package/gallery/Benchmark-hyperfine.ui +1 -0
- package/gallery/Benchmark-runs-list.ui +9 -0
- package/gallery/Benchmark-runs.ui +1 -0
- package/gallery/Benchmark-vitest.ui +1 -0
- package/gallery/Benchmark.ui +9 -0
- package/gallery/Board-bb-tasks.ui +1 -0
- package/gallery/Board-gh-issues.ui +1 -0
- package/gallery/Board-gh-prs.ui +1 -0
- package/gallery/Board.ui +12 -0
- package/gallery/Button-destructive.ui +4 -0
- package/gallery/Button.ui +5 -0
- package/gallery/Calendar.ui +15 -0
- package/gallery/CalendarHeatmap-short.ui +11 -0
- package/gallery/CalendarHeatmap.ui +4 -0
- package/gallery/Callout.ui +4 -0
- package/gallery/Card.ui +4 -0
- package/gallery/Carousel.ui +8 -0
- package/gallery/Changes-paths.ui +1 -0
- package/gallery/Changes-uncommitted.ui +1 -0
- package/gallery/Changes.ui +1 -0
- package/gallery/Chart-area.ui +3 -0
- package/gallery/Chart-funnel.ui +3 -0
- package/gallery/Chart-heatmap.ui +11 -0
- package/gallery/Chart-pie.ui +3 -0
- package/gallery/Chart-radar.ui +3 -0
- package/gallery/Chart-scatter.ui +4 -0
- package/gallery/Chart-stacked.ui +5 -0
- package/gallery/Chart.ui +3 -0
- package/gallery/Checklist.ui +6 -0
- package/gallery/Choice.ui +8 -0
- package/gallery/Code.ui +3 -0
- package/gallery/Commits-limit.ui +1 -0
- package/gallery/Commits.ui +1 -0
- package/gallery/Compare-conditions.ui +4 -0
- package/gallery/Compare-three.ui +7 -0
- package/gallery/Compare.ui +4 -0
- package/gallery/Coverage-go.ui +1 -0
- package/gallery/Coverage-lcov.ui +1 -0
- package/gallery/Coverage-python.ui +1 -0
- package/gallery/Coverage.ui +1 -0
- package/gallery/DateInput.ui +5 -0
- package/gallery/Decision-percent.ui +10 -0
- package/gallery/Decision.ui +12 -0
- package/gallery/Dependencies-audit.ui +1 -0
- package/gallery/Dependencies-both.ui +1 -0
- package/gallery/Dependencies-list.ui +7 -0
- package/gallery/Dependencies-pip.ui +1 -0
- package/gallery/Dependencies-pnpm.ui +1 -0
- package/gallery/Dependencies.ui +1 -0
- package/gallery/Details.ui +4 -0
- package/gallery/Diagram-flow.ui +16 -0
- package/gallery/Diagram-sequence.ui +15 -0
- package/gallery/Diagram.ui +16 -0
- package/gallery/Diff.ui +3 -0
- package/gallery/Divider.ui +5 -0
- package/gallery/EditableTable.ui +9 -0
- package/gallery/Entity-list.ui +6 -0
- package/gallery/Entity.ui +8 -0
- package/gallery/Environments.ui +1 -0
- package/gallery/Estimate-points.ui +6 -0
- package/gallery/Estimate.ui +8 -0
- package/gallery/File.ui +4 -0
- package/gallery/Findings-biome.ui +1 -0
- package/gallery/Findings-eslint.ui +1 -0
- package/gallery/Findings-oxlint.ui +1 -0
- package/gallery/Findings-review.ui +1 -0
- package/gallery/Findings-ruff.ui +1 -0
- package/gallery/Findings-sarif.ui +1 -0
- package/gallery/Findings-ship.ui +11 -0
- package/gallery/Findings-tsc.ui +1 -0
- package/gallery/Findings.ui +7 -0
- package/gallery/Flashcards.ui +3 -0
- package/gallery/FollowUps.ui +4 -0
- package/gallery/Form.ui +8 -0
- package/gallery/Gallery-links.ui +8 -0
- package/gallery/Gallery-strip.ui +14 -0
- package/gallery/Gallery.ui +10 -0
- package/gallery/Gauge.ui +7 -0
- package/gallery/Grid.ui +8 -0
- package/gallery/Hotspots-photo.ui +8 -0
- package/gallery/Hotspots.ui +11 -0
- package/gallery/Icon.ui +5 -0
- package/gallery/Image.ui +4 -0
- package/gallery/ImageSearch-aspects.ui +6 -0
- package/gallery/ImageSearch.ui +4 -0
- package/gallery/Input.ui +5 -0
- package/gallery/Json-api.ui +1 -0
- package/gallery/Json.ui +8 -0
- package/gallery/KeyValue.ui +3 -0
- package/gallery/List-dots.ui +8 -0
- package/gallery/List.ui +8 -0
- package/gallery/ListEditor.ui +5 -0
- package/gallery/Logs-jsonl.ui +1 -0
- package/gallery/Logs-lines.ui +11 -0
- package/gallery/Logs-python.ui +1 -0
- package/gallery/Logs.ui +1 -0
- package/gallery/Map-city.ui +8 -0
- package/gallery/Map.ui +8 -0
- package/gallery/Metric.ui +3 -0
- package/gallery/OptionCards-multi.ui +10 -0
- package/gallery/OptionCards.ui +10 -0
- package/gallery/Pipeline-checks.ui +1 -0
- package/gallery/Pipeline-passed.ui +1 -0
- package/gallery/Pipeline-running.ui +1 -0
- package/gallery/Pipeline-stages.ui +13 -0
- package/gallery/Pipeline.ui +1 -0
- package/gallery/Plan.ui +9 -0
- package/gallery/Progress.ui +4 -0
- package/gallery/Provenance-deploy.ui +6 -0
- package/gallery/Provenance-no-receipt.ui +1 -0
- package/gallery/Provenance.ui +1 -0
- package/gallery/PullRequest.ui +1 -0
- package/gallery/Quiz.ui +6 -0
- package/gallery/Radio.ui +5 -0
- package/gallery/Rating.ui +5 -0
- package/gallery/Roadmap-sprint.ui +8 -0
- package/gallery/Roadmap-year.ui +9 -0
- package/gallery/Roadmap.ui +12 -0
- package/gallery/Row.ui +3 -0
- package/gallery/SegmentedBar-status.ui +8 -0
- package/gallery/SegmentedBar.ui +8 -0
- package/gallery/Select.ui +5 -0
- package/gallery/Slider.ui +7 -0
- package/gallery/Snippet.ui +3 -0
- package/gallery/Sources-chips.ui +4 -0
- package/gallery/Sources.ui +8 -0
- package/gallery/Sparkline.ui +6 -0
- package/gallery/Stack.ui +7 -0
- package/gallery/StackTrace-aggregate.ui +1 -0
- package/gallery/StackTrace-browser.ui +1 -0
- package/gallery/StackTrace-go.ui +1 -0
- package/gallery/StackTrace-java.ui +1 -0
- package/gallery/StackTrace-python.ui +1 -0
- package/gallery/StackTrace-rust.ui +1 -0
- package/gallery/StackTrace.ui +1 -0
- package/gallery/Status.ui +9 -0
- package/gallery/Stepper.ui +5 -0
- package/gallery/Steps.ui +8 -0
- package/gallery/Swatches-long-names.ui +4 -0
- package/gallery/Swatches-picker.ui +5 -0
- package/gallery/Swatches.ui +10 -0
- package/gallery/Table.ui +7 -0
- package/gallery/Tabs.ui +8 -0
- package/gallery/Tests-junit.ui +1 -0
- package/gallery/Tests-vitest-junit.ui +1 -0
- package/gallery/Tests-vitest.ui +1 -0
- package/gallery/Tests.ui +9 -0
- package/gallery/Text.ui +4 -0
- package/gallery/TextArea.ui +5 -0
- package/gallery/Threads-ids.ui +1 -0
- package/gallery/Threads.ui +1 -0
- package/gallery/Timeline.ui +8 -0
- package/gallery/Timer.ui +4 -0
- package/gallery/Toggle.ui +5 -0
- package/gallery/Transcript-text.ui +1 -0
- package/gallery/Transcript-untimed.ui +1 -0
- package/gallery/Transcript-whisper.ui +1 -0
- package/gallery/Transcript.ui +1 -0
- package/gallery/Tree.ui +3 -0
- package/gallery/Treemap-du.ui +1 -0
- package/gallery/Treemap-esbuild.ui +1 -0
- package/gallery/Treemap-tree.ui +7 -0
- package/gallery/Treemap.ui +18 -0
- package/gallery/Walkthrough.ui +9 -0
- package/gallery/Waterfall-har.ui +1 -0
- package/gallery/Waterfall-trace.ui +1 -0
- package/gallery/Waterfall.ui +11 -0
- package/gallery/data/acceptance/coil/viewport-lifecycle.json +25 -0
- package/gallery/data/acceptance/coil-gate.json +153 -0
- package/gallery/data/acceptance/orbit-baseline.json +163 -0
- package/gallery/data/acceptance/shots/gate-cell-opened.png +0 -0
- package/gallery/data/acceptance/shots/gate-light.png +0 -0
- package/gallery/data/acceptance/shots/gate-phone-dark.png +0 -0
- package/gallery/data/activity.json +1 -0
- package/gallery/data/bb-tasks-orb.json +420 -0
- package/gallery/data/biome.json +229 -0
- package/gallery/data/compare/status-after.png +0 -0
- package/gallery/data/compare/status-before.png +0 -0
- package/gallery/data/compare/status-dark-2x.png +0 -0
- package/gallery/data/compare/status-rerender.png +0 -0
- package/gallery/data/coverage-py.json +1 -0
- package/gallery/data/coverage-summary.json +1 -0
- package/gallery/data/coverage.out +23 -0
- package/gallery/data/deploys.json +1 -0
- package/gallery/data/device-runs.json +285 -0
- package/gallery/data/du-node-modules.txt +359 -0
- package/gallery/data/esbuild-meta.json +292 -0
- package/gallery/data/eslint.json +266 -0
- package/gallery/data/eslint.sarif +343 -0
- package/gallery/data/findings-review.json +76 -0
- package/gallery/data/gh-issues-nextjs.json +293 -0
- package/gallery/data/gh-pr-checks.json +272 -0
- package/gallery/data/gh-prs-nextjs.json +419 -0
- package/gallery/data/gh-run-failed.json +1 -0
- package/gallery/data/gh-run-passed.json +1 -0
- package/gallery/data/gh-run-running.json +1 -0
- package/gallery/data/github-release.json +57 -0
- package/gallery/data/hyperfine-startup.json +187 -0
- package/gallery/data/lcov.info +234 -0
- package/gallery/data/logs/media-sync.log +127 -0
- package/gallery/data/logs/thumbnailer.jsonl +17 -0
- package/gallery/data/logs/vitest-run.log +89 -0
- package/gallery/data/npm-audit.json +672 -0
- package/gallery/data/npm-outdated.json +179 -0
- package/gallery/data/oxlint.json +156 -0
- package/gallery/data/pip-outdated.json +68 -0
- package/gallery/data/pnpm-outdated.json +58 -0
- package/gallery/data/provenance/devicectl-install.json +35 -0
- package/gallery/data/provenance/install-timeout.log +4 -0
- package/gallery/data/provenance/iphone-install.json +72 -0
- package/gallery/data/provenance/iphone-no-receipt.json +57 -0
- package/gallery/data/provenance/live-home.jpg +0 -0
- package/gallery/data/provenance/live-probe.txt +10 -0
- package/gallery/data/provenance/xcodebuild-2811.log +19 -0
- package/gallery/data/provenance/xcodebuild-4712.log +19 -0
- package/gallery/data/pytest-junit.xml +64 -0
- package/gallery/data/python-org-trace.json +35 -0
- package/gallery/data/python-org.har +31 -0
- package/gallery/data/retry.ts +13 -0
- package/gallery/data/ruff.json +261 -0
- package/gallery/data/src/durations.py +63 -0
- package/gallery/data/src/json-logic.ts +156 -0
- package/gallery/data/src/mapMath.ts +135 -0
- package/gallery/data/src/semver/bump.go +73 -0
- package/gallery/data/traces/browser-error.json +5 -0
- package/gallery/data/traces/go/main.go +27 -0
- package/gallery/data/traces/go-panic.txt +16 -0
- package/gallery/data/traces/java-caused-by.txt +15 -0
- package/gallery/data/traces/node-aggregate.txt +30 -0
- package/gallery/data/traces/node-cause.txt +21 -0
- package/gallery/data/traces/py/deploy.json +5 -0
- package/gallery/data/traces/py/deploy.py +17 -0
- package/gallery/data/traces/py/settings.py +19 -0
- package/gallery/data/traces/python-chain.txt +30 -0
- package/gallery/data/traces/rust/main.rs +11 -0
- package/gallery/data/traces/rust-panic.txt +16 -0
- package/gallery/data/traces/shop/cart.mjs +19 -0
- package/gallery/data/traces/shop/pricing.mjs +12 -0
- package/gallery/data/traces/shop/status.mjs +9 -0
- package/gallery/data/traces/web/app.js +11 -0
- package/gallery/data/traces/web/index.html +1 -0
- package/gallery/data/traces/web/search.js +10 -0
- package/gallery/data/transcript/call-noisy.wav +0 -0
- package/gallery/data/transcript/call.json +362 -0
- package/gallery/data/transcript/call.wav +0 -0
- package/gallery/data/transcript/mlx-whisper-tiny.en-noisy.json +1 -0
- package/gallery/data/transcript/whisper-cpp-base.en-noisy.json +658 -0
- package/gallery/data/transcript/whisper-cpp-base.en.json +658 -0
- package/gallery/data/transcript/whisper-cpp-base.en.srt +16 -0
- package/gallery/data/transcript/whisper-cpp-tiny.en-noisy.json +619 -0
- package/gallery/data/tsc.txt +4 -0
- package/gallery/data/vitest-bench.json +119 -0
- package/gallery/data/vitest-junit.xml +133 -0
- package/gallery/data/vitest-report.json +612 -0
- package/gallery/images/bell.svg +10 -0
- package/gallery/images/bike.svg +47 -0
- package/gallery/images/footer-planet.webp +0 -0
- package/gallery/images/front-light.svg +13 -0
- package/gallery/images/golden-hour.webp +0 -0
- package/gallery/images/option-flowers.webp +0 -0
- package/gallery/images/option-hilltop.webp +0 -0
- package/gallery/images/option-meadow.webp +0 -0
- package/gallery/images/option-sky.webp +0 -0
- package/gallery/images/u-lock.svg +10 -0
- package/index.client.tsx +164 -0
- package/index.server.ts +216 -0
- package/package.json +50 -4
- package/paseo-plugin.json +14 -0
- package/server/data-source.ts +407 -0
- package/server/project.ts +981 -0
- package/server/web/address.ts +57 -0
- package/server/web/cache.ts +85 -0
- package/server/web/commons.ts +126 -0
- package/server/web/geocode.ts +40 -0
- package/server/web/image.ts +35 -0
- package/server/web/lookups.ts +248 -0
- package/server/web/net.ts +210 -0
- package/shared/core/acceptance.ts +744 -0
- package/shared/core/ast.ts +76 -0
- package/shared/core/benchmark.ts +743 -0
- package/shared/core/catalog.ts +733 -0
- package/shared/core/compare.ts +225 -0
- package/shared/core/coverage.ts +456 -0
- package/shared/core/data.ts +334 -0
- package/shared/core/decisions.ts +527 -0
- package/shared/core/dependencies.ts +604 -0
- package/shared/core/diagnostics.ts +24 -0
- package/shared/core/environments.ts +212 -0
- package/shared/core/evaluate.ts +737 -0
- package/shared/core/fields.ts +66 -0
- package/shared/core/findings.ts +1172 -0
- package/shared/core/form.ts +69 -0
- package/shared/core/index.ts +44 -0
- package/shared/core/junit.ts +252 -0
- package/shared/core/lexer.ts +275 -0
- package/shared/core/logs.ts +496 -0
- package/shared/core/markdown.ts +2374 -0
- package/shared/core/parser.ts +355 -0
- package/shared/core/perf-units.ts +228 -0
- package/shared/core/pipeline.ts +679 -0
- package/shared/core/project.ts +472 -0
- package/shared/core/provenance.ts +725 -0
- package/shared/core/stack-trace.ts +718 -0
- package/shared/core/streaming.ts +136 -0
- package/shared/core/test-results.ts +562 -0
- package/shared/core/thread-activity.ts +203 -0
- package/shared/core/tracking.ts +887 -0
- package/shared/core/transcript.ts +1364 -0
- package/shared/core/treemap.ts +446 -0
- package/shared/core/types.ts +70 -0
- package/shared/core/validate.ts +574 -0
- package/shared/core/waterfall.ts +536 -0
- package/shared/diagram/flowchart.ts +205 -0
- package/shared/diagram/layout.ts +440 -0
- package/shared/diagram/markdown.ts +135 -0
- package/shared/diagram/sequence.ts +122 -0
- package/shared/diagram/zoom.ts +114 -0
- package/shared/fallback.ts +120 -0
- package/shared/genui.test.ts +122 -0
- package/shared/instructions.ts +144 -0
- package/shared/logic/calendarMath.ts +130 -0
- package/shared/logic/chartMath.ts +289 -0
- package/shared/logic/choose-logic.ts +196 -0
- package/shared/logic/compare-logic.ts +270 -0
- package/shared/logic/content-model.ts +227 -0
- package/shared/logic/environments-logic.ts +53 -0
- package/shared/logic/explain-logic.ts +315 -0
- package/shared/logic/fieldLogic.ts +138 -0
- package/shared/logic/gallery-search.ts +71 -0
- package/shared/logic/icons.ts +322 -0
- package/shared/logic/json-logic.ts +153 -0
- package/shared/logic/mapMath.ts +133 -0
- package/shared/logic/project-logic.ts +39 -0
- package/shared/logic/references-model.ts +201 -0
- package/shared/logic/sketch.ts +165 -0
- package/shared/logic/supersession.ts +77 -0
- package/shared/logic/values.ts +133 -0
- package/shared/logic/widget-logic.ts +163 -0
- package/shared/rpc.ts +252 -0
- package/shared/segment.ts +81 -0
- package/shared/settings.ts +12 -0
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// What a Form sends: one user message with a `label: value` line per field.
|
|
2
|
+
import type { ComponentName } from "./catalog";
|
|
3
|
+
import type { Binding, UiNode, Value } from "./types";
|
|
4
|
+
|
|
5
|
+
/** The inputs a Form collects, in any layout inside it. */
|
|
6
|
+
const FIELDS: Partial<Record<ComponentName, true>> = {
|
|
7
|
+
Slider: true,
|
|
8
|
+
Stepper: true,
|
|
9
|
+
Toggle: true,
|
|
10
|
+
Choice: true,
|
|
11
|
+
Radio: true,
|
|
12
|
+
Select: true,
|
|
13
|
+
Checklist: true,
|
|
14
|
+
ListEditor: true,
|
|
15
|
+
Input: true,
|
|
16
|
+
TextArea: true,
|
|
17
|
+
DateInput: true,
|
|
18
|
+
Rating: true,
|
|
19
|
+
OptionCards: true,
|
|
20
|
+
// Only a bound Swatches is a field; a palette has no value to send.
|
|
21
|
+
Swatches: true,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const NUMBER = new Intl.NumberFormat("en-US", { maximumFractionDigits: 2 });
|
|
25
|
+
|
|
26
|
+
function isNode(value: Value): value is UiNode {
|
|
27
|
+
return typeof value === "object" && value !== null && "kind" in value && value.kind === "node";
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function isBinding(value: Value | undefined): value is Binding {
|
|
31
|
+
return typeof value === "object" && value !== null && "kind" in value && value.kind === "binding";
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** A field value as message text; "" when empty. */
|
|
35
|
+
function answer(value: Value): string {
|
|
36
|
+
if (value === null) return "";
|
|
37
|
+
if (typeof value === "boolean") return value ? "Yes" : "No";
|
|
38
|
+
if (typeof value === "number") return Number.isFinite(value) ? NUMBER.format(value) : "";
|
|
39
|
+
if (typeof value === "string") return value.trim();
|
|
40
|
+
if (Array.isArray(value)) return value.map(answer).filter((item) => item !== "").join(", ");
|
|
41
|
+
return JSON.stringify(value);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** One `label: value` line per field, in document order, at any depth. */
|
|
45
|
+
function collect(value: Value, lines: string[]): void {
|
|
46
|
+
if (Array.isArray(value)) {
|
|
47
|
+
for (const item of value) collect(item, lines);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (!isNode(value)) return;
|
|
51
|
+
const binding = value.props.value;
|
|
52
|
+
if (value.type !== "Error" && value.type !== "Loading" && FIELDS[value.type] && isBinding(binding)) {
|
|
53
|
+
const label = typeof value.props.label === "string" ? value.props.label.trim() : "";
|
|
54
|
+
// An unrated Rating holds 0: no answer, not a zero.
|
|
55
|
+
const given = value.type === "Rating" && binding.value === 0 ? null : binding.value;
|
|
56
|
+
const name = label || binding.name;
|
|
57
|
+
// A label that is a question or ends in a colon already reads as a prompt.
|
|
58
|
+
lines.push(`${name}${/[?:]$/.test(name) ? "" : ":"} ${answer(given) || "—"}`);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
for (const prop of Object.values(value.props)) collect(prop, lines);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The message a Form's submit sends: `label: value` per field inside it (label, else state name; lists joined; Yes/No; empty "—"). */
|
|
65
|
+
export function formMessage(form: UiNode): string {
|
|
66
|
+
const lines: string[] = [];
|
|
67
|
+
collect(form.props.children ?? null, lines);
|
|
68
|
+
return lines.join("\n");
|
|
69
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// The UI spec language: compile once (server and client), then evaluate on
|
|
2
|
+
// every state change. Pure TypeScript, no eval, no host globals.
|
|
3
|
+
import type { Program } from "./ast";
|
|
4
|
+
import { LIMITS } from "./catalog";
|
|
5
|
+
import { DiagnosticList } from "./diagnostics";
|
|
6
|
+
import { tokenize } from "./lexer";
|
|
7
|
+
import { parse } from "./parser";
|
|
8
|
+
import type { Diagnostic } from "./types";
|
|
9
|
+
import { validate } from "./validate";
|
|
10
|
+
|
|
11
|
+
export type { Program } from "./ast";
|
|
12
|
+
export { collectLoads, dataKey, MAX_SNIPPET_LINES, parseDataFile, type DataRequest, type LoadedData } from "./data";
|
|
13
|
+
export { evaluate, initialState } from "./evaluate";
|
|
14
|
+
export { formMessage } from "./form";
|
|
15
|
+
export {
|
|
16
|
+
imageSearches,
|
|
17
|
+
specToMarkdown,
|
|
18
|
+
toMarkdown,
|
|
19
|
+
toMarkdownWithImages,
|
|
20
|
+
type SearchedImage,
|
|
21
|
+
} from "./markdown";
|
|
22
|
+
export { compileStreaming } from "./streaming";
|
|
23
|
+
|
|
24
|
+
/** Parses and validates a spec. `program` is null iff there is at least one error. */
|
|
25
|
+
export function compile(source: string): { program: Program | null; diagnostics: Diagnostic[] } {
|
|
26
|
+
const diagnostics = new DiagnosticList();
|
|
27
|
+
if (source.length > LIMITS.sourceChars) {
|
|
28
|
+
diagnostics.report(
|
|
29
|
+
{ line: 1, column: 1 },
|
|
30
|
+
`the spec has ${source.length} characters; the limit is ${LIMITS.sourceChars}`,
|
|
31
|
+
);
|
|
32
|
+
return { program: null, diagnostics: diagnostics.items };
|
|
33
|
+
}
|
|
34
|
+
const statements = parse(tokenize(source, diagnostics), diagnostics);
|
|
35
|
+
// Validating after syntax errors would mostly report their fallout.
|
|
36
|
+
const program = diagnostics.hasErrors ? null : validate(statements, diagnostics);
|
|
37
|
+
const sorted = [...diagnostics.items].sort((a, b) => a.line - b.line || a.column - b.column);
|
|
38
|
+
return { program: diagnostics.hasErrors ? null : program, diagnostics: sorted };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** One diagnostic per line: `3:5 error: message`. */
|
|
42
|
+
export function formatDiagnostics(diagnostics: readonly Diagnostic[]): string {
|
|
43
|
+
return diagnostics.map((d) => `${d.line}:${d.column} ${d.severity}: ${d.message}`).join("\n");
|
|
44
|
+
}
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
// JUnit XML, the report pytest --junitxml, gotestsum, cargo-nextest,
|
|
2
|
+
// Maven/Gradle, jest-junit and vitest's junit reporter write, read into the
|
|
3
|
+
// list `Tests` takes. The XML reader is deliberately small and strict: no
|
|
4
|
+
// DOCTYPE (so no entity declarations and nothing to expand), only the five
|
|
5
|
+
// predefined entities and character references, every tag closed in order.
|
|
6
|
+
// It reads the text in one pass with indexOf, and its output is never longer
|
|
7
|
+
// than its input, so the data loader's size limit bounds the work.
|
|
8
|
+
import { LIMITS } from "./catalog";
|
|
9
|
+
import type { Value } from "./types";
|
|
10
|
+
|
|
11
|
+
/** A reason the file is not a readable JUnit report, already phrased for the user. */
|
|
12
|
+
export class JUnitError extends Error {}
|
|
13
|
+
|
|
14
|
+
interface XmlElement {
|
|
15
|
+
readonly name: string;
|
|
16
|
+
readonly attributes: ReadonlyMap<string, string>;
|
|
17
|
+
readonly children: XmlElement[];
|
|
18
|
+
/** Character data directly inside the element (CDATA included), concatenated. */
|
|
19
|
+
text: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Elements nested deeper than this are refused; real reports nest a handful. */
|
|
23
|
+
const MAX_DEPTH = 64;
|
|
24
|
+
|
|
25
|
+
const NAME = /[A-Za-z_:][-A-Za-z0-9_:.]*/y;
|
|
26
|
+
const ATTRIBUTE = /\s+([A-Za-z_:][-A-Za-z0-9_:.]*)\s*=\s*("([^"<]*)"|'([^'<]*)')/y;
|
|
27
|
+
const TAG_END = /\s*(\/?)>/y;
|
|
28
|
+
const PREDEFINED: Readonly<Record<string, string>> = { lt: "<", gt: ">", amp: "&", quot: '"', apos: "'" };
|
|
29
|
+
|
|
30
|
+
/** 1-based line of `index` in `text`, for messages. */
|
|
31
|
+
function lineAt(text: string, index: number): number {
|
|
32
|
+
let line = 1;
|
|
33
|
+
for (let at = text.indexOf("\n"); at !== -1 && at < index; at = text.indexOf("\n", at + 1)) line++;
|
|
34
|
+
return line;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Character and predefined entity references resolved; anything else is an error. */
|
|
38
|
+
function decode(raw: string, source: string, start: number): string {
|
|
39
|
+
if (!raw.includes("&")) return raw;
|
|
40
|
+
return raw.replace(/&([^;&\s]*);?/g, (match, body: string, offset: number) => {
|
|
41
|
+
const fail = (message: string): never => {
|
|
42
|
+
throw new JUnitError(`line ${lineAt(source, start + offset)}: ${message}`);
|
|
43
|
+
};
|
|
44
|
+
if (!match.endsWith(";")) fail('a "&" that starts no entity (write &)');
|
|
45
|
+
if (Object.hasOwn(PREDEFINED, body)) return PREDEFINED[body];
|
|
46
|
+
const numeric = /^#(?:x([0-9A-Fa-f]{1,6})|([0-9]{1,7}))$/.exec(body);
|
|
47
|
+
if (numeric === null) return fail(`the entity &${body}; is not one XML predefines`);
|
|
48
|
+
const code = numeric[1] !== undefined ? parseInt(numeric[1], 16) : Number(numeric[2]);
|
|
49
|
+
if (code === 0 || code > 0x10ffff || (code >= 0xd800 && code <= 0xdfff)) fail(`&${body}; is not a character`);
|
|
50
|
+
return String.fromCodePoint(code);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Parses an XML document into its root element. Throws JUnitError with a line number. */
|
|
55
|
+
export function parseXml(source: string): XmlElement {
|
|
56
|
+
const text = source.startsWith("\uFEFF") ? source.slice(1) : source;
|
|
57
|
+
const stack: XmlElement[] = [];
|
|
58
|
+
let root: XmlElement | null = null;
|
|
59
|
+
let index = 0;
|
|
60
|
+
const fail = (message: string, at = index): never => {
|
|
61
|
+
throw new JUnitError(`line ${lineAt(text, at)}: ${message}`);
|
|
62
|
+
};
|
|
63
|
+
/** Skips past `terminator`, which must exist after `index`. */
|
|
64
|
+
const skipPast = (terminator: string, what: string) => {
|
|
65
|
+
const end = text.indexOf(terminator, index);
|
|
66
|
+
if (end === -1) fail(`${what} never closes`);
|
|
67
|
+
index = end + terminator.length;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
while (index < text.length) {
|
|
71
|
+
const open = text.indexOf("<", index);
|
|
72
|
+
const chunkEnd = open === -1 ? text.length : open;
|
|
73
|
+
if (chunkEnd > index) {
|
|
74
|
+
const chunk = text.slice(index, chunkEnd);
|
|
75
|
+
const current = stack[stack.length - 1];
|
|
76
|
+
if (current === undefined) {
|
|
77
|
+
if (chunk.trim() !== "") fail(root === null ? "text before the root element" : "text after the root element");
|
|
78
|
+
} else {
|
|
79
|
+
current.text += decode(chunk, text, index);
|
|
80
|
+
}
|
|
81
|
+
index = chunkEnd;
|
|
82
|
+
if (open === -1) break;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (text.startsWith("<!--", index)) {
|
|
86
|
+
skipPast("-->", "a comment");
|
|
87
|
+
} else if (text.startsWith("<![CDATA[", index)) {
|
|
88
|
+
const current = stack[stack.length - 1];
|
|
89
|
+
if (current === undefined) fail("CDATA outside an element");
|
|
90
|
+
const start = index + 9;
|
|
91
|
+
const end = text.indexOf("]]>", start);
|
|
92
|
+
if (end === -1) fail("a CDATA section never closes");
|
|
93
|
+
current!.text += text.slice(start, end);
|
|
94
|
+
index = end + 3;
|
|
95
|
+
} else if (text.startsWith("<!", index)) {
|
|
96
|
+
// DOCTYPE and other declarations: refusing them refuses entity declarations, so nothing can expand.
|
|
97
|
+
fail("DOCTYPE and other declarations are not read; a JUnit report needs none");
|
|
98
|
+
} else if (text.startsWith("<?", index)) {
|
|
99
|
+
skipPast("?>", "a processing instruction");
|
|
100
|
+
} else if (text.startsWith("</", index)) {
|
|
101
|
+
NAME.lastIndex = index + 2;
|
|
102
|
+
const name = NAME.exec(text)?.[0];
|
|
103
|
+
if (name === undefined) fail("a closing tag without a name");
|
|
104
|
+
const current = stack.pop();
|
|
105
|
+
if (current === undefined) fail(`</${name}> closes nothing`);
|
|
106
|
+
if (current!.name !== name) fail(`</${name}> closes <${current!.name}>`);
|
|
107
|
+
const close = /\s*>/y;
|
|
108
|
+
close.lastIndex = index + 2 + name!.length;
|
|
109
|
+
if (close.exec(text) === null) fail(`</${name}> is malformed`);
|
|
110
|
+
index = close.lastIndex;
|
|
111
|
+
} else {
|
|
112
|
+
NAME.lastIndex = index + 1;
|
|
113
|
+
const name = NAME.exec(text)?.[0];
|
|
114
|
+
if (name === undefined) fail('a "<" that starts no tag (write <)');
|
|
115
|
+
let at = index + 1 + name!.length;
|
|
116
|
+
const attributes = new Map<string, string>();
|
|
117
|
+
for (;;) {
|
|
118
|
+
ATTRIBUTE.lastIndex = at;
|
|
119
|
+
const attribute = ATTRIBUTE.exec(text);
|
|
120
|
+
if (attribute === null) break;
|
|
121
|
+
const [, key, , doubleQuoted, singleQuoted] = attribute;
|
|
122
|
+
if (attributes.has(key)) fail(`<${name}> repeats the attribute ${key}`, at);
|
|
123
|
+
const raw = doubleQuoted ?? singleQuoted;
|
|
124
|
+
attributes.set(key, decode(raw, text, at + attribute[0].indexOf(raw)));
|
|
125
|
+
at = ATTRIBUTE.lastIndex;
|
|
126
|
+
}
|
|
127
|
+
TAG_END.lastIndex = at;
|
|
128
|
+
const end = TAG_END.exec(text);
|
|
129
|
+
if (end === null) fail(`<${name}> is malformed`, at);
|
|
130
|
+
if (stack.length === 0 && root !== null) fail("a second root element");
|
|
131
|
+
if (stack.length >= MAX_DEPTH) fail(`elements are nested more than ${MAX_DEPTH} deep`);
|
|
132
|
+
const element: XmlElement = { name: name!, attributes, children: [], text: "" };
|
|
133
|
+
if (stack.length === 0) root = element;
|
|
134
|
+
else stack[stack.length - 1].children.push(element);
|
|
135
|
+
if (end![1] !== "/") stack.push(element);
|
|
136
|
+
index = TAG_END.lastIndex;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (stack.length > 0) fail(`<${stack[stack.length - 1].name}> never closes`, text.length);
|
|
140
|
+
if (root === null) throw new JUnitError("the file has no XML element");
|
|
141
|
+
return root;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** One test as `Tests` takes it; optional fields are absent rather than null. */
|
|
145
|
+
type TestRecord = Record<string, Value>;
|
|
146
|
+
|
|
147
|
+
interface SuiteContext {
|
|
148
|
+
readonly name: string | null;
|
|
149
|
+
readonly file: string | null;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function attribute(element: XmlElement, name: string): string | null {
|
|
153
|
+
const value = element.attributes.get(name)?.trim();
|
|
154
|
+
return value === undefined || value === "" ? null : value;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** JUnit `time` is seconds; tests take milliseconds, rounded to a hundredth. */
|
|
158
|
+
function milliseconds(element: XmlElement): number | null {
|
|
159
|
+
const time = attribute(element, "time");
|
|
160
|
+
if (time === null) return null;
|
|
161
|
+
const seconds = Number(time.replace(/,/g, ""));
|
|
162
|
+
return Number.isFinite(seconds) && seconds >= 0 ? Math.round(seconds * 100_000) / 100 : null;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** A failure or error: its message attribute and its body (usually the trace), without repeating the message. */
|
|
166
|
+
function problemText(element: XmlElement): string | null {
|
|
167
|
+
const message = attribute(element, "message");
|
|
168
|
+
const type = attribute(element, "type");
|
|
169
|
+
const body = element.text.replace(/^\s*\n|\s+$/g, "");
|
|
170
|
+
if (body.trim() === "") return message ?? type;
|
|
171
|
+
if (message === null || body.includes(message)) return body;
|
|
172
|
+
return `${message}\n\n${body}`;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function testRecord(testcase: XmlElement, suite: SuiteContext): TestRecord {
|
|
176
|
+
const name = attribute(testcase, "name") ?? attribute(testcase, "classname") ?? "(unnamed test)";
|
|
177
|
+
const problem = testcase.children.find((child) => child.name === "failure" || child.name === "error");
|
|
178
|
+
const skipped = testcase.children.find((child) => child.name === "skipped");
|
|
179
|
+
const status = problem !== undefined ? (problem.name === "error" ? "error" : "failed") : skipped !== undefined ? "skipped" : "passed";
|
|
180
|
+
const record: TestRecord = { name, status };
|
|
181
|
+
const suiteName = attribute(testcase, "classname") ?? suite.name;
|
|
182
|
+
if (suiteName !== null) record.suite = suiteName;
|
|
183
|
+
const file = attribute(testcase, "file") ?? suite.file;
|
|
184
|
+
if (file !== null) record.file = file;
|
|
185
|
+
const line = Number(attribute(testcase, "line"));
|
|
186
|
+
if (Number.isSafeInteger(line) && line > 0) record.line = line;
|
|
187
|
+
const ms = milliseconds(testcase);
|
|
188
|
+
if (ms !== null) record.ms = ms;
|
|
189
|
+
const message = problem !== undefined ? problemText(problem) : skipped !== undefined ? problemText(skipped) : null;
|
|
190
|
+
if (message !== null) record.message = message;
|
|
191
|
+
// Captured output matters next to a failure; for passing tests it is noise the value would carry for nothing.
|
|
192
|
+
if (problem !== undefined) {
|
|
193
|
+
const output = testcase.children
|
|
194
|
+
.filter((child) => child.name === "system-out" || child.name === "system-err")
|
|
195
|
+
.map((child) => child.text.trim())
|
|
196
|
+
.filter((text) => text !== "")
|
|
197
|
+
.join("\n");
|
|
198
|
+
if (output !== "") record.output = output;
|
|
199
|
+
}
|
|
200
|
+
return record;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* The tests of a JUnit report (`<testsuites>` or a lone `<testsuite>`), in
|
|
205
|
+
* document order. A report with more tests than one list may hold comes back
|
|
206
|
+
* as suites instead, `[{suite, tests: [...]}]`, each within the list limit,
|
|
207
|
+
* which `Tests` reads the same way.
|
|
208
|
+
*/
|
|
209
|
+
export function readJUnit(source: string): Value {
|
|
210
|
+
const root = parseXml(source);
|
|
211
|
+
if (root.name !== "testsuites" && root.name !== "testsuite") {
|
|
212
|
+
throw new JUnitError(`only JUnit test reports are read from .xml files; this one's root is <${root.name}>, not <testsuites> or <testsuite>`);
|
|
213
|
+
}
|
|
214
|
+
const tests: { readonly suite: string; readonly record: TestRecord }[] = [];
|
|
215
|
+
const pending: { readonly element: XmlElement; readonly context: SuiteContext }[] = [
|
|
216
|
+
{ element: root, context: { name: null, file: null } },
|
|
217
|
+
];
|
|
218
|
+
// Depth-first in document order, without recursion.
|
|
219
|
+
while (pending.length > 0) {
|
|
220
|
+
const { element, context } = pending.pop()!;
|
|
221
|
+
const inner: SuiteContext =
|
|
222
|
+
element.name === "testsuite"
|
|
223
|
+
? { name: attribute(element, "name") ?? context.name, file: attribute(element, "file") ?? context.file }
|
|
224
|
+
: context;
|
|
225
|
+
for (let at = element.children.length - 1; at >= 0; at--) {
|
|
226
|
+
const child = element.children[at];
|
|
227
|
+
if (child.name === "testsuite") pending.push({ element: child, context: inner });
|
|
228
|
+
else if (child.name === "testcase") pending.push({ element: child, context: inner });
|
|
229
|
+
}
|
|
230
|
+
if (element.name === "testcase") {
|
|
231
|
+
const record = testRecord(element, context);
|
|
232
|
+
tests.push({ suite: typeof record.suite === "string" ? record.suite : (context.name ?? "Tests"), record });
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
if (tests.length <= LIMITS.listLength) return tests.map(({ record }) => record);
|
|
236
|
+
|
|
237
|
+
const groups: { suite: string; tests: TestRecord[] }[] = [];
|
|
238
|
+
const open = new Map<string, { suite: string; tests: TestRecord[] }>();
|
|
239
|
+
for (const { suite, record } of tests) {
|
|
240
|
+
let group = open.get(suite);
|
|
241
|
+
if (group === undefined || group.tests.length >= LIMITS.listLength) {
|
|
242
|
+
group = { suite, tests: [] };
|
|
243
|
+
open.set(suite, group);
|
|
244
|
+
groups.push(group);
|
|
245
|
+
}
|
|
246
|
+
group.tests.push(record);
|
|
247
|
+
}
|
|
248
|
+
if (groups.length > LIMITS.listLength) {
|
|
249
|
+
throw new JUnitError(`the report has ${tests.length} tests in ${groups.length} suites; the limit is ${LIMITS.listLength} suites`);
|
|
250
|
+
}
|
|
251
|
+
return groups;
|
|
252
|
+
}
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import type { DiagnosticList, Pos } from "./diagnostics";
|
|
2
|
+
|
|
3
|
+
export type Token =
|
|
4
|
+
| (Pos & { readonly t: "num"; readonly value: number })
|
|
5
|
+
| (Pos & { readonly t: "str"; readonly value: string })
|
|
6
|
+
/** `quasis` has one more entry than `parts`; each part ends with an `eof` token at its `}`. */
|
|
7
|
+
| (Pos & {
|
|
8
|
+
readonly t: "tpl";
|
|
9
|
+
readonly quasis: readonly string[];
|
|
10
|
+
readonly parts: readonly (readonly Token[])[];
|
|
11
|
+
})
|
|
12
|
+
/** `state` holds the name without `$`. */
|
|
13
|
+
| (Pos & { readonly t: "ident" | "state" | "punct"; readonly value: string })
|
|
14
|
+
/** A statement separator: a newline at bracket depth 0, or `;`. */
|
|
15
|
+
| (Pos & { readonly t: "sep"; readonly value: "\n" | ";" })
|
|
16
|
+
| (Pos & { readonly t: "eof" });
|
|
17
|
+
|
|
18
|
+
const PUNCTUATION = [
|
|
19
|
+
"===", "!==",
|
|
20
|
+
"==", "!=", "<=", ">=", "&&", "||", "??", "=>", "**",
|
|
21
|
+
"(", ")", "[", "]", "{", "}", ",", ":", ".", "?", "<", ">", "+", "-", "*", "/", "%", "!", "=",
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
/** A newline right after one of these continues the statement on the next line. */
|
|
25
|
+
const CONTINUES_AFTER: Record<string, true> = {
|
|
26
|
+
"+": true, "-": true, "*": true, "/": true, "%": true, "**": true,
|
|
27
|
+
"==": true, "!=": true, "===": true, "!==": true,
|
|
28
|
+
"<": true, "<=": true, ">": true, ">=": true,
|
|
29
|
+
"&&": true, "||": true, "??": true, "?": true, ":": true,
|
|
30
|
+
".": true, "=": true, "=>": true,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/** A line starting with one of these continues the previous line (none can start an expression). */
|
|
34
|
+
const CONTINUES_BEFORE: Record<string, true> = {
|
|
35
|
+
"+": true, "*": true, "/": true, "%": true, "**": true,
|
|
36
|
+
"==": true, "!=": true, "===": true, "!==": true,
|
|
37
|
+
"<": true, "<=": true, ">": true, ">=": true,
|
|
38
|
+
"&&": true, "||": true, "??": true, "?": true, ":": true,
|
|
39
|
+
".": true, "=>": true,
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const NUMBER = /\d(?:_?\d)*(?:\.\d(?:_?\d)*)?(?:[eE][+-]?\d+)?/y;
|
|
43
|
+
const IDENT = /[A-Za-z_][A-Za-z0-9_]*/y;
|
|
44
|
+
const IDENT_CHAR = /[A-Za-z0-9_]/;
|
|
45
|
+
const HEX = /^[0-9a-fA-F]+$/;
|
|
46
|
+
|
|
47
|
+
const ESCAPES: Record<string, string> = {
|
|
48
|
+
n: "\n", t: "\t", r: "\r", "\\": "\\", '"': '"', "'": "'", "`": "`", $: "$",
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export function tokenize(source: string, diagnostics: DiagnosticList): Token[] {
|
|
52
|
+
return new Lexer(source, diagnostics).run(false);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
class Lexer {
|
|
56
|
+
private i = 0;
|
|
57
|
+
private line = 1;
|
|
58
|
+
private column = 1;
|
|
59
|
+
|
|
60
|
+
constructor(
|
|
61
|
+
private readonly src: string,
|
|
62
|
+
private readonly diagnostics: DiagnosticList,
|
|
63
|
+
) {}
|
|
64
|
+
|
|
65
|
+
private advance(count = 1): void {
|
|
66
|
+
for (let n = 0; n < count; n++) {
|
|
67
|
+
if (this.src[this.i] === "\n") {
|
|
68
|
+
this.line++;
|
|
69
|
+
this.column = 1;
|
|
70
|
+
} else {
|
|
71
|
+
this.column++;
|
|
72
|
+
}
|
|
73
|
+
this.i++;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
private match(re: RegExp): string | null {
|
|
78
|
+
re.lastIndex = this.i;
|
|
79
|
+
const m = re.exec(this.src);
|
|
80
|
+
return m ? m[0] : null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Top level: tokens until end of input, separators at bracket depth 0.
|
|
85
|
+
* Inside a template `${`: tokens until the matching `}`, no separators.
|
|
86
|
+
*/
|
|
87
|
+
run(inTemplate: boolean): Token[] {
|
|
88
|
+
const out: Token[] = [];
|
|
89
|
+
let depth = 0;
|
|
90
|
+
for (;;) {
|
|
91
|
+
this.skipBlank();
|
|
92
|
+
const pos = { line: this.line, column: this.column };
|
|
93
|
+
const c = this.src[this.i];
|
|
94
|
+
if (c === undefined) {
|
|
95
|
+
if (inTemplate) this.diagnostics.report(pos, "unterminated template: `${` is missing its `}`");
|
|
96
|
+
out.push({ ...pos, t: "eof" });
|
|
97
|
+
return inTemplate ? out : joinContinuations(out);
|
|
98
|
+
}
|
|
99
|
+
if (c === "\n") {
|
|
100
|
+
this.advance();
|
|
101
|
+
if (!inTemplate && depth === 0) out.push({ ...pos, t: "sep", value: "\n" });
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (c === ";") {
|
|
105
|
+
this.advance();
|
|
106
|
+
out.push({ ...pos, t: "sep", value: ";" });
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (inTemplate && c === "}" && depth === 0) {
|
|
110
|
+
out.push({ ...pos, t: "eof" });
|
|
111
|
+
this.advance();
|
|
112
|
+
return out;
|
|
113
|
+
}
|
|
114
|
+
const number = this.match(NUMBER);
|
|
115
|
+
if (number !== null) {
|
|
116
|
+
this.advance(number.length);
|
|
117
|
+
if (IDENT_CHAR.test(this.src[this.i] ?? "")) {
|
|
118
|
+
this.diagnostics.report(pos, `invalid number \`${number}${this.src[this.i]}\``);
|
|
119
|
+
}
|
|
120
|
+
out.push({ ...pos, t: "num", value: Number(number.replaceAll("_", "")) });
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
const ident = this.match(IDENT);
|
|
124
|
+
if (ident !== null) {
|
|
125
|
+
this.advance(ident.length);
|
|
126
|
+
out.push({ ...pos, t: "ident", value: ident });
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
if (c === "$") {
|
|
130
|
+
this.advance();
|
|
131
|
+
while (this.src[this.i] === " " || this.src[this.i] === "\t") this.advance();
|
|
132
|
+
const name = this.match(IDENT);
|
|
133
|
+
if (name === null) {
|
|
134
|
+
this.diagnostics.report(pos, "expected a state name after `$`, as in `$count`");
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
this.advance(name.length);
|
|
138
|
+
out.push({ ...pos, t: "state", value: name });
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
if (c === '"' || c === "'") {
|
|
142
|
+
const value = this.string(c, pos);
|
|
143
|
+
if (value !== null) out.push({ ...pos, t: "str", value });
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
if (c === "`") {
|
|
147
|
+
out.push(this.template(pos));
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
const punct = PUNCTUATION.find((p) => this.src.startsWith(p, this.i));
|
|
151
|
+
if (punct === undefined) {
|
|
152
|
+
this.diagnostics.report(pos, `unexpected character \`${c}\``);
|
|
153
|
+
this.advance();
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
if (punct === "(" || punct === "[" || punct === "{") depth++;
|
|
157
|
+
if ((punct === ")" || punct === "]" || punct === "}") && depth > 0) depth--;
|
|
158
|
+
this.advance(punct.length);
|
|
159
|
+
out.push({ ...pos, t: "punct", value: punct });
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
private skipBlank(): void {
|
|
164
|
+
for (;;) {
|
|
165
|
+
const c = this.src[this.i];
|
|
166
|
+
if (c === " " || c === "\t" || c === "\r") {
|
|
167
|
+
this.advance();
|
|
168
|
+
} else if (c === "/" && this.src[this.i + 1] === "/") {
|
|
169
|
+
while (this.i < this.src.length && this.src[this.i] !== "\n") this.advance();
|
|
170
|
+
} else {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Reads one escape after `\`; returns the decoded text or null after reporting. */
|
|
177
|
+
private escape(): string | null {
|
|
178
|
+
const pos = { line: this.line, column: this.column };
|
|
179
|
+
this.advance();
|
|
180
|
+
const c = this.src[this.i];
|
|
181
|
+
if (c !== undefined && ESCAPES[c] !== undefined) {
|
|
182
|
+
this.advance();
|
|
183
|
+
return ESCAPES[c];
|
|
184
|
+
}
|
|
185
|
+
if (c === "u") {
|
|
186
|
+
const braced = this.src[this.i + 1] === "{";
|
|
187
|
+
const end = braced ? this.src.indexOf("}", this.i + 2) : this.i + 5;
|
|
188
|
+
const hex = braced ? this.src.slice(this.i + 2, end) : this.src.slice(this.i + 1, end);
|
|
189
|
+
const code = parseInt(hex, 16);
|
|
190
|
+
if (end > 0 && HEX.test(hex) && (braced || hex.length === 4) && code <= 0x10ffff) {
|
|
191
|
+
this.advance(end - this.i + (braced ? 1 : 0));
|
|
192
|
+
return String.fromCodePoint(code);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
this.diagnostics.report(pos, `unknown escape \`\\${c ?? ""}\`; use \\n \\t \\\\ \\" \\' \\$ or \\u{hex}`);
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
private string(quote: string, pos: Pos): string | null {
|
|
200
|
+
this.advance();
|
|
201
|
+
let value = "";
|
|
202
|
+
for (;;) {
|
|
203
|
+
const c = this.src[this.i];
|
|
204
|
+
if (c === undefined || c === "\n") {
|
|
205
|
+
this.diagnostics.report(pos, `unterminated string: close it with ${quote} on the same line`);
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
208
|
+
if (c === quote) {
|
|
209
|
+
this.advance();
|
|
210
|
+
return value;
|
|
211
|
+
}
|
|
212
|
+
if (c === "\\") {
|
|
213
|
+
value += this.escape() ?? "";
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
value += c;
|
|
217
|
+
this.advance();
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
private template(pos: Pos): Token {
|
|
222
|
+
this.advance();
|
|
223
|
+
const quasis: string[] = [];
|
|
224
|
+
const parts: Token[][] = [];
|
|
225
|
+
let text = "";
|
|
226
|
+
for (;;) {
|
|
227
|
+
const c = this.src[this.i];
|
|
228
|
+
if (c === undefined) {
|
|
229
|
+
this.diagnostics.report(pos, "unterminated template: close it with `");
|
|
230
|
+
quasis.push(text);
|
|
231
|
+
return { ...pos, t: "tpl", quasis, parts };
|
|
232
|
+
}
|
|
233
|
+
if (c === "`") {
|
|
234
|
+
this.advance();
|
|
235
|
+
quasis.push(text);
|
|
236
|
+
return { ...pos, t: "tpl", quasis, parts };
|
|
237
|
+
}
|
|
238
|
+
if (c === "\\") {
|
|
239
|
+
text += this.escape() ?? "";
|
|
240
|
+
} else if (c === "$" && this.src[this.i + 1] === "{") {
|
|
241
|
+
this.advance(2);
|
|
242
|
+
quasis.push(text);
|
|
243
|
+
text = "";
|
|
244
|
+
parts.push(this.run(true));
|
|
245
|
+
} else {
|
|
246
|
+
text += c;
|
|
247
|
+
this.advance();
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Drops redundant separators, and newlines that sit between an operator and
|
|
255
|
+
* its operand, so `a +⏎ b` and `cond⏎ ? x⏎ : y` stay one statement.
|
|
256
|
+
*/
|
|
257
|
+
function joinContinuations(tokens: readonly Token[]): Token[] {
|
|
258
|
+
const out: Token[] = [];
|
|
259
|
+
for (const token of tokens) {
|
|
260
|
+
const last = out[out.length - 1];
|
|
261
|
+
if (token.t === "sep") {
|
|
262
|
+
if (last === undefined || last.t === "sep") continue;
|
|
263
|
+
if (token.value === "\n" && last.t === "punct" && CONTINUES_AFTER[last.value]) continue;
|
|
264
|
+
} else if (
|
|
265
|
+
token.t === "punct" &&
|
|
266
|
+
CONTINUES_BEFORE[token.value] &&
|
|
267
|
+
last?.t === "sep" &&
|
|
268
|
+
last.value === "\n"
|
|
269
|
+
) {
|
|
270
|
+
out.pop();
|
|
271
|
+
}
|
|
272
|
+
out.push(token);
|
|
273
|
+
}
|
|
274
|
+
return out;
|
|
275
|
+
}
|