Skip to content

Conversation

@zeme-wana
Copy link
Collaborator

See linked private issue.

When using choice with multiple alternatives wrapped in try, megaparsec can produce misleading or uninformative error messages. This is a fundamental limitation of how megaparsec reports parse errors when backtracking is involved.

The try combinator fully backtracks on failure, restoring the input stream to its original position. This means that even if a parser consumed significant input before failing, the input position is reset.

Because try backtracks fully, the error message only reflects the outer parser's expectations, not the specific alternative that was attempted. All the detailed error information from deep within a failing alternative is lost.

In order to this fix, we need move the try deeper into each choice.

Additionally, Megaparsec uses the longest expected SrcSpan when reporting a failure caused by a choice with multiple try branches. As a result, when matching a list of symbols, the length of the longest string is used to construct the SrcSpan, which can make the error message misleading. We can mitigate this by adding a fail case with an explicit error message.

@kwxm
Copy link
Contributor

kwxm commented Dec 26, 2025

Note also #7391.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants