5

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.

Andre Silva
  • 4,782
  • 9
  • 52
  • 65

1 Answers1

5

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

Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
  • 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()`. – user2592325 Jul 18 '13 at 05:14
  • 2
    @user2592325 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`. – Sam Harwell Jul 18 '13 at 05:24