nextToken()不从流中消耗令牌?

时间:2015-10-03 18:52:07

标签: antlr antlr4

我能够生成以下RuntimeException并且不确定原因。我的理解是nextToken()推进了底层流。违规令牌是我正在解析的文档中间的普通令牌,而不像EOF那样奇怪。

class TokenStreamRelease implements TokenStream
{
    private final MyLexer _lexer; // MyLexer extends org.antlr.v4.runtime.Lexer

    Token _nextToken;

    @Override
    public Token next()
    {
        Token toReturn = _nextToken;
        _nextToken = _lexer.nextToken();
        if(_nextToken == _lexer.getToken())
        {
            throw new RuntimeException();
        }
        return toReturn;
    }
}

1 个答案:

答案 0 :(得分:1)

您的代码基本上说:如果当前令牌等于当前令牌,则抛出RuntimeException

Lexer.getToken定义为:

public Token getToken() { return _token; }

Lexer.nextToken更新和returns _token

因此,getToken之后立即调用nextToken会获得与结果相同的令牌。