我如何通过 Parser ctx 获取行号?
how could I get the line number by Parser ctx?
public void enterStatus(Parser.Context ctx)
{
}
Parser.Context ctx 没有 getLinuxNumber() 之类的方法。我如何获取解析器行号?
你可以使用ctx.getStart().getLine()
public Token getStart() Get the initial token in this context. Note
that the range from start to stop is inclusive, so for rules that do
not consume anything (for example, zero length or error productions)
this token may exceed stop
来自文档:http://www.antlr.org/api/Java/org/antlr/v4/runtime/ParserRuleContext.html#getStart()
和
int getLine() The line number on which the 1st character of this token
was matched, line=1..n
来自:http://www.antlr.org/api/Java/org/antlr/v4/runtime/Token.html#getLine()
public void enterStatus(Parser.Context ctx)
{
}
Parser.Context ctx 没有 getLinuxNumber() 之类的方法。我如何获取解析器行号?
你可以使用ctx.getStart().getLine()
public Token getStart() Get the initial token in this context. Note that the range from start to stop is inclusive, so for rules that do not consume anything (for example, zero length or error productions) this token may exceed stop
来自文档:http://www.antlr.org/api/Java/org/antlr/v4/runtime/ParserRuleContext.html#getStart()
和
int getLine() The line number on which the 1st character of this token was matched, line=1..n
来自:http://www.antlr.org/api/Java/org/antlr/v4/runtime/Token.html#getLine()