获取相对于文件开头的令牌起始字符位置 [英] Getting token start character position relative to the beginning of file

查看:23
本文介绍了获取相对于文件开头的令牌起始字符位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

antlr4 API 有没有可靠的方法来获取相对于文件开头而不是行的令牌起始字符位置?在做了一些研究之后,我发现唯一的方法是使用 IntStream 的一些自定义实现,它不会将 '\n' 视为行终止符,但也许我错过了一些更简单的方法?如果重要的话,我正在使用访问者 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.

推荐答案

您可以使用以下任何一种,具体取决于您是否拥有 TokenTerminalNode.

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

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

这篇关于获取相对于文件开头的令牌起始字符位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