Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why ErrorListner in TS needs Generic in class? #4752

Open
IrwinJuice opened this issue Dec 31, 2024 · 0 comments
Open

Why ErrorListner in TS needs Generic in class? #4752

IrwinJuice opened this issue Dec 31, 2024 · 0 comments

Comments

@IrwinJuice
Copy link

IrwinJuice commented Dec 31, 2024

  • [+ ] I have reproduced my issue using the latest version of ANTLR
  • [ -] I have asked at stackoverflow
  • [ +] 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<TSymbol>

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<Token> {
  override syntaxError(recognizer: Recognizer<Token>, 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<number>

export declare class Recognizer<TSymbol> {

    state: number;

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

Thanks.

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

No branches or pull requests

1 participant