0

I don't want antlr's generated classes and methods exposed to the public API.

There's a 6-year old answer Antlr generated classes access modifier to internal. But I hope there's a more modern and true way to do it.

WhiteBlackGoose
  • 110
  • 2
  • 8
  • If your goal is to hide the generated classes from others in a commercial project, obfuscation is the only "modern and true" approach. Flipping public/internal is never the way. – Lex Li Aug 31 '20 at 20:00
  • No, not commerical. Just for general conveniance & for public api analyzers – WhiteBlackGoose Sep 01 '20 at 08:19
  • Seems like https://github.com/antlr/antlrcs/blob/master/doc/README.md#extra-features-in-the-c-targets has the answer, but it doesn't work on antlr4 – WhiteBlackGoose Sep 01 '20 at 11:19

1 Answers1

0

AFAIK, you can't. You could use the superClass option in the grammar so that you extend a custom parser class where you define only those methods you want to be public:

grammar T;

options {
  superClass=MyParser;
}

parse
 : id+ EOF
 ;

id
 : ID
 ;

ID
 : [a-zA-Z] [a-zA-Z0-9_]*
 ;

and your MyParser class could look like this:

public abstract class MyParser extends Parser {

    public MyParser(TokenStream input) {
        super(input);
    }

    abstract TParser.ParseContext parse() throws RecognitionException;
}

and then instantiate your parser like this:

TLexer lexer = new TLexer(CharStreams.fromString("foo"));
MyParser parser = new TParser(new CommonTokenStream(lexer));
parser.parse(); // OK
parser.id();    // No can do

but I need to actually hide the whole class. This class should be internal, it's being called in inner methods in the lib

That is not something you can do AFAIK. You'll have to add that yourself with some post-build script (simple search/replace), I think.

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • Aha thanks, but I need to actually hide the whole class. This class should be internal, it's being called in inner methods in the lib – WhiteBlackGoose Sep 01 '20 at 08:20