Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
RobEin opened this issue May 22, 2024 · 2 comments

Comments

@RobEin
Copy link
Contributor

RobEin commented May 22, 2024

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 RobEin changed the title Missing constructor call to the superclass of the generated parser or lexer Go target: missing constructor call to the superclass of the generated parser or lexer May 23, 2024
RobEin added a commit to RobEin/antlr4 that referenced this issue May 23, 2024
RobEin added a commit to RobEin/antlr4 that referenced this issue May 23, 2024
related issue: antlr#4625

Signed-off-by: Robert Einhorn <robert.einhorn.hu@gmail.com>
RobEin added a commit to RobEin/antlr4 that referenced this issue May 23, 2024
related issue: antlr#4625

Signed-off-by: Robert Einhorn <robert.einhorn.hu@gmail.com>
@RobEin
Copy link
Contributor Author

RobEin commented May 23, 2024

I submitted a PR to fix it.

RobEin added a commit to RobEin/antlr4 that referenced this issue May 24, 2024
related issue: antlr#4625

Signed-off-by: Robert Einhorn <robert.einhorn.hu@gmail.com>
@jimidle
Copy link
Collaborator

jimidle commented May 24, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants