Exception 在“中包含空格”;“投入时无可行的替代方案”-ANTLR4中的异常

Exception 在“中包含空格”;“投入时无可行的替代方案”-ANTLR4中的异常,exception,exception-handling,antlr,antlr4,Exception,Exception Handling,Antlr,Antlr4,当解析我的文档并提供与ANTLR4没有可行替代方案的代码时,我在我的ErrorHandler中正确地接收到一个SyntaxError,消息为:input处没有可行替代方案'/mig100100moveto250230linetostroke350150moveto200200{' 因为我用下面的Lexer规则从输入中去掉了空格 NEWLINE: ('\r')? '\n' -> skip; WHITESPACE: (' '|'\t')+ -> skip; 它不再包含空格和新行 有

当解析我的文档并提供与ANTLR4没有可行替代方案的代码时,我在我的ErrorHandler中正确地接收到一个SyntaxError,消息为:
input处没有可行替代方案'/mig100100moveto250230linetostroke350150moveto200200{'

因为我用下面的Lexer规则从输入中去掉了空格

NEWLINE: ('\r')? '\n' -> skip;
WHITESPACE:   (' '|'\t')+ -> skip;
它不再包含空格和新行

有没有办法在ErrorHandler中获得格式良好的消息(可能来自输入流),该消息可以打印出来,并且仍然包含空格

no viable alternative at input '/mig
100 100 moveto
250 230 lineto
stroke

350 150 moveto
200 200 {'
我在从
BaseErrorListener

public override void SyntaxError(IRecognizer recognizer, IToken offendingSymbol, int line, int charPositionInLine, string msg, RecognitionException e)
{
    var interval = new Interval(offendingSymbol.StartIndex, offendingSymbol.StopIndex);
    var inputStream = offendingSymbol.InputStream as AntlrInputStream;
    if (inputStream != null)
    {
        // TODO: If you get the source-code without the spaces, use this original text instead.
        var originalText = inputStream.GetText(interval);
    }

    Console.WriteLine("Line {0}:{1} {2}", line, charPositionInLine, msg);
    Console.WriteLine("Line {0}:{1} {2}", line, charPositionInLine, originalText);
}
但是产生的
originalText
只包含有问题的符号
{
,而不是解析器没有找到可行替代方案的地方


如果我能得到索引,我会很高兴,因为解析器没有找到可行的替代方案(在
/mig
的开头)

而不是跳过标记,您可以将它们设置为隐藏通道。 这将在显示错误消息时保留空白

NEWLINE:    ('\r')? '\n' -> channel(HIDDEN);
WHITESPACE: (' '|'\t')+  -> channel(HIDDEN);

正是我需要的!