返回当前标记的最后一个字符的行号 [英] Return the line number of the last character for current token
问题描述
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, get last line from token 但这将是特定于规则.我想要更通用的东西,但在 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.
推荐答案
没有直接的方法可以获取此信息.但是,如果您没有任何 ->在您的词法分析器中跳过
命令,您可以从以下标记派生它.
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()
.
这篇关于返回当前标记的最后一个字符的行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!