Better Lua parse error reporting

This commit is contained in:
Zef Hemel
2025-03-28 12:50:35 +01:00
parent 64e537732b
commit 2787c40ebe
3 changed files with 8 additions and 37 deletions
-6
View File
@@ -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));
});
+1 -24
View File
@@ -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) {
+7 -7
View File
@@ -103,18 +103,18 @@ export async function lintLua({ tree }: LintEvent): Promise<LintDiagnostic[]> {
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);
}