Core: Fixed greedy targeting bug #1932
Merged
+114
−30
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.
This PR fixes a bug of greedy matching.
The
target
parameter defines the token which causes rematching. Rematching is done by recursively callingmatchGrammar
wheretarget
will be the current token name. The rematching is done if either a match was found or we reached the target token, whatever comes first.The bug is that a token is identified by its name. But the name alone doesn't uniquely identify a token in a grammar because many tokens can have the same name:
The fix is quite obvious.
target
will no be a combination of the token name and its index.Note: This is a technically a breaking change because
matchGrammar
is publicly exposed. But the function is also not documented, so its behavior is undefined anyway.Edit:
An example of the bug is the following JS code:
Before:
![image](https://user-images.githubusercontent.com/20878432/59156998-29ad3e80-8aa4-11e9-9a09-6c28f9fa07c6.png)
After:
![image](https://user-images.githubusercontent.com/20878432/59157016-4184c280-8aa4-11e9-85f3-8067ee2c064b.png)