Antlr generated classes access modifier to internal
Asked Answered
I

5

7

I am building a library which contains certain parsers. These parsers are internally built with ANTLR4. Since the generated classes are all public, users of my library are able to see all the classes they do not need to see. Also the Sandcastle documentation contains all these classes. Is there any way I can tell Antlr to make the generated classes internal instead of public?

Immoralist answered 22/4, 2014 at 11:19 Comment(0)
F
2

Actually, it's relatively easy to do. Internally, ANTLR uses StringTemplate files to generate code for each of the supported languages. For C#, you can find the template here in the ANTLR's JAR file:

org\antlr\v4\tool\templates\codegen\CSharp\CSharp.stg

Just make a copy of this file, and modify it as needed. For example, I usually remove CLSCompliant(false) attributes to get rid of the compiler's warnings, and make all the classes and interfaces internal.

Then, you need to tell the ANTLR to use the modified template during code generation. In order to do this, you need to put it in the CLASSPATH before ANTLR's JAR, and make sure that you keep the original folder structure, so that you point to a folder where the org directory is located, not to the CSharp.stg itself.

Here is an example of the folder structure that you can use:

enter image description here

In this case, Generate.bat should look something as follows (assuming that java.exe is in your PATH):

pushd %~dp0
set CLASSPATH=.;antlr-4.7-complete.jar
java org.antlr.v4.Tool -Dlanguage=CSharp Grammar.g4

Happy coding!

Fauces answered 16/10, 2017 at 17:9 Comment(0)
H
1

We have not implemented public/private on the generated classes yet I don't think.

Hershel answered 22/4, 2014 at 14:26 Comment(1)
Actually, you did :-) Thank you again for making an amazing tool.Raycher
P
0

You could modify the string templates.

You might start here.

I have never done this myself to be honest and you might have to repeat the step once a new release is published (although the merge will help you, you will still have to check if there are new "public"s).

Plaything answered 23/4, 2014 at 11:45 Comment(0)
M
0

One quite effective approach is to implement a facade over the Antlr generated code while at the same time obfuscating the generated code. This gives you clear, independent control over what your users can see and use. The ProGuard obfuscator well handles this type of use.

Mikesell answered 24/4, 2014 at 3:44 Comment(0)
R
0

C# target supports '@modifier'. For example, to make generated classes internal, you write

@modifier{internal}

If your parser source is split across multiple source files (e.g. lexer, parser, and tree) then each file is allowed to have its own modifier.

Raycher answered 16/4, 2020 at 15:27 Comment(2)
In which version is this option available? Do you have a link to a documentation that mentions it?Finial
Ah, found it: github.com/antlr/antlrcs/tree/master/…. It's for ANTLR 3.Finial

© 2022 - 2024 — McMap. All rights reserved.