Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rubberduck and Transpiling? #5496

Open
sancarn opened this issue May 10, 2020 · 2 comments
Open

Rubberduck and Transpiling? #5496

sancarn opened this issue May 10, 2020 · 2 comments
Labels
support Whether you're using Rubberduck or you've forked it and have questions, never hesitate to ask!

Comments

@sancarn
Copy link

sancarn commented May 10, 2020

Hi All, it's me again.

Over the past few days I started the VBAPackager project which intends to provide means to embed VBA source code into an xlsm/docm file without the need for the VBE. It intends to be a stepping stone for moving VBA out of the VBE and into external IDEs like VSCode. This ontop of #5176 could pave the way for VBA as a language of the future. (P.S. The project is a long way off being in a working state)

But this got me thinking, what features am I actually missing in VBA? Lambda expressions, stack traces, line numbers (for errors), reflection, multiline strings to name a few.

Historically I've tried adding these in the "brute force way" by hooking into vb runtime, or by making BNF/CFG classes to parse lambda("$1+$2") (also not completed this 😛). However lambdas really could be a result of transpilation:

Sub test()
   Debug.print lambda("(a1,a2)->a1+a2")(1,2)
End Sub

-->

Sub test()
   Debug.print stdCallback.Create("module","globals","a000001")(1,2)
End Sub
Function a000001(ByVal a1,ByVal a2)
  a000001 = a1+a2
End Function

This would be significantly more optimal than parsing and evaluating at runtime! P.S. stdCallback is something I have completed

Similarly with line numbers and stack tracing:

Sub Main()
   Call DoSomething()
End Sub
Sub DoSomething()
   Dim a1 as integer: a1=1
   Dim a2 as integer: a2=2
   Debug.Print a1+a2
End Sub

--> Okay it looks ugly but:

Sub Main()
stdError.addStack("Main")
01: Call DoSomething()
stdError.popStack
End Sub
Sub DoSomething()
stdError.addStack("DoSomething")
01: Dim a1 as integer: a1=1
02: Dim a2 as integer: a2=2
03: Debug.Print a1+a2
stdError.popStack
End Sub

This is where rubberduck comes in, I'm hoping.

How easy would it be to take the existing RubberDuck Parser and add language features like the above by pushing and popping nodes into/out of the AST and transpile back down to VBA afterwards?

In theory something like lambda(head(a1,a2)=a1+a2) is already valid syntax, so I imagine this would be relatively easy? Adding new syntax would require modification of the grammar, which would likely be less viable in a dynamic way I assume?

I understand this isn't really in the scope of the Rubberduck IDE, at least for now unless VBE is deprecated, but this could do to VBA what babel has done to JavaScript...

In general it would be cool to get a setup like the following going:

Source --> AST --> plugins --> AST --> Source

Main question is how easy would it be to achieve? And is Rubberduck.Parsing simply drag and drop? Or does it require the VBE? What classes/methods do I need/need to watch out for?

@sancarn sancarn added the support Whether you're using Rubberduck or you've forked it and have questions, never hesitate to ask! label May 10, 2020
@Vogel612
Copy link
Member

We've tried to confine the part of Rubberduck that relies on the VBE into the Rubberduck.VBEEditor, Rubberduck.VBEditor.VB6 and Rubberduck.VBEditor.VBA projects.

The problem is that Rubberduck.Parsing directly depends on that VBEEditor project to obtain the source for parsing. So it's not a simple drop-in by any stretch of the imagination.

The parser itself theoretically supports parsing VBA code from raw text input and will give you an AST, but that's where it gets hairy again.
The AST node classes are generated from the grammar file at build time and if you want to modify these classes you won't get around modifying the grammar.

Theoretically it should be possible to extend the AST capabilities by writing custom classes derived from the ANTLR baseclass for contexts, but it's probably not the easiest way to go about this ...

From what I can tell you've gotten yourself in at the deep end... If you want the fully resolved ParserState that rubberduck is working off of, you'll have to deal with the ParseCoordinator, which handles the different steps of the parsing process. If you just want an AST, you could try looking into IStringParser and its implementations. With a bit of luck that allows you to not deal with the VBE at all.

As it is, the Parsing setup we have for Rubberduck is probably significantly too complex for your purposes, though...

@sancarn
Copy link
Author

sancarn commented May 13, 2020

Thanks I'll look into it at a later date, but sounds promising at least :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support Whether you're using Rubberduck or you've forked it and have questions, never hesitate to ask!
Projects
None yet
Development

No branches or pull requests

2 participants