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

Function parse_case_expr

src/cypher/cypher.c:1397–1444  ·  view source on GitHub ↗

Parse CASE WHEN ... THEN ... [ELSE ...] END */

Source from the content-addressed store, hash-verified

1395
1396/* Parse CASE WHEN ... THEN ... [ELSE ...] END */
1397static cbm_case_expr_t *parse_case_expr(parser_t *p) {
1398 /* CASE already consumed */
1399 cbm_case_expr_t *kase = calloc(CBM_ALLOC_ONE, sizeof(cbm_case_expr_t));
1400 if (!kase) {
1401 return NULL;
1402 }
1403 int bcap = CYP_INIT_CAP4;
1404 kase->branches = malloc(bcap * sizeof(cbm_case_branch_t));
1405 if (!kase->branches) {
1406 free(kase);
1407 return NULL;
1408 }
1409
1410 while (check(p, TOK_WHEN)) {
1411 advance(p);
1412 cbm_expr_t *when = parse_or_expr(p);
1413 if (!expect(p, TOK_THEN)) {
1414 expr_free(when);
1415 break;
1416 }
1417 const char *then_val = parse_value_literal(p);
1418 if (kase->branch_count >= bcap) {
1419 int new_bcap = bcap * PAIR_LEN;
1420 void *tmp = realloc(kase->branches, new_bcap * sizeof(cbm_case_branch_t));
1421 if (!tmp) {
1422 expr_free(when);
1423 safe_str_free(&then_val);
1424 for (int i = 0; i < kase->branch_count; i++) {
1425 expr_free(kase->branches[i].when_expr);
1426 safe_str_free(&kase->branches[i].then_val);
1427 }
1428 free(kase->branches);
1429 free(kase);
1430 return NULL;
1431 }
1432 kase->branches = tmp;
1433 bcap = new_bcap;
1434 }
1435 kase->branches[kase->branch_count++] =
1436 (cbm_case_branch_t){.when_expr = when, .then_val = then_val};
1437 }
1438
1439 if (match(p, TOK_ELSE)) {
1440 kase->else_val = parse_value_literal(p);
1441 }
1442 expect(p, TOK_END);
1443 return kase;
1444}
1445
1446/* Parse a single RETURN/WITH item (aggregate, string func, CASE, or plain var.prop).
1447 * Returns 0 on success, -1 on error. */

Callers 1

parse_return_itemFunction · 0.85

Calls 8

advanceFunction · 0.85
parse_or_exprFunction · 0.85
expectFunction · 0.85
expr_freeFunction · 0.85
parse_value_literalFunction · 0.85
safe_str_freeFunction · 0.85
matchFunction · 0.85
checkFunction · 0.70

Tested by

no test coverage detected