本文介绍了返回当前令牌的最后一个字符的行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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().

这篇关于返回当前令牌的最后一个字符的行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 18:23