Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lang-core-triple-quotes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@openuidev/lang-core": patch
---

Accept Python-style triple-quoted strings (`"""…"""` / `'''…'''`) for multi-line values such as code snippets, including while streaming. Quotes inside `//` and `#` comments no longer throw off string detection.
27 changes: 27 additions & 0 deletions packages/lang-core/src/parser/__tests__/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,30 @@ a = Title("y")`;
expect(titleText(sp.push(`")\n`))).toBe("y");
});
});

describe("triple-quoted strings", () => {
const text = (input: string) => {
const root = parse(input, schema).root as unknown as {
props: { children: { props: { text: string } }[] };
};
return root.props.children[0].props.text;
};

it("keeps newlines, quotes and # lines in the body", () => {
const src = `root = Stack([a])\na = Title(code)\ncode = """\nimport os # list\nprint("hi", 'x')\n"""`;
expect(text(src)).toBe(`import os # list\nprint("hi", 'x')`);
});

it("supports ''' delimiters", () => {
expect(text(`root = Stack([Title('''a\nb''')])`)).toBe("a\nb");
});

it("streams an unclosed body without the partial closing quotes", () => {
const sp = createStreamParser(schema);
sp.push(`root = Stack([a])\na = Title(code)\ncode = """\nline 1\n`);
const root = sp.push(`line 2""`).root as unknown as {
props: { children: { props: { text: string } }[] };
};
expect(root.props.children[0].props.text).toBe("line 1\nline 2");
});
});
4 changes: 2 additions & 2 deletions packages/lang-core/src/parser/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { ASTNode } from "./ast";
import { walkAST } from "./ast";
import { parseExpression } from "./expressions";
import { tokenize } from "./lexer";
import { stripFences } from "./parser";
import { normalizeTripleQuotes, stripFences } from "./parser";
import { split } from "./statements";

interface ParsedStatement {
Expand Down Expand Up @@ -57,7 +57,7 @@ function splitStatementSource(input: string): string[] {
}

function parseStatements(input: string): ParsedStatement[] {
const trimmed = input.trim();
const trimmed = normalizeTripleQuotes(input.trim());
if (!trimmed) return [];

const result: ParsedStatement[] = [];
Expand Down
67 changes: 65 additions & 2 deletions packages/lang-core/src/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,72 @@ function stripComments(input: string): string {
.join("\n");
}

/** Clean LLM response: strip fences, comments, whitespace. */
/**
* Rewrite Python-style triple-quoted strings ("""…""" or '''…''') as JSON
* string literals, so the rest of the pipeline only sees ordinary strings.
* LLMs reach for them for multi-line code (`code = """…"""`); without this the
* lexer reads `"""` as an empty string followed by an open quote.
*
* The body is kept raw (backslashes are literal, newlines preserved). One
* newline right after the opening quotes and a whitespace-only last line before
* the closing quotes are dropped. An unclosed body (streaming) becomes an
* unclosed string, with any trailing quotes held back since they may be the
* start of the closing delimiter.
*/
export function normalizeTripleQuotes(input: string): string {
if (!input.includes('"""') && !input.includes("'''")) return input;
let out = "";
let i = 0;
let inStr: false | '"' | "'" = false;
while (i < input.length) {
const c = input[i];
if (inStr) {
out += c;
if (c === "\\" && i + 1 < input.length) {
out += input[i + 1];
i += 2;
continue;
}
if (c === inStr) inStr = false;
i++;
continue;
}
if ((c === '"' || c === "'") && input.startsWith(c.repeat(3), i)) {
const delim = c.repeat(3);
const bodyStart = i + 3;
const close = input.indexOf(delim, bodyStart);
let body = input.slice(bodyStart, close === -1 ? input.length : close);
if (body.startsWith("\r\n")) body = body.slice(2);
else if (body.startsWith("\n")) body = body.slice(1);
if (close === -1) {
body = body.replace(new RegExp(`${c}{1,2}$`), "");
out += JSON.stringify(body).slice(0, -1);
break;
}
body = body.replace(/\r?\n[ \t]*$/, "");
out += JSON.stringify(body);
i = close + 3;
continue;
}
// Copy // and # comments through untouched, so an apostrophe in one
// can't open a string (stripComments removes them later).
if (c === "#" || (c === "/" && input[i + 1] === "/")) {
const eol = input.indexOf("\n", i);
const end = eol === -1 ? input.length : eol;
out += input.slice(i, end);
i = end;
continue;
}
if (c === '"' || c === "'") inStr = c;
out += c;
i++;
}
return out;
}

/** Clean LLM response: strip fences, triple quotes, comments, whitespace. */
function preprocess(input: string): string {
return stripComments(stripFences(input.trim())).trim();
return stripComments(normalizeTripleQuotes(stripFences(input.trim()))).trim();
}

/**
Expand Down
Loading