-1

The application I'm building targets portuguese speaking users. The users can input a sentence which will be interpreted by ANTLR. To provide the users the best feedback from their input I will need to have errors displayed in portuguese.

I didn't found an configuration or file where i could change the error tokens like in some other libraries.

2 Answers2

2

You cannot change the language of errors/exception coming out of an ANTLR generated parser. You will need to catch these exceptions yourself, and make (for your audience) human readable messages. ANTLR's exceptions have enough for you to do this: line number, column index, tokens: everything is available for you.

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
1

As i searched deeper the best alternative is to override the methods on DefaultErrorStrategy and build the message the way you want.

You can set an error Handler to your parser, it should be your new custom error strategy.

typescript DefaultErrorStrategy

Here is my override for reportInputMismatch method with custom translation:

reportInputMismatch(recognizer: Parser, e: InputMismatchException): void {
    const expected = e.expectedTokens;
    const expectedString = expected ? this.getFriendlyExpectedTokens(expected, recognizer.vocabulary) : '';
    const input = this.getTokenErrorDisplay(e.getOffendingToken(recognizer));
    const msg = `entrada ${input} não compatível, espera-se ${expectedString}`;
    this.notifyErrorListeners(recognizer, msg, e);
}