Skip to content

Commit 1d6a140

Browse files
tthayerclaude
andcommitted
feat(pkl): extract calls, branches, throws and decorators
Pkl was wired end-to-end but shallow: on a real-world 172-file Pkl corpus the graph had 386 IMPORTS edges and 0 CALLS edges, because pkl_call_types was empty_types. Pkl has no dedicated call node. `unqualifiedAccessExpr` and `qualifiedAccessExpr` are the same node whether they are a call (`helper(a)`) or a bare property read (`host`) — the only discriminator is an `argumentList` child. extract_pkl_callee gates on that child, and is dispatched from extract_callee_name with an unconditional return: falling through to field-based or generic first-identifier resolution would mint a CALLS edge for every property read in every Pkl file, since a bare access expr's first child is an identifier. Callee resolution: helper(a) -> "helper" utils.fallback(a) -> "utils.fallback" (module-qualified; cbm.c shortens to the last dotted segment) s.trim().toLowerCase() -> "toLowerCase" (a receiver that is itself a call has parens in its text and is not prefixed) new Server { ... } -> "Server" (links to the class def) Also fills the other empty slots in the Pkl spec, all confirmed against real parse trees rather than guessed: typeAlias (class), importGlobClause / importExpr (import), ifExpr / whenGenerator / forGenerator (branch), throwExpr (throw), annotation (decorator). forGenerator is additionally registered in cbm_is_loop_node_type so `for (x in xs)` counts toward loop-nesting depth; the name is Pkl-unique so no other grammar collides. The Pkl repro test moves from the structural battery (dims 1-5) to the full callable battery (dims 1-8) and adds an inline negative assertion that bare property reads do NOT become CALLS edges — the regression the argumentList gate exists to prevent. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Signed-off-by: Tony Thayer-Osborne <tony.thayerosborne@conductorone.com>
1 parent 31b611a commit 1d6a140

4 files changed

Lines changed: 151 additions & 50 deletions

File tree

‎internal/cbm/extract_calls.c‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,65 @@ static char *extract_nickel_callee(CBMArena *a, TSNode node, const char *source,
10651065
return NULL;
10661066
}
10671067

1068+
// Pkl: `unqualifiedAccessExpr` / `qualifiedAccessExpr` are the same node whether
1069+
// they are a call (`helper(a)`) or a bare property read (`host`) — the only
1070+
// discriminator is an `argumentList` child, so both are gated on it. For a
1071+
// qualified call the method name is the `identifier` child that is not the
1072+
// `receiver`; the receiver is prefixed only when it is itself a plain name
1073+
// (`utils.fallback(a)` -> "utils.fallback", module-qualified, which cbm.c
1074+
// shortens to the last dotted segment when resolving). A receiver that is itself
1075+
// a call must NOT be prefixed: `s.trim().toLowerCase()` -> "toLowerCase", since
1076+
// the receiver's text carries parens and would never resolve.
1077+
// `newExpr` resolves to its `declaredType` so `new Server {}` links to the class.
1078+
static char *extract_pkl_callee(CBMArena *a, TSNode node, const char *source, const char *nk) {
1079+
if (strcmp(nk, "newExpr") == 0) {
1080+
// `new { ... }` with an inferred type has no declaredType child.
1081+
TSNode dt = cbm_find_child_by_kind(node, "declaredType");
1082+
return ts_node_is_null(dt) ? NULL : cbm_node_text(a, dt, source);
1083+
}
1084+
1085+
bool qualified = strcmp(nk, "qualifiedAccessExpr") == 0;
1086+
if (!qualified && strcmp(nk, "unqualifiedAccessExpr") != 0) {
1087+
return NULL;
1088+
}
1089+
// No argument list -> property read, not a call.
1090+
if (ts_node_is_null(cbm_find_child_by_kind(node, "argumentList"))) {
1091+
return NULL;
1092+
}
1093+
1094+
TSNode recv = ts_node_child_by_field_name(node, TS_FIELD("receiver"));
1095+
TSNode name = (TSNode){0};
1096+
uint32_t nc = ts_node_named_child_count(node);
1097+
for (uint32_t i = 0; i < nc; i++) {
1098+
TSNode child = ts_node_named_child(node, i);
1099+
if (!ts_node_is_null(recv) && ts_node_eq(child, recv)) {
1100+
continue;
1101+
}
1102+
if (strcmp(ts_node_type(child), "identifier") == 0) {
1103+
name = child;
1104+
break;
1105+
}
1106+
}
1107+
if (ts_node_is_null(name)) {
1108+
return NULL;
1109+
}
1110+
char *mn = cbm_node_text(a, name, source);
1111+
if (!mn || !mn[0]) {
1112+
return NULL;
1113+
}
1114+
if (!qualified || ts_node_is_null(recv)) {
1115+
return mn;
1116+
}
1117+
if (strcmp(ts_node_type(recv), "unqualifiedAccessExpr") == 0 &&
1118+
ts_node_is_null(cbm_find_child_by_kind(recv, "argumentList"))) {
1119+
char *rt = cbm_node_text(a, recv, source);
1120+
if (rt && rt[0]) {
1121+
return cbm_arena_sprintf(a, "%s.%s", rt, mn);
1122+
}
1123+
}
1124+
return mn;
1125+
}
1126+
10681127
// Typst: a `call` node's callee is its `item` field (an ident), matching the
10691128
// def-side resolution of `#let greet(name) = ...`.
10701129
static char *extract_typst_callee(CBMArena *a, TSNode node, const char *source, const char *nk) {
@@ -1692,6 +1751,15 @@ static char *extract_callee_name(CBMArena *a, TSNode node, const char *source, C
16921751
}
16931752
}
16941753

