Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

Extract definitions for 16 previously-Module-only grammars and Kotlin imports

Martin Vogel0366b3c04d74
diff --git a/internal/cbm/extract_defs.c b/internal/cbm/extract_defs.c
index 45d8887f5..3c5cb7160 100644
--- a/internal/cbm/extract_defs.c
+++ b/internal/cbm/extract_defs.c
@@ -582,7 +582,8 @@ static TSNode resolve_func_name_c_family(TSNode *node_ptr, CBMLanguage lang, con
return null_node;
}
if ((lang == CBM_LANG_C || lang == CBM_LANG_CPP || lang == CBM_LANG_CUDA ||
- lang == CBM_LANG_GLSL) &&
+ lang == CBM_LANG_GLSL || lang == CBM_LANG_HLSL || lang == CBM_LANG_ISPC ||
+ lang == CBM_LANG_SLANG) &&
strcmp(kind, "function_definition") == 0) {
return resolve_c_declarator_name(*node_ptr);
}
@@ -631,6 +632,93 @@ static TSNode resolve_func_name(TSNode node, CBMLanguage lang) {
}
}
+ /* Cairo / D / Odin / Squirrel: the def node has no `name` field; the name
+ * is a plain `identifier` child (same shape as the Swift/Kotlin case). */
+ if ((lang == CBM_LANG_CAIRO || lang == CBM_LANG_DLANG || lang == CBM_LANG_ODIN ||
+ lang == CBM_LANG_SQUIRREL) &&
+ (strcmp(kind, "function_definition") == 0 || strcmp(kind, "function_signature") == 0 ||
+ strcmp(kind, "function_declaration") == 0 ||
+ strcmp(kind, "procedure_declaration") == 0)) {
+ TSNode id = cbm_find_child_by_kind(node, "identifier");
+ if (!ts_node_is_null(id)) {
+ return id;
+ }
+ }
+
+ /* Ada: subprogram_body/_declaration carry the `name` field on a nested
+ * procedure_specification/function_specification child, not on themselves. */
+ if (lang == CBM_LANG_ADA &&
+ (strcmp(kind, "subprogram_body") == 0 || strcmp(kind, "subprogram_declaration") == 0)) {
+ TSNode spec = cbm_find_child_by_kind(node, "procedure_specification");
+ if (ts_node_is_null(spec)) {
+ spec = cbm_find_child_by_kind(node, "function_specification");
+ }
+ if (!ts_node_is_null(spec)) {
+ TSNode nm = ts_node_child_by_field_name(spec, TS_FIELD("name"));
+ if (!ts_node_is_null(nm)) {
+ return nm;
+ }
+ }
+ }
+
+ /* Pascal: defProc carries the `name` field on its `header` (declProc) child. */
+ if (lang == CBM_LANG_PASCAL && strcmp(kind, "defProc") == 0) {
+ TSNode hdr = ts_node_child_by_field_name(node, TS_FIELD("header"));
+ if (!ts_node_is_null(hdr)) {
+ TSNode nm = ts_node_child_by_field_name(hdr, TS_FIELD("name"));
+ if (!ts_node_is_null(nm)) {
+ return nm;
+ }
+ }
+ }
+
+ /* ReScript: the `function` (arrow) node — already in func_types — has no
+ * name; the binding name is on the enclosing let_binding's `pattern` field.
+ * Resolving via the parent keeps plain value let-bindings out of func_types. */
+ if (lang == CBM_LANG_RESCRIPT && strcmp(kind, "function") == 0) {
+ TSNode parent = ts_node_parent(node);
+ if (!ts_node_is_null(parent) && strcmp(ts_node_type(parent), "let_binding") == 0) {
+ TSNode pat = ts_node_child_by_field_name(parent, TS_FIELD("pattern"));
+ if (!ts_node_is_null(pat)) {
+ return pat;
+ }
+ }
+ }
+
+ /* Fortran: subroutine/function wrap an inner *_statement that carries the
+ * `name` field; the outer node walk_defs matched has no name itself. */
+ if (lang == CBM_LANG_FORTRAN &&
+ (strcmp(kind, "subroutine") == 0 || strcmp(kind, "function") == 0)) {
+ TSNode stmt = cbm_find_child_by_kind(node, "subroutine_statement");
+ if (ts_node_is_null(stmt)) {
+ stmt = cbm_find_child_by_kind(node, "function_statement");
+ }
+ if (!ts_node_is_null(stmt)) {
+ TSNode nm = ts_node_child_by_field_name(stmt, TS_FIELD("name"));
+ if (!ts_node_is_null(nm)) {
+ return nm;
+ }
+ }
+ }
+
+ /* F#: function_or_value_defn's name is on a function_declaration_left /
+ * value_declaration_left child (a bare identifier, no `name` field). */
+ if (lang == CBM_LANG_FSHARP && strcmp(kind, "function_or_value_defn") == 0) {
+ TSNode lhs = cbm_find_child_by_kind(node, "function_declaration_left");
+ if (ts_node_is_null(lhs)) {
+ lhs = cbm_find_child_by_kind(node, "value_declaration_left");
+ }
+ if (!ts_node_is_null(lhs)) {
+ TSNode nm = cbm_find_child_by_kind(lhs, "identifier");
+ if (ts_node_is_null(nm)) {
+ nm = cbm_find_child_by_kind(lhs, "long_identifier");
+ }
+ if (!ts_node_is_null(nm)) {
+ return nm;
+ }
+ }
+ }
+
{
TSNode r = resolve_toplevel_arrow_name(node, kind);
if (!ts_node_is_null(r)) {
@@ -3827,6 +3915,66 @@ static void extract_c_macro_def(CBMExtractCtx *ctx, TSNode node) {
cbm_defs_push(&ctx->result->defs, a, def);
}
+// Clojure/Racket/Scheme: definitions are macro forms inside a generic `list`
+// node — these grammars have no dedicated def node. Detect a definition head
+// symbol and pull the name from the following form:
+// (defn foo [] ...) / (define (foo) ...) / (define foo ...) -> "foo".
+static bool lisp_is_def_head(const char *t) {
+ if (!t) {
+ return false;
+ }
+ static const char *heads[] = {
+ "defn", "defn-", "def", "defmacro",
+ "defmulti", "defmethod", "defprotocol", "defrecord",
+ "deftype", "definterface", "defonce", // Clojure
+ "define", "define-syntax", "define-values", "define-syntax-rule",
+ "define-struct", "define-record-type", "define/contract", // Scheme/Racket
+ NULL};
+ for (int i = 0; heads[i]; i++) {
+ if (strcmp(t, heads[i]) == 0) {
+ return true;
+ }
+ }
+ return false;
+}
+
+static void extract_lisp_def(CBMExtractCtx *ctx, TSNode node) {
+ CBMArena *a = ctx->arena;
+ if (ts_node_named_child_count(node) < 2) {
+ return;
+ }
+ char *head = cbm_node_text(a, ts_node_named_child(node, 0), ctx->source);
+ if (!lisp_is_def_head(head)) {
+ return;
+ }
+ TSNode target = ts_node_named_child(node, 1);
+ const char *tk = ts_node_type(target);
+ TSNode name_node = target;
+ // (define (foo args) ...) — the name is the head symbol of the nested list.
+ if ((strcmp(tk, "list") == 0 || strcmp(tk, "list_lit") == 0) &&
+ ts_node_named_child_count(target) > 0) {
+ name_node = ts_node_named_child(target, 0);
+ }
+ if (ts_node_is_null(name_node)) {
+ return;
+ }
+ char *name = cbm_node_text(a, name_node, ctx->source);
+ if (!name || !name[0]) {
+ return;
+ }
+ CBMDefinition def;
+ memset(&def, 0, sizeof(def));
+ def.name = name;
+ def.qualified_name = cbm_fqn_compute(a, ctx->project, ctx->rel_path, name);
+ def.label = "Function";
+ def.file_path = ctx->rel_path;
+ def.start_line = ts_node_start_point(node).row + TS_LINE_OFFSET;
+ def.end_line = ts_node_end_point(node).row + TS_LINE_OFFSET;
+ def.lines = (int)(def.end_line - def.start_line + TS_LINE_OFFSET);
+ def.is_exported = true;
+ cbm_defs_push(&ctx->result->defs, a, def);
+}
+
static void walk_defs(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec, int depth_unused) {
(void)depth_unused;
walk_defs_frame_t stack[CBM_WALK_DEFS_STACK_CAP];
@@ -3864,6 +4012,13 @@ static void walk_defs(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec,
// fall through: descend into the body for nested defines
}
+ if ((ctx->language == CBM_LANG_CLOJURE || ctx->language == CBM_LANG_RACKET ||
+ ctx->language == CBM_LANG_SCHEME) &&
+ (strcmp(kind, "list") == 0 || strcmp(kind, "list_lit") == 0)) {
+ extract_lisp_def(ctx, node);
+ // fall through: descend into children so nested defs are captured too
+ }
+
if (cbm_kind_in_set(node, spec->function_node_types)) {
if (!is_template_class_node(node, ctx->language)) {
extract_func_def(ctx, node, spec);
diff --git a/internal/cbm/extract_imports.c b/internal/cbm/extract_imports.c
index 2d9430fe2..249e9c350 100644
--- a/internal/cbm/extract_imports.c
+++ b/internal/cbm/extract_imports.c
@@ -29,6 +29,7 @@ static void parse_c_imports(CBMExtractCtx *ctx);
static void parse_ruby_imports(CBMExtractCtx *ctx);
static void parse_lua_imports(CBMExtractCtx *ctx);
static void parse_r_imports(CBMExtractCtx *ctx);
+static void parse_kotlin_imports(CBMExtractCtx *ctx);
static void parse_generic_imports(CBMExtractCtx *ctx, const char *node_type);
static void parse_wolfram_imports(CBMExtractCtx *ctx);
@@ -865,6 +866,42 @@ static void parse_generic_imports(CBMExtractCtx *ctx, const char *node_type) {
ts_tree_cursor_delete(&cursor);
}
+// --- Kotlin imports ---
+// tree-sitter-kotlin nests imports: source_file -> import_list -> import_header*.
+// parse_generic_imports only scans the DIRECT children of root, and "import" is
+// the keyword token (anon_sym_import), not a statement node — so a generic
+// match on "import" finds nothing. Descend into import_list (and accept a bare
+// import_header for grammar variants) and reuse the generic path extractors.
+static void extract_one_import_header(CBMExtractCtx *ctx, TSNode header) {
+ if (!try_generic_path_fields(ctx, header)) {
+ generic_import_from_text(ctx, header);
+ }
+}
+
+static void parse_kotlin_imports(CBMExtractCtx *ctx) {
+ TSTreeCursor cursor = ts_tree_cursor_new(ctx->root);
+ if (!ts_tree_cursor_goto_first_child(&cursor)) {
+ ts_tree_cursor_delete(&cursor);
+ return;
+ }
+ do {
+ TSNode node = ts_tree_cursor_current_node(&cursor);
+ const char *kind = ts_node_type(node);
+ if (strcmp(kind, "import_header") == 0) {
+ extract_one_import_header(ctx, node);
+ } else if (strcmp(kind, "import_list") == 0) {
+ uint32_t nc = ts_node_child_count(node);
+ for (uint32_t j = 0; j < nc; j++) {
+ TSNode child = ts_node_child(node, j);
+ if (strcmp(ts_node_type(child), "import_header") == 0) {
+ extract_one_import_header(ctx, child);
+ }
+ }
+ }
+ } while (ts_tree_cursor_goto_next_sibling(&cursor));
+ ts_tree_cursor_delete(&cursor);
+}
+
// --- Wolfram imports ---
// get_top: << "package" (Get["file"])
// apply where first child is builtin_symbol "Needs" with string arg
@@ -1046,7 +1083,7 @@ void cbm_extract_imports(CBMExtractCtx *ctx) {
parse_java_imports(ctx);
break;
case CBM_LANG_KOTLIN:
- parse_generic_imports(ctx, "import");
+ parse_kotlin_imports(ctx);
break;
case CBM_LANG_SCALA:
parse_generic_imports(ctx, "import_declaration");
diff --git a/internal/cbm/lang_specs.c b/internal/cbm/lang_specs.c
index 2043d65d7..4f5539c79 100644
--- a/internal/cbm/lang_specs.c
+++ b/internal/cbm/lang_specs.c
@@ -501,7 +501,9 @@ static const char *elixir_branch_types[] = {"call", NULL};
static const char *elixir_var_types[] = {"binary_operator", NULL};
// ==================== HASKELL ====================
-static const char *haskell_func_types[] = {"function", "signature", NULL};
+/* "bind" = a nullary value binding (`foo = 1`); has a `name` field like `function`.
+ * `signature` (type annotations) is suppressed in resolve_func_name so it never doubles. */
+static const char *haskell_func_types[] = {"function", "signature", "bind", NULL};
static const char *haskell_class_types[] = {"class", "data_type", "newtype", NULL};
static const char *haskell_module_types[] = {"haskell", NULL};
static const char *haskell_call_types[] = {"infix", "apply", NULL};
@@ -691,7 +693,12 @@ static const char *clojure_module_types[] = {"source", NULL};
static const char *clojure_call_types[] = {"list_lit", NULL};
// ==================== F# ====================
-static const char *fsharp_func_types[] = {"function_declaration", "value_declaration", NULL};
+/* Top-level `let f () = ...` parses to function_or_value_defn (module-level
+ * value_declaration is aliased to declaration_expression, which wraps it). The
+ * name lives on a function_declaration_left/value_declaration_left child — see
+ * the CBM_LANG_FSHARP branch in resolve_func_name. */
+static const char *fsharp_func_types[] = {"function_declaration", "value_declaration",
+ "function_or_value_defn", NULL};
static const char *fsharp_class_types[] = {"type_definition", "exception_definition", NULL};
static const char *fsharp_module_types[] = {"file", NULL};
static const char *fsharp_call_types[] = {"application_expression", "dot_expression", NULL};
@@ -748,7 +755,10 @@ static const char *elm_import_types[] = {"import", NULL};
static const char *elm_branch_types[] = {"case_of_expr", "if_else_expr", NULL};
// ==================== FORTRAN ====================
-static const char *fortran_func_types[] = {"function", "subroutine", "function_statement", NULL};
+/* subroutine/function wrap an inner *_statement that carries the `name` field;
+ * function_statement was already present, subroutine_statement was missing. */
+static const char *fortran_func_types[] = {"function", "subroutine", "function_statement",
+ "subroutine_statement", NULL};
static const char *fortran_class_types[] = {"derived_type_definition", "enum_statement", NULL};
static const char *fortran_module_types[] = {"translation_unit", NULL};
static const char *fortran_call_types[] = {"call_expression", "keyword_argument", "call", NULL};
diff --git a/internal/cbm/vendored/grammars/MANIFEST.md b/internal/cbm/vendored/grammars/MANIFEST.md
index f7f986854..c57c2d05a 100644
--- a/internal/cbm/vendored/grammars/MANIFEST.md
+++ b/internal/cbm/vendored/grammars/MANIFEST.md
@@ -16,6 +16,39 @@ The grammars were originally vendored as bare `parser.c`+`scanner.c` with **no r
> ⚠️ **Pinned commit = the revision nvim-treesitter/Helix vendor** (battle-tested, canonical source), not bleeding-edge HEAD. When re-vendoring, update the pinned commit here.
+## Custom extraction handling (definition extraction)
+
+The grammars below carry **custom definition-extraction support** in
+`internal/cbm/extract_defs.c` (and `internal/cbm/lang_specs.c`). Their function /
+definition nodes do **not** expose a `name` field that the generic extractor reads
+— the name lives on a nested/child/parent node, or (for the Lisp family) a
+definition is a macro form inside a generic `list` node with no dedicated def
+node. Without this handling these grammars produce only a file-level `Module`
+node and **zero functions/types**. A future grammar refresh that changes these
+node shapes must update the corresponding branch.
+
+Guarded by the `contract_all_grammars_in_graph` graph-breadth test in
+`tests/test_lang_contract.c` (each was reproduced as a failing case before the fix).
+
+| grammar | custom handling |
+|---|---|
+| ada | `resolve_func_name`: `subprogram_body`/`subprogram_declaration` → `procedure_specification`/`function_specification` child's `name` field |
+| cairo | `resolve_func_name`: `function_definition`/`function_signature` → `identifier` child |
+| clojure | `extract_lisp_def`: `(defn …)` / `(def …)` head-symbol forms in `list_lit` |
+| d | `resolve_func_name`: `function_declaration` → `identifier` child |
+| fortran | `resolve_func_name`: `subroutine`/`function` → inner `*_statement`'s `name` field |
+| fsharp | `func_types` += `function_or_value_defn`; `resolve_func_name` → `function_declaration_left`/`value_declaration_left` identifier |
+| haskell | `func_types` += `bind` (nullary value bindings; `signature` suppressed) |
+| hlsl | added to the C-family declarator-name gate (tree-sitter-cpp derivative) |
+| ispc | added to the C-family declarator-name gate (extends tree-sitter-c) |
+| odin | `resolve_func_name`: `procedure_declaration` → `identifier` child |
+| pascal | `resolve_func_name`: `defProc` → `header` (`declProc`) child's `name` field |
+| racket | `extract_lisp_def`: `(define …)` head-symbol forms in `list` |
+| rescript | `resolve_func_name`: `function` (arrow) → enclosing `let_binding`'s `pattern` field |
+| scheme | `extract_lisp_def`: `(define …)` head-symbol forms in `list` |
+| slang | added to the C-family declarator-name gate (tree-sitter-cpp/hlsl fork) |
+| squirrel | `resolve_func_name`: `function_declaration` → `identifier` child |
+
## Vendored from verified upstream
| grammar | cur ABI | upstream repo | pinned commit | verdict | LICENSE |