lmok guide

Incremental tree-sitter parsing, JSON and lmcode rule dispatch, SQLite knowledge indexing, and standard lmcode_std_v3 REST and CLI interfaces in lmok.

Overview

lmok combines tree-sitter 0.27.1 (with 12 compiled grammars), SQLite 3.54.1 (with FTS5 trigram full-text search), and the C++ lmcode virtual machine in a single binary. It supports both declarative JSON rule packs (18 bundled packs with 126 rules) and programmatic lmcode CST rules (16 bundled rule programs).

Embedded grammars and LMScript injection

lmok ships grammars for json, c, cpp, python, bash, java, go, javascript, typescript, tsx, lmscript, and lmcode. When analyzing .lms files, lmok parses the outer LMScript grammar and automatically extracts embedded _lmcode_ bodies, parsing them with the lmcode grammar while mapping their row, column, and byte offsets back to the enclosing .lms file.

Tier-1 JSON packs and Tier-2 lmcode @NODE_KIND rules

Rule authoring is split into two tiers:

p = _lmcode_ cli="lmok";
@call_expression
fn check_gets(n: Node) -> void implements Rule {
  if (n.child_count() > 0 && n.child(0).text() == "gets") {
    ts.report(n, "unsafe call to gets", "LBC001", "critical");
  }
}
end _lmcode_

Incremental reparsing and the SQLite knowledge tree

Inside _lmcode_ scripts, Tree.edit(start_byte, old_end_byte, new_end_byte) records a byte edit on a parsed tree, ts.reparse(old_tree, new_text) (and language-specific ts.reparse_c, ts.reparse_cpp, ts.reparse_python, ts.reparse_bash, ts.reparse_java) incrementally re-parses, and ts.changed_ranges(old_tree, new_tree) returns the modified syntax ranges as JSON.

On the CLI, lmok index extracts definitions, candidate references, and include/import edges using embedded queries/<lang>/facts.scm files and stores them alongside an FTS5 trigram source index in SQLite. Stored facts can be queried from the CLI (symbols, refs, find, findings, outline, status) or from _lmcode_ rules via ts.db_files(pattern), ts.db_symbols(pattern), and ts.db_refs(name) for cross-file checks.

REST server (lmok serve) and standard lmcode_std_v3 interfaces

Running lmok serve exposes every CLI analysis verb over a local HTTP/1.1 JSON API with configurable presets, an input-hash result cache, and an lmok remote client. In addition, the embedded lmcode VM registers all 226 canonical lmcode_std_v3 signatures, including RestGet..RestOptions, RestHandler, CliCommand, CliOption, :param and {param} route templates, rest_config, rest_route, rest_serve, rest_stop, and rest_cli. This describes current main; a released host only has these if its pinned std catalog does, so check lmok --lmcode-signatures on the installed binary.

p = _lmcode_ cli="lmok";
@"/parse/:lang"
fn handle_parse(x: RestExchange) -> void implements RestGet {
  let t = ts.parse("{\"ok\":true}");
  x.json("{\"lang\":\"" + x.param("lang") + "\",\"nodes\":" + str(t.node_count()) + "}");
}

print(rest_cli(str_split("/parse/json", " ")));
end _lmcode_