Skip to content

Commit 3bb1735

Browse files
turtacnclaude
andcommitted
feat(perl): merge wave-3 — CROSS-FILE LSP (the DeusData#1 gap), Moose attrs, push/unshift @isa, Exporter model, require imports
Merges worktree-agent-a024837bce6f596c2 onto main. THE headline: implements cbm_run_perl_lsp_cross (perl_lsp.c) and wires CBM_LANG_PERL into pass_lsp_cross.c (capability gate + cbm_pxc_run_one switch) — Perl multi-file repos now get resolved cross-file CALLS edges (was the single biggest Perl gap, rated 0/5 in the resolver playbook). Plus Moose/Moo 'has' synthetic accessors + extends/with inheritance, push/unshift @isa as inheritance sources, @export vs @EXPORT_OK default-export model, and require-expression imports. Conflict resolution: perl_lsp.c (main's Corinna + worktree's Moose both additive — kept both), test files (took the worktree's new cross-file/Moose/ISA test bodies + registrations; dropped a stale Corinna-disabled comment main had already superseded). 15 new perl tests, registration-definition verified consistent. Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011DnF6i8nxpNUYsNE3tdVvM
2 parents c6ff9e1 + 7315793 commit 3bb1735

14 files changed

Lines changed: 1689 additions & 47 deletions

‎internal/cbm/extract_defs.c‎

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6086,6 +6086,70 @@ static bool is_perl_var_type(const char *ck) {
60866086
strcmp(ck, "scalar") == 0 || strcmp(ck, "array") == 0 || strcmp(ck, "hash") == 0;
60876087
}
60886088

6089+
// Append the words found in a Perl export-list RHS (quoted_word_list blobs —
6090+
// ONE string_content carries the whole space-separated list — plus discrete
6091+
// string literals) into buf as a '|'-joined list. Depth-capped; skips the
6092+
// LHS variable_declaration subtree (it contains no strings anyway).
6093+
static void perl_export_words_walk(CBMExtractCtx *ctx, TSNode node, char *buf, size_t cap,
6094+
size_t *len, int depth) {
6095+
if (ts_node_is_null(node) || depth > 4) {
6096+
return;
6097+
}
6098+
const char *k = ts_node_type(node);
6099+
if (strcmp(k, "variable_declaration") == 0) {
6100+
return;
6101+
}
6102+
if (strcmp(k, "quoted_word_list") == 0 || strcmp(k, "string_literal") == 0) {
6103+
TSNode content = ts_node_child_by_field_name(node, TS_FIELD("content"));
6104+
if (ts_node_is_null(content)) {
6105+
uint32_t nc = ts_node_named_child_count(node);
6106+
for (uint32_t i = 0; i < nc; i++) {
6107+
TSNode c = ts_node_named_child(node, i);
6108+
if (strcmp(ts_node_type(c), "string_content") == 0) {
6109+
content = c;
6110+
break;
6111+
}
6112+
}
6113+
}
6114+
if (ts_node_is_null(content)) {
6115+
return;
6116+
}
6117+
char *blob = cbm_node_text(ctx->arena, content, ctx->source);
6118+
if (!blob) {
6119+
return;
6120+
}
6121+
const char *p = blob;
6122+
while (*p) {
6123+
while (*p && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')) {
6124+
p++;
6125+
}
6126+
const char *start = p;
6127+
while (*p && *p != ' ' && *p != '\t' && *p != '\n' && *p != '\r') {
6128+
p++;
6129+
}
6130+
size_t wl = (size_t)(p - start);
6131+
if (wl == 0 || start[0] == ':' || start[0] == '$' || start[0] == '@' ||
6132+
start[0] == '%') {
6133+
continue; /* tags and variable exports are not callable names */
6134+
}
6135+
if (*len + wl + 2 >= cap) {
6136+
return; /* full — keep what fits (whole words only) */
6137+
}
6138+
if (*len > 0) {
6139+
buf[(*len)++] = '|';
6140+
}
6141+
memcpy(buf + *len, start, wl);
6142+
*len += wl;
6143+
buf[*len] = '\0';
6144+
}
6145+
return;
6146+
}
6147+
uint32_t nc = ts_node_named_child_count(node);
6148+
for (uint32_t i = 0; i < nc && i < 64; i++) {
6149+
perl_export_words_walk(ctx, ts_node_named_child(node, i), buf, cap, len, depth + 1);
6150+
}
6151+
}
6152+
60896153
// Perl variable extraction: handle direct variable nodes and assignment_expression.
60906154
static void extract_perl_vars(CBMExtractCtx *ctx, TSNode node, CBMArena *a) {
60916155
uint32_t n = ts_node_named_child_count(node);
@@ -6116,7 +6180,27 @@ static void extract_perl_vars(CBMExtractCtx *ctx, TSNode node, CBMArena *a) {
61166180
}
61176181
}
61186182
}
6119-
push_var_def(ctx, strip_perl_sigil(cbm_node_text(a, left, ctx->source)), node);
6183+
char *pv_name = strip_perl_sigil(cbm_node_text(a, left, ctx->source));
6184+
push_var_def(ctx, pv_name, node);
6185+
/* perl-exports-model: `our @EXPORT = qw(...)` (and @EXPORT_OK) carry
6186+
* the module's Exporter surface. Store the '|'-joined word list on the
6187+
* just-pushed Variable def's return_type so the cross-file LSP can
6188+
* resolve `use Mod;` (no import list) to Mod's @EXPORT defaults.
6189+
* Pointer-compare the def's name to confirm push_var_def did not skip
6190+
* the row (empty/"_" names are dropped there). */
6191+
if (pv_name &&
6192+
(strcmp(pv_name, "EXPORT") == 0 || strcmp(pv_name, "EXPORT_OK") == 0) &&
6193+
ctx->result->defs.count > 0 &&
6194+
ctx->result->defs.items[ctx->result->defs.count - 1].name == pv_name) {
6195+
char words[1024];
6196+
size_t wlen = 0;
6197+
words[0] = '\0';
6198+
perl_export_words_walk(ctx, child, words, sizeof(words), &wlen, 0);
6199+
if (wlen > 0) {
6200+
ctx->result->defs.items[ctx->result->defs.count - 1].return_type =
6201+
cbm_arena_strdup(a, words);
6202+
}
6203+
}
61206204
return;
61216205
}
61226206
}

