@@ -3584,6 +3584,325 @@ static const char *join_route_paths(CBMArena *a, const char *prefix, const char
35843584 return cbm_arena_sprintf(a, "%s%s", prefix, path);
35853585}
35863586
3587+ /* ── ASP.NET Core attribute routing (C#, #1041 / #1692) ──────────────────────
3588+ * Controllers route through attributes rather than Spring-style mapping names:
3589+ * [Route("api/[controller]")] class QuotesController -> class prefix
3590+ * [HttpGet("{symbol}")] / [HttpPost] + [Route("x/{id}")] -> action route
3591+ * [AcceptVerbs("GET", "HEAD", Route = "x")]
3592+ * A template is relative to the class prefix unless it starts with "/" or
3593+ * "~/". The [controller]/[action]/[area] tokens are replaced and constraints,
3594+ * defaults and optional markers reduce to the bare parameter ("{id:int}" ->
3595+ * "{id}"), so the result leads with '/' and canonicalizes like any other
3596+ * decorator route. */
3597+ typedef struct {
3598+ const char *method; /* HTTP verb of the first verb attribute */
3599+ const char *verb_tmpl; /* its template (AcceptVerbs: Route = "...") */
3600+ const char *route_tmpl; /* template of the first [Route] */
3601+ const char *action_name; /* [ActionName("x")] */
3602+ const char *area; /* [Area("x")] */
3603+ bool has_route; /* a [Route] attribute was seen */
3604+ } aspnet_attrs_t;
3605+
3606+ /* Attribute name without namespace qualifier or "Attribute" suffix:
3607+ * Microsoft.AspNetCore.Mvc.HttpGetAttribute -> HttpGet. */
3608+ static const char *aspnet_attr_name(CBMArena *a, TSNode attr, const char *source) {
3609+ TSNode name_node = ts_node_child_by_field_name(attr, TS_FIELD("name"));
3610+ if (ts_node_is_null(name_node)) {
3611+ return NULL;
3612+ }
3613+ const char *name = cbm_node_text(a, name_node, source);
3614+ if (!name) {
3615+ return NULL;
3616+ }
3617+ const char *sep = strrchr(name, '.');
3618+ const char *colon = strrchr(name, ':');
3619+ if (colon > sep) {
3620+ sep = colon;
3621+ }
3622+ if (sep) {
3623+ name = sep + SKIP_CHAR;
3624+ }
3625+ static const char suffix[] = "Attribute";
3626+ size_t len = strlen(name);
3627+ size_t slen = sizeof(suffix) - SKIP_CHAR;
3628+ if (len > slen && strcmp(name + len - slen, suffix) == 0) {
3629+ return cbm_arena_strndup(a, name, len - slen);
3630+ }
3631+ return name;
3632+ }
3633+
3634+ static const char *aspnet_verb(const char *name) {
3635+ static const char *const verbs[][PAIR_CHARS] = {
3636+ {"HttpGet", "GET"}, {"HttpPost", "POST"}, {"HttpPut", "PUT"},
3637+ {"HttpDelete", "DELETE"}, {"HttpPatch", "PATCH"}, {"HttpHead", "HEAD"},
3638+ {"HttpOptions", "OPTIONS"}};
3639+ for (size_t i = 0; i < sizeof(verbs) / sizeof(verbs[0]); i++) {
3640+ if (strcmp(name, verbs[i][0]) == 0) {
3641+ return verbs[i][SKIP_CHAR];
3642+ }
3643+ }
3644+ return NULL;
3645+ }
3646+
3647+ /* Unquoted text of a C# "x" or @"x" string literal node; NULL otherwise. */
3648+ static const char *aspnet_string_value(CBMArena *a, TSNode node, const char *source) {
3649+ const char *kind = ts_node_type(node);
3650+ bool verbatim = strcmp(kind, "verbatim_string_literal") == 0;
3651+ if (!verbatim && strcmp(kind, "string_literal") != 0) {
3652+ return NULL;
3653+ }
3654+ const char *text = cbm_node_text(a, node, source);
3655+ size_t open = verbatim ? PAIR_CHARS : SKIP_CHAR; /* `@"` or `"` */
3656+ size_t len = text ? strlen(text) : 0;
3657+ if (len < open + SKIP_CHAR) {
3658+ return NULL;
3659+ }
3660+ return cbm_arena_strndup(a, text + open, len - open - SKIP_CHAR);
3661+ }
3662+
3663+ /* The first positional string argument (key == NULL) or the string value of
3664+ * the named argument `key` (Route = "..."). */
3665+ static const char *aspnet_attr_arg(CBMArena *a, TSNode attr, const char *source, const char *key) {
3666+ TSNode args = cbm_find_child_by_kind(attr, "attribute_argument_list");
3667+ if (ts_node_is_null(args)) {
3668+ return NULL;
3669+ }
3670+ uint32_t nc = ts_node_named_child_count(args);
3671+ for (uint32_t i = 0; i < nc; i++) {
3672+ TSNode arg = ts_node_named_child(args, i);
3673+ uint32_t vc = ts_node_named_child_count(arg);
3674+ if (strcmp(ts_node_type(arg), "attribute_argument") != 0 || vc == 0) {
3675+ continue;
3676+ }
3677+ TSNode name = ts_node_child_by_field_name(arg, TS_FIELD("name"));
3678+ TSNode value = ts_node_named_child(arg, vc - SKIP_CHAR);
3679+ if (ts_node_is_null(name) != (key == NULL)) {
3680+ continue; /* positional wanted but named seen, or vice versa */
3681+ }
3682+ if (key) {
3683+ const char *n = cbm_node_text(a, name, source);
3684+ if (!n || strcmp(n, key) != 0) {
3685+ continue;
3686+ }
3687+ }
3688+ return aspnet_string_value(a, value, source);
3689+ }
3690+ return NULL;
3691+ }
3692+
3693+ /* [AcceptVerbs("get", ...)]: first verb upper-cased; letters only, since the
3694+ * verb lands in the Route QN and its JSON properties unescaped. */
3695+ static const char *aspnet_accept_verb(CBMArena *a, const char *verb) {
3696+ enum { VERB_MAX = 7 }; /* OPTIONS */
3697+ size_t len = verb ? strlen(verb) : 0;
3698+ if (len == 0 || len > VERB_MAX) {
3699+ return "ANY";
3700+ }
3701+ char *up = cbm_arena_strndup(a, verb, len);
3702+ for (size_t i = 0; i < len; i++) {
3703+ if (!isalpha((unsigned char)up[i])) {
3704+ return "ANY";
3705+ }
3706+ up[i] = (char)toupper((unsigned char)up[i]);
3707+ }
3708+ return up;
3709+ }
3710+
3711+ static void aspnet_note_attr(CBMArena *a, TSNode attr, const char *source, aspnet_attrs_t *out) {
3712+ const char *name = aspnet_attr_name(a, attr, source);
3713+ if (!name) {
3714+ return;
3715+ }
3716+ const char *verb = aspnet_verb(name);
3717+ if (verb || strcmp(name, "AcceptVerbs") == 0) {
3718+ if (!out->method) {
3719+ out->method =
3720+ verb ? verb : aspnet_accept_verb(a, aspnet_attr_arg(a, attr, source, NULL));
3721+ out->verb_tmpl = aspnet_attr_arg(a, attr, source, verb ? NULL : "Route");
3722+ }
3723+ } else if (strcmp(name, "Route") == 0) {
3724+ if (!out->has_route) {
3725+ out->has_route = true;
3726+ out->route_tmpl = aspnet_attr_arg(a, attr, source, NULL);
3727+ }
3728+ } else if (strcmp(name, "ActionName") == 0) {
3729+ out->action_name = aspnet_attr_arg(a, attr, source, NULL);
3730+ } else if (strcmp(name, "Area") == 0) {
3731+ out->area = aspnet_attr_arg(a, attr, source, NULL);
3732+ }
3733+ }
3734+
3735+ static void aspnet_scan_attrs(CBMArena *a, TSNode owner, const char *source, aspnet_attrs_t *out) {
3736+ memset(out, 0, sizeof(*out));
3737+ TSNode wrappers[MAX_ATTR_WRAPPERS];
3738+ int wn = find_jvm_modifiers(owner, CBM_LANG_CSHARP, wrappers, MAX_ATTR_WRAPPERS);
3739+ for (int w = 0; w < wn; w++) {
3740+ uint32_t cc = ts_node_named_child_count(wrappers[w]);
3741+ for (uint32_t ci = 0; ci < cc; ci++) {
3742+ TSNode child = ts_node_named_child(wrappers[w], ci);
3743+ if (strcmp(ts_node_type(child), "attribute") == 0) {
3744+ aspnet_note_attr(a, child, source, out);
3745+ }
3746+ }
3747+ }
3748+ }
3749+
3750+ /* `name` minus `suffix` when it is a proper suffix (QuotesController ->
3751+ * Quotes, GetAsync -> Get); `name` otherwise. */
3752+ static const char *aspnet_trim_suffix(CBMArena *a, const char *name, const char *suffix) {
3753+ size_t len = name ? strlen(name) : 0;
3754+ size_t slen = strlen(suffix);
3755+ if (len > slen && strcmp(name + len - slen, suffix) == 0) {
3756+ return cbm_arena_strndup(a, name, len - slen);
3757+ }
3758+ return name;
3759+ }
3760+
3761+ typedef struct {
3762+ char *buf;
3763+ size_t len;
3764+ size_t cap;
3765+ } aspnet_buf_t;
3766+
3767+ static void aspnet_putc(aspnet_buf_t *b, char c) {
3768+ if (c == '/' && b->len > 0 && b->buf[b->len - SKIP_CHAR] == '/') {
3769+ return; /* "api/" + "/x" and empty token values never double a slash */
3770+ }
3771+ if (b->len + SKIP_CHAR < b->cap) {
3772+ b->buf[b->len++] = c;
3773+ }
3774+ }
3775+
3776+ static void aspnet_putn(aspnet_buf_t *b, const char *s, size_t n) {
3777+ for (size_t i = 0; i < n && s[i]; i++) {
3778+ aspnet_putc(b, s[i]);
3779+ }
3780+ }
3781+
3782+ /* Index just past the '}' closing the '{' at t[open] (braces nest, as in
3783+ * "{code:regex(^\d{{3}}$)}"); 0 when unterminated. */
3784+ static size_t aspnet_brace_end(const char *t, size_t open) {
3785+ int depth = 0;
3786+ for (size_t j = open; t[j]; j++) {
3787+ if (t[j] == '{') {
3788+ depth++;
3789+ } else if (t[j] == '}' && --depth == 0) {
3790+ return j + SKIP_CHAR;
3791+ }
3792+ }
3793+ return 0;
3794+ }
3795+
3796+ /* "{id:int}", "{page=1}", "{slug?}", "{**path}" -> "{id}", "{page}", "{slug}",
3797+ * "{**path}": keep the catch-all stars and the parameter name. */
3798+ static void aspnet_put_param(aspnet_buf_t *b, const char *t, size_t open, size_t end) {
3799+ size_t i = open + SKIP_CHAR;
3800+ aspnet_putc(b, '{');
3801+ while (i < end && t[i] == '*') {
3802+ aspnet_putc(b, t[i++]);
3803+ }
3804+ while (i < end && (isalnum((unsigned char)t[i]) || t[i] == '_')) {
3805+ aspnet_putc(b, t[i++]);
3806+ }
3807+ aspnet_putc(b, '}');
3808+ }
3809+
3810+ enum { ASPNET_TOKENS = 3 }; /* [controller], [action], [area] */
3811+
3812+ /* Replacement for the [controller]/[action]/[area] token starting at t[i]
3813+ * (values[k] belongs to tokens[k]); NULL when t[i] starts no known token or
3814+ * its value is unknown, so the token is kept verbatim. */
3815+ static const char *aspnet_token_value(const char *t, size_t i, const char *const *values,
3816+ size_t *tok_len) {
3817+ static const char *const tokens[ASPNET_TOKENS] = {"[controller]", "[action]", "[area]"};
3818+ for (size_t k = 0; k < ASPNET_TOKENS; k++) {
3819+ size_t n = strlen(tokens[k]);
3820+ if (values[k] && strncmp(t + i, tokens[k], n) == 0) {
3821+ *tok_len = n;
3822+ return values[k];
3823+ }
3824+ }
3825+ return NULL;
3826+ }
3827+
3828+ /* Normalize one ASP.NET route template into a '/'-led path (see the section
3829+ * comment above). values = {controller, action, area}. */
3830+ static const char *aspnet_route_template(CBMArena *a, const char *t, const char *const *values) {
3831+ size_t extra = 0;
3832+ for (int k = 0; k < ASPNET_TOKENS; k++) {
3833+ extra += values[k] ? strlen(values[k]) : 0;
3834+ }
3835+ size_t brackets = 0;
3836+ for (const char *p = t; *p; p++) {
3837+ brackets += *p == '[';
3838+ }
3839+ aspnet_buf_t b = {0};
3840+ b.cap = strlen(t) + (brackets * extra) + PAIR_CHARS;
3841+ b.buf = (char *)cbm_arena_alloc(a, b.cap);
3842+ if (!b.buf) {
3843+ return NULL;
3844+ }
3845+ aspnet_putc(&b, '/');
3846+ size_t i = (t[0] == '~') ? SKIP_CHAR : 0;
3847+ while (t[i]) {
3848+ size_t tok_len = 0;
3849+ const char *val = (t[i] == '[') ? aspnet_token_value(t, i, values, &tok_len) : NULL;
3850+ size_t end = (t[i] == '{' && t[i + SKIP_CHAR] != '{') ? aspnet_brace_end(t, i) : 0;
3851+ if (val) {
3852+ aspnet_putn(&b, val, strlen(val));
3853+ i += tok_len;
3854+ } else if (end) {
3855+ aspnet_put_param(&b, t, i, end);
3856+ i = end;
3857+ } else if ((t[i] == '{' || t[i] == '}' || t[i] == '[' || t[i] == ']') &&
3858+ t[i + SKIP_CHAR] == t[i]) {
3859+ aspnet_putc(&b, t[i]); /* "{{" / "[[" escapes a literal brace */
3860+ i += PAIR_CHARS;
3861+ } else {
3862+ aspnet_putc(&b, t[i++]);
3863+ }
3864+ }
3865+ if (b.len > SKIP_CHAR && b.buf[b.len - SKIP_CHAR] == '/') {
3866+ b.len--;
3867+ }
3868+ b.buf[b.len] = '\0';
3869+ return b.buf;
3870+ }
3871+
3872+ static bool aspnet_template_is_absolute(const char *t) {
3873+ return t[0] == '/' || (t[0] == '~' && t[SKIP_CHAR] == '/');
3874+ }
3875+
3876+ /* Route of one controller action: sets out_path and out_method when the method
3877+ * carries a verb attribute, [AcceptVerbs] or [Route]; clears them otherwise. */
3878+ static void aspnet_action_route(CBMArena *a, TSNode method_node, TSNode class_node,
3879+ const char *method_name, const char *source, const char **out_path,
3880+ const char **out_method) {
3881+ *out_path = NULL;
3882+ *out_method = NULL;
3883+ aspnet_attrs_t act;
3884+ aspnet_scan_attrs(a, method_node, source, &act);
3885+ if (!act.method && !act.has_route) {
3886+ return;
3887+ }
3888+ aspnet_attrs_t cls;
3889+ aspnet_scan_attrs(a, class_node, source, &cls);
3890+ TSNode cname = ts_node_child_by_field_name(class_node, TS_FIELD("name"));
3891+ const char *ctrl = ts_node_is_null(cname) ? NULL : cbm_node_text(a, cname, source);
3892+ const char *values[ASPNET_TOKENS] = {
3893+ aspnet_trim_suffix(a, ctrl, "Controller"),
3894+ act.action_name ? act.action_name : aspnet_trim_suffix(a, method_name, "Async"),
3895+ act.area ? act.area : cls.area,
3896+ };
3897+ const char *tmpl = act.verb_tmpl ? act.verb_tmpl : act.route_tmpl;
3898+ const char *path = tmpl ? aspnet_route_template(a, tmpl, values) : NULL;
3899+ if ((!tmpl || !aspnet_template_is_absolute(tmpl)) && cls.route_tmpl) {
3900+ path = join_route_paths(a, aspnet_route_template(a, cls.route_tmpl, values), path);
3901+ }
3902+ *out_path = path ? path : "/";
3903+ *out_method = act.method ? act.method : "ANY";
3904+ }
3905+
35873906static const char *spring_class_route_prefix(CBMArena *a, TSNode class_node, const char *source,
35883907 const CBMLangSpec *spec) {
35893908 const char *map_path = NULL;
@@ -8442,6 +8761,10 @@ static void push_method_def(CBMExtractCtx *ctx, TSNode child, TSNode class_node,
84428761 def.route_path = prefix ? join_route_paths(a, prefix, def.route_path) : NULL;
84438762 def.route_method = prefix ? def.route_method : NULL;
84448763 }
8764+ if (ctx->language == CBM_LANG_CSHARP) {
8765+ aspnet_action_route(a, child, class_node, name, ctx->source, &def.route_path,
8766+ &def.route_method);
8767+ }
84458768 def.docstring = extract_docstring(ctx, child, name);
84468769
84478770 if (spec->branching_node_types && spec->branching_node_types[0]) {
0 commit comments