Quantcast
Channel: Gradle Forums - Latest posts
Viewing all articles
Browse latest Browse all 19888

Using ANTLR plugin to process separate lexer and parser grammars

$
0
0

I know its been 4 years since this was posted but I couldnt find a good solution, so I wrote a kotlin helper function to solve it, append and call from your build.gradle.kts:

import java.io.BufferedReader
import java.io.File
import java.io.InputStreamReader

//your build.gradle.kts file...
fun Project.antlr(
        grammarFile: String,
        packageName: String = "$group.$name",
        outputDir: String = "$rootDir/src/gen/java",
        inputDir: String = "$rootDir/src/main/antlr"
) {
    val pkgPath = packageName.replace(".", "/")

    val compileOnlyDepFiles = configurations.compileOnly.get().files
    project.logger.debug("checking for antlr4 ($antlrToolJarRegex) configuartions.compileOnly.files: $compileOnlyDepFiles")
    val antlrJars = compileOnlyDepFiles.filter { it.name.matches(antlrToolJarRegex) }
    project.logger.info("found antlr jars: ${antlrJars.joinToString("\n","\n")}")

    val antlrJar = antlrJars.firstOrNull()

    if(antlrJar == null || ! antlrJar.exists()) { throw RuntimeException("failed to find antlr compiler tool $antlrJar") }

    File(outputDir).mkdirs()

    val cmd = arrayOf(
            "java", "-jar", antlrJar.absolutePath,
            "-encoding", "UTF-8",
            "-o", "$outputDir/$pkgPath",
            "-package", packageName,
            "-lib", outputDir,
            "$inputDir/$grammarFile"
    )

    println("exec ~= ${cmd.joinToString(" ")}")
    val exec = Runtime.getRuntime().exec(cmd)
    val messages = BufferedReader(InputStreamReader(exec.errorStream)).use { err ->
        generateSequence { err.readLine() }.toList()
    }
    val result = exec.waitFor()

    if (result != 0) {
        throw RuntimeException(
                """antlr exec ~= ${cmd.joinToString(" ")}
                  |failed with code=${result} because:
                  |    ${messages.joinToString("")}
                """.trimMargin()
        )
    }
}

usage:

dependencies {
    //...
    compileOnly("org.antlr:antlr4:4.8-1:complete")
}

tasks {
    register("generateLexer") {
        doLast {
            antlr("MyLexer.g4")
        }
    }
    register("generateParser") {
        doLast {
            antlr("MyParser.g4")
        }
    }
}

Viewing all articles
Browse latest Browse all 19888

Trending Articles