antlr4
antlr4 copied to clipboard
Go visitor pattern
Having a Go at enabling the visitor pattern.
New output does not require you to write the Visit() function, and the Visit() function does not consist of a switch statement. Instead, the type system is used to pick which function to distribute the visitor to.
Here is an example project showing how to use the output:
https://github.com/sheik/antlr4-go-visitor-test
This is an example grammar from the definitive ANTLR4 reference.
The main files of interest are main.go and parser/expr_visitor.go.
You can see the implementation of Visit functions in parser/expr_base_visitor.go.
@jcking do you feel like taking a look at this PR?
Hmmm.
So "generics" are landing in Go in the next couple of months I believe. I would hate to use interface{} and then have some nice generic approach available and force migration. We should double check whether generics will be usable in this situation.
Yeah, let's wait.
I started a comment on this and just made it an issue. Since it seems thats where this discussion is heading. https://github.com/antlr/antlr4/issues/3457