Getting token start character position relative to the beginning of file
Asked Answered
W

1

6

Is there any reliable way with antlr4 API to get Token start character position relative to the beginning of file, not line? After doing some research the only way I found is to use some custom implementation of IntStream, that does not treat '\n' as line terminators, but perhaps I'm missing some easier way? I'm using Visitor API if it matters.

The application I'm working on parses source files and provides insertion coordinates for another application that uses provided coordinates to insert additional code. It would be much more convenient if that another application got symbol position in file, rather than line:positionInLine pair.

Whip answered 17/7, 2013 at 17:26 Comment(0)
M
6

You can use any of the following, depending on whether you have a Token or a TerminalNode.

Medlin answered 17/7, 2013 at 22:3 Comment(2)
Thanks! I wasn't very happy with Token.getStartIndex(), because it is marked as optional in the Token javadoc, and didn't knew about the TerminalNode way. Guess I'll just give it a try with getStartIndex().Whip
@Whip I was not correct regarding getSourceInterval. You'll need to use Token.getStartIndex(), and use TerminalNode.getSymbol() if necessary to get the actual Token for a TerminalNode.Medlin

© 2022 - 2024 — McMap. All rights reserved.