Add option to auto-unwrap rule results #505
Open
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.
Summary
This PR adds an option
@autoUnwrap
. If enabled, rules that consist of a single symbol will have their results unwrapped automatically (like adding{% id %}
to them).Update: In the meantime, I also implemented this feature as a stand-alone package: nearley-auto-unwrap.
Motivation
Before this PR, parsing the string
foobar
with the above grammar would yield the following result:With this PR, the result will be
IMHO this makes postprocessing much more intuitive or completely alleviates the need for it in some cases.
@autoUnwrap true
also fixes #498. It generally improves macro usability and makes nested macros feasible:The result of parsing
foobar
with above grammar is"foobar"
while with@autoUnwrap false
the result is[[[[[ "foobar" ]]]]]
(that is a nesting depth of 5).Implementation
The implementation is pretty straight forward: during compilation, if
@autoUnwrap
istrue
, modify thepostprocessing
property of every rule that only has one token, such that the original postprocessing function will be called withdata[0]
instead ofdata
. Thus, there is no need for additional rule properties and there is no runtime overhead compared to manually callingid
.In more detail, if an affected rule had the postprocess property
fn
, we will replace it withid._auto(fn)
whereI have decided to attach this helper function to
id
to avoid any naming collisions with custom preprocessors of existing grammars. The name also makes some sense since it's an automatic application of theid
function.TODOs
There are still a few things left to do. I will address those if this feature is wanted by the maintainers:
I hope you think of this as a useful addition. I really liked writing a parser using nearley and this feature would resolve the only thing that bothered me when using it.