Skip to content

Commit 37e535e

Browse files
authored
Merge pull request DeusData#2457 from Zhuoxi2000/fix-haskell-infix-def-name
fix(haskell): name infix-form definitions after the operator
2 parents 402cc81 + 954350d commit 37e535e

3 files changed

Lines changed: 66 additions & 0 deletions

File tree

‎internal/cbm/extract_defs.c‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,33 @@ static TSNode resolve_lean_func_name(TSNode node, TSNode name) {
416416
return cbm_find_child_by_kind(node, "ident");
417417
}
418418

419+
// Haskell infix-form definition head (`a <+> b`, ``x `plus` y``): the defined
420+
// name is the operator, not the left operand that is the head's first child. A
421+
// backticked `infix_id` yields its inner variable (`plus`); a symbolic `operator`
422+
// is returned as-is and parenthesised by cbm_func_name_node_text, matching the
423+
// prefix form `(<+>) a b = ...` (#2440).
424+
static TSNode resolve_haskell_infix_head_name(TSNode head) {
425+
enum { INFIX_MIN_CHILDREN = 3, INFIX_OP_IDX = 1 };
426+
TSNode op = ts_node_child_by_field_name(head, TS_FIELD("operator"));
427+
if (ts_node_is_null(op) && ts_node_child_count(head) >= INFIX_MIN_CHILDREN) {
428+
op = ts_node_child(head, INFIX_OP_IDX);
429+
}
430+
if (!ts_node_is_null(op)) {
431+
const char *ok = ts_node_type(op);
432+
if (strcmp(ok, "operator") == 0) {
433+
return op;
434+
}
435+
if (strcmp(ok, "infix_id") == 0) {
436+
TSNode v = cbm_find_child_by_kind(op, "variable");
437+
if (!ts_node_is_null(v)) {
438+
return v;
439+
}
440+
}
441+
}
442+
TSNode null_node = {0};
443+
return null_node;
444+
}
445+
419446
// Haskell: resolve function name from first named child (variable/name).
420447
static TSNode resolve_haskell_func_name(TSNode node) {
421448
if (ts_node_named_child_count(node) > 0) {
@@ -424,6 +451,9 @@ static TSNode resolve_haskell_func_name(TSNode node) {
424451
if (strcmp(hk, "variable") == 0 || strcmp(hk, "name") == 0) {
425452
return head;
426453
}
454+
if (strcmp(hk, "infix") == 0) {
455+
return resolve_haskell_infix_head_name(head);
456+
}
427457
if (ts_node_named_child_count(head) > 0) {
428458
TSNode v = ts_node_named_child(head, 0);
429459
const char *vk = ts_node_type(v);

‎internal/cbm/helpers.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,14 @@ char *cbm_func_name_node_text(CBMArena *a, TSNode name_node, const char *source,
12131213
if (text && lang == CBM_LANG_NIX) {
12141214
cbm_nix_strip_attr_quotes(text);
12151215
}
1216+
/* Haskell infix-form operator definition: `a <+> b = …` resolves to the bare
1217+
* `operator` node `<+>`, while the prefix form `(<+>) a b = …` is named by its
1218+
* `prefix_id` text `(<+>)`. Parenthesise here, not in the resolver, so both
1219+
* forms mint the same name and the def name and the call-scope QN, which both
1220+
* route through this function, cannot disagree (#2440). */
1221+
if (text && lang == CBM_LANG_HASKELL && strcmp(ts_node_type(name_node), "operator") == 0) {
1222+
return cbm_arena_sprintf(a, "(%s)", text);
1223+
}
12161224
return text;
12171225
}
12181226

‎tests/test_extraction.c‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,6 +1777,33 @@ TEST(haskell_function) {
17771777
PASS();
17781778
}
17791779

1780+
/* #2440: an infix-form definition (`a <+> b = ...`, ``x `plus` y = ...``) has
1781+
* no `name` field; its first named child is an `infix` node whose first child
1782+
* is the LEFT OPERAND, so the def was named `a` / `x`. It must be named after
1783+
* the operator, consistent with the prefix form `(<+>) a b = ...`. */
1784+
TEST(haskell_infix_definition_name) {
1785+
CBMFileResult *r = extract("module M where\n"
1786+
"\n"
1787+
"(<+>) :: Int -> Int -> Int\n"
1788+
"a <+> b = a + b\n"
1789+
"\n"
1790+
"plus :: Int -> Int -> Int\n"
1791+
"x `plus` y = x + y\n"
1792+
"\n"
1793+
"f :: Int\n"
1794+
"f = 1 <+> 2\n",
1795+
CBM_LANG_HASKELL, "t", "M.hs");
1796+
ASSERT_NOT_NULL(r);
1797+
ASSERT_FALSE(r->has_error);
1798+
ASSERT(has_def(r, "Function", "f"));
1799+
ASSERT(has_def(r, "Function", "plus"));
1800+
ASSERT(has_def(r, "Function", "(<+>)"));
1801+
ASSERT_FALSE(has_def(r, "Function", "a"));
1802+
ASSERT_FALSE(has_def(r, "Function", "x"));
1803+
cbm_free_result(r);
1804+
PASS();
1805+
}
1806+
17801807
/* --- OCaml --- */
17811808
TEST(ocaml_function) {
17821809
CBMFileResult *r =
@@ -13177,6 +13204,7 @@ SUITE(extraction) {
1317713204
RUN_TEST(elixir_function);
1317813205
RUN_TEST(elixir_call_string_argument);
1317913206
RUN_TEST(haskell_function);
13207+
RUN_TEST(haskell_infix_definition_name);
1318013208
RUN_TEST(ocaml_function);
1318113209
RUN_TEST(erlang_function);
1318213210

0 commit comments

Comments
 (0)