获取令牌起始字符相对于文件开头的位置 [英] Getting token start character position relative to the beginning of file
问题描述
是否有使用antlr4 API的可靠方法来获取Token起始字符相对于文件开头而不是行的位置?经过研究后,我发现的唯一方法是使用IntStream的一些自定义实现,该实现不将'\ n'视为行终止符,但也许我缺少一些更简单的方法了吗?如果需要的话,我正在使用Visitor API.
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.
我正在处理的应用程序解析源文件,并为另一个使用提供的坐标插入其他代码的应用程序提供插入坐标.如果另一个应用程序在文件中获得符号位置,而不是使用line:positionInLine对,将会更加方便.
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.
推荐答案
根据是否具有 TerminalNode
.
You can use any of the following, depending on whether you have a Token
or a TerminalNode
.
-
Token.getStartIndex()
-
> c3>这将返回令牌索引,不是字符位置..a
-
TerminalNode.getSymbol()
.getStartIndex()
Token.getStartIndex()
This will return the token index, not the character position.TerminalNode.getSourceInterval()
.a
TerminalNode.getSymbol()
.getStartIndex()
这篇关于获取令牌起始字符相对于文件开头的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!