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.

Add grammar-only import parsers + pipeline edges (WRITES/CONFIGURES/DEPENDS_ON)

Martin Vogel6afb45427ca8
diff --git a/internal/cbm/extract_defs.c b/internal/cbm/extract_defs.c
index 9f03cf717..b4112db5f 100644
--- a/internal/cbm/extract_defs.c
+++ b/internal/cbm/extract_defs.c
@@ -394,15 +394,24 @@ static TSNode resolve_elm_func_name(TSNode node) {
return null_node;
}
-// Wolfram: resolve set/set_delayed name from LHS apply→user_symbol.
+// Wolfram: resolve set/set_delayed name from LHS apply→symbol.
+// The defined symbol's head can be a user_symbol (lowercase user names) OR a
+// builtin_symbol (capitalized names like Square/Cube, which the grammar tags as
+// builtin even when user-defined). For a bare `Name = value` (set_top with no
+// apply), the LHS is the symbol itself. Accept all three forms so multiple
+// defs in one file each resolve to a distinct name instead of collapsing.
static TSNode resolve_wolfram_func_name(TSNode node) {
if (ts_node_named_child_count(node) > 0) {
TSNode lhs = ts_node_named_child(node, 0);
- if (strcmp(ts_node_type(lhs), "apply") == 0 && ts_node_named_child_count(lhs) > 0) {
+ const char *lk = ts_node_type(lhs);
+ if (strcmp(lk, "apply") == 0 && ts_node_named_child_count(lhs) > 0) {
TSNode head = ts_node_named_child(lhs, 0);
- if (strcmp(ts_node_type(head), "user_symbol") == 0) {
+ const char *hk = ts_node_type(head);
+ if (strcmp(hk, "user_symbol") == 0 || strcmp(hk, "builtin_symbol") == 0) {
return head;
}
+ } else if (strcmp(lk, "user_symbol") == 0 || strcmp(lk, "builtin_symbol") == 0) {
+ return lhs;
}
}
TSNode null_node = {0};
@@ -703,6 +712,18 @@ static TSNode resolve_func_name(TSNode node, CBMLanguage lang) {
}
}
+ /* Just: a `recipe` carries its name on the nested `recipe_header`'s
+ * `name` field (an identifier), not on the recipe node itself. */
+ if (lang == CBM_LANG_JUST && strcmp(kind, "recipe") == 0) {
+ TSNode hdr = cbm_find_child_by_kind(node, "recipe_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. */
@@ -1691,6 +1712,32 @@ static int collect_bases_from_field(CBMArena *a, TSNode field_node, const char *
}
// Extract base class names from a class node.
+// Julia: a subtype declaration `Foo <: Bar` is a `binary_expression` (operator
+// `<:`) inside the def's `type_head`. The base is the RHS identifier.
+static const char **extract_julia_base_classes(CBMArena *a, TSNode node, const char *source) {
+ TSNode th = cbm_find_child_by_kind(node, "type_head");
+ if (ts_node_is_null(th)) {
+ return NULL;
+ }
+ TSNode inner = ts_node_named_child_count(th) > 0 ? ts_node_named_child(th, 0) : th;
+ if (ts_node_is_null(inner) || strcmp(ts_node_type(inner), "binary_expression") != 0 ||
+ ts_node_named_child_count(inner) < 2) {
+ return NULL;
+ }
+ TSNode base = ts_node_named_child(inner, ts_node_named_child_count(inner) - 1);
+ char *bname = cbm_node_text(a, base, source);
+ if (!bname || !bname[0]) {
+ return NULL;
+ }
+ const char **result = (const char **)cbm_arena_alloc(a, 2 * sizeof(const char *));
+ if (!result) {
+ return NULL;
+ }
+ result[0] = bname;
+ result[1] = NULL;
+ return result;
+}
+
static const char **extract_base_classes(CBMArena *a, TSNode node, const char *source,
CBMLanguage lang) {
// Languages whose heritage is not exposed via a tree-sitter field need
@@ -1740,6 +1787,127 @@ static const char **extract_base_classes(CBMArena *a, TSNode node, const char *s
}
}
}
+ if (lang == CBM_LANG_JULIA) {
+ const char **jb = extract_julia_base_classes(a, node, source);
+ if (jb) {
+ return jb;
+ }
+ }
+ /* F#: `inherit Base(...)` appears as a `class_inherits_decl` descendant of
+ * the type_definition; the base type is the `simple_type` it carries. */
+ if (lang == CBM_LANG_FSHARP) {
+ TSNode inh =
+ find_first_descendant_by_kind(node, "class_inherits_decl", CBM_DESCENDANT_MAX_DEPTH);
+ if (!ts_node_is_null(inh)) {
+ TSNode st = cbm_find_child_by_kind(inh, "simple_type");
+ char *bn = ts_node_is_null(st) ? NULL : cbm_node_text(a, st, source);
+ if (bn && bn[0]) {
+ const char **result = (const char **)cbm_arena_alloc(a, 2 * sizeof(const char *));
+ if (result) {
+ result[0] = bn;
+ result[1] = NULL;
+ return result;
+ }
+ }
+ }
+ }
+ /* D: `class Dog : Animal, IFoo` — class_declaration lists one `base_class`
+ * child per base, each wrapping an identifier/qualified name. */
+ if (lang == CBM_LANG_DLANG) {
+ const char *pbases[MAX_BASES];
+ int pc = 0;
+ uint32_t nc = ts_node_child_count(node);
+ for (uint32_t i = 0; i < nc && pc < MAX_BASES_MINUS_1; i++) {
+ TSNode c = ts_node_child(node, i);
+ if (strcmp(ts_node_type(c), "base_class") != 0) {
+ continue;
+ }
+ char *bn = cbm_node_text(a, c, source);
+ if (bn && bn[0]) {
+ pbases[pc++] = bn;
+ }
+ }
+ if (pc > 0) {
+ const char **result =
+ (const char **)cbm_arena_alloc(a, (pc + NULL_TERM) * sizeof(const char *));
+ if (result) {
+ for (int i = 0; i < pc; i++) {
+ result[i] = pbases[i];
+ }
+ result[pc] = NULL;
+ return result;
+ }
+ }
+ }
+ /* PowerShell: `class Dog : Animal` — class_statement lists `simple_name`
+ * children with a `:` token separating the class name from the base name(s).
+ * Collect every simple_name that appears AFTER the first `:` token. */
+ if (lang == CBM_LANG_POWERSHELL && strcmp(ts_node_type(node), "class_statement") == 0) {
+ const char *pbases[MAX_BASES];
+ int pc = 0;
+ bool seen_colon = false;
+ uint32_t nc = ts_node_child_count(node);
+ for (uint32_t i = 0; i < nc && pc < MAX_BASES_MINUS_1; i++) {
+ TSNode c = ts_node_child(node, i);
+ const char *ck = ts_node_type(c);
+ if (strcmp(ck, ":") == 0) {
+ seen_colon = true;
+ continue;
+ }
+ if (strcmp(ck, "{") == 0) {
+ break; /* class body begins; base list is done */
+ }
+ if (seen_colon && strcmp(ck, "simple_name") == 0) {
+ char *bn = cbm_node_text(a, c, source);
+ if (bn && bn[0]) {
+ pbases[pc++] = bn;
+ }
+ }
+ }
+ if (pc > 0) {
+ const char **result =
+ (const char **)cbm_arena_alloc(a, (pc + NULL_TERM) * sizeof(const char *));
+ if (result) {
+ for (int i = 0; i < pc; i++) {
+ result[i] = pbases[i];
+ }
+ result[pc] = NULL;
+ return result;
+ }
+ }
+ }
+ /* Pascal: declClass carries one or more `parent` fields, each a `typeref`
+ * (`= class(TBase, IFoo)`). Collect all parent typeref identifiers. */
+ if (lang == CBM_LANG_PASCAL && strcmp(ts_node_type(node), "declClass") == 0) {
+ const char *pbases[MAX_BASES];
+ int pc = 0;
+ uint32_t nc = ts_node_child_count(node);
+ for (uint32_t i = 0; i < nc && pc < MAX_BASES_MINUS_1; i++) {
+ const char *fn = ts_node_field_name_for_child(node, i);
+ if (!fn || strcmp(fn, "parent") != 0) {
+ continue;
+ }
+ TSNode pn = ts_node_child(node, i);
+ if (!ts_node_is_named(pn)) {
+ continue; /* the '(' / ')' delimiters are also tagged `parent` */
+ }
+ char *bn = cbm_node_text(a, pn, source);
+ if (bn && bn[0]) {
+ pbases[pc++] = bn;
+ }
+ }
+ if (pc > 0) {
+ const char **result =
+ (const char **)cbm_arena_alloc(a, (pc + NULL_TERM) * sizeof(const char *));
+ if (result) {
+ for (int i = 0; i < pc; i++) {
+ result[i] = pbases[i];
+ }
+ result[pc] = NULL;
+ return result;
+ }
+ }
+ }
static const char *fields[] = {"superclass",
"superclasses",
"superinterfaces",
@@ -2604,6 +2772,57 @@ static void extract_class_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
ctx->language == CBM_LANG_PONY)) {
name_node = cbm_find_child_by_kind(node, "identifier");
}
+ // F#: type_definition wraps an `anon_type_defn` (or similar) whose
+ // `type_name` child carries the type name on its own `type_name` field.
+ if (ts_node_is_null(name_node) && ctx->language == CBM_LANG_FSHARP) {
+ TSNode tn = find_first_descendant_by_kind(node, "type_name", CBM_DESCENDANT_MAX_DEPTH);
+ if (!ts_node_is_null(tn)) {
+ TSNode id = ts_node_child_by_field_name(tn, "type_name", 9);
+ if (ts_node_is_null(id)) {
+ id = cbm_find_child_by_kind(tn, "identifier");
+ }
+ if (!ts_node_is_null(id)) {
+ name_node = id;
+ }
+ }
+ }
+ // D: class/struct/interface/union/enum _declaration nodes carry the name on
+ // a plain `identifier` child (no `name` field).
+ if (ts_node_is_null(name_node) && ctx->language == CBM_LANG_DLANG) {
+ name_node = cbm_find_child_by_kind(node, "identifier");
+ }
+ // PowerShell: class_statement / enum_statement have no `name` field; the
+ // name is the FIRST `simple_name` child (`class Dog : Animal { ... }`).
+ if (ts_node_is_null(name_node) && ctx->language == CBM_LANG_POWERSHELL) {
+ name_node = cbm_find_child_by_kind(node, "simple_name");
+ }
+ // Pascal: a class/interface body (declClass/declIntf) has no name of its
+ // own; the type name is on the enclosing `declType`'s `name` field
+ // (`TFoo = class ... end`).
+ if (ts_node_is_null(name_node) && ctx->language == CBM_LANG_PASCAL) {
+ TSNode parent = ts_node_parent(node);
+ if (!ts_node_is_null(parent) && strcmp(ts_node_type(parent), "declType") == 0) {
+ name_node = ts_node_child_by_field_name(parent, TS_FIELD("name"));
+ }
+ }
+ // Julia (no `name` field): struct_definition / abstract_definition carry a
+ // `type_head` child whose name is either a plain identifier (`struct Foo`)
+ // or the LHS of a `<:` binary_expression (`struct Foo <: Bar`).
+ if (ts_node_is_null(name_node) && ctx->language == CBM_LANG_JULIA) {
+ TSNode th = cbm_find_child_by_kind(node, "type_head");
+ if (!ts_node_is_null(th)) {
+ TSNode inner = ts_node_named_child_count(th) > 0 ? ts_node_named_child(th, 0) : th;
+ if (!ts_node_is_null(inner) && strcmp(ts_node_type(inner), "binary_expression") == 0 &&
+ ts_node_named_child_count(inner) > 0) {
+ name_node = ts_node_named_child(inner, 0); /* LHS of `<:` */
+ } else if (!ts_node_is_null(inner) &&
+ strcmp(ts_node_type(inner), "identifier") == 0) {
+ name_node = inner;
+ } else {
+ name_node = cbm_find_child_by_kind(th, "identifier");
+ }
+ }
+ }
// Cap'n Proto (FIELD_COUNT 0): struct/interface name is type_identifier, enum
// name is enum_identifier (aliased identifier children).
if (ts_node_is_null(name_node) && ctx->language == CBM_LANG_CAPNP) {
@@ -2788,6 +3007,20 @@ static void extract_class_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
label = "Interface";
}
}
+ // F#: a `type_definition` that has a primary constructor (`type Foo(...) =`)
+ // or an `inherit` clause is an OOP class, not a plain type alias. Label it
+ // "Class" so it is registered as a resolvable inheritance target (the graph
+ // registry only indexes Function/Method/Class/Interface labels), letting
+ // `inherit Base` resolve into an INHERITS edge.
+ if (ctx->language == CBM_LANG_FSHARP && strcmp(label, "Type") == 0) {
+ if (!ts_node_is_null(
+ find_first_descendant_by_kind(node, "primary_constr_args",
+ CBM_DESCENDANT_MAX_DEPTH)) ||
+ !ts_node_is_null(find_first_descendant_by_kind(node, "class_inherits_decl",
+ CBM_DESCENDANT_MAX_DEPTH))) {
+ label = "Class";
+ }
+ }
// Go type_spec: check inner type for interface/struct
if (strcmp(kind, "type_spec") == 0) {
@@ -4706,6 +4939,7 @@ static bool lisp_is_def_head(const char *t) {
"define-struct",
"define-record-type",
"define/contract", // Scheme/Racket
+ "struct", // Racket struct
NULL};
for (int i = 0; heads[i]; i++) {
if (strcmp(t, heads[i]) == 0) {
@@ -4739,11 +4973,21 @@ static void extract_lisp_def(CBMExtractCtx *ctx, TSNode node) {
if (!name || !name[0]) {
return;
}
+ /* struct/record/type defining forms produce a type node, not a callable
+ * (Racket `(struct point ...)`, Clojure `(defrecord ...)`, etc.). */
+ const char *lisp_label = "Function";
+ if (strcmp(head, "struct") == 0 || strcmp(head, "define-struct") == 0 ||
+ strcmp(head, "define-record-type") == 0 || strcmp(head, "defrecord") == 0 ||
+ strcmp(head, "deftype") == 0) {
+ lisp_label = "Struct";
+ } else if (strcmp(head, "definterface") == 0 || strcmp(head, "defprotocol") == 0) {
+ lisp_label = "Interface";
+ }
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.label = lisp_label;
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;
@@ -4801,6 +5045,23 @@ static void walk_defs(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec,
// fall through: descend so nested defs inside the form are captured too
}
+ /* WIT: world-scoped `export name: func(...)` / `import name: func(...)`
+ * are export_item/import_item nodes (added to wit_func_types). But an
+ * export/import can also reference a non-function type (e.g. an
+ * interface) — only treat it as a Function when it actually contains a
+ * func_type; otherwise descend so its inner body is still traversed. */
+ if (ctx->language == CBM_LANG_WIT &&
+ (strcmp(kind, "export_item") == 0 || strcmp(kind, "import_item") == 0) &&
+ ts_node_is_null(
+ find_first_descendant_by_kind(node, "func_type", CBM_DESCENDANT_MAX_DEPTH))) {
+ uint32_t cc = ts_node_child_count(node);
+ for (int i = (int)cc - SKIP_CHAR; i >= 0 && top < CBM_WALK_DEFS_STACK_CAP; i--) {
+ stack[top++] =
+ (walk_defs_frame_t){ts_node_child(node, (uint32_t)i), frame.enclosing_class_qn};
+ }
+ continue;
+ }
+
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 8015f15d0..3584ad0b3 100644
--- a/internal/cbm/extract_imports.c
+++ b/internal/cbm/extract_imports.c
@@ -38,6 +38,32 @@ static void parse_wolfram_imports(CBMExtractCtx *ctx);
static void parse_php_imports(CBMExtractCtx *ctx);
static void parse_csharp_imports(CBMExtractCtx *ctx);
static void parse_spec_imports(CBMExtractCtx *ctx);
+static void parse_hare_imports(CBMExtractCtx *ctx);
+static void parse_pascal_imports(CBMExtractCtx *ctx);
+static void parse_powershell_imports(CBMExtractCtx *ctx);
+static void parse_lisp_imports(CBMExtractCtx *ctx);
+static void parse_starlark_imports(CBMExtractCtx *ctx);
+static void parse_tcl_imports(CBMExtractCtx *ctx);
+static void parse_teal_imports(CBMExtractCtx *ctx);
+static void parse_zsh_imports(CBMExtractCtx *ctx);
+static void parse_css_imports(CBMExtractCtx *ctx);
+static void parse_html_imports(CBMExtractCtx *ctx);
+static void parse_cmake_imports(CBMExtractCtx *ctx);
+static void parse_bitbake_imports(CBMExtractCtx *ctx);
+static void parse_kconfig_imports(CBMExtractCtx *ctx);
+static void parse_gn_imports(CBMExtractCtx *ctx);
+static void parse_just_imports(CBMExtractCtx *ctx);
+static void parse_nix_imports(CBMExtractCtx *ctx);
+static void parse_jsonnet_imports(CBMExtractCtx *ctx);
+static void parse_pkl_imports(CBMExtractCtx *ctx);
+static void parse_nickel_imports(CBMExtractCtx *ctx);
+static void parse_thrift_imports(CBMExtractCtx *ctx);
+static void parse_capnp_imports(CBMExtractCtx *ctx);
+static void parse_dlang_imports(CBMExtractCtx *ctx);
+static void parse_tablegen_imports(CBMExtractCtx *ctx);
+static void parse_crystal_imports(CBMExtractCtx *ctx);
+static void parse_fsharp_imports(CBMExtractCtx *ctx);
+static void parse_ada_imports(CBMExtractCtx *ctx);
// Helper: strip quotes from a string literal
static char *strip_quotes(CBMArena *a, const char *s) {
@@ -1428,6 +1454,7 @@ static void parse_embedded_imports(CBMExtractCtx *ctx) {
}
}
+
// --- Namespace / package declaration capture ---
// Java/Kotlin/C#/PHP put the file's symbols inside a namespace/package whose
// name is NOT reflected in the path-based QN scheme. Capturing it lets the
@@ -1478,6 +1505,795 @@ static void capture_namespace_decl(CBMExtractCtx *ctx) {
ts_tree_cursor_delete(&cursor);
}
+// --- Hare imports ---
+// tree-sitter-hare nests imports: module -> imports -> use_statement* ->
+// identifier. The use_statement is not a root child, so a generic scan misses
+// it. Descend into the `imports` container and take each use_statement's
+// identifier text (a `::`-separated module path).
+static void parse_hare_imports(CBMExtractCtx *ctx) {
+ CBMArena *a = ctx->arena;
+ TSNodeStack stack;
+ ts_nstack_init(&stack, a, CBM_SZ_512);
+ ts_nstack_push(&stack, a, ctx->root);
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
+ if (strcmp(ts_node_type(node), "use_statement") == 0) {
+ uint32_t nc = ts_node_named_child_count(node);
+ for (uint32_t j = 0; j < nc; j++) {
+ TSNode c = ts_node_named_child(node, j);
+ const char *ck = ts_node_type(c);
+ if (strcmp(ck, "identifier") == 0 || strcmp(ck, "scoped_identifier") == 0) {
+ char *mod = cbm_node_text(a, c, ctx->source);
+ if (mod && mod[0]) {
+ CBMImport imp = {.local_name = path_last(a, mod), .module_path = mod};
+ cbm_imports_push(&ctx->result->imports, a, imp);
+ }
+ break;
+ }
+ }
+ continue;
+ }
+ ts_nstack_push_children(&stack, a, node);
+ }
+}
+
+// --- Pascal imports ---
+// tree-sitter-pascal: declUses (uses Foo, Bar, Baz;) is nested under
+// unit -> interface (or implementation), NOT a root child, and carries one
+// `moduleName` child per imported unit. DFS for declUses nodes and emit one
+// import per moduleName.
+static void parse_pascal_imports(CBMExtractCtx *ctx) {
+ CBMArena *a = ctx->arena;
+ TSNodeStack stack;
+ ts_nstack_init(&stack, a, CBM_SZ_512);
+ ts_nstack_push(&stack, a, ctx->root);
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
+ if (strcmp(ts_node_type(node), "declUses") == 0) {
+ uint32_t nc = ts_node_named_child_count(node);
+ for (uint32_t j = 0; j < nc; j++) {
+ TSNode c = ts_node_named_child(node, j);
+ if (strcmp(ts_node_type(c), "moduleName") != 0) {
+ continue;
+ }
+ char *mod = cbm_node_text(a, c, ctx->source);
+ if (mod && mod[0]) {
+ CBMImport imp = {.local_name = path_last(a, mod), .module_path = mod};
+ cbm_imports_push(&ctx->result->imports, a, imp);
+ }
+ }
+ continue;
+ }
+ ts_nstack_push_children(&stack, a, node);
+ }
+}
+
+// --- PowerShell imports ---
+// tree-sitter-powershell models `using module ./M` / `using namespace System.IO`
+// as a plain `command` whose command_name is "using" — deeply nested under
+// statement_list -> pipeline -> pipeline_chain. The module path is the LAST
+// generic_token child of command_elements (after the "module"/"namespace"/
+// "assembly" qualifier). DFS for such commands.
+static void parse_powershell_imports(CBMExtractCtx *ctx) {
+ CBMArena *a = ctx->arena;
+ TSNodeStack stack;
+ ts_nstack_init(&stack, a, CBM_SZ_512);
+ ts_nstack_push(&stack, a, ctx->root);
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
+ if (strcmp(ts_node_type(node), "command") == 0) {
+ TSNode name = ts_node_child_by_field_name(node, TS_FIELD("command_name"));
+ char *nm = ts_node_is_null(name) ? NULL : cbm_node_text(a, name, ctx->source);
+ if (nm && strcmp(nm, "using") == 0) {
+ /* Find the last generic_token anywhere under the command — that
+ * is the module path / namespace / assembly being imported. */
+ TSNodeStack inner;
+ ts_nstack_init(&inner, a, CBM_SZ_512);
+ ts_nstack_push(&inner, a, node);
+ const char *last_tok = NULL;
+ uint32_t last_start = 0;
+ while (inner.count > 0) {
+ TSNode c = ts_nstack_pop(&inner);
+ if (strcmp(ts_node_type(c), "generic_token") == 0) {
+ char *t = cbm_node_text(a, c, ctx->source);
+ uint32_t sb = ts_node_start_byte(c);
+ if (t && t[0] && strcmp(t, "module") != 0 && strcmp(t, "namespace") != 0 &&
+ strcmp(t, "assembly") != 0 && (last_tok == NULL || sb > last_start)) {
+ last_tok = t;
+ last_start = sb;
+ }
+ }
+ ts_nstack_push_children(&inner, a, c);
+ }
+ if (last_tok && last_tok[0]) {
+ CBMImport imp = {.local_name = path_last(a, last_tok),
+ .module_path = (char *)last_tok};
+ cbm_imports_push(&ctx->result->imports, a, imp);
+ }
+ }
+ continue; /* commands don't nest imports further */
+ }
+ ts_nstack_push_children(&stack, a, node);
+ }
+}
+
+// --- Lisp-family imports (Scheme, Racket) ---
+// S-expression grammars model (import (lib ...)) / (require mod) / (load "f")
+// as a `list` whose first named child is a `symbol` naming the form. Walk
+// top-level lists; for import/require/load/use, take the remaining children as
+// module references (a symbol, a string, or a nested list whose meaningful
+// module symbol we take as-is).
+static void lisp_push_module(CBMExtractCtx *ctx, TSNode mod_node) {
+ CBMArena *a = ctx->arena;
+ const char *mk = ts_node_type(mod_node);
+ char *mod = NULL;
+ if (strcmp(mk, "symbol") == 0 || strcmp(mk, "string") == 0) {
+ mod = strip_quotes(a, cbm_node_text(a, mod_node, ctx->source));
+ } else if (strcmp(mk, "list") == 0) {
+ /* (only-in racket/math pi) → take first symbol after a leading keyword,
+ * else join inner symbols with '/'. Simplest robust choice: the whole
+ * list text minus the parens. */
+ mod = cbm_node_text(a, mod_node, ctx->source);
+ if (mod) {
+ size_t len = strlen(mod);
+ if (len >= 2 && mod[0] == '(' && mod[len - 1] == ')') {
+ mod = cbm_arena_strndup(a, mod + 1, len - 2);
+ }
+ }
+ }
+ if (mod && mod[0]) {
+ CBMImport imp = {.local_name = path_last(a, mod), .module_path = mod};
+ cbm_imports_push(&ctx->result->imports, a, imp);
+ }
+}
+
+static void parse_lisp_imports(CBMExtractCtx *ctx) {
+ CBMArena *a = ctx->arena;
+ 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);
+ if (strcmp(ts_node_type(node), "list") != 0) {
+ continue;
+ }
+ uint32_t nc = ts_node_named_child_count(node);
+ if (nc < 1) {
+ continue;
+ }
+ TSNode head = ts_node_named_child(node, 0);
+ if (strcmp(ts_node_type(head), "symbol") != 0) {
+ continue;
+ }
+ char *hn = cbm_node_text(a, head, ctx->source);
+ if (!hn || (strcmp(hn, "import") != 0 && strcmp(hn, "require") != 0 &&
+ strcmp(hn, "load") != 0 && strcmp(hn, "use") != 0 &&
+ strcmp(hn, "include") != 0)) {
+ continue;
+ }
+ for (uint32_t j = 1; j < nc; j++) {
+ TSNode mod_node = ts_node_named_child(node, j);
+ const char *mk = ts_node_type(mod_node);
+ /* (require 'json) — the quoted datum is a `quote` wrapping a symbol. */
+ if (strcmp(mk, "quote") == 0 && ts_node_named_child_count(mod_node) > 0) {
+ lisp_push_module(ctx, ts_node_named_child(mod_node, 0));
+ } else {
+ lisp_push_module(ctx, mod_node);
+ }
+ }
+ } while (ts_tree_cursor_goto_next_sibling(&cursor));
+ ts_tree_cursor_delete(&cursor);
+}
+
+// --- Starlark imports ---
+// load("//pkg:file.bzl", "sym", ...) — an expression_statement -> call whose
+// function identifier is "load"; the first string argument is the module path.
+static void parse_starlark_imports(CBMExtractCtx *ctx) {
+ CBMArena *a = ctx->arena;
+ TSNodeStack stack;
+ ts_nstack_init(&stack, a, CBM_SZ_512);
+ ts_nstack_push(&stack, a, ctx->root);
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
+ if (strcmp(ts_node_type(node), "call") == 0) {
+ TSNode fn = ts_node_child_by_field_name(node, TS_FIELD("function"));
+ char *fname = ts_node_is_null(fn) ? NULL : cbm_node_text(a, fn, ctx->source);
+ if (fname && strcmp(fname, "load") == 0) {
+ TSNode args = ts_node_child_by_field_name(node, TS_FIELD("arguments"));
+ if (!ts_node_is_null(args)) {
+ TSNode str = args;
+ if (find_first_descendant_of(args, "string", &str)) {
+ char *path = strip_quotes(a, cbm_node_text(a, str, ctx->source));
+ /* string node may carry string_start/_content/_end; if the
+ * stripped text still has quotes, fall back to content. */
+ if (path && path[0] && (path[0] == '"' || path[0] == '\'')) {
+ TSNode content = str;
+ if (find_first_descendant_of(str, "string_content", &content)) {
+ path = cbm_node_text(a, content, ctx->source);
+ }
+ }
+ if (path && path[0]) {
+ CBMImport imp = {.local_name = path_last(a, path), .module_path = path};
+ cbm_imports_push(&ctx->result->imports, a, imp);
+ }
+ }
+ }
+ }
+ }
+ ts_nstack_push_children(&stack, a, node);
+ }
+}
+
+// --- Tcl imports ---
+// source path / package require name — `command` nodes whose `name` field is
+// "source" (file include) or "package" (package require). Take the first
+// simple_word argument as the module.
+static void parse_tcl_imports(CBMExtractCtx *ctx) {
+ CBMArena *a = ctx->arena;
+ TSNodeStack stack;
+ ts_nstack_init(&stack, a, CBM_SZ_512);
+ ts_nstack_push(&stack, a, ctx->root);
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
+ if (strcmp(ts_node_type(node), "command") == 0) {
+ TSNode name = ts_node_child_by_field_name(node, TS_FIELD("name"));
+ char *nm = ts_node_is_null(name) ? NULL : cbm_node_text(a, name, ctx->source);
+ if (nm && strcmp(nm, "source") == 0) {
+ TSNode args = ts_node_child_by_field_name(node, TS_FIELD("arguments"));
+ if (!ts_node_is_null(args)) {
+ uint32_t nc = ts_node_named_child_count(args);
+ for (uint32_t j = 0; j < nc; j++) {
+ TSNode c = ts_node_named_child(args, j);
+ char *mod = strip_quotes(a, cbm_node_text(a, c, ctx->source));
+ if (mod && mod[0]) {
+ CBMImport imp = {.local_name = path_last(a, mod), .module_path = mod};
+ cbm_imports_push(&ctx->result->imports, a, imp);
+ break;
+ }
+ }
+ }
+ }
+ }
+ ts_nstack_push_children(&stack, a, node);
+ }
+}
+
+// --- Teal imports ---
+// Teal is a typed Lua dialect: `local m = require("mod")` — a function_call
+// whose called_object identifier is "require"; the first string argument is the
+// module. DFS for such calls (mirrors the Lua require pattern at AST level).
+static void parse_teal_imports(CBMExtractCtx *ctx) {
+ CBMArena *a = ctx->arena;
+ TSNodeStack stack;
+ ts_nstack_init(&stack, a, CBM_SZ_512);
+ ts_nstack_push(&stack, a, ctx->root);
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
+ if (strcmp(ts_node_type(node), "function_call") == 0) {
+ TSNode callee = ts_node_child_by_field_name(node, "called_object", 13);
+ char *cn = ts_node_is_null(callee) ? NULL : cbm_node_text(a, callee, ctx->source);
+ if (cn && strcmp(cn, "require") == 0) {
+ TSNode args = ts_node_child_by_field_name(node, TS_FIELD("arguments"));
+ TSNode str = ts_node_is_null(args) ? node : args;
+ if (!ts_node_is_null(args) && find_first_descendant_of(args, "string", &str)) {
+ char *mod = strip_quotes(a, cbm_node_text(a, str, ctx->source));
+ if (mod && mod[0] && (mod[0] == '"' || mod[0] == '\'')) {
+ TSNode content = str;
+ if (find_first_descendant_of(str, "string_content", &content)) {
+ mod = cbm_node_text(a, content, ctx->source);
+ }
+ }
+ if (mod && mod[0]) {
+ CBMImport imp = {.local_name = path_last(a, mod), .module_path = mod};
+ cbm_imports_push(&ctx->result->imports, a, imp);
+ }
+ }
+ }
+ }
+ ts_nstack_push_children(&stack, a, node);
+ }
+}
+
+// --- Zsh imports ---
+// source file / . file — `command` nodes whose command_name is "source" or ".".
+// The argument field carries the sourced path.
+static void parse_zsh_imports(CBMExtractCtx *ctx) {
+ CBMArena *a = ctx->arena;
+ TSNodeStack stack;
+ ts_nstack_init(&stack, a, CBM_SZ_512);
+ ts_nstack_push(&stack, a, ctx->root);
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
+ if (strcmp(ts_node_type(node), "command") == 0) {
+ TSNode name = ts_node_child_by_field_name(node, TS_FIELD("name"));
+ char *nm = ts_node_is_null(name) ? NULL : cbm_node_text(a, name, ctx->source);
+ if (nm && (strcmp(nm, "source") == 0 || strcmp(nm, ".") == 0)) {
+ TSNode arg = ts_node_child_by_field_name(node, TS_FIELD("argument"));
+ if (ts_node_is_null(arg)) {
+ arg = ts_node_child_by_field_name(node, TS_FIELD("arguments"));
+ }
+ if (!ts_node_is_null(arg)) {
+ char *mod = strip_quotes(a, cbm_node_text(a, arg, ctx->source));
+ if (mod && mod[0]) {
+ CBMImport imp = {.local_name = path_last(a, mod), .module_path = mod};
+ cbm_imports_push(&ctx->result->imports, a, imp);
+ }
+ }
+ }
+ }
+ ts_nstack_push_children(&stack, a, node);
+ }
+}
+
+// --- CSS / SCSS imports ---
+// CSS @import "x.css" and SCSS @use/@forward 'x' are top-level statements that
+// carry a `string_value` (whose `string_content` is the unquoted path). The
+// generic text fallback mangled these (kept the surrounding quotes), so the
+// resolver couldn't match the target file. Extract the clean string content.
+static void css_push_import_from_stmt(CBMExtractCtx *ctx, TSNode stmt) {
+ CBMArena *a = ctx->arena;
+ TSNode sv = stmt;
+ if (!find_first_descendant_of(stmt, "string_value", &sv)) {
+ /* CSS @import url("x") uses a `call_expression`/`plain_value`; fall back
+ * to the first string node. */
+ if (!find_first_descendant_of(stmt, "string_content", &sv)) {
+ return;
+ }
+ }
+ /* Prefer the inner string_content (already unquoted) when present. */
+ TSNode content = sv;
+ char *path = NULL;
+ if (find_first_descendant_of(sv, "string_content", &content)) {
+ path = cbm_node_text(a, content, ctx->source);
+ } else {
+ path = strip_quotes(a, cbm_node_text(a, sv, ctx->source));
+ }
+ if (!path || !path[0]) {
+ return;
+ }
+ CBMImport imp = {.local_name = path_last(a, path), .module_path = path};
+ cbm_imports_push(&ctx->result->imports, a, imp);
+}
+
+static void parse_css_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 *k = ts_node_type(node);
+ if (strcmp(k, "import_statement") == 0 || strcmp(k, "use_statement") == 0 ||
+ strcmp(k, "forward_statement") == 0 || strcmp(k, "include_statement") == 0) {
+ css_push_import_from_stmt(ctx, node);
+ }
+ } while (ts_tree_cursor_goto_next_sibling(&cursor));
+ ts_tree_cursor_delete(&cursor);
+}
+
+// --- HTML imports ---
+// `<script src="app.js">` and `<link href="style.css">` reference sibling
+// files. tree-sitter-html exposes these as `start_tag` -> `attribute`
+// (attribute_name src/href) -> quoted_attribute_value -> attribute_value. DFS
+// for start_tags and emit one import per src/href value. (Embedded <script>
+// bodies are still handled separately by parse_embedded_imports.)
+static void html_extract_tag_src(CBMExtractCtx *ctx, TSNode tag) {
+ CBMArena *a = ctx->arena;
+ uint32_t nc = ts_node_named_child_count(tag);
+ for (uint32_t j = 0; j < nc; j++) {
+ TSNode attr = ts_node_named_child(tag, j);
+ if (strcmp(ts_node_type(attr), "attribute") != 0) {
+ continue;
+ }
+ TSNode name = ts_node_named_child_count(attr) > 0 ? ts_node_named_child(attr, 0) : attr;
+ char *an = cbm_node_text(a, name, ctx->source);
+ if (!an || (strcmp(an, "src") != 0 && strcmp(an, "href") != 0)) {
+ continue;
+ }
+ TSNode val = attr;
+ char *path = NULL;
+ if (find_first_descendant_of(attr, "attribute_value", &val)) {
+ path = cbm_node_text(a, val, ctx->source);
+ }
+ if (path && path[0]) {
+ CBMImport imp = {.local_name = path_last(a, path), .module_path = path};
+ cbm_imports_push(&ctx->result->imports, a, imp);
+ }
+ }
+}
+
+static void parse_html_imports(CBMExtractCtx *ctx) {
+ /* First run the embedded-language walker (inline <script> JS imports). */
+ parse_embedded_imports(ctx);
+
+ TSNodeStack stack;
+ ts_nstack_init(&stack, ctx->arena, CBM_SZ_512);
+ ts_nstack_push(&stack, ctx->arena, ctx->root);
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
+ if (strcmp(ts_node_type(node), "start_tag") == 0 ||
+ strcmp(ts_node_type(node), "self_closing_tag") == 0) {
+ html_extract_tag_src(ctx, node);
+ }
+ ts_nstack_push_children(&stack, ctx->arena, node);
+ }
+}
+
+// Shared: push an import whose path is the (quote-stripped) text of `node`.
+// Strips a leading "./" so relative-import resolution matches the sibling file.
+static void push_path_import(CBMExtractCtx *ctx, TSNode node) {
+ CBMArena *a = ctx->arena;
+ char *path = strip_quotes(a, cbm_node_text(a, node, ctx->source));
+ if (!path || !path[0]) {
+ return;
+ }
+ /* Trim surrounding whitespace (some grammars include a leading space). */
+ while (*path == ' ' || *path == '\t') {
+ path++;
+ }
+ size_t len = strlen(path);
+ while (len > 0 && (path[len - 1] == ' ' || path[len - 1] == '\t' || path[len - 1] == '\n')) {
+ path[--len] = '\0';
+ }
+ if (!path[0]) {
+ return;
+ }
+ CBMImport imp = {.local_name = path_last(a, path), .module_path = path};
+ cbm_imports_push(&ctx->result->imports, a, imp);
+}
+
+// Shared: find a node's clean string path — prefers an inner unquoted content
+// child (string_content / str_literal / …); else an inner quoted `string`
+// (whose quotes push_path_import strips); else the node's own text. Never falls
+// back to text that still contains a directive keyword.
+static void push_string_descendant_import(CBMExtractCtx *ctx, TSNode node) {
+ TSNode hit = node;
+ static const char *content_kinds[] = {"string_content", "str_literal",
+ "slStringLiteralPart", "include_path",
+ "attribute_value", "path_fragment",
+ "string_fragment", "literal_content",
+ NULL};
+ for (const char **k = content_kinds; *k; k++) {
+ if (find_first_descendant_of(node, *k, &hit)) {
+ push_path_import(ctx, hit);
+ return;
+ }
+ }
+ /* Quoted string nodes (Just/Pkl/Jsonnet variants): strip the quotes. */
+ static const char *string_kinds[] = {"string", "string_value", "string_literal",
+ "static_string", "stringConstant", NULL};
+ for (const char **k = string_kinds; *k; k++) {
+ if (find_first_descendant_of(node, *k, &hit)) {
+ push_path_import(ctx, hit);
+ return;
+ }
+ }
+ push_path_import(ctx, node);
+}
+
+// --- CMake imports: include(path) / add_subdirectory(dir) ---
+static void parse_cmake_imports(CBMExtractCtx *ctx) {
+ CBMArena *a = ctx->arena;
+ TSNodeStack stack;
+ ts_nstack_init(&stack, a, CBM_SZ_512);
+ ts_nstack_push(&stack, a, ctx->root);
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
+ if (strcmp(ts_node_type(node), "normal_command") == 0) {
+ TSNode id = ts_node_named_child_count(node) > 0 ? ts_node_named_child(node, 0) : node;
+ char *cmd = cbm_node_text(a, id, ctx->source);
+ if (cmd && (strcmp(cmd, "include") == 0 || strcmp(cmd, "add_subdirectory") == 0)) {
+ TSNode args = cbm_find_child_by_kind(node, "argument_list");
+ if (!ts_node_is_null(args)) {
+ uint32_t nc = ts_node_named_child_count(args);
+ for (uint32_t j = 0; j < nc; j++) {
+ TSNode arg = ts_node_named_child(args, j);
+ push_string_descendant_import(ctx, arg);
+ break; /* first argument is the module/dir */
+ }
+ }
+ }
+ }
+ ts_nstack_push_children(&stack, a, node);
+ }
+}
+
+// --- BitBake imports: require/include path ---
+static void parse_bitbake_imports(CBMExtractCtx *ctx) {
+ TSNodeStack stack;
+ ts_nstack_init(&stack, ctx->arena, CBM_SZ_512);
+ ts_nstack_push(&stack, ctx->arena, ctx->root);
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
+ const char *k = ts_node_type(node);
+ if (strcmp(k, "require_directive") == 0 || strcmp(k, "include_directive") == 0 ||
+ strcmp(k, "inherit_directive") == 0) {
+ push_string_descendant_import(ctx, node);
+ }
+ ts_nstack_push_children(&stack, ctx->arena, node);
+ }
+}
+
+// --- Kconfig imports: source "path" ---
+static void parse_kconfig_imports(CBMExtractCtx *ctx) {
+ TSNodeStack stack;
+ ts_nstack_init(&stack, ctx->arena, CBM_SZ_512);
+ ts_nstack_push(&stack, ctx->arena, ctx->root);
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
+ if (ts_node_is_named(node) && strcmp(ts_node_type(node), "source") == 0) {
+ push_string_descendant_import(ctx, node);
+ }
+ ts_nstack_push_children(&stack, ctx->arena, node);
+ }
+}
+
+// --- GN imports: import("//path") ---
+static void parse_gn_imports(CBMExtractCtx *ctx) {
+ TSNodeStack stack;
+ ts_nstack_init(&stack, ctx->arena, CBM_SZ_512);
+ ts_nstack_push(&stack, ctx->arena, ctx->root);
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
+ if (strcmp(ts_node_type(node), "import_statement") == 0) {
+ push_string_descendant_import(ctx, node);
+ }
+ ts_nstack_push_children(&stack, ctx->arena, node);
+ }
+}
+
+// --- Just imports: import 'path' ---
+static void parse_just_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);
+ if (strcmp(ts_node_type(node), "import") == 0) {
+ push_string_descendant_import(ctx, node);
+ }
+ } while (ts_tree_cursor_goto_next_sibling(&cursor));
+ ts_tree_cursor_delete(&cursor);
+}
+
+// --- Nix imports: import ./path ---
+static void parse_nix_imports(CBMExtractCtx *ctx) {
+ CBMArena *a = ctx->arena;
+ TSNodeStack stack;
+ ts_nstack_init(&stack, a, CBM_SZ_512);
+ ts_nstack_push(&stack, a, ctx->root);
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
+ if (strcmp(ts_node_type(node), "apply_expression") == 0) {
+ /* import <path> — the function position is the identifier "import",
+ * the argument a path_expression / path_fragment / string. */
+ TSNode fn = ts_node_named_child_count(node) > 0 ? ts_node_named_child(node, 0) : node;
+ char *fname = cbm_node_text(a, fn, ctx->source);
+ if (fname && strcmp(fname, "import") == 0 && ts_node_named_child_count(node) > 1) {
+ TSNode arg = ts_node_named_child(node, 1);
+ TSNode frag = arg;
+ if (find_first_descendant_of(arg, "path_fragment", &frag) ||
+ find_first_descendant_of(arg, "string_content", &frag)) {
+ push_path_import(ctx, frag);
+ } else {
+ push_path_import(ctx, arg);
+ }
+ }
+ }
+ ts_nstack_push_children(&stack, a, node);
+ }
+}
+
+// --- Jsonnet imports: import 'path' / importstr 'path' ---
+static void parse_jsonnet_imports(CBMExtractCtx *ctx) {
+ TSNodeStack stack;
+ ts_nstack_init(&stack, ctx->arena, CBM_SZ_512);
+ ts_nstack_push(&stack, ctx->arena, ctx->root);
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
+ const char *k = ts_node_type(node);
+ if (ts_node_is_named(node) &&
+ (strcmp(k, "import") == 0 || strcmp(k, "importstr") == 0 ||
+ strcmp(k, "importbin") == 0)) {
+ push_string_descendant_import(ctx, node);
+ }
+ ts_nstack_push_children(&stack, ctx->arena, node);
+ }
+}
+
+// --- Pkl imports: amends/extends/import "path" ---
+static void parse_pkl_imports(CBMExtractCtx *ctx) {
+ TSNodeStack stack;
+ ts_nstack_init(&stack, ctx->arena, CBM_SZ_512);
+ ts_nstack_push(&stack, ctx->arena, ctx->root);
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
+ const char *k = ts_node_type(node);
+ if (strcmp(k, "extendsOrAmendsClause") == 0 || strcmp(k, "importClause") == 0 ||
+ strcmp(k, "importExpr") == 0) {
+ push_string_descendant_import(ctx, node);
+ }
+ ts_nstack_push_children(&stack, ctx->arena, node);
+ }
+}
+
+// --- Nickel imports: import "path" ---
+static void parse_nickel_imports(CBMExtractCtx *ctx) {
+ CBMArena *a = ctx->arena;
+ TSNodeStack stack;
+ ts_nstack_init(&stack, a, CBM_SZ_512);
+ ts_nstack_push(&stack, a, ctx->root);
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
+ /* The grammar emits an anonymous `import` token followed by a
+ * static_string sibling. Detect any node carrying a static_string whose
+ * preceding sibling text is "import". Simpler: scan children for the
+ * "import" keyword token and take the next static_string. */
+ uint32_t nc = ts_node_child_count(node);
+ for (uint32_t j = 0; j + 1 < nc; j++) {
+ TSNode c = ts_node_child(node, j);
+ char *t = cbm_node_text(a, c, ctx->source);
+ if (t && strcmp(t, "import") == 0) {
+ for (uint32_t m = j + 1; m < nc; m++) {
+ TSNode s = ts_node_child(node, m);
+ if (strcmp(ts_node_type(s), "static_string") == 0) {
+ push_string_descendant_import(ctx, s);
+ break;
+ }
+ }
+ }
+ }
+ ts_nstack_push_children(&stack, a, node);
+ }
+}
+
+// --- Thrift imports: include "path" ---
+static void parse_thrift_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 *k = ts_node_type(node);
+ if (strcmp(k, "include_statement") == 0 || strcmp(k, "cpp_include_statement") == 0) {
+ push_string_descendant_import(ctx, node);
+ }
+ } while (ts_tree_cursor_goto_next_sibling(&cursor));
+ ts_tree_cursor_delete(&cursor);
+}
+
+// --- Cap'n Proto imports: using X = import "path" ---
+static void parse_capnp_imports(CBMExtractCtx *ctx) {
+ TSNodeStack stack;
+ ts_nstack_init(&stack, ctx->arena, CBM_SZ_512);
+ ts_nstack_push(&stack, ctx->arena, ctx->root);
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
+ const char *k = ts_node_type(node);
+ if (ts_node_is_named(node) &&
+ (strcmp(k, "import_using") == 0 || strcmp(k, "import_path") == 0)) {
+ push_string_descendant_import(ctx, node);
+ continue; /* don't double-emit from nested import_path */
+ }
+ ts_nstack_push_children(&stack, ctx->arena, node);
+ }
+}
+
+// --- D imports: import module.path; (nested under module_def) ---
+static void parse_dlang_imports(CBMExtractCtx *ctx) {
+ CBMArena *a = ctx->arena;
+ TSNodeStack stack;
+ ts_nstack_init(&stack, a, CBM_SZ_512);
+ ts_nstack_push(&stack, a, ctx->root);
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
+ if (strcmp(ts_node_type(node), "import_declaration") == 0) {
+ TSNode fqn = node;
+ if (find_first_descendant_of(node, "module_fqn", &fqn)) {
+ char *mod = cbm_node_text(a, fqn, ctx->source);
+ if (mod && mod[0]) {
+ CBMImport imp = {.local_name = path_last(a, mod), .module_path = mod};
+ cbm_imports_push(&ctx->result->imports, a, imp);
+ }
+ }
+ continue;
+ }
+ ts_nstack_push_children(&stack, a, node);
+ }
+}
+
+// --- TableGen imports: include "path.td" ---
+static void parse_tablegen_imports(CBMExtractCtx *ctx) {
+ TSNodeStack stack;
+ ts_nstack_init(&stack, ctx->arena, CBM_SZ_512);
+ ts_nstack_push(&stack, ctx->arena, ctx->root);
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
+ const char *k = ts_node_type(node);
+ if (ts_node_is_named(node) &&
+ (strcmp(k, "include_directive") == 0 || strcmp(k, "include") == 0)) {
+ push_string_descendant_import(ctx, node);
+ continue;
+ }
+ ts_nstack_push_children(&stack, ctx->arena, node);
+ }
+}
+
+// --- Crystal imports: require "./path" ---
+static void parse_crystal_imports(CBMExtractCtx *ctx) {
+ TSNodeStack stack;
+ ts_nstack_init(&stack, ctx->arena, CBM_SZ_512);
+ ts_nstack_push(&stack, ctx->arena, ctx->root);
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
+ if (ts_node_is_named(node) && strcmp(ts_node_type(node), "require") == 0) {
+ push_string_descendant_import(ctx, node);
+ continue;
+ }
+ ts_nstack_push_children(&stack, ctx->arena, node);
+ }
+}
+
+// --- F# imports: open Module.Path ---
+static void parse_fsharp_imports(CBMExtractCtx *ctx) {
+ CBMArena *a = ctx->arena;
+ TSNodeStack stack;
+ ts_nstack_init(&stack, a, CBM_SZ_512);
+ ts_nstack_push(&stack, a, ctx->root);
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
+ const char *k = ts_node_type(node);
+ if (strcmp(k, "import_decl") == 0 || strcmp(k, "open_expression") == 0) {
+ TSNode id = node;
+ if (find_first_descendant_of(node, "long_identifier", &id) ||
+ find_first_descendant_of(node, "identifier", &id)) {
+ char *mod = cbm_node_text(a, id, ctx->source);
+ if (mod && mod[0]) {
+ CBMImport imp = {.local_name = path_last(a, mod), .module_path = mod};
+ cbm_imports_push(&ctx->result->imports, a, imp);
+ }
+ }
+ continue;
+ }
+ ts_nstack_push_children(&stack, a, node);
+ }
+}
+
+// --- Ada imports: with Pkg.Child; ---
+static void parse_ada_imports(CBMExtractCtx *ctx) {
+ CBMArena *a = ctx->arena;
+ TSNodeStack stack;
+ ts_nstack_init(&stack, a, CBM_SZ_512);
+ ts_nstack_push(&stack, a, ctx->root);
+ while (stack.count > 0) {
+ TSNode node = ts_nstack_pop(&stack);
+ if (strcmp(ts_node_type(node), "with_clause") == 0) {
+ uint32_t nc = ts_node_named_child_count(node);
+ for (uint32_t j = 0; j < nc; j++) {
+ TSNode c = ts_node_named_child(node, j);
+ const char *ck = ts_node_type(c);
+ if (strcmp(ck, "identifier") == 0 || strcmp(ck, "selected_component") == 0 ||
+ strcmp(ck, "name") == 0) {
+ char *mod = cbm_node_text(a, c, ctx->source);
+ if (mod && mod[0]) {
+ CBMImport imp = {.local_name = path_last(a, mod), .module_path = mod};
+ cbm_imports_push(&ctx->result->imports, a, imp);
+ }
+ }
+ }
+ continue;
+ }
+ ts_nstack_push_children(&stack, a, node);
+ }
+}
+
// --- Main dispatch ---
void cbm_extract_imports(CBMExtractCtx *ctx) {
@@ -1559,7 +2375,7 @@ void cbm_extract_imports(CBMExtractCtx *ctx) {
break;
case CBM_LANG_CSS:
case CBM_LANG_SCSS:
- parse_generic_imports(ctx, "import_statement");
+ parse_css_imports(ctx);
break;
case CBM_LANG_PERL:
parse_generic_imports(ctx, "use_statement");
@@ -1585,11 +2401,86 @@ void cbm_extract_imports(CBMExtractCtx *ctx) {
case CBM_LANG_WOLFRAM:
parse_wolfram_imports(ctx);
break;
+ case CBM_LANG_HARE:
+ parse_hare_imports(ctx);
+ break;
+ case CBM_LANG_PASCAL:
+ parse_pascal_imports(ctx);
+ break;
+ case CBM_LANG_POWERSHELL:
+ parse_powershell_imports(ctx);
+ break;
+ case CBM_LANG_SCHEME:
+ case CBM_LANG_RACKET:
+ parse_lisp_imports(ctx);
+ break;
+ case CBM_LANG_STARLARK:
+ parse_starlark_imports(ctx);
+ break;
+ case CBM_LANG_TCL:
+ parse_tcl_imports(ctx);
+ break;
+ case CBM_LANG_TEAL:
+ parse_teal_imports(ctx);
+ break;
+ case CBM_LANG_ZSH:
+ parse_zsh_imports(ctx);
+ break;
+ case CBM_LANG_CMAKE:
+ parse_cmake_imports(ctx);
+ break;
+ case CBM_LANG_BITBAKE:
+ parse_bitbake_imports(ctx);
+ break;
+ case CBM_LANG_KCONFIG:
+ parse_kconfig_imports(ctx);
+ break;
+ case CBM_LANG_GN:
+ parse_gn_imports(ctx);
+ break;
+ case CBM_LANG_JUST:
+ parse_just_imports(ctx);
+ break;
+ case CBM_LANG_NIX:
+ parse_nix_imports(ctx);
+ break;
+ case CBM_LANG_JSONNET:
+ parse_jsonnet_imports(ctx);
+ break;
+ case CBM_LANG_PKL:
+ parse_pkl_imports(ctx);
+ break;
+ case CBM_LANG_NICKEL:
+ parse_nickel_imports(ctx);
+ break;
+ case CBM_LANG_THRIFT:
+ parse_thrift_imports(ctx);
+ break;
+ case CBM_LANG_CAPNP:
+ parse_capnp_imports(ctx);
+ break;
+ case CBM_LANG_DLANG:
+ parse_dlang_imports(ctx);
+ break;
+ case CBM_LANG_TABLEGEN:
+ parse_tablegen_imports(ctx);
+ break;
+ case CBM_LANG_CRYSTAL:
+ parse_crystal_imports(ctx);
+ break;
+ case CBM_LANG_FSHARP:
+ parse_fsharp_imports(ctx);
+ break;
+ case CBM_LANG_ADA:
+ parse_ada_imports(ctx);
+ break;
/* Host languages whose tree-sitter grammar leaves <script> bodies as raw
* text — re-parse the embedded slice via the embedded-language spec. */
+ case CBM_LANG_HTML:
+ parse_html_imports(ctx);
+ break;
case CBM_LANG_SVELTE:
case CBM_LANG_VUE:
- case CBM_LANG_HTML:
case CBM_LANG_ASTRO:
parse_embedded_imports(ctx);
break;
diff --git a/internal/cbm/extract_semantic.c b/internal/cbm/extract_semantic.c
index 92c101aed..530ea3314 100644
--- a/internal/cbm/extract_semantic.c
+++ b/internal/cbm/extract_semantic.c
@@ -139,6 +139,58 @@ static void walk_throws(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec
// --- Read/Write detection (iterative) ---
+// Resolve an assignment LHS node to the bare name being written. Handles
+// common wrappers so WRITES resolve for more languages than a raw identifier:
+// - expression_list (Go `x = ...` desugars left to expression_list[x])
+// - index/subscript (`cache[k] = v` → write the base var `cache`)
+// - field/member/selector access (`self.total = ...`, `obj.Field = ...` →
+// write the trailing field name `total`/`Field`)
+// Returns NULL if no simple write target can be determined.
+static char *resolve_lhs_write_name(CBMExtractCtx *ctx, TSNode left) {
+ // Unwrap a single-element expression_list (Go).
+ if (strcmp(ts_node_type(left), "expression_list") == 0) {
+ if (ts_node_named_child_count(left) != 1) {
+ return NULL; // multi-assign: ambiguous, skip
+ }
+ left = ts_node_named_child(left, 0);
+ }
+ const char *lk = ts_node_type(left);
+ if (strcmp(lk, "identifier") == 0 || strcmp(lk, "simple_identifier") == 0) {
+ return cbm_node_text(ctx->arena, left, ctx->source);
+ }
+ // Indexed write: write the base operand's identifier (`cache[k]` → cache).
+ if (strcmp(lk, "index_expression") == 0 || strcmp(lk, "subscript_expression") == 0) {
+ TSNode base = ts_node_child_by_field_name(left, TS_FIELD("operand"));
+ if (ts_node_is_null(base)) {
+ base = ts_node_child_by_field_name(left, TS_FIELD("object"));
+ }
+ if (ts_node_is_null(base) && ts_node_named_child_count(left) > 0) {
+ base = ts_node_named_child(left, 0);
+ }
+ if (!ts_node_is_null(base)) {
+ const char *bk = ts_node_type(base);
+ if (strcmp(bk, "identifier") == 0 || strcmp(bk, "simple_identifier") == 0) {
+ return cbm_node_text(ctx->arena, base, ctx->source);
+ }
+ }
+ return NULL;
+ }
+ // Field/member write: write the trailing field name (`self.total` → total,
+ // `obj.Field` → Field). Covers Rust field_expression, C#/Java member access.
+ if (strcmp(lk, "field_expression") == 0 || strcmp(lk, "member_access_expression") == 0 ||
+ strcmp(lk, "field_access") == 0 || strcmp(lk, "selector_expression") == 0) {
+ TSNode fld = ts_node_child_by_field_name(left, TS_FIELD("field"));
+ if (ts_node_is_null(fld)) {
+ fld = ts_node_child_by_field_name(left, TS_FIELD("name"));
+ }
+ if (!ts_node_is_null(fld)) {
+ return cbm_node_text(ctx->arena, fld, ctx->source);
+ }
+ return NULL;
+ }
+ return NULL;
+}
+
// Try to emit a write for an assignment node.
static void try_emit_assignment_write(CBMExtractCtx *ctx, TSNode node, const char *func_qn) {
TSNode left = ts_node_child_by_field_name(node, TS_FIELD("left"));
@@ -150,11 +202,7 @@ static void try_emit_assignment_write(CBMExtractCtx *ctx, TSNode node, const cha
if (ts_node_is_null(left)) {
return;
}
- const char *lk = ts_node_type(left);
- if (strcmp(lk, "identifier") != 0 && strcmp(lk, "simple_identifier") != 0) {
- return;
- }
- char *name = cbm_node_text(ctx->arena, left, ctx->source);
+ char *name = resolve_lhs_write_name(ctx, left);
if (name && name[0] && !cbm_is_keyword(name, ctx->language)) {
CBMReadWrite rw;
rw.var_name = name;
@@ -237,16 +285,13 @@ void handle_readwrites(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec,
}
if (!ts_node_is_null(left)) {
- const char *lk = ts_node_type(left);
- if (strcmp(lk, "identifier") == 0 || strcmp(lk, "simple_identifier") == 0) {
- char *name = cbm_node_text(ctx->arena, left, ctx->source);
- if (name && name[0] && !cbm_is_keyword(name, ctx->language)) {
- CBMReadWrite rw;
- rw.var_name = name;
- rw.is_write = true;
- rw.enclosing_func_qn = state->enclosing_func_qn;
- cbm_rw_push(&ctx->result->rw, ctx->arena, rw);
- }
+ char *name = resolve_lhs_write_name(ctx, left);
+ if (name && name[0] && !cbm_is_keyword(name, ctx->language)) {
+ CBMReadWrite rw;
+ rw.var_name = name;
+ rw.is_write = true;
+ rw.enclosing_func_qn = state->enclosing_func_qn;
+ cbm_rw_push(&ctx->result->rw, ctx->arena, rw);
}
}
}
diff --git a/internal/cbm/lang_specs.c b/internal/cbm/lang_specs.c
index 62bddbfcb..2524c1aea 100644
--- a/internal/cbm/lang_specs.c
+++ b/internal/cbm/lang_specs.c
@@ -1519,7 +1519,8 @@ static const char *smithy_class_types[] = {"structure_statement", "union_stateme
static const char *smithy_field_types[] = {"shape_member", NULL};
static const char *smithy_import_types[] = {"use_statement", NULL};
static const char *smithy_module_types[] = {"source_file", NULL};
-static const char *wit_func_types[] = {"func_item", "resource_method", NULL};
+static const char *wit_func_types[] = {"func_item", "resource_method", "export_item",
+ "import_item", NULL};
static const char *wit_class_types[] = {"record_item", "resource_item", "enum_items",
"variant_items", "flags_items", NULL};
static const char *wit_field_types[] = {"record_field", NULL};
diff --git a/internal/cbm/service_patterns.c b/internal/cbm/service_patterns.c
index 75c0c1556..14ba30a64 100644
--- a/internal/cbm/service_patterns.c
+++ b/internal/cbm/service_patterns.c
@@ -154,19 +154,25 @@ static const lib_pattern_t async_libraries[] = {
{"cloud.pubsub", CBM_SVC_ASYNC, "pubsub"},
{"PubSub", CBM_SVC_ASYNC, "pubsub"},
- /* AWS — use SDK module paths to avoid false positives */
+ /* AWS — use SDK module paths to avoid false positives. cbm_fqn_compute
+ * converts path slashes to '.', so a resolved local Go QN reads
+ * "aws-sdk-go.service.sqs..."; include both slash and dot forms so the
+ * substring match fires whether the id comes from an import path or a QN. */
{"aws-sdk-go/service/sqs", CBM_SVC_ASYNC, "sqs"},
+ {"aws-sdk-go.service.sqs", CBM_SVC_ASYNC, "sqs"},
{"aws_sdk_sqs", CBM_SVC_ASYNC, "sqs"},
{"Amazon.SQS", CBM_SVC_ASYNC, "sqs"},
{"@aws-sdk/client-sqs", CBM_SVC_ASYNC, "sqs"},
{"boto3.client.sqs", CBM_SVC_ASYNC, "sqs"},
{"aws-sdk-go/service/sns", CBM_SVC_ASYNC, "sns"},
+ {"aws-sdk-go.service.sns", CBM_SVC_ASYNC, "sns"},
{"aws_sdk_sns", CBM_SVC_ASYNC, "sns"},
{"Amazon.SNS", CBM_SVC_ASYNC, "sns"},
{"@aws-sdk/client-sns", CBM_SVC_ASYNC, "sns"},
{"eventbridge", CBM_SVC_ASYNC, "eventbridge"},
{"EventBridge", CBM_SVC_ASYNC, "eventbridge"},
{"aws-sdk-go/service/lambda", CBM_SVC_ASYNC, "lambda"},
+ {"aws-sdk-go.service.lambda", CBM_SVC_ASYNC, "lambda"},
{"aws_sdk_lambda", CBM_SVC_ASYNC, "lambda"},
{"@aws-sdk/client-lambda", CBM_SVC_ASYNC, "lambda"},
{"stepfunctions", CBM_SVC_ASYNC, "stepfunctions"},
diff --git a/src/pipeline/pass_definitions.c b/src/pipeline/pass_definitions.c
index c99f33cf9..b5aad9c4c 100644
--- a/src/pipeline/pass_definitions.c
+++ b/src/pipeline/pass_definitions.c
@@ -258,10 +258,13 @@ static void process_def(cbm_pipeline_ctx_t *ctx, const CBMDefinition *def, const
def->file_path ? def->file_path : rel, (int)def->start_line, (int)def->end_line, props);
/* Register callable symbols + Interface. Interface must be in the registry
* so C#/Java `class Foo : IBar` / `class Foo implements IBar` can resolve
- * `IBar` to an INHERITS edge target during the enrichment phase. */
+ * `IBar` to an INHERITS edge target during the enrichment phase.
+ * Variable/Field defs are also registered so pass_usages.c can resolve
+ * READS/WRITES accesses (rw->var_name) to a Variable/Field node QN. */
if (node_id > 0 && def->label &&
(strcmp(def->label, "Function") == 0 || strcmp(def->label, "Method") == 0 ||
- strcmp(def->label, "Class") == 0 || strcmp(def->label, "Interface") == 0)) {
+ strcmp(def->label, "Class") == 0 || strcmp(def->label, "Interface") == 0 ||
+ strcmp(def->label, "Variable") == 0 || strcmp(def->label, "Field") == 0)) {
cbm_registry_add(ctx->registry, def->name, def->qualified_name, def->label);
}
char *file_qn = cbm_pipeline_fqn_compute(ctx->project_name, rel, "__file__");
@@ -322,6 +325,52 @@ static void create_channel_edges_for_file(cbm_pipeline_ctx_t *ctx, const CBMFile
}
}
+/* Create CONFIGURES edges for one file's env accesses. extract_env_accesses.c
+ * records every os.Getenv / process.env / Environment.GetEnvironmentVariable
+ * style access into result->env_accesses. We materialize one EnvVar node per
+ * env key and link the enclosing function (or the file node) CONFIGURES-> it,
+ * so environment-driven configuration is visible even when the accessor is a
+ * stdlib symbol that never resolves to an in-graph callee. */
+static int create_env_configures_for_file(cbm_pipeline_ctx_t *ctx, const CBMFileResult *result,
+ const char *rel) {
+ int count = 0;
+ char *file_qn = NULL;
+ const cbm_gbuf_node_t *file_node = NULL;
+ for (int j = 0; j < result->env_accesses.count; j++) {
+ const CBMEnvAccess *ea = &result->env_accesses.items[j];
+ if (!ea->env_key || !ea->env_key[0]) {
+ continue;
+ }
+ char env_qn[CBM_SZ_512];
+ snprintf(env_qn, sizeof(env_qn), "__env__%s", ea->env_key);
+ char env_props[CBM_SZ_512];
+ snprintf(env_props, sizeof(env_props), "{\"env_key\":\"%s\"}", ea->env_key);
+ int64_t env_id =
+ cbm_gbuf_upsert_node(ctx->gbuf, "EnvVar", ea->env_key, env_qn, "", 0, 0, env_props);
+ if (env_id <= 0) {
+ continue;
+ }
+ const cbm_gbuf_node_t *src = NULL;
+ if (ea->enclosing_func_qn && ea->enclosing_func_qn[0]) {
+ src = cbm_gbuf_find_by_qn(ctx->gbuf, ea->enclosing_func_qn);
+ }
+ if (!src) {
+ if (!file_qn) {
+ file_qn = cbm_pipeline_fqn_compute(ctx->project_name, rel, "__file__");
+ file_node = cbm_gbuf_find_by_qn(ctx->gbuf, file_qn);
+ }
+ src = file_node;
+ }
+ if (src && src->id != env_id) {
+ cbm_gbuf_insert_edge(ctx->gbuf, src->id, env_id, "CONFIGURES",
+ "{\"strategy\":\"env_access\"}");
+ count++;
+ }
+ }
+ free(file_qn);
+ return count;
+}
+
/* Create IMPORTS edges for one file's imports. Mirrors the resolution
* logic in pass_parallel.c register_and_link_def — keep the two in sync. */
static int create_import_edges_for_file(cbm_pipeline_ctx_t *ctx, const CBMFileResult *result,
@@ -432,6 +481,7 @@ int cbm_pipeline_pass_definitions(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t
* map is available without the cache (single-file scope). */
total_imports += create_import_edges_for_file(ctx, result, rel, NULL);
create_channel_edges_for_file(ctx, result, rel);
+ create_env_configures_for_file(ctx, result, rel);
cbm_free_result(result);
}
}
@@ -464,6 +514,7 @@ int cbm_pipeline_pass_definitions(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t
total_imports +=
create_import_edges_for_file(ctx, result, files[i].rel_path, namespace_map);
create_channel_edges_for_file(ctx, result, files[i].rel_path);
+ create_env_configures_for_file(ctx, result, files[i].rel_path);
}
cbm_pipeline_namespace_map_free(namespace_map);
if (owns_local_cache) {
diff --git a/src/pipeline/pass_k8s.c b/src/pipeline/pass_k8s.c
index 9322f46df..b6a953142 100644
--- a/src/pipeline/pass_k8s.c
+++ b/src/pipeline/pass_k8s.c
@@ -240,6 +240,155 @@ static void handle_helm_chart(cbm_pipeline_ctx_t *ctx, const char *rel_path, con
cbm_log_info("pass.k8s.helm", "file", rel_path, "deps", itoa_k8s(dep_edges));
}
+/* ── Dependency-manifest handler (go.mod / requirements.txt) ──────── */
+
+static bool is_gomod_file(const char *base) {
+ return strcmp(base, "go.mod") == 0;
+}
+
+static bool is_requirements_file(const char *base) {
+ return strcmp(base, "requirements.txt") == 0;
+}
+
+/* Emit a DEPENDS_ON edge from the manifest file node to a (shared, per-project)
+ * external Package node. Mirrors the Helm Chart.yaml DEPENDS_ON shape. */
+static int emit_dep_edge(cbm_pipeline_ctx_t *ctx, const cbm_gbuf_node_t *src, const char *rel_path,
+ const char *ecosystem, const char *name) {
+ if (!name || !name[0]) {
+ return 0;
+ }
+ char dep_qn[CBM_SZ_512];
+ snprintf(dep_qn, sizeof(dep_qn), "%s.__%s_dep__.%s", ctx->project_name, ecosystem, name);
+ char dep_props[CBM_SZ_256];
+ snprintf(dep_props, sizeof(dep_props), "{\"source\":\"%s\",\"external\":true}", ecosystem);
+ int64_t dep_id =
+ cbm_gbuf_upsert_node(ctx->gbuf, "Package", name, dep_qn, rel_path, SKIP_ONE, 0, dep_props);
+ if (dep_id > 0 && dep_id != src->id) {
+ cbm_gbuf_insert_edge(ctx->gbuf, src->id, dep_id, "DEPENDS_ON", "{}");
+ return 1;
+ }
+ return 0;
+}
+
+/* Copy the first whitespace-delimited token of `line` into `out`. */
+static void first_token(const char *line, char *out, size_t out_sz) {
+ out[0] = '\0';
+ while (*line == ' ' || *line == '\t') {
+ line++;
+ }
+ size_t n = 0;
+ while (line[n] && line[n] != ' ' && line[n] != '\t' && line[n] != '\r' && line[n] != '\n' &&
+ n + 1 < out_sz) {
+ out[n] = line[n];
+ n++;
+ }
+ out[n] = '\0';
+}
+
+/* Parse go.mod `require` directives (single-line and block forms) and emit a
+ * DEPENDS_ON edge per dependency. go.mod requires are not surfaced as imports
+ * by the extraction layer, so we parse the manifest text directly here. */
+static int parse_gomod_deps(cbm_pipeline_ctx_t *ctx, const cbm_gbuf_node_t *src,
+ const char *rel_path, const char *source) {
+ int edges = 0;
+ bool in_block = false;
+ const char *p = source;
+ while (p && *p) {
+ const char *eol = strchr(p, '\n');
+ size_t len = eol ? (size_t)(eol - p) : strlen(p);
+ char line[CBM_SZ_512];
+ size_t cp = len < sizeof(line) - 1 ? len : sizeof(line) - 1;
+ memcpy(line, p, cp);
+ line[cp] = '\0';
+ const char *t = line;
+ while (*t == ' ' || *t == '\t') {
+ t++;
+ }
+ if (in_block) {
+ if (t[0] == ')') {
+ in_block = false;
+ } else if (t[0] && t[0] != '/') {
+ char name[CBM_SZ_256];
+ first_token(t, name, sizeof(name));
+ edges += emit_dep_edge(ctx, src, rel_path, "gomod", name);
+ }
+ } else if (strncmp(t, "require", 7) == 0 && (t[7] == ' ' || t[7] == '\t' || t[7] == '(')) {
+ const char *rest = t + 7;
+ while (*rest == ' ' || *rest == '\t') {
+ rest++;
+ }
+ if (*rest == '(') {
+ in_block = true;
+ } else if (*rest) {
+ char name[CBM_SZ_256];
+ first_token(rest, name, sizeof(name));
+ edges += emit_dep_edge(ctx, src, rel_path, "gomod", name);
+ }
+ }
+ if (!eol) {
+ break;
+ }
+ p = eol + 1;
+ }
+ return edges;
+}
+
+/* Parse requirements.txt entries (one package spec per line) and emit a
+ * DEPENDS_ON edge per dependency. The package name is the leading token up to
+ * the first version/extras/comment delimiter. */
+static int parse_requirements_deps(cbm_pipeline_ctx_t *ctx, const cbm_gbuf_node_t *src,
+ const char *rel_path, const char *source) {
+ int edges = 0;
+ const char *p = source;
+ while (p && *p) {
+ const char *eol = strchr(p, '\n');
+ size_t len = eol ? (size_t)(eol - p) : strlen(p);
+ char line[CBM_SZ_512];
+ size_t cp = len < sizeof(line) - 1 ? len : sizeof(line) - 1;
+ memcpy(line, p, cp);
+ line[cp] = '\0';
+ const char *t = line;
+ while (*t == ' ' || *t == '\t') {
+ t++;
+ }
+ /* Skip blanks, comments, options (-r, --hash), and URLs. */
+ if (t[0] && t[0] != '#' && t[0] != '-' && strstr(t, "://") == NULL) {
+ char name[CBM_SZ_256];
+ size_t n = 0;
+ while (t[n] && t[n] != '=' && t[n] != '<' && t[n] != '>' && t[n] != '!' && t[n] != '~' &&
+ t[n] != '[' && t[n] != ';' && t[n] != ' ' && t[n] != '\t' && t[n] != '\r' &&
+ n + 1 < sizeof(name)) {
+ name[n] = t[n];
+ n++;
+ }
+ name[n] = '\0';
+ edges += emit_dep_edge(ctx, src, rel_path, "pypi", name);
+ }
+ if (!eol) {
+ break;
+ }
+ p = eol + 1;
+ }
+ return edges;
+}
+
+static void handle_dep_manifest(cbm_pipeline_ctx_t *ctx, const char *rel_path, const char *source,
+ const char *ecosystem) {
+ if (!source) {
+ return;
+ }
+ char *file_qn = cbm_pipeline_fqn_compute(ctx->project_name, rel_path, "__file__");
+ const cbm_gbuf_node_t *src = file_qn ? cbm_gbuf_find_by_qn(ctx->gbuf, file_qn) : NULL;
+ free(file_qn);
+ if (!src) {
+ return;
+ }
+ int dep_edges = strcmp(ecosystem, "gomod") == 0
+ ? parse_gomod_deps(ctx, src, rel_path, source)
+ : parse_requirements_deps(ctx, src, rel_path, source);
+ cbm_log_info("pass.k8s.depmanifest", "file", rel_path, "deps", itoa_k8s(dep_edges));
+}
+
/* ── Pass entry point ────────────────────────────────────────────── */
int cbm_pipeline_pass_k8s(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files, int file_count) {
@@ -264,7 +413,15 @@ int cbm_pipeline_pass_k8s(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files,
CBMFileResult *cached =
(ctx->result_cache && ctx->result_cache[i]) ? ctx->result_cache[i] : NULL;
- if (cbm_is_kustomize_file(base)) {
+ if (is_gomod_file(base) || lang == CBM_LANG_GOMOD || is_requirements_file(base)) {
+ int dep_len = 0;
+ char *dep_src = k8s_read_file(path, &dep_len);
+ if (dep_src) {
+ handle_dep_manifest(ctx, rel, dep_src,
+ is_requirements_file(base) ? "pypi" : "gomod");
+ free(dep_src);
+ }
+ } else if (cbm_is_kustomize_file(base)) {
handle_kustomize(ctx, path, rel, cached);
kustomize_count++;
} else if (lang == CBM_LANG_YAML || lang == CBM_LANG_K8S) {
diff --git a/src/pipeline/pass_parallel.c b/src/pipeline/pass_parallel.c
index 9992a3106..d7956ced6 100644
--- a/src/pipeline/pass_parallel.c
+++ b/src/pipeline/pass_parallel.c
@@ -777,9 +777,11 @@ static int register_and_link_def(cbm_pipeline_ctx_t *ctx, const CBMDefinition *d
if (!def->name || !def->qualified_name || !def->label) {
return 0;
}
- /* Register callable symbols + Interface — see pass_definitions.c for rationale. */
+ /* Register callable symbols + Interface — see pass_definitions.c for rationale.
+ * Variable/Field defs are registered too so READS/WRITES can resolve. */
if (strcmp(def->label, "Function") == 0 || strcmp(def->label, "Method") == 0 ||
- strcmp(def->label, "Class") == 0 || strcmp(def->label, "Interface") == 0) {
+ strcmp(def->label, "Class") == 0 || strcmp(def->label, "Interface") == 0 ||
+ strcmp(def->label, "Variable") == 0 || strcmp(def->label, "Field") == 0) {
cbm_registry_add(ctx->registry, def->name, def->qualified_name, def->label);
(*reg_entries)++;
}
diff --git a/tests/test_grammar_labels.c b/tests/test_grammar_labels.c
index 5a2bbe85e..e28fcdd72 100644
--- a/tests/test_grammar_labels.c
+++ b/tests/test_grammar_labels.c
@@ -196,7 +196,7 @@ static const LabelGolden LABEL_GOLDENS[] = {
{"cmake", "Function:1,Module:1"},
{"meson", "Module:1"},
{"gn", "Module:1"},
- {"just", "Module:1"},
+ {"just", "Function:1,Module:1"},
{"hcl", "Class:1,Module:1"},
{"nix", "Module:1"},
{"gomod", "Module:1"},