# Gradle Grammar-Kit Plugin
Gradle Grammar-Kit Plugin automates generating custom language lexers and parsers when using Grammar-Kit.
**Current Release**: {interpolate-variables=false}
**GitHub**: [Releases & Changelog](https://github.com/JetBrains/gradle-grammar-kit-plugin/releases), [Issue Tracker](https://github.com/JetBrains/gradle-grammar-kit-plugin/issues)
The [Gradle Grammar-Kit Plugin](https://github.com/JetBrains/gradle-grammar-kit-plugin) automates generating lexers and parsers to support building [custom language](custom_language_support.md) plugins for IntelliJ-based IDEs when using [Grammar-Kit](https://github.com/JetBrains/Grammar-Kit).
> The plugin does not support two-pass generation. Therefore, it does not support method mixins.
>
## Usage
To enable this plugin in your Gradle-based project, register the plugin in the Gradle build script's `plugins` section:
```kotlin
plugins {
id("org.jetbrains.grammarkit") version "..."
}
```
```groovy
plugins {
id "org.jetbrains.grammarkit" version "..."
}
```
Replace `...` placeholder with desired plugin version. Current release: 
> This project requires `Gradle 7.4` or newer, however it is recommended to use the latest Gradle available.
> Update it with:
> ```Bash
> ./gradlew wrapper --gradle-version=VERSION
> ```
>
> See also: [Gradle Installation](https://gradle.org/install/) guide.
>
> Please see [CONTRIBUTING](https://github.com/JetBrains/gradle-grammar-kit-plugin/blob/master/CONTRIBUTING.md) on how to submit feedback and contribute to this project.
>
> Before visiting the [Issue Tracker](https://github.com/JetBrains/gradle-grammar-kit-plugin/issues), update both plugin and Gradle to the latest versions.
>
## Configuration
### Grammar-Kit Extension
After the Gradle Grammar-Kit Plugin is applied, the `grammarKit` extension can be used to configure the plugin and common settings of the provided tasks.
**Example:**
```kotlin
grammarKit {
jflexRelease.set("1.7.0-1")
grammarKitRelease.set("2021.1.2")
intellijRelease.set("203.7717.81")
}
```
```groovy
grammarKit {
jflexRelease = "1.7.0-1"
grammarKitRelease = "2021.1.2"
intellijRelease = "203.7717.81"
}
```
#### grammarKitRelease
{#grammar-kit-extension-grammarkitrelease}
The release version of the [Grammar-Kit](https://github.com/JetBrains/Grammar-Kit) to use.
{style="narrow"}
Type
: `String`
Default value
: `2022.3.1`
#### jflexRelease
{#grammar-kit-extension-jflexrelease}
The version of the IntelliJ-patched JFlex, a [fork of JFlex](https://github.com/JetBrains/intellij-deps-jflex) lexer generator for IntelliJ Platform API.
{style="narrow"}
Type
: `String`
Default value
: `1.7.0-1`
#### intellijRelease
{#grammar-kit-extension-intellijrelease}
An optional IntelliJ version to build the classpath for [`GenerateParser`](#generateparser-task) and [`GenerateLexer`](#generatelexer-task) tasks.
If provided, [`grammarKitRelease`](#grammar-kit-extension-grammarkitrelease) and [`jflexRelease`](#grammar-kit-extension-jflexrelease) properties are ignored as both dependencies will be provided from the given IntelliJ IDEA release.
{style="narrow"}
Type
: `String`
Default value
: `null`
## Tasks
### generateLexer
{#generatelexer-task}
The `generateLexer` task generates a lexer for the given grammar.
The task is configured using common [`grammarKit`](#grammar-kit-extension) extension.
#### sourceFile
{#tasks-generatelexer-source}
The source .*flex file to generate the lexer from.
{style="narrow"}
Required
: `true`
Type
: `String`
#### targetDir
{#tasks-generatelexer-targetdir}
The path to the target directory for the generated lexer.
{style="narrow"}
Required
: `true`
Type
: `String`
#### targetClass
{#tasks-generatelexer-targetclass}
The Java file name where the generated lexer will be written.
{style="narrow"}
Required
: `true`
Type
: `String`
#### skeleton
{#tasks-generatelexer-skeleton}
An optional path to the skeleton file to use for the generated lexer.
The path will be provided as `--skel` option.
By default, it uses the [idea-flex.skeleton](https://raw.github.com/JetBrains/intellij-community/master/tools/lexer/idea-flex.skeleton) skeleton file.
{style="narrow"}
Type
: `String`
Default
: `null`
#### purgeOldFiles
{#tasks-generatelexer-purgeoldfiles}
Purge old files from the target directory before generating the lexer.
{style="narrow"}
Type
: `Boolean`
Default
: `false`
### generateParser
{#generateparser-task}
The `generateParser` task generates a parser for the given grammar.
The task is configured using common [`grammarKit`](#grammar-kit-extension) extension.
#### sourceFile
{#tasks-generateparser-source}
The source .bnf file to generate the parser from.
{style="narrow"}
Required
: `true`
Type
: `String`
#### targetRoot
{#tasks-generateparser-targetroot}
The path to the target directory for the generated parser.
{style="narrow"}
Type
: `String`
Default
: `null`
#### pathToParser
{#tasks-generateparser-pathtoparser}
The location of the generated parser class, relative to the [`targetRoot`](#tasks-generateparser-targetroot).
{style="narrow"}
Required
: `true`
Type
: `String`
#### pathToPsiRoot
{#tasks-generateparser-pathtopsiroot}
The location of the generated PSI files, relative to the [`targetRoot`](#tasks-generateparser-targetroot).
{style="narrow"}
Required
: `true`
Type
: `String`
#### purgeOldFiles
{#tasks-generateparser-purgeoldfiles}
Purge old files from the target directory before generating the parser.
{style="narrow"}
Type
: `Boolean`
Default
: `false`
## Useful Resources
* [Grammar-Kit](https://github.com/JetBrains/Grammar-Kit)
* [IntelliJ-patched JFlex Sources](https://github.com/JetBrains/intellij-deps-jflex)
* [IntelliJ-patched JFlex](https://cache-redirector.jetbrains.com/intellij-dependencies/org/jetbrains/intellij/deps/jflex/jflex/)
### Usage Examples
* [Perl5 plugin](https://github.com/Camelcade/Perl5-IDEA/blob/master/build.gradle.kts)
* [Rust plugin](https://github.com/intellij-rust/intellij-rust/blob/master/build.gradle.kts)
* [Bamboo Soy plugin](https://github.com/google/bamboo-soy/blob/master/build.gradle)