chumsky
chumsky copied to clipboard
n-tuples as output of multiple .then() parsers
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.
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).