chore: lsp + format

This commit is contained in:
2026-08-04 10:21:18 -04:00
parent fa35c3c326
commit 4533c7f61f
30 changed files with 1151 additions and 678 deletions
+21 -1
View File
@@ -35,6 +35,7 @@ DOC = re.compile(r"^\s*// ---\s?(?P<text>.*)$")
PARAM = re.compile(r"^\s*// @param (?P<name>\w+) (?P<type>\S+)(?: (?P<desc>.*))?$")
RETURN = re.compile(r"^\s*// @return (?P<type>\S+)(?: (?P<desc>.*))?$")
TAG = re.compile(r"^\s*// @(?P<tag>[\w-]+)")
CONTINUATION = re.compile(r"^\s*// (?P<text>(?!@|---)\S.*)$")
TYPES = {"int": "integer", "bool": "boolean"}
@@ -65,6 +66,7 @@ def parse(path):
current = None
doc = {"doc": [], "params": [], "returns": []}
in_table = False
continuation = None
def reset():
return {"doc": [], "params": [], "returns": []}
@@ -73,6 +75,18 @@ def parse(path):
stripped = line.strip()
where = f"{path.relative_to(ROOT)}:{number}"
wrapped = CONTINUATION.match(line)
if continuation and wrapped:
items, index, field = continuation
if field is None:
items[index] += " " + wrapped.group("text")
else:
item = list(items[index])
item[field] += " " + wrapped.group("text")
items[index] = tuple(item)
continue
continuation = None
module = MODULE.match(stripped)
if module:
pending_module = new_module(module.group("kind"), module.group("name"), module.group("class_"))
@@ -104,12 +118,15 @@ def parse(path):
target["consts"].append(
(const.group("name"), lua_type(const.group("type")), const.group("value"), const.group("desc") or "")
)
continuation = (target["consts"], len(target["consts"]) - 1, 3)
continue
fix = FIX.match(stripped)
if fix:
if not target:
raise SystemExit(f"{where}: @lua-{fix.group('where')} outside a module")
target[fix.group("where")].append(fix.group("text"))
items = target[fix.group("where")]
items.append(fix.group("text"))
continuation = (items, len(items) - 1, None)
continue
table = TABLE.match(line)
@@ -128,14 +145,17 @@ def parse(path):
match = DOC.match(line)
if match:
doc["doc"].append(match.group("text").strip())
continuation = (doc["doc"], len(doc["doc"]) - 1, None)
continue
match = PARAM.match(line)
if match:
doc["params"].append((match.group("name"), lua_type(match.group("type")), match.group("desc") or ""))
continuation = (doc["params"], len(doc["params"]) - 1, 2)
continue
match = RETURN.match(line)
if match:
doc["returns"].append((lua_type(match.group("type")), match.group("desc") or ""))
continuation = (doc["returns"], len(doc["returns"]) - 1, 1)
continue
match = TAG.match(line)
if match:
+33
View File
@@ -0,0 +1,33 @@
#!/usr/bin/env python3
import tempfile
from pathlib import Path
from gen_api import ROOT, parse
with tempfile.TemporaryDirectory(dir=ROOT) as directory:
source = Path(directory) / "wrapped.cpp"
source.write_text(
"""// @lua-module fs FsLib
// @lua-const MAX_READ_BYTES integer 65536 Largest portable
// whole-file read.
const luaL_Reg FUNCTIONS[] = {
// --- Reads one complete
// file.
// @param path string Absolute file
// path.
// @return string|nil File
// contents.
{"readFile", readFile},
};
"""
)
module = parse(source)[0]
assert module["consts"][0][3] == "Largest portable whole-file read."
doc = module["functions"][0][1]
assert doc["doc"] == ["Reads one complete file."]
assert doc["params"][0][2] == "Absolute file path."
assert doc["returns"][0][1] == "File contents."
print("ok")