-
Notifications
You must be signed in to change notification settings - Fork 354
Improve parser error for bare except blocks with clear
#4337
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Improves Jac parser diagnostics for the common misparse of bare except { ... } by attempting to detect the scenario and emit a clearer, actionable error message, with accompanying regression tests.
Changes:
- Add a parser heuristic to detect bare
except {when the parser misinterprets{as a dict and then encounters semicolons. - Emit a dedicated error message for bare
exceptusage. - Add a new fixture and unit test covering the new diagnostic.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
jac/jaclang/pycore/jac_parser.py |
Adds bare-except misparse detection and emits a specialized error message. |
jac/tests/compiler/test_parser.py |
Adds a test asserting the bare-except diagnostic message. |
jac/tests/compiler/fixtures/bare_except_error.jac |
Adds a fixture that triggers the bare-except misparse path. |
| "Bare except is not supported. Use `except Exception` or `except Exception as e`", | ||
| error_tok, | ||
| ) | ||
| return False # Stop parsing gracefully |
Copilot
AI
Jan 27, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Returning False from on_error causes Lark to re-raise the parse error, and JacParser.transform() will then log an additional generic "Unexpected token ..." error. That undermines the goal of a single clear bare-except diagnostic. Consider returning True with recovery (or setting a flag so the outer except jl.UnexpectedInput path skips logging) so only the bare-except message is reported.
| return False # Stop parsing gracefully | |
| return True # Error handled; allow Lark to recover without re-raising |
| assert len(prog.errors_had) >= 1 | ||
| # Check that the first error mentions bare except | ||
| error_msg = prog.errors_had[0].pretty_print() | ||
| assert "Bare except is not supported" in error_msg | ||
| assert "except Exception" in error_msg | ||
| assert "except Exception as e" in error_msg |
Copilot
AI
Jan 27, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test validates the new error message text, but it doesn't validate the key behavioral improvement described in the PR (pointing the error at the except line rather than the misparsed semicolon/dict context). Consider asserting the reported location (e.g., prog.errors_had[0].loc.first_line or a line <n> substring in pretty_print()) so regressions in the line/column mapping are caught.
except blocks with clear
Description
This PR improves the parser’s handling of bare
exceptblocks by detecting a common misparse scenario whereexcept {is interpreted as a dictionary context.When semicolons appear inside what the parser assumes is a dict, we now:
This prevents confusing syntax errors and provides a graceful, actionable failure:
Overall, this makes error reporting more precise, easier to understand, and safer for developers writing exception handling code.
examples:

Additionally, new test cases have been added.