‎internal/cbm/extract_imports.c‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,76 @@ static void generic_import_from_text(CBMExtractCtx *ctx, TSNode node) {
10111011
}
10121012
}
10131013

1014+
// --- Perl require imports ---
1015+
// `require Foo::Bar;` parses as expression_statement > require_expression with
1016+
// a bareword (or 'Foo/Bar.pm' string) operand — never a use_statement, so the
1017+
// generic top-level use scan cannot see it. The common patterns are
1018+
// CONDITIONAL (`if (...) { require Foo; }`, `eval { require JSON::XS; 1 }`),
1019+
// so walk the WHOLE tree for require_expression (a named node — cheap) and
1020+
// emit rows only for literal barewords and 'Foo/Bar.pm' string operands;
1021+
// variables are skipped. Depth-capped for pathological nesting.
1022+
static void perl_require_import_row(CBMExtractCtx *ctx, const char *module) {
1023+
if (!module || !module[0]) {
1024+
return;
1025+
}
1026+
CBMImport imp = {.local_name = path_last(ctx->arena, module), .module_path = module};
1027+
cbm_imports_push(&ctx->result->imports, ctx->arena, imp);
1028+
}
1029+
1030+
static void perl_collect_require_imports(CBMExtractCtx *ctx, TSNode node, int depth) {
1031+
enum { PERL_REQUIRE_MAX_DEPTH = 200 };
1032+
if (ts_node_is_null(node) || depth > PERL_REQUIRE_MAX_DEPTH) {
1033+
return;
1034+
}
1035+
if (strcmp(ts_node_type(node), "require_expression") == 0) {
1036+
uint32_t nc = ts_node_named_child_count(node);
1037+
for (uint32_t i = 0; i < nc; i++) {
1038+
TSNode c = ts_node_named_child(node, i);
1039+
const char *ck = ts_node_type(c);
1040+
if (strcmp(ck, "bareword") == 0 || strcmp(ck, "package") == 0) {
1041+
perl_require_import_row(ctx, cbm_node_text(ctx->arena, c, ctx->source));
1042+
} else if (strcmp(ck, "string_literal") == 0) {
1043+
/* require 'Legacy/Helper.pm' → Legacy::Helper */
1044+
char *raw = strip_quotes(ctx->arena, cbm_node_text(ctx->arena, c, ctx->source));
1045+
size_t n = raw ? strlen(raw) : 0;
1046+
if (n > 3 && strcmp(raw + n - 3, ".pm") == 0) {
1047+
raw[n - 3] = '\0';
1048+
size_t segs = 0;
1049+
for (const char *p = raw; *p; p++) {
1050+
if (*p == '/') {
1051+
segs++;
1052+
}
1053+
}
1054+
char *pkg = (char *)cbm_arena_alloc(ctx->arena, n + segs + 1);
1055+
if (pkg) {
1056+
size_t w = 0;
1057+
for (const char *p = raw; *p; p++) {
1058+
if (*p == '/') {
1059+
pkg[w++] = ':';
1060+
pkg[w++] = ':';
1061+
} else {
1062+
pkg[w++] = *p;
1063+
}
1064+
}
1065+
pkg[w] = '\0';
1066+
perl_require_import_row(ctx, pkg);
1067+
}
1068+
}
1069+
}
1070+
/* `require v5.36` / scalar operands: no import row. */
1071+
}
1072+
return;
1073+
}
1074+
uint32_t nc = ts_node_named_child_count(node);
1075+
for (uint32_t i = 0; i < nc; i++) {
1076+
perl_collect_require_imports(ctx, ts_node_named_child(node, i), depth + 1);
1077+
}
1078+
}
1079+
1080+
static void parse_perl_require_imports(CBMExtractCtx *ctx) {
1081+
perl_collect_require_imports(ctx, ctx->root, 0);
1082+
}
1083+
10141084
static void parse_generic_imports(CBMExtractCtx *ctx, const char *node_type) {
10151085
/* Use TSTreeCursor for O(1)-per-step sibling traversal. */
10161086
TSTreeCursor cursor = ts_tree_cursor_new(ctx->root);
@@ -3058,6 +3128,7 @@ void cbm_extract_imports(CBMExtractCtx *ctx) {
30583128
break;
30593129
case CBM_LANG_PERL:
30603130
parse_generic_imports(ctx, "use_statement");
3131+
parse_perl_require_imports(ctx);
30613132
break;
30623133
case CBM_LANG_GROOVY:
30633134
parse_generic_imports(ctx, "groovy_import");

‎internal/cbm/lang_specs.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,10 @@ static const char *perl_module_types[] = {"source_file", NULL};
615615
static const char *perl_call_types[] = {"ambiguous_function_call_expression",
616616
"function_call_expression", "func1op_call_expression",
617617
"method_call_expression", NULL};
618-
static const char *perl_import_types[] = {"use_statement", "require_statement", "require", NULL};
618+
/* require parses as expression_statement > require_expression (the previously
619+
* listed require_statement/require node kinds do not exist in the vendored
620+
* grammar — phantom names that never matched). */
621+
static const char *perl_import_types[] = {"use_statement", "require_expression", NULL};
619622
static const char *perl_branch_types[] = {"if_statement", "unless_statement", "for_statement",
620623
"foreach_statement", "while_statement", NULL};
621624
static const char *perl_var_types[] = {"variable_declaration", "expression_statement", NULL};

0 commit comments

Comments
 (0)