export * as CodeModeInputBlocks from "./input-blocks.js" import { CodeMode } from "@opencode/codemode" const OPENER = /^\/\/ @input: ([A-Za-z_$][\w$]*) <<'([A-Za-z_$][\w$]*)'$/ export type Parsed = | { readonly ok: true readonly code: string /** Absent without blocks, so a program without any sees no `inputs`. */ readonly inputs: Record | undefined /** A program line equal to a block's tag: if the program fails to parse, that block may have ended there early. */ readonly collision: { readonly line: number; readonly opener: number } | undefined } | { readonly ok: false; readonly error: CodeMode.Diagnostic } /** * Removes `// @input: <<'TAG'` ... `TAG` blocks from a program and returns their text by name. A block's text runs * up to the first line equal to its tag. Blocks may stand anywhere, though the description asks for them before the * program. Each block is replaced by empty lines, so line numbers in errors still match the source. */ export function parse(source: string): Parsed { const lines = source.split("\n") const inputs = new Map() const openers: Array<{ readonly tag: string; readonly index: number }> = [] const program: Array = [] // Once program code has started, an opener without a closing line is a plain comment. let started = false for (let index = 0; index < lines.length; index++) { const match = OPENER.exec(stripCR(lines[index])) const close = match ? lines.findIndex((line, at) => at > index && stripCR(line) === match[2]) : -1 if (!match || (close === -1 && started)) { if (lines[index].trim() !== "") started = true program.push(lines[index]) continue } const name = match[1] const tag = match[2] if (inputs.has(name)) return { ok: false, error: diagnostic(index + 1, `duplicate input block name "${name}"`) } if (close === -1) return { ok: false, error: diagnostic(index + 1, `input block "${name}" has no closing ${tag} line`) } // The last body line's \r belongs to the line break before the closing tag. inputs.set(name, stripCR(lines.slice(index + 1, close).join("\n"))) openers.push({ tag, index }) program.push(...Array.from({ length: close - index + 1 }, () => "")) index = close } const collision = program .flatMap((line, index) => { const opener = openers.findLast((item) => item.tag === stripCR(line) && item.index < index) return opener ? [{ line: index + 1, opener: opener.index + 1 }] : [] }) .at(0) return { ok: true, code: program.join("\n"), inputs: inputs.size > 0 ? Object.fromEntries(inputs) : undefined, collision, } } /** A program's parse failure, with a hint when an input block may have ended early. */ export function explain(parsed: Extract, error: CodeMode.Diagnostic): CodeMode.Diagnostic { if (error.kind !== "ParseError" || !parsed.collision) return error // The hint goes after the error's text and before its ` at :line:col` line, when it has one. const at = error.message.indexOf("\n at ") const text = at === -1 ? error.message : error.message.slice(0, at) return { ...error, message: `${text}${text.endsWith(".") ? "" : "."} Line ${parsed.collision.line} equals the tag of the input block opened at line ${parsed.collision.opener}; if the block ended early before line ${parsed.collision.line}, pick a tag the text doesn't contain.${at === -1 ? "" : error.message.slice(at)}`, } } function diagnostic(line: number, message: string): CodeMode.Diagnostic { const location = { line, column: 1 } return { kind: "ParseError", message: `SyntaxError: ${message}${CodeMode.formatLocation(location)}`, location } } function stripCR(line: string) { return line.endsWith("\r") ? line.slice(0, -1) : line }