Closed at15 closed 7 years ago
After overriding the Visit method in AstBuilder
I no longer have the nil error
func (b *AstBuilder) Visit(tree antlr.ParseTree) interface{} {
return nil
}
so I guess BasewhileVisitor
does not have Visit method? but why no compile error?
May need to list all the methods of a struct http://stackoverflow.com/questions/21397653/how-to-dump-methods-of-structs-in-golang
And using BasewhileVisitor also works, I think the problem is from embedding by pointer http://www.hydrogen18.com/blog/golang-embedding.html
Yes, it is https://groups.google.com/forum/#!topic/golang-nuts/ctRUq62cgME
One thing to keep in mind when you embed a pointer is that, well, you're embedding a pointer. If you get a new(CardboardBox), then RectPrism fields are not allocated yet - the pointer to the RectPrism fields is zeroed.
And I don't think it's the case for visitor?
As to why one should use a pointer, one example is when one wants to embed a struct containing unexported fields from another package. Since the struct has unexported fields and is from a different package, the -only- way to embed it is as a pointer. I proposed this in another thread as a solution to provide a logger that closes its output stream. Here's the reply:
So the problem is in the example, which use embedding by pointer
nil
problem when use it directly without explicit initialize the BaseTreeVisitor inside ittype PrinterwhileListener struct {
*BasewhileListener
}
type BasewhileListener struct{}
type AstBuilder struct {
*BasewhileVisitor
}
// tree.go
type BasewhileVisitor struct {
*antlr.BaseParseTreeVisitor
}
// FIXME: this will cause SIGSEGV when using visit
func NewAstBuilder() *AstBuilder {
return new(AstBuilder)
}
Related https://github.com/pboyer/antlr4/issues/93
Met this problem in 08e6f687e181258de9501b782a43d3a9e244f69d
I think the problem might be the golang runtime, but it could also be mine, there is no doc for visitor pattern
BaseParseTreeVisitor
https://github.com/antlr/antlr4/blob/master/runtime/Go/antlr/tree.go#L67 does nothing but return nilParseTreeVisitor
got aTODO
and commented out code https://github.com/antlr/antlr4/blob/master/runtime/Go/antlr/tree.go#L73Conclusion