rubberduck-vba / Rubberduck3

COM add-in for the VBIDE
GNU General Public License v3.0
89 stars 17 forks source link

Parse a module #21

Open retailcoder opened 1 year ago

retailcoder commented 1 year ago

Wire up and adjust all the necessary RD2 components to produce an ANTLR parse tree for any module loaded in the workspace.

At this stage we only care about getting a parse tree to provide the editor with syntax errors and folding ranges: we need this parse to remain sub-second and as reliably fast as possible for all module under 5K lines of code (half the maximum allowed). Preliminary in-process tests gave excellent sub-second results for all module sizes.

Technically we could also wire up the parse tree inspections/diagnostics, but for performance reasons these will have to run separately from this quick-parse syntactical pass.

Vogel612 commented 1 year ago

Note that this implies we need to actually do syntax error recovery. There are two possibilities for that: Either we introduce a SyntaxError rule that matches as few tokens as possible at the last possible alternative in every relevant rule (to scope the errors as small as possible), or we teach the generated ANTLR parser how to recover from the errors.

The first one has the advantage of being somewhat simple in theory and easy to deal with for pretty-printing and resolver passes, the second one is more "spiritually correct" and less likely to accidentally parse syntax errors where there are none (because backtracking with syntax error parser rules is wicked complicated).

retailcoder commented 1 year ago

Oh that part was done (started, at least - still need custom syntax error messages for a great UX) during the PoC ☺️ Essentially, Antlr recovers all by itself; in RD2 we just prevent it by throwing as soon as we encounter an error, but if we instead let Antlr do its thing, it's actually super clever and works exactly like we need - coming up with meaningful and appropriate error messages is the hard part.