Antlr 生成的类访问修饰符到内部 [英] Antlr generated classes access modifier to internal

查看:19
本文介绍了Antlr 生成的类访问修饰符到内部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个包含某些解析器的库.这些解析器是用 ANTLR4 内部构建的.由于生成的类都是公开的,我库的用户可以看到他们不需要看到的所有类.Sandcastle 文档也包含所有这些类.有什么办法可以告诉 Antlr 将生成的类设为内部而不是公开吗?

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?

推荐答案

实际上,这相对容易做到.在内部,ANTLR 使用 StringTemplate 文件为每种支持的语言生成代码.对于 C#,您可以在 ANTLR 的 JAR 文件中找到模板:

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

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

只需复制此文件,并根据需要进行修改.例如,我通常会删除 CLSCompliant(false) 属性以摆脱编译器的警告,并使所有类和接口成为内部.

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.

然后,您需要告诉 ANTLR 在代码生成期间使用修改后的模板.为了做到这一点,你需要把它放在 CLASSPATH before ANTLR 的 JAR 中,并确保你保持原来的文件夹结构,以便你指向一个文件夹org 目录所在的位置,而不是 CSharp.stg 本身.

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:

在这种情况下,Generate.bat 应该如下所示(假设 java.exe 在您的 PATH 中):

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

快乐编码!

这篇关于Antlr 生成的类访问修饰符到内部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