Skip to content

Commit 54a64c8

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 d6be58e commit 54a64c8

4 files changed

Lines changed: 144 additions & 27 deletions

File tree

‎internal/cbm/extract_calls.c‎

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

789+
// Pkl: `unqualifiedAccessExpr` / `qualifiedAccessExpr` are the same node whether
790+
// they are a call (`helper(a)`) or a bare property read (`host`) — the only
791+
// discriminator is an `argumentList` child, so both are gated on it. For a
792+
// qualified call the method name is the `identifier` child that is not the
793+
// `receiver`; the receiver is prefixed only when it is itself a plain name
794+
// (`utils.fallback(a)` -> "utils.fallback", module-qualified, which cbm.c
795+
// shortens to the last dotted segment when resolving). A receiver that is itself
796+
// a call must NOT be prefixed: `s.trim().toLowerCase()` -> "toLowerCase", since
797+
// the receiver's text carries parens and would never resolve.
798+
// `newExpr` resolves to its `declaredType` so `new Server {}` links to the class.
799+
static char *extract_pkl_callee(CBMArena *a, TSNode node, const char *source, const char *nk) {
800+
if (strcmp(nk, "newExpr") == 0) {
801+
// `new { ... }` with an inferred type has no declaredType child.
802+
TSNode dt = cbm_find_child_by_kind(node, "declaredType");
803+
return ts_node_is_null(dt) ? NULL : cbm_node_text(a, dt, source);
804+
}
805+
806+
bool qualified = strcmp(nk, "qualifiedAccessExpr") == 0;
807+
if (!qualified && strcmp(nk, "unqualifiedAccessExpr") != 0) {
808+
return NULL;
809+
}
810+
// No argument list -> property read, not a call.
811+
if (ts_node_is_null(cbm_find_child_by_kind(node, "argumentList"))) {
812+
return NULL;
813+
}
814+
815+
TSNode recv = ts_node_child_by_field_name(node, TS_FIELD("receiver"));
816+
TSNode name = (TSNode){0};
817+
uint32_t nc = ts_node_named_child_count(node);
818+
for (uint32_t i = 0; i < nc; i++) {
819+
TSNode child = ts_node_named_child(node, i);
820+
if (!ts_node_is_null(recv) && ts_node_eq(child, recv)) {
821+
continue;
822+
}
823+
if (strcmp(ts_node_type(child), "identifier") == 0) {
824+
name = child;
825+
break;
826+
}
827+
}
828+
if (ts_node_is_null(name)) {
829+
return NULL;
830+
}
831+
char *mn = cbm_node_text(a, name, source);
832+
if (!mn || !mn[0]) {
833+
return NULL;
834+
}
835+
if (!qualified || ts_node_is_null(recv)) {
836+
return mn;
837+
}
838+
if (strcmp(ts_node_type(recv), "unqualifiedAccessExpr") == 0 &&
839+
ts_node_is_null(cbm_find_child_by_kind(recv, "argumentList"))) {
840+
char *rt = cbm_node_text(a, recv, source);
841+
if (rt && rt[0]) {
842+
return cbm_arena_sprintf(a, "%s.%s", rt, mn);
843+
}
844+
}
845+
return mn;
846+
}
847+
789848
// Typst: a `call` node's callee is its `item` field (an ident), matching the
790849
// def-side resolution of `#let greet(name) = ...`.
791850
static char *extract_typst_callee(CBMArena *a, TSNode node, const char *source, const char *nk) {
@@ -1350,6 +1409,15 @@ static char *extract_callee_name(CBMArena *a, TSNode node, const char *source, C
13501409
}
13511410
}
13521411

1412+
/* Pkl: resolve here and return unconditionally — the access-expr call node
1413+
* types double as plain property reads, so falling through to field-based or
1414+
* generic first-identifier resolution would mint a CALLS edge for every
1415+
* property read (a bare `host` has an `identifier` first child, which the
1416+
* generic fallback would happily emit). NULL here means "not a call". */
1417+
if (lang == CBM_LANG_PKL) {
1418+
return extract_pkl_callee(a, node, source, ts_node_type(node));
1419+
}
1420+
13531421
// Helm / Go templates: resolve `include "x"` / `template "x"` to the
13541422
// referenced named template so it links to the define'd Function (#338).
13551423
if (lang == CBM_LANG_GOTEMPLATE) {

‎internal/cbm/helpers.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,8 @@ bool cbm_is_loop_node_type(const char *kind) {
540540
"while_let_expression",
541541
"repeat_statement",
542542
"repeat_while_statement",
543+
// Pkl: `for (x in xs) { ... }` inside an object body.
544+
"forGenerator",
543545
"until",
544546
"while_modifier",
545547
"until_modifier",

‎internal/cbm/lang_specs.c‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,11 +1557,20 @@ static const char *tlaplus_branch_types[] = {"if_then_else", "case", NULL};
15571557
static const char *tlaplus_var_types[] = {"variable_declaration", NULL};
15581558
static const char *tlaplus_module_types[] = {"source_file", NULL};
15591559
static const char *pkl_func_types[] = {"classMethod", "objectMethod", NULL};
1560-
static const char *pkl_class_types[] = {"clazz", NULL};
1561-
static const char *pkl_import_types[] = {"importClause", "extendsOrAmendsClause", "extends",
1562-
"import", NULL};
1560+
static const char *pkl_class_types[] = {"clazz", "typeAlias", NULL};
1561+
static const char *pkl_import_types[] = {"importClause", "importGlobClause", "importExpr",
1562+
"extendsOrAmendsClause", "extends", "import", NULL};
15631563
static const char *pkl_var_types[] = {"classProperty", "objectProperty", NULL};
15641564
static const char *pkl_module_types[] = {"module", NULL};
1565+
/* Both access exprs double as plain property reads; extract_pkl_callee keeps
1566+
* only the ones carrying an argumentList. `newExpr` resolves to its type. */
1567+
static const char *pkl_call_types[] = {"unqualifiedAccessExpr", "qualifiedAccessExpr", "newExpr",
1568+
NULL};
1569+
/* Control-flow only, matching every other spec (short-circuit operators are
1570+
* deliberately excluded). `forGenerator` is also a loop — see helpers.c. */
1571+
static const char *pkl_branch_types[] = {"ifExpr", "whenGenerator", "forGenerator", NULL};
1572+
static const char *pkl_throw_types[] = {"throwExpr", NULL};
1573+
static const char *pkl_decorator_types[] = {"annotation", NULL};
15651574
static const char *gomod_var_types[] = {"require_directive", "replace_directive", NULL};
15661575
static const char *gomod_import_types[] = {"require", NULL};
15671576
static const char *gomod_module_types[] = {"source_file", NULL};
@@ -2562,9 +2571,9 @@ static const CBMLangSpec lang_specs[CBM_LANG_COUNT] = {
25622571

25632572
// CBM_LANG_PKL
25642573
[CBM_LANG_PKL] = {CBM_LANG_PKL, pkl_func_types, pkl_class_types, empty_types, pkl_module_types,
2565-
empty_types, pkl_import_types, empty_types, empty_types, pkl_var_types,
2566-
empty_types, empty_types, NULL, empty_types, NULL, NULL, tree_sitter_pkl,
2567-
NULL},
2574+
pkl_call_types, pkl_import_types, empty_types, pkl_branch_types,
2575+
pkl_var_types, empty_types, pkl_throw_types, NULL, pkl_decorator_types, NULL,
2576+
NULL, tree_sitter_pkl, NULL},
25682577

25692578
// CBM_LANG_GOMOD
25702579
[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)