1754+
/* Pkl: resolve here and return unconditionally — the access-expr call node
1755+
* types double as plain property reads, so falling through to field-based or
1756+
* generic first-identifier resolution would mint a CALLS edge for every
1757+
* property read (a bare `host` has an `identifier` first child, which the
1758+
* generic fallback would happily emit). NULL here means "not a call". */
1759+
if (lang == CBM_LANG_PKL) {
1760+
return extract_pkl_callee(a, node, source, ts_node_type(node));
1761+
}
1762+
16951763
// Helm / Go templates: resolve `include "x"` / `template "x"` to the
16961764
// referenced named template so it links to the define'd Function (#338).
16971765
if (lang == CBM_LANG_GOTEMPLATE) {

‎internal/cbm/helpers.c‎

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -580,29 +580,14 @@ int cbm_count_branching(TSNode node, const char **branching_types) {
580580

581581
// Loop node-type names across tree-sitter grammars, for loop-nesting depth.
582582
bool cbm_is_loop_node_type(const char *kind) {
583-
static const char *const loops[] = {"for_statement",
584-
"while_statement",
585-
"do_statement",
586-
"do_while_statement",
587-
"for_in_statement",
588-
"for_of_statement",
589-
"for_each_statement",
590-
"foreach_statement",
591-
"enhanced_for_statement",
592-
"for_range_loop",
593-
"c_style_for_statement",
594-
"for_expression",
595-
"while_expression",
596-
"loop_expression",
597-
"while_let_expression",
598-
"repeat_statement",
599-
"repeat_while_statement",
600-
"until",
601-
"while_modifier",
602-
"until_modifier",
603-
"for",
604-
"while",
605-
NULL};
583+
static const char *const loops[] = {
584+
"for_statement", "while_statement", "do_statement", "do_while_statement",
585+
"for_in_statement", "for_of_statement", "for_each_statement", "foreach_statement",
586+
"enhanced_for_statement", "for_range_loop", "c_style_for_statement", "for_expression",
587+
"while_expression", "loop_expression", "while_let_expression", "repeat_statement",
588+
"repeat_while_statement",
589+
// Pkl: `for (x in xs) { ... }` inside an object body.
590+
"forGenerator", "until", "while_modifier", "until_modifier", "for", "while", NULL};
606591
for (const char *const *l = loops; *l; l++) {
607592
if (strcmp(kind, *l) == 0) {
608593
return true;

‎internal/cbm/lang_specs.c‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,11 +1586,21 @@ static const char *tlaplus_branch_types[] = {"if_then_else", "case", NULL};
15861586
static const char *tlaplus_var_types[] = {"variable_declaration", NULL};
15871587
static const char *tlaplus_module_types[] = {"source_file", NULL};
15881588
static const char *pkl_func_types[] = {"classMethod", "objectMethod", NULL};
1589-
static const char *pkl_class_types[] = {"clazz", NULL};
1590-
static const char *pkl_import_types[] = {"importClause", "extendsOrAmendsClause", "extends",
1591-
"import", NULL};
1589+
static const char *pkl_class_types[] = {"clazz", "typeAlias", NULL};
1590+
static const char *pkl_import_types[] = {
1591+
"importClause", "importGlobClause", "importExpr", "extendsOrAmendsClause",
1592+
"extends", "import", NULL};
15921593
static const char *pkl_var_types[] = {"classProperty", "objectProperty", NULL};
15931594
static const char *pkl_module_types[] = {"module", NULL};
1595+
/* Both access exprs double as plain property reads; extract_pkl_callee keeps
1596+
* only the ones carrying an argumentList. `newExpr` resolves to its type. */
1597+
static const char *pkl_call_types[] = {"unqualifiedAccessExpr", "qualifiedAccessExpr", "newExpr",
1598+
NULL};
1599+
/* Control-flow only, matching every other spec (short-circuit operators are
1600+
* deliberately excluded). `forGenerator` is also a loop — see helpers.c. */
1601+
static const char *pkl_branch_types[] = {"ifExpr", "whenGenerator", "forGenerator", NULL};
1602+
static const char *pkl_throw_types[] = {"throwExpr", NULL};
1603+
static const char *pkl_decorator_types[] = {"annotation", NULL};
15941604
static const char *gomod_var_types[] = {"require_directive", "replace_directive", NULL};
15951605
static const char *gomod_import_types[] = {"require", NULL};
15961606
static const char *gomod_module_types[] = {"source_file", NULL};
@@ -2616,9 +2626,9 @@ static const CBMLangSpec lang_specs[CBM_LANG_COUNT] = {
26162626

26172627
// CBM_LANG_PKL
26182628
[CBM_LANG_PKL] = {CBM_LANG_PKL, pkl_func_types, pkl_class_types, empty_types, pkl_module_types,
2619-
empty_types, pkl_import_types, empty_types, empty_types, pkl_var_types,
2620-
empty_types, empty_types, NULL, empty_types, NULL, NULL, tree_sitter_pkl,
2621-
NULL},
2629+
pkl_call_types, pkl_import_types, empty_types, pkl_branch_types,
2630+
pkl_var_types, empty_types, pkl_throw_types, NULL, pkl_decorator_types, NULL,
2631+
NULL, tree_sitter_pkl, NULL},
26222632

26232633
// CBM_LANG_GOMOD
26242634
[CBM_LANG_GOMOD] = {CBM_LANG_GOMOD, empty_types, empty_types, empty_types, gomod_module_types,

‎tests/repro/repro_grammar_config.c‎

Lines changed: 59 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
* 6. calls-extracted : inv_has_call(r, callee) == 1.
4646
* Only asserted for languages that have non-empty
4747
* call_types: HCL (function_call), NICKEL (infix_expr),
48-
* JSONNET (functioncall), STARLARK (call).
48+
* JSONNET (functioncall), STARLARK (call),
49+
* PKL (unqualified/qualifiedAccessExpr, newExpr).
4950
*
5051
* FULL-PIPELINE (rh_index_files -> cbm_store_t*, via inv_count_* store helpers):
5152
* 7. callable-sourcing : inv_count_calls_by_source(store,project,&mod,&call).
@@ -85,11 +86,15 @@
8586
* Dims 1-5 ("Class"). No calls.
8687
* XML -- class_types = element -> "Class". Dims 1-5 ("Class"). No calls.
8788
* PROPERTIES -- var_types = property -> "Variable". Dims 1-5 ("Variable"). No calls.
88-
* PKL -- func_types = classMethod/objectMethod -> "Function";
89-
* class_types = clazz -> "Class"; var_types = classProperty/objectProperty.
90-
* call_types = empty_types. Dims 1-5 ("Function", "Class"). No call dim.
9189
*
9290
* LANGUAGES WITH CALLABLES (dims 1-6 + R, and pipeline dims 7-8 where applicable):
91+
* PKL -- func_types = classMethod/objectMethod -> "Function";
92+
* class_types = clazz/typeAlias -> "Class";
93+
* var_types = classProperty/objectProperty;
94+
* call_types = unqualifiedAccessExpr/qualifiedAccessExpr/newExpr.
95+
* Dims 1-8. The access-expr call types double as property reads,
96+
* so extract_pkl_callee gates them on an `argumentList` child;
97+
* the test adds an inline negative assertion for that gate.
9398
* HCL -- class_types = block -> "Class"; var_types = attribute;
9499
* call_types = function_call. Dims 1-6. No func_types so no pipeline
95100
* dim 7 (calls would be module-sourced with no Function anchor).
@@ -766,39 +771,72 @@ TEST(repro_grammar_config_ron) {
766771

767772
/* ── PKL ──────────────────────────────────────────────────────────────────────
768773
* Idiomatic PKL (Apple Pkl) module with a class definition
769-
* (pkl_class_types = {"clazz"} -> "Class"), a method inside it
774+
* (pkl_class_types = {"clazz", "typeAlias"} -> "Class"), methods inside it
770775
* (pkl_func_types = {"classMethod", "objectMethod"} -> "Function"), and
771776
* class properties (pkl_var_types = {"classProperty", "objectProperty"}).
772-
* pkl_call_types = empty_types so no call extraction occurs.
773-
*
774-
* Dims asserted: 1-5 + R ("Class" for the class def, "Function" for the method).
775-
* Dims 6-8 SKIPPED: call_types = empty_types in spec.
776-
* Expected GREEN: dims 1-5. Dim 5 RED would indicate clazz->Class or
777-
* classMethod->Function mapping is broken in the PKL grammar walker.
777+
* pkl_call_types = {"unqualifiedAccessExpr", "qualifiedAccessExpr", "newExpr"}.
778+
*
779+
* Dims asserted: 1-8 (full battery) + R.
780+
* Dim 6 GREEN: `makeUrl(host, port)` inside url() extracts callee "makeUrl".
781+
* Dim 7 GREEN: every call site in the fixture is inside a classMethod body, so
782+
* no CALLS edge is Module-sourced. (Real-world Pkl does call at module level;
783+
* the fixture deliberately avoids it because dim 7 treats Module-sourced
784+
* in-body calls as the enclosing-func gap.)
785+
* Dim 8 GREEN: makeUrl and Server are both defined in-file, so neither the
786+
* unqualified call nor the newExpr constructor edge dangles.
787+
*
788+
* PKL-SPECIFIC REGRESSION (asserted inline below): `unqualifiedAccessExpr` and
789+
* `qualifiedAccessExpr` are the same node for a call and for a bare property
790+
* read, so the interpolated `host` / `port` reads inside makeUrl must NOT be
791+
* emitted as CALLS. extract_pkl_callee gates on an `argumentList` child; without
792+
* that gate every property read in every Pkl file becomes a call edge.
778793
*/
779794
TEST(repro_grammar_config_pkl) {
780795
static const char src[] =
781796
"module cbm.Config\n"
782797
"\n"
783-
"function makeUrl(host: String, port: Int): String = \"http://\\(host):\\(port)\"\n"
798+
"typealias Port = Int\n"
799+
"\n"
800+
"function makeUrl(host: String, port: Port): String = \"http://\\(host):\\(port)\"\n"
784801
"\n"
785802
"class Server {\n"
786803
" host: String = \"localhost\"\n"
787-
" port: Int = 8080\n"
804+
" port: Port = 8080\n"
788805
" tls: Boolean = false\n"
789806
"\n"
790-
" function url(): String = \"http://\\(host):\\(port)\"\n"
791-
"}\n"
807+
" function url(): String = makeUrl(host, port)\n"
792808
"\n"
793-
"server = new Server {\n"
794-
" host = \"0.0.0.0\"\n"
795-
" port = 9000\n"
809+
" function clone(): Server = new Server { host = host }\n"
796810
"}\n";
797811
static const char bad[] = "module cbm.Config\nclass Server {\n host:";
798-
if (config_struct_battery("PKL", src, CBM_LANG_PKL, "config.pkl",
799-
"Class", "Function") != 0)
812+
if (config_callable_battery("PKL", src, CBM_LANG_PKL, "config.pkl",
813+
"Function", "makeUrl") != 0)
814+
return 1;
815+
816+
/* Bare property reads must not be calls (see PKL-SPECIFIC REGRESSION above). */
817+
CBMFileResult *pr = inv_rx(src, CBM_LANG_PKL, "config.pkl");
818+
if (!pr) {
819+
printf(" %sFAIL%s [PKL] inv_rx returned NULL\n", tf_red(), tf_reset());
820+
return 1;
821+
}
822+
int bogus = 0;
823+
for (int i = 0; i < pr->calls.count; i++) {
824+
const char *cn = pr->calls.items[i].callee_name;
825+
if (cn && (strcmp(cn, "host") == 0 || strcmp(cn, "port") == 0 ||
826+
strcmp(cn, "tls") == 0)) {
827+
bogus++;
828+
}
829+
}
830+
cbm_free_result(pr);
831+
if (bogus != 0) {
832+
printf(" %sFAIL%s [PKL] property-read-not-call: %d bare property read(s) "
833+
"emitted as a CALLS edge\n", tf_red(), tf_reset(), bogus);
834+
return 1;
835+
}
836+
837+
if (config_robustness("PKL", bad, CBM_LANG_PKL, "config.pkl") != 0)
800838
return 1;
801-
return config_robustness("PKL", bad, CBM_LANG_PKL, "config.pkl");
839+
return config_pipeline_battery("PKL", "config.pkl", src);
802840
}
803841

804842
/* ── NICKEL ───────────────────────────────────────────────────────────────────

0 commit comments

Comments
 (0)