antlr4 icon indicating copy to clipboard operation
antlr4 copied to clipboard

Go target: missing constructor call to the superclass of the generated parser or lexer

Open RobEin opened this issue 1 year ago • 2 comments

There is currently no constructor support for superclasses from the generated lexer/parser classes. Without this, the initial values of a superclass instance can only be set in individually way. Here is a short example for a lexer implementation:

my PythonLexer.g4:

lexer grammar PythonLexer;
options { superClass=PythonLexerBase; }
...

my PythonLexerBase superclass:

...

type PythonLexerBase struct {
    *antlr.BaseLexer
    ...
}

// go constructor for the superclass
// unfortunately this function is not called from the generated lexer
func NewPythonLexerBase(input antlr.CharStream) *PythonLexerBase {
    plb := new(PythonLexerBase)
    plb.BaseLexer = antlr.NewBaseLexer(input)
    ...
    return plb
}

...

current generated lexer:

...

type PythonLexer struct {
    PythonLexerBase
    ...
}

...

func NewPythonLexer(input antlr.CharStream) *PythonLexer {
    ...
    l := new(PythonLexer)
    l.BaseLexer = antlr.NewBaseLexer(input) // **** there is no constructor call for the superclass
    ...
    return l
}

...

suggested generated lexer:

...

type PythonLexer struct {
    *PythonLexerBase // **** by pointer
    ...
}

...

func NewPythonLexer(input antlr.CharStream) *PythonLexer {
    ...
    l := new(PythonLexer)
    l.PythonLexerBase = NewPythonLexerBase(input) // **** constructor call for the superclass
    ...
    return l
}

...

I think this suggestion follows the practices of the Go language and those common in other ANTLR target languages. The generated parser class should be created in the same way.

similar issue: #3446

RobEin avatar May 22 '24 18:05 RobEin

I submitted a PR to fix it.

RobEin avatar May 23 '24 18:05 RobEin

Thanks for looking at a few issues Robert. I have some big changes coming through this weekend, including generic walkers, after which cI will look at your PRs. I may already have addressed some of them I think

On May 23, 2024, at 12:25, Robert Einhorn @.***> wrote:

To fix it, I submitted a PR https://github.com/antlr/antlr4/commit/b91a6aac16d19d01e168a8c30da904262181d546 regarding this.

— Reply to this email directly, view it on GitHub https://github.com/antlr/antlr4/issues/4625#issuecomment-2127786463, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJ7TMALWA7BXNUSHATOHBTZDYYBHAVCNFSM6AAAAABIEHAXOCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMRXG44DMNBWGM. You are receiving this because you are subscribed to this thread.

jimidle avatar May 24 '24 21:05 jimidle