xephonhq / tsdb-ql

(Deprecated) A Time Series Database Query Language, see https://github.com/libtsdb and https://github.com/at15/reika
MIT License
5 stars 0 forks source link

ANTLR Golang visitor got SIGSEGV #3

Closed at15 closed 7 years ago

at15 commented 7 years ago

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

Conclusion

at15 commented 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

at15 commented 7 years ago

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:

at15 commented 7 years ago

So the problem is in the example, which use embedding by pointer

type 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)
}