chumsky icon indicating copy to clipboard operation
chumsky copied to clipboard

n-tuples as output of multiple .then() parsers

Open neunenak opened this issue 1 year ago • 1 comments

If I have a chain of then parsers, e.g.:


parse_int()
.then_ignore(parse_whitespace())
.then(parse_char())
.then_ignore(parse_whitespace()
.then(parse_float().then(parse_float()))

The output type of this parser will be a nested sequence of 2-tuples, i.e. ((i32, char), (f32, f32)).

When using .map on the output of this kind of built-up parser, it's very easy to get confused about the exact internal structure of the 2-tuples, especially when there are multiple levels of nested parser.

It would be convenient if chumsky had a combinator that could accept multiple parsers in sequence of heterogenous output type, and produce an arbitrarily-sized tuple as output. This is similar to the behavior of the tuple combinator in nom.

neunenak avatar Sep 11 '22 11:09 neunenak

I think this should be fairly easy to implement, on the assumption that all you're looking for is many parsers, one after the other. That said, I'm not sure how well it composes: any time you want to compose parsers together, you necessarily need to abandon this approach. I'll consider it for the next release, but I don't really think that the feature actually adds much (your example could be easily changes to have the output (((i32, char), f32), f32), which is more consistent to work with).

zesterer avatar Sep 11 '22 13:09 zesterer