From a4f738815f499c7f72018f730419d7e248c40901 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Sun, 31 May 2026 13:50:31 +0200 Subject: [PATCH] Lua: add AST pretty-printer and align parse/print naming --- client/boot_config.ts | 4 +- client/client.ts | 2 +- client/plugos/syscalls/lua.ts | 27 +- client/space_lua.ts | 4 +- client/space_lua/break_async.test.ts | 4 +- client/space_lua/close_attribute.test.ts | 24 +- client/space_lua/const_attribute.test.ts | 10 +- client/space_lua/context_errors.test.ts | 4 +- client/space_lua/eval.test.ts | 12 +- client/space_lua/lua.test.ts | 4 +- client/space_lua/parse.test.ts | 134 +++--- client/space_lua/parse.ts | 24 +- client/space_lua/pretty_print.test.ts | 231 +++++++++++ client/space_lua/pretty_print.ts | 391 ++++++++++++++++++ client/space_lua/rp.bench.ts | 2 +- client/space_lua/stdlib.ts | 4 +- client/space_lua/stdlib/load.ts | 4 +- client/space_lua/stdlib/pretty_print_test.lua | 25 ++ client/space_lua/stdlib/space_lua.ts | 64 ++- plug-api/syscalls/lua.ts | 22 +- plugs/index/lint.ts | 2 +- website/API/lua.md | 29 +- website/API/spacelua.md | 43 ++ website/CHANGELOG.md | 1 + 24 files changed, 943 insertions(+), 128 deletions(-) create mode 100644 client/space_lua/pretty_print.test.ts create mode 100644 client/space_lua/pretty_print.ts create mode 100644 client/space_lua/stdlib/pretty_print_test.lua diff --git a/client/boot_config.ts b/client/boot_config.ts index e8d15876..f8b6844e 100644 --- a/client/boot_config.ts +++ b/client/boot_config.ts @@ -8,7 +8,7 @@ import { configSyscalls } from "./plugos/syscalls/config.ts"; import { Config } from "./config.ts"; import { luaBuildStandardEnv } from "./space_lua/stdlib.ts"; import { exposeSyscalls } from "./space_lua_api.ts"; -import { parse } from "./space_lua/parse.ts"; +import { parseBlock } from "./space_lua/parse.ts"; import { LuaEnv, LuaStackFrame } from "./space_lua/runtime.ts"; import { evalStatement } from "./space_lua/eval.ts"; import { editorSyscalls } from "./plugos/syscalls/editor.ts"; @@ -70,7 +70,7 @@ export async function loadConfig( exposeSyscalls(rootEnv, bootSystem); // Parse the code - const chunk = parse(luaCode, {}); + const chunk = parseBlock(luaCode, {}); const sf = LuaStackFrame.createWithGlobalEnv(rootEnv, chunk.ctx); // And eval diff --git a/client/client.ts b/client/client.ts index 10be6541..597cbcfe 100644 --- a/client/client.ts +++ b/client/client.ts @@ -53,7 +53,7 @@ import { PathPageNavigator, parseRefFromURI } from "./navigator.ts"; import { EventHook } from "./plugos/hooks/event.ts"; import { Space } from "./space.ts"; import { evalStatement } from "./space_lua/eval.ts"; -import { parseExpressionString, parse as parseLua } from "./space_lua/parse.ts"; +import { parseExpressionString, parseBlock as parseLua } from "./space_lua/parse.ts"; import type { LuaCollectionQuery } from "./space_lua/query_collection.ts"; import { LuaEnv, diff --git a/client/plugos/syscalls/lua.ts b/client/plugos/syscalls/lua.ts index e39fe723..06f0bc44 100644 --- a/client/plugos/syscalls/lua.ts +++ b/client/plugos/syscalls/lua.ts @@ -1,6 +1,11 @@ import type { SysCallMapping } from "../system.ts"; import { evalExpression } from "../../space_lua/eval.ts"; -import { parse, parseExpressionString } from "../../space_lua/parse.ts"; +import { parseBlock, parseExpressionString } from "../../space_lua/parse.ts"; +import { + type PrintOptions, + prettyPrintBlock, + prettyPrintExpression, +} from "../../space_lua/pretty_print.ts"; import { LuaStackFrame, luaToString, @@ -13,12 +18,30 @@ import type { ClientSystem } from "../../client_system.ts"; export function luaSyscalls(clientSystem: ClientSystem): SysCallMapping { return { + "lua.parseBlock": (_ctx, code: string): LuaBlock => { + return parseBlock(code); + }, + // Deprecated alias for `lua.parseBlock`, kept for backwards compatibility. "lua.parse": (_ctx, code: string): LuaBlock => { - return parse(code); + return parseBlock(code); }, "lua.parseExpression": (_ctx, expression: string): LuaExpression => { return parseExpressionString(expression); }, + "lua.prettyPrintBlock": ( + _ctx, + block: LuaBlock, + opts?: PrintOptions, + ): string => { + return prettyPrintBlock(block, opts); + }, + "lua.prettyPrintExpression": ( + _ctx, + expression: LuaExpression, + opts?: PrintOptions, + ): string => { + return prettyPrintExpression(expression, opts); + }, /** * Evaluates a Lua expression and returns the result as a JavaScript value * @param _ctx diff --git a/client/space_lua.ts b/client/space_lua.ts index 33d117fb..17bc7422 100644 --- a/client/space_lua.ts +++ b/client/space_lua.ts @@ -1,7 +1,7 @@ import type { System } from "./plugos/system.ts"; import type { SpaceLuaObject } from "../plugs/index/space_lua.ts"; import { LuaEnv, LuaRuntimeError, LuaStackFrame } from "./space_lua/runtime.ts"; -import { parse as parseLua, parseExpressionString } from "./space_lua/parse.ts"; +import { parseBlock, parseExpressionString } from "./space_lua/parse.ts"; import { evalStatement } from "./space_lua/eval.ts"; import { encodeRef, @@ -53,7 +53,7 @@ export class SpaceLuaEnvironment { for (const script of allScripts) { try { console.log("Now evaluating", script.ref); - const ast = parseLua(script.script, { ref: script.ref }); + const ast = parseBlock(script.script, { ref: script.ref }); // We create a local scope for each script const scriptEnv = new LuaEnv(this.env); const sf = new LuaStackFrame(tl, ast.ctx); diff --git a/client/space_lua/break_async.test.ts b/client/space_lua/break_async.test.ts index 2979512b..1a382a35 100644 --- a/client/space_lua/break_async.test.ts +++ b/client/space_lua/break_async.test.ts @@ -1,11 +1,11 @@ import { expect, test } from "vitest"; -import { parse } from "./parse.ts"; +import { parseBlock } from "./parse.ts"; import { evalStatement } from "./eval.ts"; import { LuaEnv, LuaNativeJSFunction, LuaStackFrame } from "./runtime.ts"; import { luaBuildStandardEnv } from "./stdlib.ts"; async function evalBlock(code: string, env?: LuaEnv) { - const ast = parse(code); + const ast = parseBlock(code); // Use the same global env for both the base env's parent and the StackFrame's _GLOBAL const G = luaBuildStandardEnv(); const base = env ?? new LuaEnv(G); diff --git a/client/space_lua/close_attribute.test.ts b/client/space_lua/close_attribute.test.ts index 232109c9..fbcb3df5 100644 --- a/client/space_lua/close_attribute.test.ts +++ b/client/space_lua/close_attribute.test.ts @@ -1,12 +1,12 @@ import { expect, test } from "vitest"; import { isPromise } from "./rp.ts"; -import { parse } from "./parse.ts"; +import { parseBlock } from "./parse.ts"; import { evalStatement } from "./eval.ts"; import { LuaEnv, LuaRuntimeError, LuaStackFrame } from "./runtime.ts"; import { luaBuildStandardEnv } from "./stdlib.ts"; async function evalBlock(code: string, env?: LuaEnv): Promise { - const ast = parse(code); + const ast = parseBlock(code); const G = luaBuildStandardEnv(); const base = env ?? new LuaEnv(G); const sf = LuaStackFrame.createWithGlobalEnv(G, ast.ctx); @@ -19,7 +19,7 @@ async function evalBlock(code: string, env?: LuaEnv): Promise { } async function runAndCatch(code: string, ref = "close_attribute.lua") { - const ast = parse(code, { ref }); + const ast = parseBlock(code, { ref }); const G = luaBuildStandardEnv(); const env = new LuaEnv(G); const sf = LuaStackFrame.createWithGlobalEnv(G, ast.ctx); @@ -36,7 +36,7 @@ async function runAndCatch(code: string, ref = "close_attribute.lua") { } async function runAndCatchEnv(code: string, ref = "close_attribute.lua") { - const ast = parse(code, { ref }); + const ast = parseBlock(code, { ref }); const G = luaBuildStandardEnv(); const env = new LuaEnv(G); const sf = LuaStackFrame.createWithGlobalEnv(G, ast.ctx); @@ -57,7 +57,7 @@ async function runAndCatchEnv(code: string, ref = "close_attribute.lua") { // 1. parsing and static validation test("close: parse ok", () => { - parse( + parseBlock( ` local x = {} `, @@ -66,7 +66,7 @@ test("close: parse ok", () => { }); test("close: parse ok without init", () => { - parse( + parseBlock( ` do local x @@ -78,7 +78,7 @@ test("close: parse ok without init", () => { test("close: local list only one", () => { try { - parse( + parseBlock( ` do local a, b = {}, {} @@ -94,7 +94,7 @@ test("close: local list only one", () => { }); test("close: parse ok", () => { - parse( + parseBlock( ` do local x = 1 @@ -105,7 +105,7 @@ test("close: parse ok", () => { }); test("close: and parse ok", () => { - parse( + parseBlock( ` do local x = 1 @@ -121,7 +121,7 @@ test("close: and parse ok", () => { test("close: invalid goto into scope", () => { try { - parse( + parseBlock( ` do goto L @@ -552,7 +552,7 @@ test("close: async __close awaited on error unwind", async () => { end `; - const ast = parse(code, { ref: "close_async_unwind.lua" }); + const ast = parseBlock(code, { ref: "close_async_unwind.lua" }); const G = luaBuildStandardEnv(); const env = new LuaEnv(G); const sf = LuaStackFrame.createWithGlobalEnv(G, ast.ctx); @@ -985,7 +985,7 @@ test("close: async close error reported; later closers still run", async () => { }) end `; - const ast = parse(code, { ref: "close_async_close_error.lua" }); + const ast = parseBlock(code, { ref: "close_async_close_error.lua" }); const sf = LuaStackFrame.createWithGlobalEnv(G, ast.ctx); let err: unknown = null; diff --git a/client/space_lua/const_attribute.test.ts b/client/space_lua/const_attribute.test.ts index a71bfee1..ce34844e 100644 --- a/client/space_lua/const_attribute.test.ts +++ b/client/space_lua/const_attribute.test.ts @@ -1,11 +1,11 @@ import { expect, test } from "vitest"; -import { parse } from "./parse.ts"; +import { parseBlock } from "./parse.ts"; import { evalStatement } from "./eval.ts"; import { LuaEnv, LuaRuntimeError, LuaStackFrame } from "./runtime.ts"; import { luaBuildStandardEnv } from "./stdlib.ts"; async function evalBlock(code: string, env?: LuaEnv): Promise { - const ast = parse(code); + const ast = parseBlock(code); const G = luaBuildStandardEnv(); const base = env ?? new LuaEnv(G); const sf = LuaStackFrame.createWithGlobalEnv(G, ast.ctx); @@ -18,7 +18,7 @@ async function evalBlock(code: string, env?: LuaEnv): Promise { } async function runAndCatch(code: string, ref = "const_attribute.lua") { - const ast = parse(code, { ref }); + const ast = parseBlock(code, { ref }); const G = luaBuildStandardEnv(); const env = new LuaEnv(G); const sf = LuaStackFrame.createWithGlobalEnv(G, ast.ctx); @@ -37,7 +37,7 @@ async function runAndCatch(code: string, ref = "const_attribute.lua") { test("const: Unknown attribute (parse-time)", () => { let threw = false; try { - parse(`local x = 1`, { ref: "unknown_attribute.lua" }); + parseBlock(`local x = 1`, { ref: "unknown_attribute.lua" }); } catch (e: any) { threw = true; expect(String(e?.message ?? e)).toContain("unknown attribute 'nope'"); @@ -50,7 +50,7 @@ test("const: Unknown attribute (parse-time)", () => { test("const: Case-sensitive attribute (parse-time)", () => { let threw = false; try { - parse(`local x = 1`, { ref: "unknown_attribute.lua" }); + parseBlock(`local x = 1`, { ref: "unknown_attribute.lua" }); } catch (e: any) { threw = true; expect(String(e?.message ?? e)).toContain("unknown attribute 'Const'"); diff --git a/client/space_lua/context_errors.test.ts b/client/space_lua/context_errors.test.ts index cd1371de..9f0cbfb7 100644 --- a/client/space_lua/context_errors.test.ts +++ b/client/space_lua/context_errors.test.ts @@ -1,11 +1,11 @@ import { expect, test } from "vitest"; -import { parse } from "./parse.ts"; +import { parseBlock } from "./parse.ts"; import { evalStatement } from "./eval.ts"; import { LuaEnv, LuaRuntimeError, LuaStackFrame } from "./runtime.ts"; import { luaBuildStandardEnv } from "./stdlib.ts"; async function runAndCatch(code: string, ref = "ctx_test.lua") { - const ast = parse(code, { ref }); + const ast = parseBlock(code, { ref }); const global = luaBuildStandardEnv(); const env = new LuaEnv(global); const sf = LuaStackFrame.createWithGlobalEnv(global, ast.ctx); diff --git a/client/space_lua/eval.test.ts b/client/space_lua/eval.test.ts index d3ef6902..7a57f672 100644 --- a/client/space_lua/eval.test.ts +++ b/client/space_lua/eval.test.ts @@ -7,7 +7,7 @@ import { luaValueToJS, singleResult, } from "./runtime.ts"; -import { parse } from "./parse.ts"; +import { parseBlock } from "./parse.ts"; import type { LuaBlock, LuaFunctionCallStatement } from "./ast.ts"; import { evalExpression, evalStatement } from "./eval.ts"; import { luaBuildStandardEnv } from "./stdlib.ts"; @@ -15,13 +15,13 @@ import { luaBuildStandardEnv } from "./stdlib.ts"; const sf = LuaStackFrame.lostFrame; function evalExpr(s: string, e = new LuaEnv(), sf?: LuaStackFrame): any { - const node = parse(`e(${s})`).statements[0] as LuaFunctionCallStatement; + const node = parseBlock(`e(${s})`).statements[0] as LuaFunctionCallStatement; sf = sf || new LuaStackFrame(e, node.ctx); return evalExpression(node.call.args[0], e, sf); } async function evalBlock(s: string, e = new LuaEnv()): Promise { - const node = parse(s) as LuaBlock; + const node = parseBlock(s) as LuaBlock; const sf = new LuaStackFrame(e, node.ctx); await evalStatement(node, e, sf); } @@ -111,7 +111,7 @@ test("Parser rejects unary plus - parenthesized", () => { ]) { let err: any = null; try { - parse(src); + parseBlock(src); } catch (e: any) { err = e; } @@ -132,7 +132,7 @@ test("Parser rejects unary plus - variables and calls", () => { ]) { let err: any = null; try { - parse(src); + parseBlock(src); } catch (e: any) { err = e; } @@ -152,7 +152,7 @@ test("Parser rejects unary plus - whitespace/newlines", () => { ]) { let err: any = null; try { - parse(src); + parseBlock(src); } catch (e: any) { err = e; } diff --git a/client/space_lua/lua.test.ts b/client/space_lua/lua.test.ts index 27ce2810..6b982b80 100644 --- a/client/space_lua/lua.test.ts +++ b/client/space_lua/lua.test.ts @@ -1,5 +1,5 @@ import { expect, test } from "vitest"; -import { parse } from "./parse.ts"; +import { parseBlock } from "./parse.ts"; import { luaBuildStandardEnv } from "./stdlib.ts"; import { LuaEnv, LuaRuntimeError, LuaStackFrame } from "./runtime.ts"; import { evalStatement } from "./eval.ts"; @@ -58,7 +58,7 @@ async function runLuaTest(luaPath: string) { fileURLToPath(new URL(luaPath, import.meta.url)), "utf-8", ); - const chunk = parse(luaFile, {}); + const chunk = parseBlock(luaFile, {}); const env = new LuaEnv(luaBuildStandardEnv()); const sf = LuaStackFrame.createWithGlobalEnv(env, chunk.ctx); diff --git a/client/space_lua/parse.test.ts b/client/space_lua/parse.test.ts index e2fb2efe..74dcdf21 100644 --- a/client/space_lua/parse.test.ts +++ b/client/space_lua/parse.test.ts @@ -1,104 +1,104 @@ import { expect, test } from "vitest"; -import { parse, parseExpressionString, stripLuaComments } from "./parse.ts"; +import { parseBlock, parseExpressionString, stripLuaComments } from "./parse.ts"; import type { LuaNumberLiteral } from "./ast.ts"; test("Test Lua parser", () => { // Basic block test - parse(` + parseBlock(` print("Hello, World!") print(10) `); - parse(""); + parseBlock(""); // Expression tests - parse( + parseBlock( `e(1, 1.2, -3.8, -4, #lst, true, false, nil, "string", "", "Hello there \x00", ...)`, ); - parse(`e([[hel]lo]], "Grinny face\\u{1F600}")`); - parse(`e([=[Hello page [[index]] end scene]=], [[yo]])`); + parseBlock(`e([[hel]lo]], "Grinny face\\u{1F600}")`); + parseBlock(`e([=[Hello page [[index]] end scene]=], [[yo]])`); - parse(`e([==[Hello page [[index]] end scene]==], [==[yo]==])`); + parseBlock(`e([==[Hello page [[index]] end scene]==], [==[yo]==])`); console.log( JSON.stringify( - parse(`e([==[Hello page [[bla]] + parseBlock(`e([==[Hello page [[bla]] ]==])`), null, 2, ), ); - parse(`e(10 << 10, 10 >> 10, 10 & 10, 10 | 10, 10 ~ 10)`); + parseBlock(`e(10 << 10, 10 >> 10, 10 & 10, 10 | 10, 10 ~ 10)`); - parse(`e(true and false or true)`); - parse(`e(a < 3 and b > 4 or b == 5 or c <= 6 and d >= 7 or a /= 8)`); - parse(`e(a.b.c)`); - parse(`e((1+2))`); + parseBlock(`e(true and false or true)`); + parseBlock(`e(a < 3 and b > 4 or b == 5 or c <= 6 and d >= 7 or a /= 8)`); + parseBlock(`e(a.b.c)`); + parseBlock(`e((1+2))`); // Use keywordy variables - parse(`e(order, limit, where)`); + parseBlock(`e(order, limit, where)`); // Table expressions - parse(`e({})`); - parse(`e({1, 2, 3, })`); - parse(`e({1 ; 2 ; 3})`); - parse(`e({a = 1, b = 2, c = 3})`); - parse(`e({[3] = 1, [10 * 10] = "sup"})`); - parse(`e(tbl.name)`); - parse(`e(tbl["name" + 10])`); - parse(`e(test().bla)`); + parseBlock(`e({})`); + parseBlock(`e({1, 2, 3, })`); + parseBlock(`e({1 ; 2 ; 3})`); + parseBlock(`e({a = 1, b = 2, c = 3})`); + parseBlock(`e({[3] = 1, [10 * 10] = "sup"})`); + parseBlock(`e(tbl.name)`); + parseBlock(`e(tbl["name" + 10])`); + parseBlock(`e(test().bla)`); // Function calls - parse(`e(func(), func(1, 2, 3), a.b(), a.b.c:hello(), (a.b)(7))`); + parseBlock(`e(func(), func(1, 2, 3), a.b(), a.b.c:hello(), (a.b)(7))`); // Function expression - parse(`e(function(a, b) test() end)`); - parse(`e(function(a, b, ...) end)`); + parseBlock(`e(function(a, b) test() end)`); + parseBlock(`e(function(a, b, ...) end)`); // Statements - parse(`do end`); - parse(`do print() end`); - parse(`::hello:: + parseBlock(`do end`); + parseBlock(`do print() end`); + parseBlock(`::hello:: goto hello`); - parse(`while true do print() end`); - parse(`repeat print() until false`); - parse(`if 1 == 2 then print() elseif 1 < 2 then print2() else print3() end`); - parse(`if true then print() end`); - parse(`if true then print() else print2() end`); - parse(`if true then print() elseif false then print2() end`); + parseBlock(`while true do print() end`); + parseBlock(`repeat print() until false`); + parseBlock(`if 1 == 2 then print() elseif 1 < 2 then print2() else print3() end`); + parseBlock(`if true then print() end`); + parseBlock(`if true then print() else print2() end`); + parseBlock(`if true then print() elseif false then print2() end`); // For loops - parse(`for i = 1, 10, 1 do print(i) end`); - parse(`for i = 1, 10 do print(i) end`); - parse(`for el in each({1, 2, 3}) do print(i) end`); - parse(`for i, l in 1, pairs() do print(i) end`); + parseBlock(`for i = 1, 10, 1 do print(i) end`); + parseBlock(`for i = 1, 10 do print(i) end`); + parseBlock(`for el in each({1, 2, 3}) do print(i) end`); + parseBlock(`for i, l in 1, pairs() do print(i) end`); // Function statements - parse(`function a() end`); - parse(`function a:b() end`); - parse(`function a.b.c:d() end`); - parse(`function a.b.c() end`); - parse(`function hello(a, b) end`); - parse(`function hello(a, b, ...) end`); - parse(`local function hello() end`); + parseBlock(`function a() end`); + parseBlock(`function a:b() end`); + parseBlock(`function a.b.c:d() end`); + parseBlock(`function a.b.c() end`); + parseBlock(`function hello(a, b) end`); + parseBlock(`function hello(a, b, ...) end`); + parseBlock(`local function hello() end`); // Assignments, local variables etc. - parse(`a = 1`); - parse(`a, b = 1, 2`); - parse(`a.b.c = 1`); - parse(`a["name"] = 1`); - parse(`local a, b`); - parse(`local a = 1`); - parse(`local a = 4`); - parse(`local a, b = 1, 2`); + parseBlock(`a = 1`); + parseBlock(`a, b = 1, 2`); + parseBlock(`a.b.c = 1`); + parseBlock(`a["name"] = 1`); + parseBlock(`local a, b`); + parseBlock(`local a = 1`); + parseBlock(`local a = 4`); + parseBlock(`local a, b = 1, 2`); // Function calls - parse(`a(1, 2, 3)`); - parse(`print "Sup"`); - parse(`e(1 + print "8")`); + parseBlock(`a(1, 2, 3)`); + parseBlock(`print "Sup"`); + parseBlock(`e(1 + print "8")`); // Return statements - parse(`return`); - parse(`return 1`); - parse(`return 1, 2, 3`); + parseBlock(`return`); + parseBlock(`return 1`); + parseBlock(`return 1, 2, 3`); }); test("Test comment handling", () => { @@ -122,24 +122,24 @@ hello }); test("Test query parsing", () => { - parse( + parseBlock( `_(query[[from p = index.tag("page") where p.name == "John" limit 10, 3]])`, ); - parse(`_(query[[from index.tag("page") select {name="hello", age=10}]])`); - parse( + parseBlock(`_(query[[from index.tag("page") select {name="hello", age=10}]])`); + parseBlock( `_(query[[from p = index.tag("page") order by p.lastModified desc, p.name]])`, ); - parse(`_(query[[from p = index.tag("page") order by p.lastModified]])`); + parseBlock(`_(query[[from p = index.tag("page") order by p.lastModified]])`); // group by single key - parse(`_(query[[from p = index.tag("page") group by p.category]])`); + parseBlock(`_(query[[from p = index.tag("page") group by p.category]])`); // group by multiple keys - parse(`_(query[[from p = index.tag("page") group by p.category, p.status]])`); + parseBlock(`_(query[[from p = index.tag("page") group by p.category, p.status]])`); // group by + having - parse( + parseBlock( `_(query[[from p = index.tag("page") group by p.category having #group > 1]])`, ); // group by + having + select - parse( + parseBlock( `_(query[[from p = index.tag("page") group by p.category, p.status having #group > 2 select { key = key, count = #group }]])`, ); }); diff --git a/client/space_lua/parse.ts b/client/space_lua/parse.ts index 2871f5be..e22b0506 100644 --- a/client/space_lua/parse.ts +++ b/client/space_lua/parse.ts @@ -67,7 +67,7 @@ function parseChunk(t: ParseTree, ctx: ASTCtx): LuaBlock { if (t.type !== "Chunk") { throw new Error(`Expected Chunk, got ${t.type}`); } - return parseBlock(t.children![0], ctx); + return parseBlockNode(t.children![0], ctx); } function hasCloseLocal(names: LuaAttName[] | undefined): boolean { @@ -636,7 +636,7 @@ function exprCapturesNames(e: LuaExpression, names: Set): boolean { } } -function parseBlock(t: ParseTree, ctx: ASTCtx): LuaBlock { +function parseBlockNode(t: ParseTree, ctx: ASTCtx): LuaBlock { if (t.type !== "Block") { throw new Error(`Expected Block, got ${t.type}`); } @@ -846,20 +846,20 @@ function parseStatement(t: ParseTree, ctx: ASTCtx): LuaStatement { ctx: context(t, ctx), }; case "Scope": - return parseBlock(t.children![1], ctx); + return parseBlockNode(t.children![1], ctx); case ";": return { type: "Semicolon", ctx: context(t, ctx) }; case "WhileStatement": return { type: "While", condition: parseExpression(t.children![1], ctx), - block: parseBlock(t.children![3], ctx), + block: parseBlockNode(t.children![3], ctx), ctx: context(t, ctx), }; case "RepeatStatement": return { type: "Repeat", - block: parseBlock(t.children![1], ctx), + block: parseBlockNode(t.children![1], ctx), condition: parseExpression(t.children![3], ctx), ctx: context(t, ctx), }; @@ -880,12 +880,12 @@ function parseStatement(t: ParseTree, ctx: ASTCtx): LuaStatement { if (token === "if" || token === "elseif") { conditions.push({ condition: parseExpression(t.children![i + 1], ctx), - block: parseBlock(t.children![i + 3], ctx), + block: parseBlockNode(t.children![i + 3], ctx), from: child.from, to: child.to, }); } else if (token === "else") { - elseBlock = parseBlock(t.children![i + 1], ctx); + elseBlock = parseBlockNode(t.children![i + 1], ctx); } else if (token === "end") { break; } else { @@ -903,7 +903,7 @@ function parseStatement(t: ParseTree, ctx: ASTCtx): LuaStatement { if (t.children![1].type === "ForNumeric") { const forNumeric = t.children![1]; const name = forNumeric.children![0].children![0].text!; - const block = parseBlock(t.children![3], ctx); + const block = parseBlockNode(t.children![3], ctx); const node: LuaStatement = { type: "For", name, @@ -923,7 +923,7 @@ function parseStatement(t: ParseTree, ctx: ASTCtx): LuaStatement { } const forGeneric = t.children![1]; const names = parseNameList(forGeneric.children![0]); - const block = parseBlock(t.children![3], ctx); + const block = parseBlockNode(t.children![3], ctx); const node: LuaStatement = { type: "ForIn", names, @@ -1512,7 +1512,7 @@ function parseFunctionBody(t: ParseTree, ctx: ASTCtx): LuaFunctionBody { (c) => c.type && ["Name", "Ellipsis"].includes(c.type), ) .map((c) => c.children![0].text!), - block: parseBlock(t.children![3], ctx), + block: parseBlockNode(t.children![3], ctx), ctx: context(t, ctx), }; } @@ -1686,7 +1686,7 @@ export function stripLuaComments(s: string): string { return result; } -export function parse(s: string, ctx: ASTCtx = {}): LuaBlock { +export function parseBlock(s: string, ctx: ASTCtx = {}): LuaBlock { try { const t = parseToAST(stripLuaComments(s)); // console.log("Clean tree", JSON.stringify(t, null, 2)); @@ -1743,6 +1743,6 @@ function luaUnexpectedSymbolMessage(src: string, from: number): string { * Helper function to parse a Lua expression string */ export function parseExpressionString(expr: string): LuaExpression { - const parsedLua = parse(`_(${expr})`) as LuaBlock; + const parsedLua = parseBlock(`_(${expr})`) as LuaBlock; return (parsedLua.statements[0] as LuaFunctionCallStatement).call.args[0]; } diff --git a/client/space_lua/pretty_print.test.ts b/client/space_lua/pretty_print.test.ts new file mode 100644 index 00000000..41f8eadb --- /dev/null +++ b/client/space_lua/pretty_print.test.ts @@ -0,0 +1,231 @@ +import { expect, test } from "vitest"; +import { parseBlock, parseExpressionString } from "./parse.ts"; +import { prettyPrintBlock, prettyPrintExpression } from "./pretty_print.ts"; +import type { PrintOptions } from "./pretty_print.ts"; + +function fmtExpr(code: string, opts?: PrintOptions): string { + return prettyPrintExpression(parseExpressionString(code), opts); +} +function fmtBlock(code: string, opts?: PrintOptions): string { + return prettyPrintBlock(parseBlock(code), opts); +} + +test("literals", () => { + expect(fmtExpr("nil")).toBe("nil"); + expect(fmtExpr("true")).toBe("true"); + expect(fmtExpr("false")).toBe("false"); + expect(fmtExpr("42")).toBe("42"); + expect(fmtExpr("3.5")).toBe("3.5"); + expect(fmtExpr('"hi"')).toBe('"hi"'); +}); + +test("string re-quoting and escaping", () => { + expect(fmtExpr(`'hi'`)).toBe('"hi"'); + expect(fmtExpr(`'say "hi"'`)).toBe('"say \\"hi\\""'); + expect(fmtExpr(`"a\\nb"`)).toBe('"a\\nb"'); + expect(fmtExpr(`'x'`, { quote: "single" })).toBe("'x'"); +}); + +test("variable and access expressions", () => { + expect(fmtExpr("foo")).toBe("foo"); + expect(fmtExpr("a.b.c")).toBe("a.b.c"); + expect(fmtExpr("t[1]")).toBe("t[1]"); + expect(fmtExpr('t["k"]')).toBe('t["k"]'); + // the parser drops redundant parens around a bare variable + expect(fmtExpr("(a)")).toBe("a"); +}); + +test("binary operators with spacing", () => { + expect(fmtExpr("1+2")).toBe("1 + 2"); + expect(fmtExpr("a..b")).toBe("a .. b"); + expect(fmtExpr("a and b or c")).toBe("a and b or c"); + expect(fmtExpr("a==b")).toBe("a == b"); +}); + +test("unary operators", () => { + expect(fmtExpr("not x")).toBe("not x"); + expect(fmtExpr("-x")).toBe("-x"); + expect(fmtExpr("#t")).toBe("#t"); + expect(fmtExpr("~x")).toBe("~x"); +}); + +test("precedence parenthesization preserves meaning", () => { + expect(fmtExpr("(1+2)*3")).toBe("(1 + 2) * 3"); + expect(fmtExpr("1+2*3")).toBe("1 + 2 * 3"); + expect(fmtExpr("(a or b) and c")).toBe("(a or b) and c"); + expect(fmtExpr("a-(b-c)")).toBe("a - (b - c)"); + expect(fmtExpr("-x^2")).toBe("-x ^ 2"); + expect(fmtExpr("(-x)^2")).toBe("(-x) ^ 2"); +}); + +test("tables: empty and single field inline", () => { + expect(fmtExpr("{}")).toBe("{}"); + expect(fmtExpr("{a=1}")).toBe("{a = 1}"); + expect(fmtExpr("{42}")).toBe("{42}"); + expect(fmtExpr('{["a b"]=1}')).toBe('{["a b"] = 1}'); +}); + +test("tables: 2+ fields break multiline with trailing comma", () => { + expect(fmtExpr("{a=1,b=2}")).toBe(`{ + a = 1, + b = 2, +}`); + expect(fmtExpr("{1,2,3}")).toBe(`{ + 1, + 2, + 3, +}`); +}); + +test("tables: trailingComma option off", () => { + expect(fmtExpr("{a=1,b=2}", { trailingComma: false })).toBe(`{ + a = 1, + b = 2 +}`); +}); + +test("function definitions", () => { + expect(fmtExpr("function() end")).toBe("function() end"); + expect(fmtExpr("function(a,b) return a end")).toBe(`function(a, b) + return a +end`); +}); + +test("function calls and sugar", () => { + expect(fmtExpr("f(1, 2)")).toBe("f(1, 2)"); + expect(fmtExpr("obj:m(1)")).toBe("obj:m(1)"); + expect(fmtExpr('f "x"')).toBe('f "x"'); + expect(fmtExpr('f("x")')).toBe('f "x"'); + expect(fmtExpr("f {a=1,b=2}")).toBe(`f { + a = 1, + b = 2, +}`); +}); + +test("simple statements", () => { + expect(fmtBlock("local x = 1")).toBe("local x = 1"); + expect(fmtBlock("local a, b = 1, 2")).toBe("local a, b = 1, 2"); + expect(fmtBlock("local x = 1")).toBe("local x = 1"); + expect(fmtBlock("x = 1")).toBe("x = 1"); + expect(fmtBlock("a, b = 1, 2")).toBe("a, b = 1, 2"); + expect(fmtBlock("print(1)")).toBe("print(1)"); + expect(fmtBlock("return 1, 2")).toBe("return 1, 2"); + expect(fmtBlock("return")).toBe("return"); + expect(fmtBlock("do return end")).toBe(`do + return +end`); +}); + +test("multi-statement block, one per line", () => { + expect(fmtBlock("local x = 1\nlocal y = 2\nreturn x + y")).toBe(`local x = 1 +local y = 2 +return x + y`); +}); + +test("goto, label, break", () => { + expect(fmtBlock("::top::")).toBe("::top::"); + expect(fmtBlock("goto done\n::done::")).toBe(`goto done +::done::`); + expect(fmtBlock("while true do break end")).toBe(`while true do + break +end`); +}); + +test("if / elseif / else", () => { + expect(fmtBlock("if a then return 1 end")).toBe(`if a then + return 1 +end`); + expect( + fmtBlock("if a then return 1 elseif b then return 2 else return 3 end"), + ).toBe(`if a then + return 1 +elseif b then + return 2 +else + return 3 +end`); +}); + +test("while / repeat", () => { + expect(fmtBlock("while a do f() end")).toBe(`while a do + f() +end`); + expect(fmtBlock("repeat f() until a")).toBe(`repeat + f() +until a`); +}); + +test("numeric and generic for", () => { + expect(fmtBlock("for i=1,10 do f() end")).toBe(`for i = 1, 10 do + f() +end`); + expect(fmtBlock("for i=1,10,2 do f() end")).toBe(`for i = 1, 10, 2 do + f() +end`); + expect(fmtBlock("for k,v in pairs(t) do f() end")).toBe(`for k, v in pairs(t) do + f() +end`); +}); + +test("function statements", () => { + expect(fmtBlock("function a.b:m(x) return x end")).toBe(`function a.b:m(x) + return x +end`); + expect(fmtBlock("local function g() end")).toBe("local function g() end"); +}); + +test("blank line between definitions", () => { + expect(fmtBlock("function a() end\nfunction b() end\nlocal x = 1")) + .toBe(`function a() end + +function b() end + +local x = 1`); +}); + +test("query expression", () => { + const code = + 'query[[from p = index.pages() where p.tag == "x" order by p.name limit 5]]'; + expect(fmtExpr(code)).toBe(`query[[ + from p = index.pages() + where p.tag == "x" + order by p.name + limit 5 +]]`); +}); + +test("query order by direction", () => { + const code = "query[[from p = t order by p.n desc]]"; + expect(fmtExpr(code)).toBe(`query[[ + from p = t + order by p.n desc +]]`); +}); + +test("idempotency: print(parse(print(parse(x)))) === print(parse(x))", () => { + const samples = [ + `local x = 1 +return x + 2`, + `config.define("a", { + type = "object", + properties = { + x = true, + y = false, + }, +})`, + `function f(a, b) + if a then + return b + end +end`, + `for i = 1, 10 do + print(i) +end`, + "local t = {a = 1, b = {c = 2, d = 3}}", + ]; + for (const code of samples) { + const once = prettyPrintBlock(parseBlock(code)); + const twice = prettyPrintBlock(parseBlock(once)); + expect(twice).toBe(once); + } +}); diff --git a/client/space_lua/pretty_print.ts b/client/space_lua/pretty_print.ts new file mode 100644 index 00000000..5e648d4c --- /dev/null +++ b/client/space_lua/pretty_print.ts @@ -0,0 +1,391 @@ +import type { + LuaBlock, + LuaExpression, + LuaOrderBy, + LuaQueryClause, + LuaStatement, + LuaTableField, +} from "./ast.ts"; +import { luaFormatNumber } from "./runtime.ts"; + +export type PrintOptions = { + indentWidth?: number; + quote?: "double" | "single"; + trailingComma?: boolean; +}; + +type ResolvedOptions = { + indentWidth: number; + quote: "double" | "single"; + trailingComma: boolean; +}; + +function resolveOptions(opts?: PrintOptions): ResolvedOptions { + return { + indentWidth: opts?.indentWidth ?? 2, + quote: opts?.quote ?? "double", + trailingComma: opts?.trailingComma ?? true, + }; +} + +const RESERVED = new Set([ + "and", "break", "do", "else", "elseif", "end", "false", "for", "function", + "goto", "if", "in", "local", "nil", "not", "or", "repeat", "return", "then", + "true", "until", "while", +]); + +function isLuaIdentifier(s: string): boolean { + return /^[A-Za-z_][A-Za-z0-9_]*$/.test(s) && !RESERVED.has(s); +} + +const BINARY_PREC: Record = { + "or": 1, + "and": 2, + "<": 3, ">": 3, "<=": 3, ">=": 3, "~=": 3, "==": 3, + "|": 4, + "~": 5, // binary bitwise xor + "&": 6, + "<<": 7, ">>": 7, + "..": 8, + "+": 9, "-": 9, + "*": 10, "/": 10, "//": 10, "%": 10, + "^": 12, +}; +const UNARY_PREC = 11; +const RIGHT_ASSOC = new Set(["..", "^"]); +const ATOM_PREC = 99; + +function exprPrec(e: LuaExpression): number { + if (e.type === "Binary") return BINARY_PREC[e.operator] ?? ATOM_PREC; + if (e.type === "Unary") return UNARY_PREC; + return ATOM_PREC; +} + +class Printer { + constructor(private opts: ResolvedOptions) { } + + private indent(depth: number): string { + return " ".repeat(depth * this.opts.indentWidth); + } + + private quoteString(s: string): string { + const q = this.opts.quote === "double" ? '"' : "'"; + let out = q; + for (const ch of s) { + switch (ch) { + case "\\": + out += "\\\\"; + break; + case "\n": + out += "\\n"; + break; + case "\r": + out += "\\r"; + break; + case "\t": + out += "\\t"; + break; + case "\0": + out += "\\0"; + break; + default: + out += ch === q ? `\\${ch}` : ch; + } + } + return out + q; + } + + expression(e: LuaExpression, depth = 0): string { + switch (e.type) { + case "Nil": + return "nil"; + case "Boolean": + return e.value ? "true" : "false"; + case "Number": + return luaFormatNumber(e.value, e.numericType); + case "String": + return this.quoteString(e.value); + case "Variable": + return e.name; + case "PropertyAccess": + return `${this.expression(e.object, depth)}.${e.property}`; + case "TableAccess": + return `${this.expression(e.object, depth)}[${this.expression(e.key, depth)}]`; + case "Parenthesized": + return `(${this.expression(e.expression, depth)})`; + case "Binary": { + const p = BINARY_PREC[e.operator] ?? ATOM_PREC; + const rightAssoc = RIGHT_ASSOC.has(e.operator); + const left = this.operand(e.left, p, false, rightAssoc, depth); + const right = this.operand(e.right, p, true, rightAssoc, depth); + return `${left} ${e.operator} ${right}`; + } + case "Unary": { + const arg = this.unaryOperand(e.argument, depth); + return /^[a-z]+$/.test(e.operator) + ? `${e.operator} ${arg}` + : `${e.operator}${arg}`; + } + case "TableConstructor": + return this.tableConstructor(e.fields, depth); + case "FunctionDefinition": + return "function" + + this.functionRest(e.body.parameters, e.body.block, depth); + case "FunctionCall": + return this.functionCall(e, depth); + case "Query": { + const lines = e.clauses.map((c) => + this.indent(depth + 1) + this.queryClause(c, depth + 1) + ); + return `query[[\n${lines.join("\n")}\n${this.indent(depth)}]]`; + } + case "FilteredCall": + return `${this.expression(e.call, depth)} filter ${this.expression(e.filter, depth) + }`; + case "AggregateCall": + return `${this.expression(e.call, depth)} order by ${this.orderByList(e.orderBy, depth) + }`; + default: + throw new Error( + `pretty_print: unsupported expression type: ${(e as any).type}`, + ); + } + } + + private operand( + e: LuaExpression, + parentPrec: number, + isRight: boolean, + parentRightAssoc: boolean, + depth: number, + ): string { + const cp = exprPrec(e); + let parens = false; + if (cp < parentPrec) { + parens = true; + } else if (cp === parentPrec) { + // left-assoc parent: the right child of equal precedence needs parens. + // right-assoc parent: the left child of equal precedence needs parens. + parens = parentRightAssoc ? !isRight : isRight; + } + const s = this.expression(e, depth); + return parens ? `(${s})` : s; + } + + private unaryOperand(e: LuaExpression, depth: number): string { + const s = this.expression(e, depth); + return exprPrec(e) < UNARY_PREC ? `(${s})` : s; + } + + private field(f: LuaTableField, depth: number): string { + switch (f.type) { + case "PropField": + return isLuaIdentifier(f.key) + ? `${f.key} = ${this.expression(f.value, depth)}` + : `[${this.quoteString(f.key)}] = ${this.expression(f.value, depth)}`; + case "DynamicField": + return `[${this.expression(f.key, depth)}] = ${this.expression(f.value, depth)}`; + case "ExpressionField": + return this.expression(f.value, depth); + } + } + + private tableConstructor(fields: LuaTableField[], depth: number): string { + if (fields.length === 0) return "{}"; + if (fields.length === 1) return `{${this.field(fields[0], depth)}}`; + const lines = fields.map((f) => + this.indent(depth + 1) + this.field(f, depth + 1) + ); + const tail = this.opts.trailingComma ? "," : ""; + return `{\n${lines.join(",\n")}${tail}\n${this.indent(depth)}}`; + } + + // returns the part of a function after the `function` keyword (+ optional + // name): "(params) end" or "(params)\n \nend" + private functionRest( + params: string[], + body: LuaBlock, + depth: number, + ): string { + const head = `(${params.join(", ")})`; + const rendered = this.block(body, depth + 1); + if (!rendered) return `${head} end`; + return `${head}\n${rendered}\n${this.indent(depth)}end`; + } + + private functionCall( + e: Extract, + depth: number, + ): string { + let base = this.expression(e.prefix, depth); + if (e.name) base += `:${e.name}`; + const args = e.args; + const orderBy = e.orderBy + ? ` order by ${this.orderByList(e.orderBy, depth)}` + : ""; + if ( + !e.orderBy && args.length === 1 && + (args[0].type === "TableConstructor" || args[0].type === "String") + ) { + return `${base} ${this.expression(args[0], depth)}`; + } + return `${base}(${args.map((a) => this.expression(a, depth)).join(", ")})${orderBy}`; + } + + private fieldList(fields: LuaTableField[], depth: number): string { + return fields.map((f) => this.field(f, depth)).join(", "); + } + + private orderByList(orders: LuaOrderBy[], depth: number): string { + return orders + .map((o) => { + let s = this.expression(o.expression, depth); + if (o.direction === "desc") s += " desc"; + if (o.nulls) s += ` nulls ${o.nulls}`; + return s; + }) + .join(", "); + } + + private queryClause(c: LuaQueryClause, depth: number): string { + switch (c.type) { + case "From": + return `from ${this.fieldList(c.fields, depth)}`; + case "Where": + return `where ${this.expression(c.expression, depth)}`; + case "Limit": + return `limit ${this.expression(c.limit, depth)}` + + (c.offset ? ` offset ${this.expression(c.offset, depth)}` : ""); + case "Offset": + return `offset ${this.expression(c.offset, depth)}`; + case "OrderBy": + return `order by ${this.orderByList(c.orderBy, depth)}`; + case "Select": + return `select ${this.fieldList(c.fields, depth)}`; + case "GroupBy": + return `group by ${this.fieldList(c.fields, depth)}`; + case "Having": + return `having ${this.expression(c.expression, depth)}`; + } + } + + block(b: LuaBlock, depth: number): string { + const stmts = b.statements.filter((s) => s.type !== "Semicolon"); + const isDef = (s: LuaStatement) => + s.type === "Function" || s.type === "LocalFunction"; + let out = ""; + for (let i = 0; i < stmts.length; i++) { + if (i > 0) { + out += "\n"; + // blank line separating function definitions from neighbours + if (isDef(stmts[i - 1]) || isDef(stmts[i])) out += "\n"; + } + out += this.statement(stmts[i], depth); + } + return out; + } + + private statement(s: LuaStatement, depth: number): string { + const ind = this.indent(depth); + switch (s.type) { + case "Local": { + const names = s.names + .map((n) => + n.name + + (n.attribute + ? ` <${n.attribute}>` + : (n.attributes?.length ? ` <${n.attributes[0]}>` : "")) + ) + .join(", "); + const exprs = s.expressions?.length + ? ` = ${s.expressions.map((e) => this.expression(e, depth)).join(", ")}` + : ""; + return `${ind}local ${names}${exprs}`; + } + case "Assignment": + return `${ind}${s.variables.map((v) => this.expression(v, depth)).join(", ") + } = ${s.expressions.map((e) => this.expression(e, depth)).join(", ")}`; + case "Return": + return s.expressions.length + ? `${ind}return ${s.expressions.map((e) => this.expression(e, depth)).join(", ") + }` + : `${ind}return`; + case "FunctionCallStatement": + return ind + this.expression(s.call, depth); + case "Break": + return `${ind}break`; + case "Goto": + return `${ind}goto ${s.name}`; + case "Label": + return `${ind}::${s.name}::`; + case "Block": { + const body = this.block(s, depth + 1); + return body ? `${ind}do\n${body}\n${ind}end` : `${ind}do end`; + } + case "If": { + let out = ""; + s.conditions.forEach((c, i) => { + const kw = i === 0 ? "if" : "elseif"; + const head = i === 0 ? `${ind}${kw} ` : `\n${ind}${kw} `; + const body = this.block(c.block, depth + 1); + out += `${head}${this.expression(c.condition, depth)} then` + + (body ? `\n${body}` : ""); + }); + if (s.elseBlock) { + const body = this.block(s.elseBlock, depth + 1); + out += `\n${ind}else${body ? `\n${body}` : ""}`; + } + out += `\n${ind}end`; + return out; + } + case "While": { + const body = this.block(s.block, depth + 1); + return `${ind}while ${this.expression(s.condition, depth)} do` + + (body ? `\n${body}` : "") + `\n${ind}end`; + } + case "Repeat": { + const body = this.block(s.block, depth + 1); + return `${ind}repeat` + (body ? `\n${body}` : "") + + `\n${ind}until ${this.expression(s.condition, depth)}`; + } + case "For": { + const step = s.step ? `, ${this.expression(s.step, depth)}` : ""; + const body = this.block(s.block, depth + 1); + return `${ind}for ${s.name} = ${this.expression(s.start, depth)}, ${this.expression(s.end, depth) + }${step} do${body ? `\n${body}` : ""}\n${ind}end`; + } + case "ForIn": { + const body = this.block(s.block, depth + 1); + return `${ind}for ${s.names.join(", ")} in ${s.expressions.map((e) => this.expression(e, depth)).join(", ") + } do${body ? `\n${body}` : ""}\n${ind}end`; + } + case "Function": { + const name = s.name.propNames.join(".") + + (s.name.colonName ? `:${s.name.colonName}` : ""); + return `${ind}function ${name}` + + this.functionRest(s.body.parameters, s.body.block, depth); + } + case "LocalFunction": + return `${ind}local function ${s.name}` + + this.functionRest(s.body.parameters, s.body.block, depth); + default: + throw new Error( + `pretty_print: unsupported statement type: ${(s as any).type}`, + ); + } + } +} + +export function prettyPrintExpression( + expr: LuaExpression, + opts?: PrintOptions, +): string { + return new Printer(resolveOptions(opts)).expression(expr, 0); +} + +export function prettyPrintBlock( + block: LuaBlock, + opts?: PrintOptions, +): string { + return new Printer(resolveOptions(opts)).block(block, 0); +} diff --git a/client/space_lua/rp.bench.ts b/client/space_lua/rp.bench.ts index ced808a2..357fe79c 100644 --- a/client/space_lua/rp.bench.ts +++ b/client/space_lua/rp.bench.ts @@ -27,7 +27,7 @@ import { LuaTable, luaTypeOf, } from "./runtime.ts"; -import { parse as parseLua } from "./parse.ts"; +import { parseBlock as parseLua } from "./parse.ts"; const LOOP = 100000; const SMALL = 20000; diff --git a/client/space_lua/stdlib.ts b/client/space_lua/stdlib.ts index 5692ac59..450d2bfc 100644 --- a/client/space_lua/stdlib.ts +++ b/client/space_lua/stdlib.ts @@ -26,7 +26,7 @@ import { osApi } from "./stdlib/os.ts"; import { jsApi } from "./stdlib/js.ts"; import { spaceluaApi } from "./stdlib/space_lua.ts"; import { mathApi } from "./stdlib/math.ts"; -import { parse } from "./parse.ts"; +import { parseBlock } from "./parse.ts"; import { evalStatement } from "./eval.ts"; import { encodingApi } from "./stdlib/encoding.ts"; import { luaToNumberDetailed } from "./tonumber.ts"; @@ -350,7 +350,7 @@ const dofileFunction = new LuaBuiltinFunction(async (sf, filename: string) => { )) as Uint8Array; const code = new TextDecoder().decode(file); try { - const parsedExpr = parse(code); + const parsedExpr = parseBlock(code); const env = new LuaEnv(global); await evalStatement(parsedExpr, env, sf.withCtx(parsedExpr.ctx)); } catch (e: any) { diff --git a/client/space_lua/stdlib/load.ts b/client/space_lua/stdlib/load.ts index 6c84ecef..2f07850f 100644 --- a/client/space_lua/stdlib/load.ts +++ b/client/space_lua/stdlib/load.ts @@ -5,7 +5,7 @@ import { type LuaStackFrame, type LuaValue, } from "../runtime.ts"; -import { parse } from "../parse.ts"; +import { parseBlock } from "../parse.ts"; import { evalStatement } from "../eval.ts"; // Returns a function (callable chunk) or (nil, "error message") pair. @@ -13,7 +13,7 @@ export function luaLoad(code: LuaValue, sf: LuaStackFrame): LuaValue { const s = typeof code === "string" ? code : String(code); try { - const block = parse(s, sf.astCtx || {}); + const block = parseBlock(s, sf.astCtx || {}); const globalEnvMaybe = sf.threadLocal.get("_GLOBAL"); // Be vocal when no _GLOBAL is set diff --git a/client/space_lua/stdlib/pretty_print_test.lua b/client/space_lua/stdlib/pretty_print_test.lua new file mode 100644 index 00000000..7ea2ac76 --- /dev/null +++ b/client/space_lua/stdlib/pretty_print_test.lua @@ -0,0 +1,25 @@ +local function assertEquals(actual, expected, message) + if actual ~= expected then + error('Assertion failed: ' .. message .. ' (got: ' .. tostring(actual) .. ')') + end +end + +-- expression printing +local expr = spacelua.parseExpression("1+2") +assertEquals(spacelua.prettyPrintExpression(expr), "1 + 2", "prettyPrintExpression 1+2") + +-- block printing (round-trip through parse) +local block = spacelua.parseBlock("local x=1\nlocal y=2") +assertEquals(spacelua.prettyPrintBlock(block), "local x = 1\nlocal y = 2", "prettyPrintBlock locals") + +-- options: indent width +local block2 = spacelua.parseBlock("if a then return 1 end") +assertEquals( + spacelua.prettyPrintBlock(block2, { indentWidth = 4 }), + "if a then\n return 1\nend", + "prettyPrintBlock indentWidth=4" +) + +-- options: single quotes +local s = spacelua.parseExpression('"hi"') +assertEquals(spacelua.prettyPrintExpression(s, { quote = "single" }), "'hi'", "single quotes") diff --git a/client/space_lua/stdlib/space_lua.ts b/client/space_lua/stdlib/space_lua.ts index 2df7faa0..1af3d079 100644 --- a/client/space_lua/stdlib/space_lua.ts +++ b/client/space_lua/stdlib/space_lua.ts @@ -1,6 +1,11 @@ -import { parseExpressionString } from "../parse.ts"; -import type { LuaExpression } from "../ast.ts"; +import { parseBlock, parseExpressionString } from "../parse.ts"; +import type { LuaBlock, LuaExpression } from "../ast.ts"; import { evalExpression } from "../eval.ts"; +import { + type PrintOptions, + prettyPrintBlock, + prettyPrintExpression, +} from "../pretty_print.ts"; import { jsToLuaValue, LuaBuiltinFunction, @@ -101,6 +106,25 @@ export async function interpolateLuaString( return result; } +/** + * Converts an optional Lua options table into a `PrintOptions` object, + * keeping only the recognised keys with the expected types. + */ +function toPrintOptions( + sf: LuaStackFrame, + opts?: LuaTable, +): PrintOptions | undefined { + if (!opts) return undefined; + const js = luaValueToJS(opts, sf) as Record; + const result: PrintOptions = {}; + if (typeof js.indentWidth === "number") result.indentWidth = js.indentWidth; + if (js.quote === "double" || js.quote === "single") result.quote = js.quote; + if (typeof js.trailingComma === "boolean") { + result.trailingComma = js.trailingComma; + } + return result; +} + export const spaceluaApi = new LuaTable({ /** * Parses a lua expression and returns the parsed expression. @@ -112,6 +136,42 @@ export const spaceluaApi = new LuaTable({ parseExpression: new LuaBuiltinFunction((_sf, luaExpression: string) => { return parseExpressionString(luaExpression); }), + /** + * Parses a lua chunk (block) and returns the parsed AST block. + * + * @param sf - The current space_lua state. + * @param code - The lua code to parse. + * @returns The parsed block. + */ + parseBlock: new LuaBuiltinFunction((_sf, code: string): LuaBlock => { + return parseBlock(code); + }), + /** + * Pretty-prints a parsed lua block AST back to formatted source. + * + * @param sf - The current space_lua state. + * @param block - The parsed lua block. + * @param opts - Optional formatting options. + * @returns The formatted lua source. + */ + prettyPrintBlock: new LuaBuiltinFunction( + (sf, block: LuaBlock, opts?: LuaTable): string => { + return prettyPrintBlock(block, toPrintOptions(sf, opts)); + }, + ), + /** + * Pretty-prints a parsed lua expression AST back to formatted source. + * + * @param sf - The current space_lua state. + * @param expr - The parsed lua expression. + * @param opts - Optional formatting options. + * @returns The formatted lua source. + */ + prettyPrintExpression: new LuaBuiltinFunction( + (sf, expr: LuaExpression, opts?: LuaTable): string => { + return prettyPrintExpression(expr, toPrintOptions(sf, opts)); + }, + ), /** * Evaluates a parsed lua expression and returns the result. * diff --git a/plug-api/syscalls/lua.ts b/plug-api/syscalls/lua.ts index f8029221..7ca75aec 100644 --- a/plug-api/syscalls/lua.ts +++ b/plug-api/syscalls/lua.ts @@ -1,8 +1,14 @@ import { syscall } from "../syscall.ts"; import type { LuaBlock, LuaExpression } from "../../client/space_lua/ast.ts"; +import type { PrintOptions } from "../../client/space_lua/pretty_print.ts"; +export function parseBlock(code: string): Promise { + return syscall("lua.parseBlock", code); +} + +/** @deprecated use {@link parseBlock} instead */ export function parse(code: string): Promise { - return syscall("lua.parse", code); + return syscall("lua.parseBlock", code); } export function parseExpression(expression: string): Promise { @@ -12,3 +18,17 @@ export function parseExpression(expression: string): Promise { export function evalExpression(expression: string): Promise { return syscall("lua.evalExpression", expression); } + +export function prettyPrintBlock( + block: LuaBlock, + opts?: PrintOptions, +): Promise { + return syscall("lua.prettyPrintBlock", block, opts); +} + +export function prettyPrintExpression( + expression: LuaExpression, + opts?: PrintOptions, +): Promise { + return syscall("lua.prettyPrintExpression", expression, opts); +} diff --git a/plugs/index/lint.ts b/plugs/index/lint.ts index 0985342a..267a9723 100644 --- a/plugs/index/lint.ts +++ b/plugs/index/lint.ts @@ -139,7 +139,7 @@ export async function lintLua({ tree }: LintEvent): Promise { } const luaCode = renderToText(codeText); try { - await lua.parse(luaCode); + await lua.parseBlock(luaCode); } catch (e: any) { const offset = codeText.from!; let from = codeText.from!; diff --git a/website/API/lua.md b/website/API/lua.md index fba40c08..6820303b 100644 --- a/website/API/lua.md +++ b/website/API/lua.md @@ -2,8 +2,8 @@ The Lua API provides functions for parsing and evaluating Lua code. -### lua.parse(code) -Parses a string of Lua code into an abstract syntax tree (AST). +### lua.parseBlock(code) +Parses a string of Lua code (a block of statements) into an abstract syntax tree (AST). Parameters: - `code`: The Lua code to parse @@ -12,10 +12,13 @@ Returns a LuaBlock object representing the parsed code. Example: ```lua -local ast = lua.parse("print('Hello')") +local ast = lua.parseBlock("print('Hello')") -- ast contains the parsed syntax tree ``` +> **note** Note +> `lua.parse` is a deprecated alias for `lua.parseBlock` and is kept for backwards compatibility. + ### lua.parseExpression(expression) Parses a Lua expression into an abstract syntax tree (AST). @@ -42,4 +45,22 @@ Example: ```lua local result = lua.evalExpression("1 + 2 * 3") print(result) -- prints: 7 -``` \ No newline at end of file +``` + +### lua.prettyPrintBlock(block, options?) +Pretty-prints a parsed Lua block AST (as returned by [[#lua.parseBlock(code)]]) back to formatted Lua source. + +The optional `options` table accepts `indentWidth` (number, default `2`), `quote` (`"double"` or `"single"`, default `"double"`), and `trailingComma` (boolean, default `true`). Comments are not preserved. + +Example: +```lua +local formatted = lua.prettyPrintBlock(lua.parseBlock("if a then return 1 end")) +``` + +### lua.prettyPrintExpression(expression, options?) +Pretty-prints a parsed Lua expression AST (as returned by [[#lua.parseExpression(expression)]]) back to formatted Lua source. Accepts the same `options` table as [[#lua.prettyPrintBlock(block, options?)]]. + +Example: +```lua +local formatted = lua.prettyPrintExpression(lua.parseExpression("{a=1,b=2}")) +``` \ No newline at end of file diff --git a/website/API/spacelua.md b/website/API/spacelua.md index 60c6a8e3..5bb2d3a9 100644 --- a/website/API/spacelua.md +++ b/website/API/spacelua.md @@ -10,6 +10,49 @@ Example: local parsedExpression = spacelua.parseExpression("1 + 1") ``` +## spacelua.parseBlock(code) +Parses a Lua chunk (a block of statements) and returns the parsed block as an AST. + +Example: +```lua +local parsedBlock = spacelua.parseBlock("local x = 1\nreturn x + 2") +``` + +## spacelua.prettyPrintExpression(parsedExpr, options?) +Pretty-prints a parsed Lua expression AST back to formatted Lua source. + +The optional `options` table accepts: +* `indentWidth` (number, default `2`): number of spaces per indentation level. +* `quote` (`"double"` or `"single"`, default `"double"`): quote style for strings. +* `trailingComma` (boolean, default `true`): whether multi-line tables get a trailing comma. + +> **note** Comments +> The parser does not retain comments, so pretty-printing a parsed AST does not preserve any comments from the original source. + +Example: +```lua +local parsedExpr = spacelua.parseExpression("{a=1,b=2}") +print(spacelua.prettyPrintExpression(parsedExpr)) +-- prints: +-- { +-- a = 1, +-- b = 2, +-- } +``` + +## spacelua.prettyPrintBlock(parsedBlock, options?) +Pretty-prints a parsed Lua block AST back to formatted Lua source. Accepts the same `options` table as [[#spacelua.prettyPrintExpression(parsedExpr, options?)]]. + +Reformatting a chunk of Lua source is a parse followed by a pretty-print: +```lua +local formatted = spacelua.prettyPrintBlock(spacelua.parseBlock("if a then return 1 end")) +print(formatted) +-- prints: +-- if a then +-- return 1 +-- end +``` + ## spacelua.evalExpression(parsedExpr, envAugmentation?) Evaluates a parsed Lua expression and returns the result. Optionally accepts an environment table to augment the global environment. diff --git a/website/CHANGELOG.md b/website/CHANGELOG.md index 766e98cf..e4e62603 100644 --- a/website/CHANGELOG.md +++ b/website/CHANGELOG.md @@ -3,6 +3,7 @@ An attempt at documenting the changes/new features introduced in each release. ## Edge Whenever a commit is pushed to the `main` branch, within ~5 minutes, it will be released as a docker image with the `:v2` tag, and a binary in the [edge release](https://github.com/silverbulletmd/silverbullet/releases/tag/edge). If you want to live on the bleeding edge of SilverBullet goodness (or regression) this is where to do it. +* Space Lua: added `spacelua.prettyPrintBlock` / `spacelua.prettyPrintExpression` to pretty-print a parsed Lua AST back to formatted source. Supports `indentWidth`, `quote` and `trailingComma` options. Comments are not preserved. * New [[Object/relation]] indexed object capturing generalized object-to-object relationships: typed edges from frontmatter, inline `[key: value]` attributes, and `#tag` fenced data blocks; untyped mentions; and co-mention edges between refs co-occurring in the same item, nested item, or paragraph. The (now) legacy `link` is reimplemented as a virtual collection on top of `relation` now and should keep acting as before. * Picker fuzzy search: replaced Fuse.js with a custom scorer that supports multi-token queries, path-aware ranking, and some typo tolerance. * UX: on narrow viewports (<800px) header `#` markers no longer get pushed off-screen when the cursor enters a heading