antlr4 icon indicating copy to clipboard operation
antlr4 copied to clipboard

Unable to customize error messages on top of error messages generated by ANTLR4 Parser

Open ViswaTejaKommireddy opened this issue 1 year ago • 1 comments

Generating custom error messages on top of error messages generated by ANTLR4

I am trying to customize the error messages generated by ANTLR4 by categorizing them. I am using syntax error generated by ErrorListner in ANTLR4 class CustomErrorListener<TSymbol = any> implements ErrorListener<TSymbol> { private syntaxErrors: { line: number; column: number; lastCharPosition: number; message: string }[] = []; syntaxError( recognizer: Recognizer<TSymbol>, offendingSymbol: TSymbol, line: number, column: number, message: string, exception?: RecognitionException ): void {}

But I am unable to distinguish the exceptions using the Exception types. Please refer the below code if (exception instanceof RecognitionException) { message = "Recoginition Error: " + message; } if (exception instanceof NoViableAltException) { message = "No Viable Alternative Error: " + message; }

Please let me know if there are any ways to customize the error messages

ViswaTejaKommireddy avatar May 10 '24 06:05 ViswaTejaKommireddy

I was just looking at error reporting. https://github.com/antlr/antlr4/issues/3700#issuecomment-2102551486. You have to override the error strategy as well since that is where the error message is constructed from the parser state.

kaby76 avatar May 10 '24 10:05 kaby76