Getting token start character position relative to the beginning of file

走远了吗. 提交于 2020-01-24 05:44:26

问题


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.


回答1:


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

  • Token.getStartIndex()
  • TerminalNode.getSourceInterval().a Edit: This will return the token index, not the character position.
  • TerminalNode.getSymbol().getStartIndex()


来源:https://stackoverflow.com/questions/17706327/getting-token-start-character-position-relative-to-the-beginning-of-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!