Added map_with and try_map_with to replace existing mapper functions #537
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There are big changes in here! For those reading this PR because they're not sure why their build has broken:
The problem
This PR is an attempt to remove all of the arbitrary
map_<whatever>
methods onParser
. Chumsky has managed to grow a lot of them, such as:map_with_span
map_span
map_with_state
map_with_ctx
Additionally, we had the following 'same but with extra stuff' methods:
foldl_with_state
foldr_with_state
try_map_with_state
Despite all of these extra methods to remember, we still often hit annoying limitations. For example, it was really common for users to have to invent their own spans by combining existing spans in
foldl
/foldr
functions, despite the fact that chumsky really should be able to produce a span on the fly. It was also common to see users needing to screw around with multiple mapping functions one after the other to get access to the information that they actually needed.The solution
To solve all of this, we're replacing the zoo of methods above with the following four:
map_with
foldl_with
foldr_with
try_map_with
These functions are all nice and consistent. They work exactly like their easier equivalents (
map
,foldl
,foldr
,try_map
), except that you get an extra argument, aMapExtra
. This is a type with a few useful methods that you can call on it:extra.span()
: get the span of the parsed outputextra.slice()
: get the slice covering the parsed outputextra.state()
: get access to the parser state (ignore this if you've never needed parser state)extra.ctx()
: get access to the parser context (ignore this if you've never needed to do context-sensitive parsing)This should massively improve the flexibility of the mapping functions and bring an end to the need to keep a dozen arbitrary mapping functions in your head!
Other changes
In addition, there have been a few other changes:
validate
's closure is aMapExtra
(see above) instead of aSpan
(you can just doextra.span()
in the closure to get the old behaviour back)Parser::slice
has been renamedParser::to_slice
so as to be consistent withParser::to
andParser::to_span
.Migrating
In most cases, the changes involved should be trivial: just switch the old combinators to their
_with
counterpart. You might find the changes to thenano_rust.rs
example in this PR to be instructive!