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):
parser deals with the context-free syntactic parse
f does some validation and builds an actual AST node
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 ?
nom 7.1.2 provides a
map_res
combinator but I ended up with a common pattern not ideally handled bymap_res(parser, f)
:parser
deals with the context-free syntactic parsef
does some validation and builds an actual AST nodeIn that scenario, an error in
parser
should backtrack but an error inf
should not most of the time, as the syntactic parse was not ambiguous and trying another branch would be useless. Sincemap_res()
does backtrack, we end up with a syntactic error of the last branch in the top-levelalt()
which is quite useless, and the actual semantic validation error is discarded.So I ended up with a variant of
map_res()
that wraps theResult::Err
intonom::Err::Failure
instead ofnom::Err::Error
for this use case. Maybe it would be useful to add avalidate()
combinator to nom ?