antlr / antlr4

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.
http://antlr.org
BSD 3-Clause "New" or "Revised" License
17.04k stars 3.27k forks source link

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

Open RobEin opened 4 months ago

RobEin commented 4 months ago

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 commented 4 months ago

I submitted a PR to fix it.

jimidle commented 4 months ago

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.