Skip to content

Fix ANTLR4 JS binding to throw on syntax errors instead of returning partial trees - #108

Merged
hzhangxyz merged 3 commits into
mainfrom
copilot/fix-js-binding-error-handling
Dec 12, 2025
Merged

Fix ANTLR4 JS binding to throw on syntax errors instead of returning partial trees#108
hzhangxyz merged 3 commits into
mainfrom
copilot/fix-js-binding-error-handling

Conversation

Copilot AI commented Dec 12, 2025

Copy link
Copy Markdown
Contributor

ANTLR4's JavaScript parser was silently logging syntax errors while returning partial parse trees, allowing invalid input to proceed.

Changes

  • Added ThrowingErrorListener: Custom error listener that throws Error on syntax errors instead of logging
  • Updated parse() and unparse(): Removed default error listeners, installed throwing listener on both lexer and parser
  • Added error handling tests: 5 test cases covering malformed syntax scenarios

Before/After

// Before: logs error, returns partial tree
parse("(a + b -> c");
// line 1:7 no viable alternative at input '(a+b->'
// => "----\n"

// After: throws immediately
parse("(a + b -> c");
// Error: line 1:7 no viable alternative at input '(a+b->'

All existing tests pass. Error messages include line/column information from ANTLR4.

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=JavaScript Dsp.g4 -visitor -no-listener -o atsds_bnf (dns block)
    • Triggering command: /home/REDACTED/.local/bin/antlr4 antlr4 -Dlanguage=JavaScript Ds.g4 -visitor -no-listener -o atsds_bnf (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的js binding中,如果语法出错,他也会给出能parse的部分,需要让他throw error</issue_title>
<issue_description>在 bnf 目录下面</issue_description>

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


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits December 12, 2025 08:17
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] Fix error handling in antlr4 js binding Fix ANTLR4 JS binding to throw on syntax errors instead of returning partial trees Dec 12, 2025
Copilot AI requested a review from hzhangxyz December 12, 2025 08:23
@hzhangxyz
hzhangxyz marked this pull request as ready for review December 12, 2025 08:29
Copilot AI review requested due to automatic review settings December 12, 2025 08:29

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 fixes ANTLR4's JavaScript parser binding to throw errors on syntax errors instead of silently logging them and returning partial parse trees. This ensures invalid input is immediately rejected rather than being partially processed.

Key Changes:

  • Implemented custom ThrowingErrorListener class that throws errors instead of logging them
  • Updated parse() and unparse() functions to remove default error listeners and install the throwing listener on both lexer and parser
  • Added 5 comprehensive error handling tests covering various malformed syntax scenarios

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
bnf/atsds_bnf/index.mjs Adds ThrowingErrorListener class and integrates it into both parse() and unparse() functions by removing default listeners and adding the throwing listener to lexer and parser
bnf/tests/test_parse_unparse.mjs Adds 5 error handling test cases for missing parentheses, bad syntax, malformed expressions, incomplete binary, and malformed function syntax

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread bnf/atsds_bnf/index.mjs
Comment on lines +113 to +118
lexer.removeErrorListeners();
lexer.addErrorListener(new ThrowingErrorListener());
const tokens = new CommonTokenStream(lexer);
const parser = new DspParser(tokens);
parser.removeErrorListeners();
parser.addErrorListener(new ThrowingErrorListener());

Copilot AI Dec 12, 2025

Copy link

Choose a reason for hiding this comment

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

The error listener setup code is duplicated in both parse() and unparse() functions. Consider extracting this into a helper function to reduce code duplication and improve maintainability. For example, you could create a function like setupThrowingErrorListener(lexer, parser) that removes default listeners and adds the throwing listener to both the lexer and parser.

Copilot uses AI. Check for mistakes.
@hzhangxyz
hzhangxyz merged commit 5a10e28 into main Dec 12, 2025
118 checks passed
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的js binding中,如果语法出错,他也会给出能parse的部分,需要让他throw error

3 participants