antlr4 icon indicating copy to clipboard operation
antlr4 copied to clipboard

Go visitor pattern

Open sheik opened this issue 4 years ago • 5 comments

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.

sheik avatar Oct 10 '21 01:10 sheik

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.

sheik avatar Oct 10 '21 12:10 sheik

@jcking do you feel like taking a look at this PR?

parrt avatar Dec 26 '21 23:12 parrt

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.

jcking avatar Jan 02 '22 20:01 jcking

Yeah, let's wait.

parrt avatar Jan 02 '22 22:01 parrt

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

dragonfax avatar Jan 03 '22 04:01 dragonfax