Add contextual hints to parser error messages#18
Merged
Conversation
Improve error messages with helpful hints for common mistakes, especially for users coming from YAML: - YAML-style colon: "TOML uses = for key-value pairs, not :" - Unquoted strings: "Strings must be quoted. Did you mean \"hello\"?" - YAML booleans: "TOML booleans are true or false, not yes/no/on/off" - Semver strings: "This looks like a version string. Must be quoted" - Unclosed brackets/braces: Hints about proper syntax - Multiple values on line: "Each key-value pair must be on its own line" - Missing key: "A key is required before =" - Consecutive dots: "Empty key segment. Keys cannot have consecutive dots" The ParseError class already supports hints via the format() method.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Improves parser error messages with helpful hints for common mistakes, especially for users coming from YAML/JSON backgrounds.
The
ParseErrorclass already supports hints via theformat()method andParseExceptionhas ahintproperty - this PR adds contextual hints to error messages.Before / After
version: "1.0"Expected =Expected =+ hint: TOML uses `=` for key-value pairs, not `:`. Example: key = "value"name = helloExpected valueExpected value+ hint: Strings must be quoted in TOML. Did you mean `"hello"`?version = 1.0.0Invalid token: \1.0.0``Invalid token: \1.0.0`+ hint: *This looks like a version string. Strings must be quoted: \"1.0.0"`*enabled = yesExpected valueExpected value+ hint: TOML booleans are `true` or `false`, not `yes`.]in[databaseExpected ]Expected ]+ hint: Unclosed bracket. Table headers use [name] syntax.}in{ x = 1Expected }Expected }+ hint: Unclosed brace. Inline tables use { key = value } syntax.a = 1 b = 2Expected newline or end of input after value= "value"Unexpected token: \=``a..b = 1Expected keyExpected key+ hint: Empty key segment. Keys cannot have consecutive dots.