feat(errors): improve error messages with example values

Add token type examples to make error messages more helpful. Created an
ExpectedToken enum to replace string literals for better type safety,
added example values for each token type, and enhanced error display to
show concrete examples of valid syntax.
This commit is contained in:
Khaïs COLIN 2025-06-03 19:12:50 +02:00
parent e78511f692
commit 567aa31c07
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
8 changed files with 105 additions and 25 deletions

View file

@ -235,7 +235,7 @@ i will use rustyline, since it seems like the most feature-complete
insert <id:int> <username:string> <email:string>
** DONE parse row insert
* DONE separate statements with semicolons
* TODO this error message could be better
* DONE this error message could be better
#+begin example
Error: unexpected token
╭─[ <stdin>:1:24 ]
@ -247,8 +247,29 @@ Error: unexpected token
│ Note: expected token type to be one of ["semicolon"]
───╯
#+end example
* TODO correct all instances of <unknown> in locations
** plan
1. Create an example mapping system
- Define a mapping of token types to example values
- Example: "integer" → "42", "string" → "example", "semicolon" → ";"
2. Enhance CommandParseError
- Add a method to generate user-friendly error messages
- Include both the expected token type and concrete examples
3. Implementation approach
- Create a static lookup table or function that returns examples
- Extend existing error handling to include examples in messages
- Make sure the examples follow SQL syntax conventions
4. Error display refinement
- Update error_display.rs to include these examples
- Format error messages to show both what was expected and example syntax
5. Testing
- Add tests that verify the error messages include helpful examples
- Ensure examples are contextually appropriate
This will make errors like "expected semicolon" more helpful by showing "expected semicolon (example: ;)".
TODO * TODO correct all instances of <unknown> in locations
* TODO meta-commands must be followed by end-of-file
* TODO project code documentation
* TODO project usage documentation
* DONE in case of parse error, skip until next semicolon to better recover
* TODO serialize/deserialize row to/from raw bytes
** TODO look for best practices for creating binary formats