refactor: deduplicate repeat parsers alloc logic
Deduplicates the results allocation logic used in MPC_TYPE_MANY and MPC_TYPE_MANY1 on a `grow_results` function. This will aid in implementing new repeat parser types. A new one is neeeded for `sepby1` as discussed on https://github.com/orangeduck/mpc/pull/165#discussion_r1292878771.
This commit is contained in:
34
mpc.c
34
mpc.c
@@ -1046,12 +1046,26 @@ enum {
|
|||||||
|
|
||||||
#define MPC_MAX_RECURSION_DEPTH 1000
|
#define MPC_MAX_RECURSION_DEPTH 1000
|
||||||
|
|
||||||
|
static mpc_result_t *grow_results(mpc_input_t *i, int j, mpc_result_t *results_stk, mpc_result_t *results){
|
||||||
|
mpc_result_t *tmp_results = results;
|
||||||
|
|
||||||
|
if (j == MPC_PARSE_STACK_MIN) {
|
||||||
|
int results_slots = j + j / 2;
|
||||||
|
tmp_results = mpc_malloc(i, sizeof(mpc_result_t) * results_slots);
|
||||||
|
memcpy(tmp_results, results_stk, sizeof(mpc_result_t) * MPC_PARSE_STACK_MIN);
|
||||||
|
} else if (j >= MPC_PARSE_STACK_MIN) {
|
||||||
|
int results_slots = j + j / 2;
|
||||||
|
tmp_results = mpc_realloc(i, tmp_results, sizeof(mpc_result_t) * results_slots);
|
||||||
|
}
|
||||||
|
|
||||||
|
return tmp_results;
|
||||||
|
}
|
||||||
|
|
||||||
static int mpc_parse_run(mpc_input_t *i, mpc_parser_t *p, mpc_result_t *r, mpc_err_t **e, int depth) {
|
static int mpc_parse_run(mpc_input_t *i, mpc_parser_t *p, mpc_result_t *r, mpc_err_t **e, int depth) {
|
||||||
|
|
||||||
int j = 0, k = 0;
|
int j = 0, k = 0;
|
||||||
mpc_result_t results_stk[MPC_PARSE_STACK_MIN];
|
mpc_result_t results_stk[MPC_PARSE_STACK_MIN];
|
||||||
mpc_result_t *results;
|
mpc_result_t *results;
|
||||||
int results_slots = MPC_PARSE_STACK_MIN;
|
|
||||||
|
|
||||||
if (depth == MPC_MAX_RECURSION_DEPTH)
|
if (depth == MPC_MAX_RECURSION_DEPTH)
|
||||||
{
|
{
|
||||||
@@ -1176,14 +1190,7 @@ static int mpc_parse_run(mpc_input_t *i, mpc_parser_t *p, mpc_result_t *r, mpc_e
|
|||||||
|
|
||||||
while (mpc_parse_run(i, p->data.repeat.x, &results[j], e, depth+1)) {
|
while (mpc_parse_run(i, p->data.repeat.x, &results[j], e, depth+1)) {
|
||||||
j++;
|
j++;
|
||||||
if (j == MPC_PARSE_STACK_MIN) {
|
results = grow_results(i, j, results_stk, results);
|
||||||
results_slots = j + j / 2;
|
|
||||||
results = mpc_malloc(i, sizeof(mpc_result_t) * results_slots);
|
|
||||||
memcpy(results, results_stk, sizeof(mpc_result_t) * MPC_PARSE_STACK_MIN);
|
|
||||||
} else if (j >= results_slots) {
|
|
||||||
results_slots = j + j / 2;
|
|
||||||
results = mpc_realloc(i, results, sizeof(mpc_result_t) * results_slots);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
*e = mpc_err_merge(i, *e, results[j].error);
|
*e = mpc_err_merge(i, *e, results[j].error);
|
||||||
@@ -1198,14 +1205,7 @@ static int mpc_parse_run(mpc_input_t *i, mpc_parser_t *p, mpc_result_t *r, mpc_e
|
|||||||
|
|
||||||
while (mpc_parse_run(i, p->data.repeat.x, &results[j], e, depth+1)) {
|
while (mpc_parse_run(i, p->data.repeat.x, &results[j], e, depth+1)) {
|
||||||
j++;
|
j++;
|
||||||
if (j == MPC_PARSE_STACK_MIN) {
|
results = grow_results(i, j, results_stk, results);
|
||||||
results_slots = j + j / 2;
|
|
||||||
results = mpc_malloc(i, sizeof(mpc_result_t) * results_slots);
|
|
||||||
memcpy(results, results_stk, sizeof(mpc_result_t) * MPC_PARSE_STACK_MIN);
|
|
||||||
} else if (j >= results_slots) {
|
|
||||||
results_slots = j + j / 2;
|
|
||||||
results = mpc_realloc(i, results, sizeof(mpc_result_t) * results_slots);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (j == 0) {
|
if (j == 0) {
|
||||||
|
Reference in New Issue
Block a user