-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Support the Visitor Pattern for GoLang code generation #1807
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
base: master
Are you sure you want to change the base?
Conversation
@pboyer I wasn't sure if I should have forked your tree, or the main tree, as it's a Go-specific change. I'd love to hear your thoughts on this pull request. |
Got rid of a mix-up in rule nodes vs. visitor & context names, and use the same pattern as the Java code uses in VisitChildren, including the helper functions.
Hey @cyberfox, Thanks for the code. Couple of comments. The VisitChildren code isn't idiomatic Go, and I found it quite confusing.
Thinking about it, this still isn't idiomatic Go.
This does mean the Result would have to be the visitor implementation, but I don't thing this is an issue. Lastly, a small change to the example makes it clear how use visitor to capture precedence.
|
It turns out that the visitor only actually works if the delegate (super) is set. Go is not object-oriented in the same way as Java, and "super" classes don't delegate to "subclasses". I've pushed code to I want to look at another way of doing this before sending you a PR. |
@cyberfox take a look at https://github.com/wxio/antlr4/tree/go-visitor If there are any error its because I'm working a branch with other features and extracted the visitor changes, but might have made a mistake. This is now more Go like. Cheers |
@millergarym I'd love to take a look at your version! It would be helpful if you could show how someone would implement the visitor pattern using your code? I think I'm getting what you're trying to do, but an example (like the one I posted at the top of the pull request) would make it much easier to follow. Also, instead of commenting out lines, go ahead and delete them. It's version control, I have them still. :) For what it's worth, I specifically pointed out that it requires a little extra work (that being the I am new to Go, so I expect my code isn't 'Go like', though. |
…orrectly support them yet. antlr/antlr4#1807
is there any update on this issue? |
@@ -16,7 +16,7 @@ import ( | |||
"reflect" | |||
"strconv" | |||
|
|||
"github.com/antlr/antlr4/runtime/Go/antlr" | |||
"github.com/cyberfox/antlr4/runtime/Go/antlr" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❗️ These lines were used for testing but cannot change like this in the main repository.
Is it still unresolved? The original issue writer might not be as interested in the issue but we still a fix |
Any status update? |
I ended up having to do other things for a while, and having to put my rules engine (my itch that I'm scratching with this) on hold. I'm open to making any changes needed to help with this, I got the idea that I'm misunderstanding how this is supposed to be used. I also was definitely wrong to do this from |
I would be quite interested in this feature. Is there anyone who is currently working on it? Maybe @cyberfox? |
The GoLang ANTLR4 parser did not seem to have all the pieces necessary to implement the visitor pattern over the resulting Tree. This code attempts to resolve that, with two caveats. The first is that I am only learning Go, so it may not be standard style. The second is that it requires some extra work on the part of someone deriving a visitor.
After the visitor is generated, you can write code that looks like the following to get notified when sections of the parse tree are visited. Let's say that the grammar is named
Piano
, and you want to be notified whenSharps
are visited.The initial calling function would look something like this:
This is my first Go contribution, so I'm expecting that I'm getting some of the formatting or style wrong.
Thanks muchly for taking a look!