// 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:

++++
<sample id="useAntlrPlugin" dir="antlr" title="Using the ANTLR plugin">
            <sourcefile file="build.gradle" snippet="use-plugin"/>
        </sample>
++++


[[sec:antlr_tasks]]
=== Tasks

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

.ANTLR plugin - tasks
[cols="a,a,a,a", options="header"]
|===
| Task name
| Depends on
| Type
| Description

| `generateGrammarSource`
| -
| api:org.gradle.api.plugins.antlr.AntlrTask[]
| Generates the source files for all production ANTLR grammars.

| `generateTestGrammarSource`
| -
| api:org.gradle.api.plugins.antlr.AntlrTask[]
| Generates the source files for all test ANTLR grammars.

| `generate__SourceSet__GrammarSource`
| -
| api:org.gradle.api.plugins.antlr.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


.ANTLR plugin - project layout
[cols="a,a", options="header"]
|===
| Directory
| Meaning

| `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.

++++
<sample id="declareAntlrVersion" dir="antlr" title="Declare ANTLR version">
            <sourcefile file="build.gradle" snippet="declare-dependency"/>
        </sample>
++++

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 plugin - source set properties
[cols="a,a,a,a", options="header"]
|===
| Property name
| Type
| Default value
| Description

| `antlr`
| api:org.gradle.api.file.SourceDirectorySet[] (read-only)
| Not null
| 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.

| `antlr.srcDirs`
| `Set&lt;File&gt;`. Can set using anything described in <<sec:specifying_multiple_files>>.
| `[__projectDir__/src/__name__/antlr]`
| The source directories containing the ANTLR grammar files of this source set.
|===


[[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 api:org.gradle.api.plugins.antlr.AntlrTask[] can be used. To pass additional command-line arguments, append to the `arguments` property of api:org.gradle.api.plugins.antlr.AntlrTask[].

++++
<sample id="advanced" dir="antlr" title="setting custom max heap size and extra arguments for ANTLR">
            <sourcefile file="build.gradle" snippet="generate-grammar-settings"/>
        </sample>
++++
