Skip to content

Comments

Improve UPLC parser error messages#7489

Merged
zeme-wana merged 2 commits intomasterfrom
bad-error-messages
Jan 13, 2026
Merged

Improve UPLC parser error messages#7489
zeme-wana merged 2 commits intomasterfrom
bad-error-messages

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.

@zeme-wana zeme-wana merged commit 49fa834 into master Jan 13, 2026
0 of 2 checks passed
@zeme-wana zeme-wana deleted the bad-error-messages branch January 13, 2026 10:53
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.

2 participants