Add ThrowingErrorListener to Python ANTLR4 parser to match JavaScript behavior - #110
Merged
Merged
Conversation
… implementation Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com>
Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Throw error on syntax issues in antlr4 python bindings
Add ThrowingErrorListener to Python ANTLR4 parser to match JavaScript behavior
Dec 12, 2025
hzhangxyz
approved these changes
Dec 12, 2025
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds error-throwing behavior to the Python ANTLR4 parser to match the existing JavaScript implementation. Previously, the Python parser would return partial parse trees on syntax errors, which could lead to unexpected behavior or downstream errors.
- Implements
ThrowingErrorListenerclass that raises exceptions on syntax errors - Updates both
parse()andunparse()functions to use the custom error listener - Adds comprehensive test coverage for error scenarios across both parse and unparse operations
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| bnf/apyds_bnf/init.py | Adds ThrowingErrorListener class and integrates it into both parse() and unparse() functions by removing default listeners and attaching the custom error listener to lexer and parser |
| bnf/tests/test_parse_unparse.py | Adds 5 new error handling test cases covering malformed syntax scenarios using pytest.raises() to verify exceptions are properly thrown |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
The Python ANTLR4 parser was returning partial parse trees on syntax errors instead of throwing exceptions, diverging from the JavaScript implementation.
Changes
ThrowingErrorListener: Custom error listener that raises exceptions on syntax errors with format"line {line}:{column} {msg}"parse()andunparse(): Both functions now remove default error listeners and attachThrowingErrorListenerto lexer and parserpytest.raises()Example
Before:
After:
Implementation mirrors JavaScript's
ThrowingErrorListenerclass for consistent cross-language behavior.Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
central.sonatype.com/home/REDACTED/.local/bin/antlr4 antlr4 -Dlanguage=Python3 Ds.g4 -visitor -no-listener -o apyds_bnf(dns block)/home/REDACTED/.local/bin/antlr4 antlr4 -Dlanguage=JavaScript Dsp.g4 -visitor -no-listener -o atsds_bnf github.com(dns block)/home/REDACTED/.local/bin/antlr4 antlr4 -Dlanguage=JavaScript Ds.g4 -visitor -no-listener -o atsds_bnf github.com(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.