是否有任何类型的“在线访问者"在Eclipse JDT Java分析器中?如果没有,有人知道一个好的解决方法吗? [英] Is there any kind of "line visitor" in Eclipse JDT Java Parser? If there is not, does someone knows a good workaround?

查看:83
本文介绍了是否有任何类型的“在线访问者"在Eclipse JDT Java分析器中?如果没有,有人知道一个好的解决方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Java文件访问AST中的节点,并提取一些与这些节点有关的信息.但是,我想通过由源代码文件中的各行引导的AST.我知道有关于与每个节点关联的行的信息,但是问题是,访问节点的默认方式是通过特定的访问者.因此:1.避免冗余访问节点,2.在尝试枚举所有可能的节点类型(或访问者)时不会产生开销,并且3.以有序方式访问节点中的信息,我需要一种的行访客",这样我就可以按照源代码文件中的行访问AST节点中的信息.有人知道使用Eclipse JDT API甚至是解决方法的标准方法吗?

解决方案

我不能直接从Eclipse AST的知识中受益.但是,就这些 传统AST简单用Java表示的程度而言,在没有任何其他帮助的情况下,您访问树节点的方式几乎就是通过走树来实现的.

当然,您可能可以通过Eclipse与此类节点关联的某种类型的文件位置信息(行,列,...)来过滤AST节点,并仅过滤标记有所需行的AST.除非您真的真正在乎这需要多长时间(最糟糕的情况是文件大小呈线性,我在其他系统上的经验表明,每个源行平均可获得约5-7个节点),这对于您的目的应该足够了.

如果您想直接访问与特定行号关联的树节点,我想您很不走运.显然,您可以自己走一棵树并收集所有具有特定线数的节点,从而自己构建这样的地图.那么您便可以拥有所需的访问权限. [您实际上只需要关联一条线的 first AST(在有序树状路径的最左侧),此地图才能使用] 同样,用于构建此列表的树步是线性时间,您只需支付一次即可. FWIW,我已经开发了处理AST的工具大约30年来,并没有发现它特别有用.

如果您坚持要降低构建该地图的成本,那么我将查看解析机制并对其进行修改以完成此工作.它制造所有这些AST节点,并且在制造这样的节点时知道正在处理的源的行号.随着AST节点的生成,构建地图应该很容易.如果您的解析器很好,那么它实际上就是线性时间,添加这项工作不会改变线性度.

I wanna visit the nodes in an AST from a Java file and extract some information related to these nodes. But, I wanna pass by the AST guided by the lines in the source code file. I know there is information about the lines associated with each node, but the problem is that the default way to access the nodes is through specific visitors. So: 1. to avoid redundant visits to the nodes, 2. do not generates overhead while trying to enumerate all the possible node types (or visitors), and 3. to access the information in nodes in an ordered way, I need a kind of "Line Visitor", so that I can access information in AST nodes, following the lines in the source code file. Does someone know a standard way to do it with Eclipse JDT API or even a workaround?

解决方案

I can't speak from direct knowledge of Eclipse ASTs. However, to the extent that these are traditional ASTs simply represented in Java, then pretty much the way you have to visit the tree nodes in the absence of any other help, is by walking the tree.

Of course, you can probably filter AST nodes by some type of file position information (line, column, ...) that Eclipse associates with such nodes, and simply filter for ASTs stamped with the line you want. Unless you really, really care about how long this takes (it is worst case linear in the file size, my experience with other systems suggest you get ~5-7 nodes per source line average), this should be good enough for your purposes.

If you wanted direct access to the tree nodes associated with a specific line number, my guess is you are out of luck. Obviously, you can build such a map yourself by walking the tree once and collecting all the nodes that have a specific line numbe; then you could have the access you want. [You really only need to associate the first AST of a line {leftmost in an inorder tree-walk) for this map to be usable] Again the tree walk to build this list is linear time and you'll only pay it once. FWIW, I've been building tools that process ASTs for ~~30 years and have not found this to be particularly useful.

If you insist, and you want to lower the cost of building this map, I'd look inside the parsing machinery and modify it to do this work. It manufactures all those AST nodes, and it knows the line number of the source being processed when it manufactures such a node. It should be easy to build the map as the AST nodes are generated. If your parser is any good, it is effectively linear time, and adding this work won't change the linearity.

这篇关于是否有任何类型的“在线访问者"在Eclipse JDT Java分析器中?如果没有,有人知道一个好的解决方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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