Skip to content

Add ThrowingErrorListener to Python ANTLR4 parser to match JavaScript behavior - #110

Merged
hzhangxyz merged 3 commits into
mainfrom
copilot/throw-error-on-syntax-issue
Dec 12, 2025
Merged

Add ThrowingErrorListener to Python ANTLR4 parser to match JavaScript behavior#110
hzhangxyz merged 3 commits into
mainfrom
copilot/throw-error-on-syntax-issue

Conversation

Copilot AI commented Dec 12, 2025

Copy link
Copy Markdown
Contributor

The Python ANTLR4 parser was returning partial parse trees on syntax errors instead of throwing exceptions, diverging from the JavaScript implementation.

Changes

  • Added ThrowingErrorListener: Custom error listener that raises exceptions on syntax errors with format "line {line}:{column} {msg}"
  • Updated parse() and unparse(): Both functions now remove default error listeners and attach ThrowingErrorListener to lexer and parser
  • Added error handling tests: 5 test cases covering malformed syntax scenarios using pytest.raises()

Example

Before:

from apyds_bnf import parse

parse("(a + b -> c")  # Prints error to stderr, returns partial tree, causes IndexError

After:

from apyds_bnf import parse

parse("(a + b -> c")  # Raises: Exception("line 1:7 no viable alternative at input '(a+b->'")

Implementation mirrors JavaScript's ThrowingErrorListener class 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
    • Triggering command: /home/REDACTED/.local/bin/antlr4 antlr4 -Dlanguage=Python3 Ds.g4 -visitor -no-listener -o apyds_bnf (dns block)
    • Triggering command: /home/REDACTED/.local/bin/antlr4 antlr4 -Dlanguage=JavaScript Dsp.g4 -visitor -no-listener -o atsds_bnf github.com (dns block)
    • Triggering command: /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

This section details on the original issue you should resolve

<issue_title>antlr4的python binding中,如果语法出错,他也会给出能parse的部分,需要让他throw error</issue_title>
<issue_description>在 bnf 目录下面

需要参考 js 的做法, 也在bnf目录中, 最好能一比一复刻做法</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 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.

Copilot AI and others added 2 commits December 12, 2025 08:56
… 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
Copilot AI requested a review from hzhangxyz December 12, 2025 09:02
@hzhangxyz
hzhangxyz marked this pull request as ready for review December 12, 2025 09:04
Copilot AI review requested due to automatic review settings December 12, 2025 09:04
@hzhangxyz
hzhangxyz merged commit 5ed0e1c into main Dec 12, 2025
110 checks passed

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ThrowingErrorListener class that raises exceptions on syntax errors
  • Updates both parse() and unparse() 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

antlr4的python binding中,如果语法出错,他也会给出能parse的部分,需要让他throw error

3 participants