disambiguate fold vs parenthesized assignment #239
Merged
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.
Currently, there is an ambiguity in parsing fold expressions and parenthesized assignment expressions. This is described in issues #201 and #212.
This arises in the following trace:
The crux of the issue is that we do not know whether to shift the
=
forward, or to reduce the<id>
to anexpression_not_binary
. If we do the former, we can no longer parse a fold expression, which expects<exp> <fold_operator> '...'
. If we do the latter, we can no longer parse a parenthesized assignment expression, as an assignment expression does not permit arbitrary expressions to appear on the LHS.The current behavior is that
tree-sitter-cpp
will reduce the<id>
, and thus fail to parse any parenthesized assignment expression. Unfortunately, these are reasonably common, so this is actually disastrous.This PR makes it so that parenthesized expressions specifically allow assignment expressions which feature expressions on the LHS, so that it can still parse even if we reduce the identifier. This causes the grammar to be a little more permissive than it needs to be, but it does not disallow valid programs, as another proposed solution to this problem does.
I believe it is better for us to be more permissive, in this case, so that we no longer disallow valid programs, with the understanding that we can restore stringency at a later date.