Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced

Source of release 0.4.1

0.4.1 / tools / grammars.py

#!/usr/bin/env python3
"""Embed our small grammars into a Luce module (a fallback baseline seeded on first run) and
write index.json (extension/scope -> grammar file) so the editor can load grammars lazily.
Re-run after adding or changing a grammar."""
import json
import pathlib

ROOT = pathlib.Path(__file__).resolve().parents[1]
GRAMMARS = ROOT / "grammars"
OUT = ROOT / "src/language/default_grammars.luc"


def escape(text):
    out = []
    for ch in text:
        code = ord(ch)
        if ch == "\\":
            out.append("\\\\")
        elif ch == "\"":
            out.append("\\\"")
        elif ch == "\n":
            out.append("\\n")
        elif ch == "\r":
            out.append("\\r")
        elif ch == "\t":
            out.append("\\t")
        elif code < 32:
            out.append("\\u{%x}" % code)
        else:
            out.append(ch)
    return "".join(out)


# Only our own small grammars are embedded, as a fallback baseline when the bundled grammars
# directory beside the binary is not found. The larger vendored community grammars ship as files
# and are copied from that bundle into the config directory at startup (see language.grammars).
OWN = ["luce.tmLanguage.json", "prism.tmLanguage.json"]


def main():
    files = [GRAMMARS / name for name in OWN if (GRAMMARS / name).exists()]
    lines = [
        "## Generated by tools/grammars.py — do not edit. The grammars shipped with luced,",
        "## written into the config directory on first run so highlighting works out of the box.",
        "from luce_std import files",
        "import workspace.paths as paths",
        "",
        "func write_default(directory: str, name: str, content: str) -> unit!:",
        "    files.create_text(paths.join(directory, name), content) catch failure:",
        "        if failure.code != files.already_exists:",
        "            error(failure.code, failure.message)",
        "",
        "## Write each bundled grammar into `directory`, keeping any file the user already has.",
        "pub func seed(directory: str) -> unit!:",
    ]
    for path in files:
        lines.append("    write_default(directory, \"%s\", \"%s\")" % (path.name, escape(path.read_text())))
    fallback = {"extensions": {}, "scopes": {}}
    for path in files:
        data = json.loads(path.read_text())
        if data.get("scopeName"):
            fallback["scopes"][data["scopeName"]] = path.name
        for ext in data.get("fileTypes", []):
            fallback["extensions"][ext] = path.name
    lines.append("    write_default(directory, \"index.json\", \"%s\")" %
                 escape(json.dumps(fallback, separators=(",", ":")) + "\n"))
    if not files:
        lines.append("    return")
    OUT.write_text("\n".join(lines) + "\n")
    print("wrote %s (%d embedded grammars)" % (OUT, len(files)))

    # The manifest: which grammar file serves each extension and each scope, so the editor loads
    # and compiles a grammar only when a file of that type is opened.
    index = {"extensions": {}, "scopes": {}}
    for path in sorted(GRAMMARS.glob("*.tmLanguage.json")):
        data = json.loads(path.read_text())
        scope = data.get("scopeName", "")
        if scope:
            index["scopes"][scope] = path.name
        for ext in data.get("fileTypes", []):
            index["extensions"][ext] = path.name
    (GRAMMARS / "index.json").write_text(json.dumps(index, separators=(",", ":")) + "\n")
    print("wrote index.json (%d extensions, %d scopes)" % (len(index["extensions"]), len(index["scopes"])))


if __name__ == "__main__":
    main()