@@ -8923,6 +8923,68 @@ static void extract_objc_impl_methods(CBMExtractCtx *ctx, TSNode impl_node, cons
89238923 }
89248924}
89258925
8926+ /* True for a C# 14 extension block. The vendored tree-sitter-c-sharp has no
8927+ * `extension` production, so `extension(string text) { ... }` parses as a
8928+ * constructor_declaration named `extension` and nothing flags it: has_error
8929+ * stays 0. A constructor carries its own type's name, so this shape can only be
8930+ * a genuine constructor inside a type literally called `extension` (#2071). */
8931+ static bool csharp_is_extension_block(TSNode node, TSNode class_node, const char *source) {
8932+ if (strcmp(ts_node_type(node), "constructor_declaration") != 0) {
8933+ return false;
8934+ }
8935+ TSNode name = resolve_method_name(node, CBM_LANG_CSHARP);
8936+ if (ts_node_is_null(name)) {
8937+ return false;
8938+ }
8939+ const size_t kw_len = sizeof("extension") - 1;
8940+ uint32_t len = ts_node_end_byte(name) - ts_node_start_byte(name);
8941+ if (len != kw_len || strncmp(source + ts_node_start_byte(name), "extension", kw_len) != 0) {
8942+ return false;
8943+ }
8944+ TSNode type_name = ts_node_child_by_field_name(class_node, TS_FIELD("name"));
8945+ if (!ts_node_is_null(type_name)) {
8946+ const char *type_text = source + ts_node_start_byte(type_name);
8947+ uint32_t tlen = ts_node_end_byte(type_name) - ts_node_start_byte(type_name);
8948+ /* A verbatim identifier names the same type: class @extension may
8949+ * declare a genuine constructor spelled extension(). */
8950+ if (tlen > 0 && type_text[0] == '@') {
8951+ type_text++;
8952+ tlen--;
8953+ }
8954+ if (tlen == kw_len && strncmp(type_text, "extension", kw_len) == 0) {
8955+ return false;
8956+ }
8957+ }
8958+ return true;
8959+ }
8960+
8961+ /* Lift the members of a misparsed C# 14 extension block onto the enclosing
8962+ * type. They sit in the block the grammar took for a constructor body, which
8963+ * nothing recurses into, so every one of them was dropped. Methods land there
8964+ * as local_function_statement; a property in the block parses as ERROR and is
8965+ * beyond recovery without a grammar that knows the construct (#2071). */
8966+ static void extract_csharp_extension_members(CBMExtractCtx *ctx, TSNode ext_node, TSNode class_node,
8967+ const char *class_qn, const CBMLangSpec *spec) {
8968+ TSNode body = cbm_find_child_by_kind(ext_node, "block");
8969+ if (ts_node_is_null(body)) {
8970+ return;
8971+ }
8972+ member_iter_t it;
8973+ member_iter_init(&it, body, ctx->language, false);
8974+ TSNode member;
8975+ while (member_iter_next(&it, &member)) {
8976+ if (strcmp(ts_node_type(member), "local_function_statement") != 0) {
8977+ continue;
8978+ }
8979+ TSNode name = resolve_method_name(member, ctx->language);
8980+ if (ts_node_is_null(name)) {
8981+ continue;
8982+ }
8983+ push_method_def(ctx, member, class_node, class_qn, spec, name);
8984+ }
8985+ member_iter_done(&it);
8986+ }
8987+
89268988// Extract methods inside a class body
89278989static void extract_class_methods(CBMExtractCtx *ctx, TSNode class_node, const char *class_qn,
89288990 const CBMLangSpec *spec) {
@@ -8936,6 +8998,15 @@ static void extract_class_methods(CBMExtractCtx *ctx, TSNode class_node, const c
89368998 TSNode child;
89378999 while (member_iter_next(&it, &child)) {
89389000
9001+ /* C# 14 extension block: emit the members it holds as methods of the
9002+ * enclosing type, rather than the block itself as a constructor named
9003+ * `extension` with its members buried in the body (#2071). */
9004+ if (ctx->language == CBM_LANG_CSHARP &&
9005+ csharp_is_extension_block(child, class_node, ctx->source)) {
9006+ extract_csharp_extension_members(ctx, child, class_node, class_qn, spec);
9007+ continue;
9008+ }
9009+
89399010 if (ctx->language == CBM_LANG_OBJC &&
89409011 strcmp(ts_node_type(child), "implementation_definition") == 0) {
89419012 extract_objc_impl_methods(ctx, child, class_qn, spec);
0 commit comments