Fix ANTLR4 JS binding to throw on syntax errors instead of returning partial trees - #108
Conversation
Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com>
Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com>
There was a problem hiding this comment.
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
ThrowingErrorListenerclass that throws errors instead of logging them - Updated
parse()andunparse()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.
| lexer.removeErrorListeners(); | ||
| lexer.addErrorListener(new ThrowingErrorListener()); | ||
| const tokens = new CommonTokenStream(lexer); | ||
| const parser = new DspParser(tokens); | ||
| parser.removeErrorListeners(); | ||
| parser.addErrorListener(new ThrowingErrorListener()); |
There was a problem hiding this comment.
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.
ANTLR4's JavaScript parser was silently logging syntax errors while returning partial parse trees, allowing invalid input to proceed.
Changes
ThrowingErrorListener: Custom error listener that throwsErroron syntax errors instead of loggingparse()andunparse(): Removed default error listeners, installed throwing listener on both lexer and parserBefore/After
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/home/REDACTED/.local/bin/antlr4 antlr4 -Dlanguage=JavaScript Dsp.g4 -visitor -no-listener -o atsds_bnf(dns block)/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
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.