Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve chevrotain parser error recovery #1822

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

msujew
Copy link
Member

@msujew msujew commented Mar 2, 2025

While looking into the Chevrotain documentation on fault tolerance, I've noticed that we're not supposed to catch the errors thrown by Chevrotain ourselves, but let them propagate. We've been effectively starving the catch block in this method, which is in charge of actually recovering from the error.

This results in some less-than-optimal behavior from Langium when it comes to error recovery. I've been playing around with this change using our example languages and it seems to behave pretty well. Refer to the additional comments to see why some of the changes were required.

@msujew msujew added the parser Parser related issue label Mar 2, 2025
@msujew msujew requested a review from spoenemann March 2, 2025 17:10
@msujew
Copy link
Member Author

msujew commented Mar 2, 2025

@cdietrich Are you interested in testing this? I believe this should be beneficial for most adopters of Langium. I can create a next release of this change if you want to.

@msujew msujew force-pushed the msujew/improve-error-recovery branch from bab10b4 to f9f40ce Compare March 2, 2025 17:16
@msujew
Copy link
Member Author

msujew commented Mar 5, 2025

This PR is available as 3.4.0-next.10c0eb7 on npm.

Copy link
Contributor

@spoenemann spoenemann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't seen any difference in error recovery with the example languages. Do you have an example where the recovery behaves differently?

One minor difference: in the statemachine example, when I insert state state between the events and the initialState, the previous version showed two parser errors

Expecting token of type 'initialState' but found state.statemachine
Expecting end of file but found state.

while the new version shows only one. But in both versions, the recovery does not work in this case: I get no cross-reference resolution for the rest of the file.

@msujew
Copy link
Member Author

msujew commented Mar 9, 2025

I haven't seen any difference in error recovery with the example languages. Do you have an example where the recovery behaves differently?

Me neither (at least with the examples). It might be that the only difference actually is related to the improved parser error message. Thinking further about this change, it's likely that this change mainly improves the error message behavior, as we likely end up at the same recovered parser state, but with less steps (and therefore fewer parser errors).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
parser Parser related issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants