MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / parse_phpdoc_template_args

Function parse_phpdoc_template_args

internal/cbm/lsp/php_lsp.c:551–609  ·  view source on GitHub ↗

Resolve a PHPDoc-style type spec (e.g. "App\\Foo|null", "?App\\Foo", * "Collection ", "string") to a CBMType. We strip nullables, take the * leftmost class in unions, drop generic <...> tails, then resolve. */ Parse a `<...>` argument list into an arena-allocated NULL-terminated * array of CBMType*. Caller has already skipped past the leading '<'. * Returns the number of args parsed; *out

Source from the content-addressed store, hash-verified

549 * Returns the number of args parsed; *out_args is the array; *out_close
550 * is set to the position just after the matching '>'. */
551static int parse_phpdoc_template_args(PHPLSPContext *ctx, const char *p, const CBMType ***out_args,
552 const char **out_close) {
553 /* Allocate a small buffer; grow if needed. */
554 int cap = 4;
555 int count = 0;
556 const CBMType **buf =
557 (const CBMType **)cbm_arena_alloc(ctx->arena, (size_t)(cap + 1) * sizeof(*buf));
558 if (!buf) {
559 *out_args = NULL;
560 *out_close = p;
561 return 0;
562 }
563 int depth = 1;
564 while (*p && depth > 0) {
565 while (*p == ' ' || *p == '\t' || *p == ',')
566 p++;
567 if (*p == '>')
568 break;
569 const char *arg_start = p;
570 while (*p && depth > 0) {
571 if (*p == '<')
572 depth++;
573 else if (*p == '>') {
574 depth--;
575 if (depth == 0)
576 break;
577 } else if (*p == ',' && depth == 1) {
578 break;
579 }
580 p++;
581 }
582 size_t arg_len = (size_t)(p - arg_start);
583 while (arg_len > 0 && (arg_start[arg_len - 1] == ' ' || arg_start[arg_len - 1] == '\t')) {
584 arg_len--;
585 }
586 if (arg_len > 0) {
587 char *arg_text = cbm_arena_strndup(ctx->arena, arg_start, arg_len);
588 const CBMType *arg_type = resolve_phpdoc_type(ctx, arg_text);
589 if (count >= cap) {
590 int new_cap = cap * 2;
591 const CBMType **new_buf = (const CBMType **)cbm_arena_alloc(
592 ctx->arena, (size_t)(new_cap + 1) * sizeof(*new_buf));
593 if (new_buf) {
594 for (int i = 0; i < count; i++)
595 new_buf[i] = buf[i];
596 buf = new_buf;
597 cap = new_cap;
598 }
599 }
600 buf[count++] = arg_type;
601 }
602 }
603 if (*p == '>')
604 p++;
605 buf[count] = NULL;
606 *out_args = buf;
607 *out_close = p;
608 return count;

Callers 1

resolve_phpdoc_typeFunction · 0.85

Calls 3

resolve_phpdoc_typeFunction · 0.85
cbm_arena_allocFunction · 0.50
cbm_arena_strndupFunction · 0.50

Tested by

no test coverage detected