From 2787c40ebefbecf92e8e57ff404db26b0f1a1e6d Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Fri, 28 Mar 2025 12:50:35 +0100 Subject: [PATCH] Better Lua parse error reporting --- plug-api/lib/tree.test.ts | 6 ------ plug-api/lib/tree.ts | 25 +------------------------ plugs/index/lint.ts | 14 +++++++------- 3 files changed, 8 insertions(+), 37 deletions(-) diff --git a/plug-api/lib/tree.test.ts b/plug-api/lib/tree.test.ts index f5b967e4..32949c92 100644 --- a/plug-api/lib/tree.test.ts +++ b/plug-api/lib/tree.test.ts @@ -4,7 +4,6 @@ import { collectNodesMatching, findParentMatching, nodeAtPos, - parseTreeToAST, removeParentPointers, renderToText, replaceNodesMatching, @@ -69,8 +68,3 @@ Deno.test("Test parsing", () => { parse(extendedMarkdownLanguage, mdTest3); // console.log(JSON.stringify(mdTree3, null, 2)); }); - -Deno.test("AST functions", () => { - const mdTree = parse(extendedMarkdownLanguage, mdTest1); - console.log(JSON.stringify(parseTreeToAST(mdTree), null, 2)); -}); diff --git a/plug-api/lib/tree.ts b/plug-api/lib/tree.ts index c4b65ce3..a731face 100644 --- a/plug-api/lib/tree.ts +++ b/plug-api/lib/tree.ts @@ -214,33 +214,10 @@ export function cloneTree(tree: ParseTree): ParseTree { return newTree; } -export function parseTreeToAST(tree: ParseTree, omitTrimmable = true): AST { - if (tree.type === "⚠") { - // console.info("Parse error", JSON.stringify(tree, null, 2)); - throw new Error( - `Parse error in: ${renderToText(tree)}`, - ); - } - if (tree.text !== undefined) { - return tree.text; - } - const ast: AST = [tree.type!]; - for (const node of tree.children!) { - if (node.type && !node.type.endsWith("Mark") && node.type !== "Comment") { - ast.push(parseTreeToAST(node, omitTrimmable)); - } - if (node.text && (omitTrimmable && node.text.trim() || !omitTrimmable)) { - ast.push(node.text); - } - } - return ast; -} - export function cleanTree(tree: ParseTree, omitTrimmable = true): ParseTree { if (tree.type === "⚠") { - // console.info("Parse error", JSON.stringify(tree, null, 2)); throw new Error( - `Parse error in: ${renderToText(tree)}`, + `Parse error at pos ${tree.from}`, ); } if (tree.text !== undefined) { diff --git a/plugs/index/lint.ts b/plugs/index/lint.ts index ef3c4c95..57ed01a3 100644 --- a/plugs/index/lint.ts +++ b/plugs/index/lint.ts @@ -103,18 +103,18 @@ export async function lintLua({ tree }: LintEvent): Promise { const offset = codeText.from!; let from = codeText.from!; let to = codeText.to!; - if (e.message.includes("Parse error (")) { - const errorMatch = errorRegex.exec(e.message); - if (errorMatch) { - from = offset + parseInt(errorMatch[1], 10); - to = offset + parseInt(errorMatch[2], 10); - } + let message = e.message; + if (message.includes("Parse error")) { + const pos = +message.slice("Parse error at pos ".length); + from = offset + pos; + to = offset + pos; + message = "Parse error"; } diagnostics.push({ from, to, severity: "error", - message: e.message, + message, }); console.log("Lua error", e); }