Fix parser shift/reduce conflicts #4178
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.
Motivation
I introduced shift/reduce conflicts in the grammar while adding support for begin-less ranges with
..
and...
(#3313). This PR fixes them.Example when compiling the parser before this PR:
The approach is the same as the one used by Whitequark (https://github.com/whitequark/parser/blob/de619a2ff36a39b4d43efa9b86cdec98621ed435/lib/parser/lexer.rl#L2026-L2035): duplicating the tokens
tDOT2
andtDOT3
and lexing them differently depending on the Ruby version used.One quirk with this approach is the way we display the parsing errors. Without any additional change, the error message would be:
In order to get a friendlier output, I tweaker the error generation method (see second commit) so we get back to the expected following error:
Compiling the parser after this PR:
No more shift/reduce conflict warning. 🎉
Test plan
See included automated tests.