Improve error messages on failed parsing#48
Conversation
- Add detailed error messages with line and column information - Show error context with surrounding lines - Provide visual pointer to exact error location - Fix issue SciML#45 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
- Add tests for error location reporting - Test various parsing error scenarios - Test helper functions directly - Verify file error messages include filename - Ensure valid code still parses correctly 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
|
Tests pass locally too, and it seems to do what it's supposed to. Only issue is that the error doesn't print out like expected, it looks like ParserException("Failed to parse BaseModelica at line 1, column 2.\n\nError context:\n→ package Test\n ^~~~~~~~~~\n model TestModel\n Real x;\n\nParser stopped at position 1 of 109\n") |
|
That looks correct? when it's displayed the new lines would be rendered? |
|
Sorry should have put the whole thing, julia> BaseModelica.parse_str(invalid_modelica)
ERROR: ParserException("Failed to parse BaseModelica at line 1, column 2.\n\nError context:\n→ package Test\n ^~~~~~~~~~\n model TestModel\n Real x;\n\nParser stopped at position 1 of 109\n")
Stacktrace:
[1] parse_str(data::String)
@ BaseModelica ~/Documents/Work/dev/Modelica/BaseModelica.jl/src/parser.jl:692
[2] top-level scope
@ REPL[7]:1
caused by: ParserException("cannot parse")
Stacktrace:
[1] once(channel::Channel{Any})
@ ParserCombinator ~/.julia/packages/ParserCombinator/kIa3L/src/core/parsers.jl:196
[2] single_result
@ ~/.julia/packages/ParserCombinator/kIa3L/src/core/parsers.jl:201 [inlined]
[3] parse_str(data::String)
@ BaseModelica ~/Documents/Work/dev/Modelica/BaseModelica.jl/src/parser.jl:655
[4] top-level scope
@ REPL[7]:1it's not displaying correctly |
The error messages now display correctly with proper line breaks and formatting. Added a custom Base.showerror method for ParserCombinator.ParserException to ensure the error context with arrows and carets displays properly. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
|
Fixed the display issue! The error messages now render properly with newlines. The problem was that there was no custom Added a simple custom display method that properly prints the formatted error message with all the line breaks and visual indicators (arrows and carets) intact. |
|
Looking closer it always points to the first character of the model. debug.max_iter is always one. Don't think the debug API of ParserCombinator is being used correctly. |
Co-authored-by: Jadon Clugston <34165782+jClugstor@users.noreply.github.com>
Co-authored-by: Jadon Clugston <34165782+jClugstor@users.noreply.github.com>
|
Check this one. |
|
Gives the correct line and column now, but without the |
This type piracy is necessary to properly display error messages with newlines. Without this, the formatted error context appears as escaped strings instead of rendering the line breaks and visual indicators correctly. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
|
Added the Base.showerror overload for ParserCombinator.ParserException to properly render the error messages with newlines. The type piracy is necessary since we're defining a method for Base.showerror on a type from ParserCombinator package. |
…rectly See the discussion in SciML/BaseModelica.jl#48
|
The display issue was upstreamed so that can be followed at andrewcooke/ParserCombinator.jl#47 |
Summary
This PR improves error messages when parsing fails by providing detailed location information and context, addressing issue #45.
Changes
get_position_infofunction to calculate line and column numbers from string positionformat_error_contextfunction to show the problematic code with visual indicatorsparse_strto use ParserCombinator's debug mode and generate informative error messagesparse_fileto include filename in error messagesExample Output
Instead of the generic error:
Users now get:
Test Results
Fixes #45
🤖 Generated with Claude Code