silverbullet/common/space_lua/parse.test.ts

101 lines
2.5 KiB
TypeScript
Raw Normal View History

2024-09-12 03:17:56 +08:00
import { parse } from "$common/space_lua/parse.ts";
2024-09-29 21:09:13 +08:00
2024-09-12 03:17:56 +08:00
Deno.test("Test Lua parser", () => {
2024-09-30 18:50:54 +08:00
// Basic block test
parse(`
2024-09-12 03:17:56 +08:00
print("Hello, World!")
print(10)
`);
2024-09-30 18:50:54 +08:00
parse("");
// Expression tests
parse(
2024-10-04 00:40:06 +08:00
`e(1, 1.2, -3.8, +4, #lst, true, false, nil, "string", "", "Hello there \x00", ...)`,
2024-09-30 18:50:54 +08:00
);
2024-10-07 15:08:36 +08:00
parse(`e([[hel]lo]], "Grinny face\\u{1F600}")`);
2024-09-24 16:15:22 +08:00
2024-09-30 18:50:54 +08:00
parse(`e(10 << 10, 10 >> 10, 10 & 10, 10 | 10, 10 ~ 10)`);
2024-09-12 03:17:56 +08:00
2024-09-30 18:50:54 +08:00
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))`);
2024-09-12 03:17:56 +08:00
2024-09-30 18:50:54 +08:00
// 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)`);
2024-09-12 03:17:56 +08:00
2024-09-30 18:50:54 +08:00
// Function calls
parse(`e(func(), func(1, 2, 3), a.b(), a.b.c:hello(), (a.b)(7))`);
2024-09-12 03:17:56 +08:00
2024-09-30 18:50:54 +08:00
// Function expression
parse(`e(function(a, b) test() end)`);
parse(`e(function(a, b, ...) end)`);
2024-09-12 19:40:43 +08:00
2024-09-30 18:50:54 +08:00
// Statements
parse(`do end`);
parse(`do print() end`);
parse(`::hello::
2024-09-29 21:09:13 +08:00
goto hello`);
2024-09-30 18:50:54 +08:00
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`);
2024-09-12 19:40:43 +08:00
2024-09-30 18:50:54 +08:00
// 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`);
2024-09-12 19:40:43 +08:00
2024-09-30 18:50:54 +08:00
// 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`);
2024-09-12 19:40:43 +08:00
2024-09-30 18:50:54 +08:00
// 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<const>`);
parse(`local a = 1`);
parse(`local a<const> = 4`);
parse(`local a, b = 1, 2`);
2024-09-12 19:40:43 +08:00
2024-09-30 18:50:54 +08:00
// Function calls
parse(`a(1, 2, 3)`);
parse(`print "Sup"`);
parse(`e(1 + print "8")`);
2024-09-27 23:09:25 +08:00
2024-09-30 18:50:54 +08:00
// Return statements
parse(`return`);
parse(`return 1`);
parse(`return 1, 2, 3`);
});
Deno.test("Test comment handling", () => {
parse(`
-- Single line comment
--[[ Multi
line
comment ]]
2024-10-07 15:08:36 +08:00
f([[
hello
-- yo
]])`);
2024-09-12 03:17:56 +08:00
});