One MATCH ... RETURN block: the whole query, or one branch of a UNION. On * failure p->error holds the message and *out stays NULL. */
| 2049 | /* One MATCH ... RETURN block: the whole query, or one branch of a UNION. On |
| 2050 | * failure p->error holds the message and *out stays NULL. */ |
| 2051 | static int parse_query_block(parser_t *p, cbm_query_t **out) { |
| 2052 | *out = NULL; |
| 2053 | /* Each block starts with a clean message, as it did when every UNION |
| 2054 | * branch had a parser of its own. */ |
| 2055 | p->error[0] = '\0'; |
| 2056 | |
| 2057 | /* Check for unsupported leading keywords */ |
| 2058 | const char *unsup = unsupported_clause_error(peek(p)->type); |
| 2059 | if (unsup) { |
| 2060 | snprintf(p->error, sizeof(p->error), "%s", unsup); |
| 2061 | return CBM_NOT_FOUND; |
| 2062 | } |
| 2063 | |
| 2064 | cbm_query_t *q = calloc(CBM_ALLOC_ONE, sizeof(cbm_query_t)); |
| 2065 | |
| 2066 | if (check(p, TOK_UNWIND)) { |
| 2067 | parse_unwind_clause(p, q); |
| 2068 | } |
| 2069 | |
| 2070 | bool first_optional = false; |
| 2071 | if (check(p, TOK_OPTIONAL)) { |
| 2072 | advance(p); |
| 2073 | first_optional = true; |
| 2074 | } |
| 2075 | if (!expect(p, TOK_MATCH)) { |
| 2076 | parse_error_default(p, "expected MATCH"); |
| 2077 | cbm_query_free(q); |
| 2078 | return CBM_NOT_FOUND; |
| 2079 | } |
| 2080 | |
| 2081 | int pat_cap = CYP_INIT_CAP4; |
| 2082 | q->patterns = malloc(pat_cap * sizeof(cbm_pattern_t)); |
| 2083 | q->pattern_optional = malloc(pat_cap * sizeof(bool)); |
| 2084 | |
| 2085 | if (parse_match_pattern(p, &q->patterns[0]) < 0) { |
| 2086 | parse_error_default(p, "failed to parse pattern"); |
| 2087 | cbm_query_free(q); |
| 2088 | return CBM_NOT_FOUND; |
| 2089 | } |
| 2090 | q->pattern_optional[0] = first_optional; |
| 2091 | q->pattern_count = SKIP_ONE; |
| 2092 | |
| 2093 | if (parse_match_chain(p, q, &pat_cap) < 0) { |
| 2094 | parse_error_default(p, "failed to parse additional pattern"); |
| 2095 | cbm_query_free(q); |
| 2096 | return CBM_NOT_FOUND; |
| 2097 | } |
| 2098 | |
| 2099 | if (parse_where(p, &q->where) < 0) { |
| 2100 | parse_error_default(p, "failed to parse WHERE"); |
| 2101 | cbm_query_free(q); |
| 2102 | return CBM_NOT_FOUND; |
| 2103 | } |
| 2104 | |
| 2105 | if (parse_post_where(p, q, &pat_cap) < 0) { |
| 2106 | parse_error_default(p, "failed to parse query"); |
| 2107 | cbm_query_free(q); |
| 2108 | return CBM_NOT_FOUND; |
no test coverage detected