问题
Is there a way in ANTLR 4 to be able to return the line number of the the last character for the current token ?
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.
回答1:
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.
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().
来源:https://stackoverflow.com/questions/17487718/return-the-line-number-of-the-last-character-for-current-token