Improve UPLC parser error messages #7489
Open
+163
−70
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.
See linked private issue.
When using
choicewith multiple alternatives wrapped intry, megaparsec can produce misleading or uninformative error messages. This is a fundamental limitation of how megaparsec reports parse errors when backtracking is involved.The
trycombinator 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
trybacktracks 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
trydeeper into each choice.Additionally, Megaparsec uses the longest expected
SrcSpanwhen reporting a failure caused by achoicewith multiple trybranches. As a result, when matching a list of symbols, the length of the longest string is used to construct theSrcSpan, which can make the error message misleading. We can mitigate this by adding afailcase with an explicit error message.