问题描述
ANTLR 4中是否可以返回当前令牌的最后一个字符的行号?
Is there a way in ANTLR 4 to be able to return the line number of the the last character for the current token ?
我提到了 Antlr,从令牌中获取最后一行,但是那是特定于规则.我想要更通用的东西,但在ANTLR API中找不到适合我的东西.
I referred Antlr, get last line from token but that would be specific to a rule. I wanted something more generic but couldn't find what would suit me in the ANTLR API.
推荐答案
没有直接的方法来获取此信息.但是,如果您的词法分析器中没有任何-> skip
命令,则可以从以下标记派生它.
There is no direct way to get this information. However, if you don't have any -> skip
commands in your lexer you can derive it from the following token.
假设令牌b
跟在令牌a
之后.如果为b.getCharPositionInLine()==0
,则a
的最后一个字符在行b.getLine()-1
上.否则,a
的最后一个字符在行b.getLine()
上.
Suppose token b
follows token a
. If b.getCharPositionInLine()==0
, the last character of a
is on line b.getLine()-1
. Otherwise, the last character of a
is on line b.getLine()
.
这篇关于返回当前令牌的最后一个字符的行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!