// Copyright 2017 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

[[antlr_plugin]]
= The ANTLR Plugin

The ANTLR plugin extends the Java plugin to add support for generating parsers using http://www.antlr.org/[ANTLR].

NOTE: The ANTLR plugin supports ANTLR version 2, 3 and 4.

[[sec:antlr_usage]]
== Usage

To use the ANTLR plugin, include the following in your build script:

.Using the ANTLR plugin
====
include::sample[dir="antlr/groovy",files="build.gradle[tags=use-plugin]"]
include::sample[dir="antlr/kotlin",files="build.gradle.kts[tags=use-plugin]"]
====

[[sec:antlr_tasks]]
== Tasks

The ANTLR plugin adds a number of tasks to your project, as shown below.

`generateGrammarSource` — link:{groovyDslPath}/org.gradle.api.plugins.antlr.AntlrTask.html[AntlrTask]::
Generates the source files for all production ANTLR grammars.

`generateTestGrammarSource` — link:{groovyDslPath}/org.gradle.api.plugins.antlr.AntlrTask.html[AntlrTask]::
Generates the source files for all test ANTLR grammars.

`generate__SourceSet__GrammarSource` — link:{groovyDslPath}/org.gradle.api.plugins.antlr.AntlrTask.html[AntlrTask]::
Generates the source files for all ANTLR grammars for the given source set.

The ANTLR plugin adds the following dependencies to tasks added by the Java plugin.

.ANTLR plugin - additional task dependencies
[cols="a,a", options="header"]
|===
| Task name
| Depends on
| `compileJava`
| `generateGrammarSource`

| `compileTestJava`
| `generateTestGrammarSource`

| `compile__SourceSet__Java`
| `generate__SourceSet__GrammarSource`
|===


[[sec:antlr_project_layout]]
== Project layout

`src/main/antlr`::
Production ANTLR grammar files. If the ANTLR grammar is organized in packages, the structure in the antlr folder should reflect the package structure. This ensures that the generated sources end up in the correct target subfolder.

`src/test/antlr`::
Test ANTLR grammar files.

`src/__sourceSet__/antlr`::
ANTLR grammar files for the given source set.


[[sec:antlr_dependency_management]]
== Dependency management

The ANTLR plugin adds an `antlr` dependency configuration which provides the ANTLR implementation to use. The following example shows how to use ANTLR version 3.

.Declare ANTLR version
====
include::sample[dir="antlr/groovy",files="build.gradle[tags=declare-dependency]"]
include::sample[dir="antlr/kotlin",files="build.gradle.kts[tags=declare-dependency]"]
====

If no dependency is declared, `antlr:antlr:2.7.7` will be used as the default. To use a different ANTLR version add the appropriate dependency to the `antlr` dependency configuration as above.

[[sec:antlr_convention_properties]]
== Convention properties

The ANTLR plugin does not add any convention properties.

[[sec:antlr_source_set_properties]]
== Source set properties

The ANTLR plugin adds the following properties to each source set in the project.

`antlr` — link:{groovyDslPath}/org.gradle.api.file.SourceDirectorySet.html[SourceDirectorySet]::
The ANTLR grammar files of this source set. Contains all `.g` or `.g4` files found in the ANTLR source directories, and excludes all other types of files. _Default value is non-null._

`antlr.srcDirs` — `Set&lt;File&gt;`::
The source directories containing the ANTLR grammar files of this source set.
Can set using anything <<working_with_files.adoc#sec:specifying_multiple_files, that implicitly converts to a file collection>>.
Default value is `[__projectDir__/src/__name__/antlr]`.

[[sec:controlling_the_antlr_generator_process]]
== Controlling the ANTLR generator process

The ANTLR tool is executed in a forked process. This allows fine grained control over memory settings for the ANTLR process. To set the heap size of an ANTLR process, the `maxHeapSize` property of link:{groovyDslPath}/org.gradle.api.plugins.antlr.AntlrTask.html[AntlrTask] can be used. To pass additional command-line arguments, append to the `arguments` property of link:{groovyDslPath}/org.gradle.api.plugins.antlr.AntlrTask.html[AntlrTask].

.Setting custom max heap size and extra arguments for ANTLR
====
include::sample[dir="antlr/groovy",files="build.gradle[tags=generate-grammar-settings]"]
include::sample[dir="antlr/kotlin",files="build.gradle.kts[tags=generate-grammar-settings]"]
====

