-
Notifications
You must be signed in to change notification settings - Fork 292
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
Conversation
To not cause naming conflicts as mentioned on orangeduck#166 (comment)
493506c
to
cfa7d43
Compare
`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.
cfa7d43
to
b866236
Compare
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); |
There was a problem hiding this comment.
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
.
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("*"); | ||
} |
There was a problem hiding this comment.
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*))*
Great work! |
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 formpc_and
and another formpc_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.