Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced

Source of release 0.4.1

0.4.1 / src / language / grammars.luc

## A set of TextMate grammars loaded from a directory, indexed by the file extensions they claim.
## A missing directory or an unreadable or malformed grammar is skipped, so highlighting simply
## falls back rather than failing to open the editor.
from luce_textmate.textmate import Grammar
from luce_std import files
import os
import workspace.paths as paths

## A lazily-compiled set of TextMate grammars. At startup only a small `index.json` manifest
## (extension -> file) is read; a grammar's patterns are compiled the first time a document of that
## type is opened, then cached. Reading a small manifest keeps launch instant — scanning every
## grammar for its file types would be far too slow, since a Luce string is scalar-indexed and the
## bundled grammars are 200 KB single lines.
pub class GrammarSet:
    var dir: str
    var ext_to_file: map[str, str]
    var cache: map[str, Grammar]

    pub func init(self, directory: str) -> unit:
        self.dir = directory
        self.ext_to_file = {}
        self.cache = {}
        let text = files.read_text(paths.join(directory, "index.json"), 4000000) catch failure:
            return
        self.ext_to_file = manifest_pairs(text)

    ## Load and cache the grammar in `filename`, or none if it is missing or malformed.
    func load_file(self, filename: str) -> Grammar?:
        if let cached = self.cache[filename]:
            return cached
        let text = files.read_text(paths.join(self.dir, filename), 16000000) catch failure:
            return none
        let grammar = Grammar(text) catch failure:
            return none
        self.cache[filename] = grammar
        return grammar

    ## The grammar for a file path's extension (compiled on first use), or none.
    pub func grammar_for(self, path: str) -> Grammar?:
        let ext = extension_of(path)
        if ext == "":
            return none
        let filename = self.ext_to_file[ext] else return none
        return self.load_file(filename)

    ## Load the grammar in `filename` directly (when the user picks a highlighter by hand), or none.
    pub func grammar_named(self, filename: str) -> Grammar?:
        return self.load_file(filename)

    ## The distinct grammar filenames the set knows, so a menu can offer them.
    pub func languages(self) -> list[str]:
        var seen: map[str, bool] = {}
        var names: list[str] = []
        for ext in self.ext_to_file.keys():
            let file = self.ext_to_file[ext] else ""
            if file != "" and not (seen[file] else false):
                seen[file] = true
                names.append(file)
        return names

    pub func count(self) -> int:
        return self.ext_to_file.length

## Copy each grammar bundled beside the binary into `config_grammars`, overwriting so that fixes,
## a refreshed manifest and newly added grammars reach an existing config. The bundle sits near the
## executable; a few conventional locations are tried and the first with grammars wins. A user's own
## grammar files (names the bundle does not have) are left untouched.
pub func install_bundled(config_grammars: str) -> unit:
    let exe = os.executable() catch failure:
        return
    let exe_dir = paths.parent(exe)
    let parent = paths.parent(exe_dir)
    let candidates = [paths.join(exe_dir, "grammars"), paths.join(parent, "grammars"), paths.join(parent, "share/luced/grammars")]
    for candidate in candidates:
        if copy_grammars(candidate, config_grammars):
            return

func copy_grammars(source: str, target: str) -> bool:
    let names = files.list(source) catch failure:
        return false
    var found = false
    for name in names:
        if not name.ends_with(".json"):
            continue
        found = true
        let text = files.read_text(paths.join(source, name), 16000000) catch failure: continue
        files.write_text(paths.join(target, name), text) catch failure: continue
    return found

## Parse the flat `index.json` manifest (an object of `"key":"value"` string pairs, possibly under
## `"extensions"`/`"scopes"` sub-objects) into a map. Both extension and scope entries map to a
## grammar filename; only string values are captured, so the container keys are skipped. The file is
## a few KB, so scanning it by scalar index is fine.
func manifest_pairs(text: str) -> map[str, str]:
    var out: map[str, str] = {}
    let n = text.length
    var i = 0
    while i < n:
        if text[i..<i + 1] != "\"":
            i += 1
            continue
        let key_end = find_quote_end(text, i + 1, n)
        if key_end < 0:
            break
        let key = text[i + 1..<key_end]
        i = key_end + 1
        while i < n and is_ws(text[i..<i + 1]):
            i += 1
        if i >= n or text[i..<i + 1] != ":":
            continue
        i += 1
        while i < n and is_ws(text[i..<i + 1]):
            i += 1
        if i >= n:
            break
        if text[i..<i + 1] != "\"":
            continue
        let value_end = find_quote_end(text, i + 1, n)
        if value_end < 0:
            break
        out[key] = text[i + 1..<value_end]
        i = value_end + 1
    return out

func find_quote_end(text: str, start: int, n: int) -> int:
    var i = start
    while i < n:
        if text[i..<i + 1] == "\"":
            return i
        i += 1
    return -1

func is_ws(c: str) -> bool:
    return c == " " or c == "\t" or c == "\n" or c == "\r"

## The extension of a path without its dot ("src/main.rs" -> "rs"), or "" when it has none.
func extension_of(path: str) -> str:
    let base = path.replace("\\", "/").split("/").last else path
    let parts = base.split(".")
    if parts.length < 2:
        return ""
    return parts.last else ""