第四十章. ANTLR 插件

Chapter 40. The ANTLR Plugin

ANTLR 插件扩展自 Java 插件并添加了使用 ANTLR 生成语法分析器的支持。
The ANTLR plugin extends the Java plugin to add support for generating parsers using ANTLR.

ANTLR 插件只支持 ANTLR 版本 2。
The ANTLR plugin only supports ANTLR version 2.

40.1. 用法

40.1. Usage

要使用 ANTLR 插件,请在构建脚本中包含以下语句:
To use the ANTLR plugin, include in your build script:

示例 40.1. 使用 ANTLR 插件 - Example 40.1. Using the ANTLR plugin

build.gradle

apply plugin: 'antlr'

40.2. 任务

40.2. Tasks

ANTLR 插件向你的项目添加了大量的任务,如下所示。
The ANTLR plugin adds a number of tasks to your project, as shown below.

表 40.1. ANTLR 插件 - 任务 - Table 40.1. ANTLR plugin - tasks

任务名称
Task name
依赖于
Depends on
类型
Type
描述
Description
generateGrammarSource - AntlrTask 为所有生产 ANTLR 语法生成源文件。
Generates the source files for all production ANTLR grammars.
generateTestGrammarSource - AntlrTask 为所有测试 ANTLR 语法生成源文件。
Generates the source files for all test ANTLR grammars.
generateSourceSetGrammarSource - AntlrTask 为给定的 source set 的所有 ANTLR 语法生成源文件。
Generates the source files for all ANTLR grammars for the given source set.

ANTLR 插件向 Java 插件所加入的 tasks 添加了以下的依赖。
The ANTLR plugin adds the following dependencies to tasks added by the Java plugin.

表 40.2. ANTLR 插件 - 附加的任务依赖 - Table 40.2. ANTLR plugin - additional task dependencies

任务名称
Task name
依赖于
Depends on
compileJava generateGrammarSource
compileTestJava generateTestGrammarSource
compileSourceSetJava generateSourceSetGrammarSource

40.3. 项目布局

40.3. Project layout

表 40.3. ANTLR 插件 - 项目布局 - Table 40.3. ANTLR plugin - project layout

目录
Directory
意义
Meaning
src/main/antlr 生产 ANTLR 语法文件。
Production ANTLR grammar files.
src/test/antlr 测试 ANTLR 语法文件。
Test ANTLR grammar files.
src/sourceSet/antlr 对于给定的source set 的 ANTLR 语法文件。
ANTLR grammar files for the given source set.

40.4. 依赖管理

40.4. Dependency management

ANTLR 插件添加了一个 antlr 的依赖配置:你可以使用它来声明你想要使用的 ANTLR 依赖。
The ANTLR plugin adds an antlr dependency configuration. You use this to declare the ANTLR dependency that you wish to use.

示例 40.2. 声明 ANTLR 版本 - Example 40.2. Declare ANTLR version

build.gradle

repositories {
    mavenCentral()
}

dependencies {
    antlr 'antlr:antlr:2.7.7'
}

40.5. 约定属性

40.5. Convention properties

ANTLR 插件不会添加任何约定属性。
The ANTLR plugin does not add any convention properties.

40.6. Source set 属性

40.6. Source set properties

ANTLR 插件向 project 的每一个source set 添加了下列属性。
The ANTLR plugin adds the following properties to each source set in the project.

表 40.4. ANTLR 插件 - source set 属性 - Table 40.4. ANTLR plugin - source set properties

属性名称
Property name
类型
Type
默认值
Default value
描述
Description
antlr SourceDirectorySet (read-only) 非空
Not null
给定的source set 的 ANTLR 语法文件。包含在 ANTLR 源目录中找到的所有的.g,并排除所有其他类型的文件。
The ANTLR grammar files of this source set. Contains all .g found in the ANTLR source directories, and excludes all other types of files.
antlr.srcDirs Set<File>. 可以使用 16.5 章节,"指定一组输入文件"中所讲到的任何一个来设置。
Set<File>. Can set using anything described in Section 16.5, “Specifying a set of input files”.
[projectDir/src/name/antlr] 该源目录包含该 source set 中的 ANTLR 语法文件。
The source directories containing the ANTLR grammar files of this source set.