antlr / antlr/antlr4

Why ErrorListner in TS needs Generic in class?

Open
#4,752 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
19k
Forks
3.5k
PR merge metrics
No merged PRs in 30d

Description

- [+ ] I have reproduced my issue using the latest version of ANTLR
- [ -] I have asked at [stackoverflow](http://stackoverflow.com/questions/tagged/antlr4)
- [ +] Responses from the above seem to indicate that my issue could be an ANTLR bug
- [+ ] I have done a search of the existing issues to make sure I'm not sending in a duplicate

target Java and TypeScript

Hi,

Could you explain why ErrorListner in TS needs Generic in class ? Maybe I missed something

`export declare class ErrorListener`

For example Java doesn't have it

` public interface ANTLRErrorListener`

The issue is that I can't use same custom ErrorListener in Lexer and Parser.

```
class ConsoleErrorListener extends ErrorListener {
override syntaxError(recognizer: Recognizer, offendingSymbol: Token, line: number, column: number, msg: string, e: RecognitionException | undefined) {
console.log(`ERROR line: ${line}; column: ${column}; message: ${msg}`);
}
}

export function parseTreeStr(input: string) {
const lexer = createLexer(input);
lexer.removeErrorListeners();
lexer.addErrorListener(new ConsoleErrorListener());
const parser = createParserFromLexer(lexer);
parser.removeErrorListeners();
parser.addErrorListener(new ConsoleErrorListener());
const tree = parser.formula();
return tree.toStringTree(parser.ruleNames, parser);
}
```
lexer.addErrorListener for some reason need number as generic.

`Argument of type ConsoleErrorListener is not assignable to parameter of type ErrorListener`

```
export declare class Recognizer {

state: number;

removeErrorListeners(): void;
addErrorListener(listener: ErrorListener): void; <--- For what we need this generic?
getErrorListener(): ErrorListener;
getLiteralNames(): string[];
getSymbolicNames(): string[];
}
```

Thanks.

Contributor guide

Open the contributing guide

Research direction

Start with the TypeScript declarations for ErrorListener and Recognizer shown in the issue, then trace the types used by createLexer, createParserFromLexer, and addErrorListener. Compare lexer and parser listener requirements and determine whether the generic prevents the shared listener or whether the current behavior is intentional; document or test the conclusion.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.