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, cannot use superClass for the lexer grammar! #3254

Closed
kaby76 opened this issue Aug 10, 2021 · 1 comment · Fixed by #3284
Closed

Go target, cannot use superClass for the lexer grammar! #3254

kaby76 opened this issue Aug 10, 2021 · 1 comment · Fixed by #3284

Comments

@kaby76
Copy link
Contributor

kaby76 commented Aug 10, 2021

I'm trying to use the superClass option in a lexer grammar. It works fine for a parser grammar, but for a lexer grammar, it fails at runtime with a null pointer dereference in the generated constructor NewFOOBARLexer().

For example,

type CSharpLexer struct {
	*CSharpLexerBase
	channelNames []string
	modeNames    []string
	// TODO: EOF string
}

func NewCSharpLexer(input antlr.CharStream) *CSharpLexer {
	l := new(CSharpLexer)
	...
	l.BaseLexer = antlr.NewBaseLexer(input)  <<<<<< CRASHES HERE WITH NULL PTR DEREF.
	...
	return l
}

It crashes on l.BaseLexer = antlr.NewBaseLexer(input), specifically on the assignment to l.BaseLexer, and not on the call to the constructor antlr.NewBaseLexer(input).

Contrast this with the parser code:

type CSharpParser struct {
	CSharpParserBase
}

func NewCSharpParser(input antlr.TokenStream) *CSharpParser {
	this := new(CSharpParser)
	...
	this.BaseParser = antlr.NewBaseParser(input)
	...
	return this
}

This code works for the parser. The assignment to this.BaseParser works fine as it's not a null pointer.

The constructor code for the lexer fails because the type CSharpLexer struct is wrong. It defines the struct to contain*CSharpLexerBase, whereas in the parser type CSharpParser struct, the struct contains CSharpParserBase not a pointer. Changing the type struct declation for the generated lexer to CSharpLexerBase instead of *CSharpLexerBase fixes the crash, and my parser/lexer work just fine.

The problem is here-- the line erroneously contains *, whereas for the parser here, it is declared directly.

@kaby76
Copy link
Contributor Author

kaby76 commented Aug 10, 2021

The '*' is in the wrong place. See how the parser struct is declared. This change fixes the problem.

diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Go/Go.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Go/Go.stg
index 984a267dc..e2ebed5dd 100644
--- a/tool/resources/org/antlr/v4/tool/templates/codegen/Go/Go.stg
+++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Go/Go.stg
@@ -1423,7 +1423,7 @@ var lexerRuleNames []string
 <endif>

 type <lexer.name> struct {
-       *<if(superClass)><superClass><else>antlr.BaseLexer<endif>
+       <if(superClass)><superClass><else>*antlr.BaseLexer<endif>
	channelNames []string
	modeNames []string
	// TODO: EOF string

SKalt added a commit to SKalt/antlr4 that referenced this issue Sep 24, 2021
Fixes antlr#3254; this is the exact fix recommended by @kaby76.
SKalt added a commit to SKalt/antlr4 that referenced this issue Sep 24, 2021
Fixes antlr#3254; this is the exact fix recommended by @kaby76.
SKalt added a commit to SKalt/antlr4 that referenced this issue Sep 30, 2021
Fixes antlr#3254; this is the exact fix recommended by @kaby76.
@parrt parrt added this to the 4.9.3 milestone Oct 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants