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: lexer constants with the private access if g4 filename starts with the lowercase #4665

Open
n0rdy opened this issue Jul 29, 2024 · 0 comments

Comments

@n0rdy
Copy link

n0rdy commented Jul 29, 2024

Hello there! First of all, thanks for the great tool, it's quite cool and fun to use.

Lately, I've played with the ANTLR a bit while using Go as a target, and came across an interesting behavior: if the .g4 file name starts with the lowercase (as well as its grammar property value), the lexer tokens will have names that start with the lowercase letter as well (due to the filename mentioned above being used as their prefix).
While in other programming languages (like Java) this doesn't mean anything besides the code style, in Go lowercase name makes the variable private - similar to Java's private String someCoolName behavior.
This way, the tokens of the lexer become unaccessible from the outside of the lexer, which sounds wrong as the GetTokenType() function is exposed via the API of many ANTLR objects.

Changing .g4 and its grammar property name to start with the uppercase letter fixes the issue.

Steps to reproduce:

  • get a g4 file that starts with the lowercase - for example, calculator.g4 from the grammars-v4 repo
  • navigate to the folder with this file
  • run antlr -Dlanguage=Go -o parser calculator.g4
  • check the generated parser/calculator_lexer.go file in the section with constants - I got this result:
// calculatorLexer tokens.
const (
	calculatorLexerCOS               = 1
	calculatorLexerSIN                 = 2
	calculatorLexerTAN                = 3
.....
  • (optional) try accessing these constants from any other Go file to see the compilation error due to private access.

Environment:

  • MacOS 14.5
  • OpenJDK version 21
  • Go version 1.22
  • ANTLR version 4.13.1 installed from Homebrew

Please, let me know whether I should provide any more details to clarify the behavior I've noticed.

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

1 participant