Is there any kind of “line visitor” in Eclipse JDT Java Parser? If there is not, does someone knows a good workaround?

女生的网名这么多〃 提交于 2019-12-13 14:05:56

问题


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?


回答1:


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.

|improve this answer

来源:https://stackoverflow.com/questions/51164508/is-there-any-kind-of-line-visitor-in-eclipse-jdt-java-parser-if-there-is-not

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