antlr4
antlr4 copied to clipboard
Go target: lexer constants with the private access if g4 filename starts with the lowercase
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-v4repo - navigate to the folder with this file
- run
antlr -Dlanguage=Go -o parser calculator.g4 - check the generated
parser/calculator_lexer.gofile 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.