rust-bakery / nom

Rust parser combinator framework
MIT License
9.38k stars 806 forks source link

Add a map_res + cut combinator #1624

Open douglas-raillard-arm opened 1 year ago

douglas-raillard-arm commented 1 year ago

nom 7.1.2 provides a map_res combinator but I ended up with a common pattern not ideally handled by map_res(parser, f):

In that scenario, an error in parser should backtrack but an error in f should not most of the time, as the syntactic parse was not ambiguous and trying another branch would be useless. Since map_res() does backtrack, we end up with a syntactic error of the last branch in the top-level alt() which is quite useless, and the actual semantic validation error is discarded.

So I ended up with a variant of map_res() that wraps the Result::Err into nom::Err::Failure instead of nom::Err::Error for this use case. Maybe it would be useful to add a validate() combinator to nom ?

epage commented 1 year ago

As a workaround and to help show level of interest in this, nom-supreme includes map_res_cut.

Another workaround would be to use map_parser and to cut within that.