Parse a single MATCH pattern into pat. On failure pat is freed and zeroed: * the caller counts a pattern only after it parsed, so a partial one would * otherwise leak (a named path "p = (...)" failed this way on every query). */
| 1893 | * the caller counts a pattern only after it parsed, so a partial one would |
| 1894 | * otherwise leak (a named path "p = (...)" failed this way on every query). */ |
| 1895 | static int parse_match_pattern(parser_t *p, cbm_pattern_t *pat) { |
| 1896 | memset(pat, 0, sizeof(*pat)); |
| 1897 | int node_cap = CYP_INIT_CAP4; |
| 1898 | int rel_cap = CYP_INIT_CAP4; |
| 1899 | pat->nodes = malloc(node_cap * sizeof(cbm_node_pattern_t)); |
| 1900 | pat->rels = calloc(rel_cap, sizeof(cbm_rel_pattern_t)); |
| 1901 | |
| 1902 | /* A failed parse_node / parse_rel leaves its element zeroed except for |
| 1903 | * what it allocated, so counting that element frees its partial state. */ |
| 1904 | if (parse_node(p, &pat->nodes[0]) < 0) { |
| 1905 | pat->node_count = SKIP_ONE; |
| 1906 | goto fail; |
| 1907 | } |
| 1908 | pat->node_count = SKIP_ONE; |
| 1909 | |
| 1910 | while (check(p, TOK_DASH) || check(p, TOK_LT)) { |
| 1911 | if (pat->rel_count >= rel_cap) { |
| 1912 | rel_cap *= PAIR_LEN; |
| 1913 | pat->rels = safe_realloc(pat->rels, rel_cap * sizeof(cbm_rel_pattern_t)); |
| 1914 | } |
| 1915 | if (parse_rel(p, &pat->rels[pat->rel_count]) < 0) { |
| 1916 | pat->rel_count++; |
| 1917 | goto fail; |
| 1918 | } |
| 1919 | pat->rel_count++; |
| 1920 | |
| 1921 | if (pat->node_count >= node_cap) { |
| 1922 | node_cap *= PAIR_LEN; |
| 1923 | pat->nodes = safe_realloc(pat->nodes, node_cap * sizeof(cbm_node_pattern_t)); |
| 1924 | } |
| 1925 | if (parse_node(p, &pat->nodes[pat->node_count]) < 0) { |
| 1926 | pat->node_count++; |
| 1927 | goto fail; |
| 1928 | } |
| 1929 | pat->node_count++; |
| 1930 | } |
| 1931 | return 0; |
| 1932 | |
| 1933 | fail: |
| 1934 | free_pattern(pat); |
| 1935 | memset(pat, 0, sizeof(*pat)); |
| 1936 | return CBM_NOT_FOUND; |
| 1937 | } |
| 1938 | |
| 1939 | /* Parse UNWIND [...] AS var clause into query */ |
| 1940 | static void parse_unwind_clause(parser_t *p, cbm_query_t *q) { |
no test coverage detected