1

I have a custom ParsingException(String message, int location, String offendingText)

I want my parser to throw this exception when a parsing / lexing error is encountered.

Is this correct ?

@parser::members
{
  @Override
  public void notifyErrorListeners(Token offendingToken, String msg, RecognitionException ex)
  {
  throw new ParsingException(msg,offendingToken.getStartIndex(),offendingToken.getText());
  }
}
@lexer::members {
    @Override
    public void recover(RecognitionException ex)
    {
 throw new ParsingException(ex.getMessage(),getCharPositionInLine(),ex.getOffendingToken().getText());
        }
          }

I get an UnhandledException error with this.

ps_messenger
  • 111
  • 1
  • 9

1 Answers1

1

You should override syntaxError method of BaseErrorListener instead of notifyErrorListeners and recover as it decribed here: Handling errors in ANTLR4.

Community
  • 1
  • 1
Ivan Kochurkin
  • 4,413
  • 8
  • 45
  • 80
  • ok, but if I do lexer.addErrorListener(ThrowingParsingExceptionErrorListener.INSTANCE); how can I get the offending text since the offendingSymbol is null ? Thanks – ps_messenger Jul 22 '16 at 11:17