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

Add pretty errors #226

Merged
merged 1 commit into from
Apr 11, 2024
Merged

Add pretty errors #226

merged 1 commit into from
Apr 11, 2024

Conversation

johnspade
Copy link
Contributor

Add pretty error messages indicating the position of the error in the input. Currently implemented for parseString/parseChars only.

val positions = lines.scanLeft(0)(_ + _.length + 1) // +1 for newline character
val errorLineIndex = positions.indexWhere(_ > pos) - 1

val contextRange = (math.max(errorLineIndex - 2, 0), math.min(errorLineIndex + 2, lines.length - 1))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For multiline input, output the previous two lines and the next two lines, replace the rest with an ellipsis

@@ -1439,3 +1439,76 @@ object Parser {
final case class AllBranchesFailed[Err](left: ParserError[Err], right: ParserError[Err]) extends ParserError[Err]
}
}

final case class ParseFailure[+Err](input: String, error: ParserError[Err]) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The main change, a wrapper over the input and parser error

Copy link
Collaborator

Choose a reason for hiding this comment

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

I would change it's name, let's say to StringParserError. That's what it really is, compared to the generic ParserError - because we know the input was a string we can provide this extra error pretty printing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, renamed the error

@johnspade johnspade marked this pull request as ready for review March 30, 2024 15:13
@vigoo vigoo merged commit 3d1dac2 into zio:master Apr 11, 2024
15 checks passed
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