Skip to content

Viewing a live preview of parse tree

bclothier edited this page Sep 9, 2017 · 4 revisions

Understanding the shape of the parse tree can be helpful in contributing to the rubberduck, especially when creating inspections, quickfixes or implementing a new command that deals with reading or modifying the VBA source code. Unfortunately there is no simple application nor a Visual Studio plugin that will allow you to view the tree live. It is possible to view the parse via the Locals windows but going through the nodes can be tedious and does not give you a high-level picture.

For that, we need to install Eclipse plus a plugin. Once set up, it becomes possible to plop in a VBA snippet and see what kind of parse tree you will get.

Installing

You can get Eclipse here Note there are several flavors of Eclipses to pick from. You only need the Eclipse IDE for Java Developers (note: not to be confused with Eclipse IDE for Java EE Developers which is considerably bloated).

Once downloaded & installed, you should then enable ANTLR 4 IDE, which is a plugin available via Eclipse Marketplace, located in the Help menu. They have a good step by step instructions which tells you what dependencies you must satisfy, provided here. Note that it's OK if your versions is higher than those listed; it's more important that you have all dependencies added to your Eclipse.

Also, the instruction will ask you to obtain a runtime version of ANTLR. RubberDuck currently uses ANTLR 4.3 but the current version is 4.7. Therefore you should actually get runtime 4.3 to stay compatible with what Rubberduck uses and can be downloaded from here. (Note: the link points to a .jar file which can trigger alerts about unsafe file)

Once you've completed following the instructions for setting up the ANTLR project, you need to obtain the .g4 files used by Rubberduck, which are VBALexer.g4 and VBAParser.g4. Copy them into the project; one way to do this is to right-click the Project folder and choosing Import. On the dialog, choose Filesystem, then browse to those files, ensure that you choose to copy, rather than linking.

Using the live parse tree view

NOTE: the instructions I had seen did not match presumably because I had downloaded and used Eclipse Oxygen. If you don't find it, you may need to look up the equivalent action in your Eclipse versions.

The ANTLR 4 IDE provides 2 views which you can enable by going to Windows -> Views -> Others. It will open a dialog with folders; locate the folder named ANTLR 4 and click on the Parse Tree. If you want, you can repeat the steps to also view the Syntax diagram.

Open the VBAParser.g4 then put your cursor on the line 82, within the word moduleBody. This is important because the Parse Tree expects input that would match the given selection, and moduleBody is the easiest way to write out procedures copied from a VBA module.

On the Parse Tree view, you should note the caption saying VBAParser::moduleBody and the textbox will be enabled. You can then input the VBA code and thus get a graphical view of the parse tree.

Including options and declarations

Note that if you want to include the complete VBA module, including the declarations, you cannot simply copy the entire VBA module because the grammar expect attributes to be present and you can't see attributes from the VBA editor. In that case, you must export the module out of VBE first to expose the mandatory attributes and open it in a text editor so that you can copy and paste to the input. You should also change the selection in your VBAParser.g4 to module on line 26, with Parse Tree correspondingly showing VBAParser::module in order to parse the complete module correctly.

Clone this wiki locally