// Compiles a spec while its source is still streaming in: renders the longest // prefix that compiles, cut at an element boundary with its brackets closed. import type { Program } from "./ast"; import { compile } from "./index"; import type { Diagnostic } from "./types"; /** Cut points tried, latest first, before giving up on a prefix. */ const MAX_ATTEMPTS = 12; const CLOSER: Record = { "(": ")", "[": "]", "{": "}" }; /** `source.slice(0, end) + closers` is a candidate prefix. */ interface Cut { readonly end: number; readonly closers: string; } /** * Compiles a spec that may still be streaming. If the whole source compiles, * returns it with `complete: true`. Otherwise compiles the longest prefix that * does: cut at the last argument/element boundary (after a `,` or an opening * bracket, or at a top-level statement separator), drop trailing commas and * any unterminated string/template/comment, and append closers for every open * bracket in order. Tries cut points from the end backwards (at most 12) and * returns the first prefix that compiles, with `complete: false` and the FULL * source's diagnostics (so a finished-but-broken spec can still be reported). * `program` is null only when no prefix compiles (e.g. only state * declarations so far, or empty). */ export function compileStreaming(source: string): { program: Program | null; diagnostics: Diagnostic[]; complete: boolean; } { const full = compile(source); if (full.program !== null) return { ...full, complete: true }; const cuts = lastCuts(source); for (let k = cuts.length - 1; k >= 0; k--) { const { program } = compile(source.slice(0, cuts[k].end) + cuts[k].closers); if (program !== null) return { program, diagnostics: full.diagnostics, complete: false }; } return { program: null, diagnostics: full.diagnostics, complete: false }; } /** * The last MAX_ATTEMPTS cut points in source order. Scanning stops at the * first unterminated string, template or comment: no prefix containing one * can compile, so every cut lies before it. */ function lastCuts(source: string): Cut[] { const cuts: Cut[] = []; /** Closers for the open brackets, innermost first. */ let closers = ""; /** Code since the last cut, so blank lines add no duplicate cut. */ let pending = false; const record = (end: number) => { cuts.push({ end, closers }); if (cuts.length > MAX_ATTEMPTS) cuts.shift(); pending = false; }; for (let i = 0; i < source.length; i++) { const c = source[i]; const end = skipLiteral(source, i); if (end === -1) break; if (end > i) { // A comment ends before its newline, which is then scanned as a separator. pending ||= c !== "/"; i = end - 1; } else if (c === "(" || c === "[" || c === "{") { closers = CLOSER[c] + closers; record(i + 1); } else if (c === ")" || c === "]" || c === "}") { closers = closers.slice(1); pending = true; } else if (c === ",") { record(i); } else if (c === "\n" || c === ";") { if (closers === "" && pending) record(i); } else if (c !== " " && c !== "\t" && c !== "\r") { pending = true; } } return cuts; } /** * For a string, template or `//` comment starting at `i`: the index just past * it (a comment stops before its newline), or -1 if it is unterminated. * Anywhere else: `i`. */ function skipLiteral(source: string, i: number): number { const c = source[i]; if (c === "/" && source[i + 1] === "/") return source.indexOf("\n", i); if (c === '"' || c === "'") { for (let j = i + 1; j < source.length; j++) { const d = source[j]; if (d === c) return j + 1; if (d === "\n") return -1; if (d === "\\") j++; } return -1; } if (c === "`") { for (let j = i + 1; j < source.length; j++) { const d = source[j]; if (d === "`") return j + 1; if (d === "\\") { j++; } else if (d === "$" && source[j + 1] === "{") { j = skipInterpolation(source, j + 2); if (j === -1) return -1; j--; } } return -1; } return i; } /** From inside a template's `${`: the index just past its `}`, or -1 if it never closes. */ function skipInterpolation(source: string, i: number): number { let depth = 0; for (let j = i; j < source.length; j++) { const end = skipLiteral(source, j); if (end === -1) return -1; if (end > j) { j = end - 1; continue; } const c = source[j]; if (c === "(" || c === "[" || c === "{") depth++; else if (c === "}" && depth === 0) return j + 1; else if ((c === ")" || c === "]" || c === "}") && depth > 0) depth--; } return -1; }