MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / parse_powershell_imports

Function parse_powershell_imports

internal/cbm/extract_imports.c:1744–1785  ·  view source on GitHub ↗

--- 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.

Source from the content-addressed store, hash-verified

1742// generic_token child of command_elements (after the "module"/"namespace"/
1743// "assembly" qualifier). DFS for such commands.
1744static void parse_powershell_imports(CBMExtractCtx *ctx) {
1745 CBMArena *a = ctx->arena;
1746 TSNodeStack stack;
1747 ts_nstack_init(&stack, ctx, CBM_SZ_512);
1748 ts_nstack_push(&stack, ctx->root);
1749 while (stack.count > 0) {
1750 TSNode node = ts_nstack_pop(&stack);
1751 if (strcmp(ts_node_type(node), "command") == 0) {
1752 TSNode name = ts_node_child_by_field_name(node, TS_FIELD("command_name"));
1753 char *nm = ts_node_is_null(name) ? NULL : cbm_node_text(a, name, ctx->source);
1754 if (nm && strcmp(nm, "using") == 0) {
1755 /* Find the last generic_token anywhere under the command — that
1756 * is the module path / namespace / assembly being imported. */
1757 TSNodeStack inner;
1758 ts_nstack_init(&inner, ctx, CBM_SZ_512);
1759 ts_nstack_push(&inner, node);
1760 const char *last_tok = NULL;
1761 uint32_t last_start = 0;
1762 while (inner.count > 0) {
1763 TSNode c = ts_nstack_pop(&inner);
1764 if (strcmp(ts_node_type(c), "generic_token") == 0) {
1765 char *t = cbm_node_text(a, c, ctx->source);
1766 uint32_t sb = ts_node_start_byte(c);
1767 if (t && t[0] && strcmp(t, "module") != 0 && strcmp(t, "namespace") != 0 &&
1768 strcmp(t, "assembly") != 0 && (last_tok == NULL || sb > last_start)) {
1769 last_tok = t;
1770 last_start = sb;
1771 }
1772 }
1773 ts_nstack_push_children(&inner, c);
1774 }
1775 if (last_tok && last_tok[0]) {
1776 CBMImport imp = {.local_name = path_last(a, last_tok),
1777 .module_path = (char *)last_tok};
1778 cbm_imports_push(&ctx->result->imports, a, imp);
1779 }
1780 }
1781 continue; /* commands don't nest imports further */
1782 }
1783 ts_nstack_push_children(&stack, node);
1784 }
1785}
1786
1787// --- Lisp-family imports (Scheme, Racket) ---
1788// S-expression grammars model (import (lib ...)) / (require mod) / (load "f")

Callers 1

cbm_extract_importsFunction · 0.85

Calls 7

ts_nstack_initFunction · 0.85
ts_nstack_pushFunction · 0.85
ts_nstack_popFunction · 0.85
cbm_node_textFunction · 0.85
ts_nstack_push_childrenFunction · 0.85
path_lastFunction · 0.85
cbm_imports_pushFunction · 0.85

Tested by

no test coverage detected