[英]Correct way to add custom exception to Lexer/Parser files in antlr4
I have a custom ParsingException(String message, int location, String offendingText) 我有一个自定义的ParsingException(字符串消息,整数位置,字符串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. 我收到一个UnhandledException错误。
您应该重写syntaxError
的BaseErrorListener
syntaxError
方法而不是notifyErrorListeners
并按照此处syntaxError
方法进行recover
: 处理ANTLR4中的错误 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.