Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: mpc_sepby1 not applying fold correctly #167

Merged
merged 2 commits into from
Aug 22, 2023

Conversation

steve-chavez
Copy link
Contributor

Problem

mpc_sepby1 takes a single fold function but this doesn't apply to the whole list of entries. Instead it applies the fold twice, one for mpc_and and another for mpc_many. This makes it hard to build an array of structures.

Previously discussed on #165 (comment)

Solution

Introduce a new repeat repeat parser type MPC_TYPE_SEPBY1 that combines the results and applies the fold once for all entries.

`mpc_sepby1` takes a single fold function but this doesn't apply to the
whole list of entries. Instead it applies the fold twice,
one for `mpc_and` and another for `mpc_many`.

Introduce a new repeat repeat parser type MPC_TYPE_SEPBY1 that combines
the results and applies the fold once for all entries.
Comment on lines +261 to +269
mpc_parser_t* CommaSepIdent1 = mpc_sepby1(fold_vals, mpc_char(','), mpc_ident());
mpc_result_t r;

mpc_parse("<test>", "uno,dos,tres,cuatro", CommaSepIdent1, &r);
char **vals = r.output;
PT_ASSERT(strcmp("uno", vals[0]) == 0);
PT_ASSERT(strcmp("dos", vals[1]) == 0);
PT_ASSERT(strcmp("tres", vals[2]) == 0);
PT_ASSERT(strcmp("cuatro", vals[3]) == 0);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main motivation, it wasn't possible to do this with the previous sepby1.

Comment on lines +2819 to +2827
if (p->type == MPC_TYPE_SEPBY1) {
mpc_print_unretained(p->data.sepby1.x, 0);
printf(" (");
mpc_print_unretained(p->data.sepby1.sep, 0);
printf(" ");
mpc_print_unretained(p->data.sepby1.x, 0);
printf(")");
printf("*");
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't see mpc_print being tested, but I manually confirmed that:

CommaSeparated = mpc_sepby1(mpcf_strfold, mpc_char(','), mpc_ident())
mpc_print(CommaSeparated);

// result
((letter | underscore) alphanumeric*) (',' ((letter | underscore) alphanumeric*))*

@orangeduck
Copy link
Owner

Great work!

@orangeduck orangeduck merged commit d4c99b7 into orangeduck:master Aug 22, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants