From febf0f029f8d8971a292991b28cc61aafffa915f Mon Sep 17 00:00:00 2001 From: Edoardo Luppi Date: Sun, 8 Jun 2025 20:15:20 +0200 Subject: [PATCH 1/3] build: simplify Gradle scripts that use AntlrKotlinTask --- antlr-kotlin-benchmarks/build.gradle.kts | 69 +++++++-------------- antlr-kotlin-tests/build.gradle.kts | 79 +++++++----------------- 2 files changed, 44 insertions(+), 104 deletions(-) diff --git a/antlr-kotlin-benchmarks/build.gradle.kts b/antlr-kotlin-benchmarks/build.gradle.kts index 4e759047..19370f52 100644 --- a/antlr-kotlin-benchmarks/build.gradle.kts +++ b/antlr-kotlin-benchmarks/build.gradle.kts @@ -1,9 +1,7 @@ import com.strumenta.antlrkotlin.gradle.AntlrKotlinTask import com.strumenta.antlrkotlin.gradle.ext.targetsNative import kotlinx.benchmark.gradle.JvmBenchmarkTarget -import org.gradle.jvm.tasks.Jar import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl -import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask plugins { id("strumenta.multiplatform") @@ -36,6 +34,27 @@ strumentaMultiplatform { } } +val generateKotlinGrammarSource = tasks.register("generateKotlinGrammarSource") { + dependsOn("cleanGenerateKotlinGrammarSource") + + // Only include *.g4 files. This allows tools (e.g., IDE plugins) + // to generate temporary files inside the base path + source = fileTree(layout.projectDirectory.dir("antlr")) { + include("**/*.g4") + } + + val pkgName = "com.strumenta.antlrkotlin.benchmarks.generated" + packageName = pkgName + + // We want visitors alongside listeners. + // The Kotlin target language is implicit, as is the file encoding (UTF-8) + arguments = listOf("-visitor") + + // Generated files are outputted inside build/generatedAntlr + val outDir = "generatedAntlr/${pkgName.replace(".", "/")}" + outputDirectory = layout.buildDirectory.dir(outDir).get().asFile +} + kotlin { js { nodejs() @@ -60,7 +79,7 @@ kotlin { sourceSets { commonMain { kotlin { - srcDir(layout.buildDirectory.dir("generatedAntlr")) + srcDir(generateKotlinGrammarSource) } dependencies { @@ -99,47 +118,3 @@ benchmark { register("mingwX64") } } - -tasks { - val generateKotlinGrammarSource = register("generateKotlinGrammarSource") { - dependsOn("cleanGenerateKotlinGrammarSource") - - // Only include *.g4 files. This allows tools (e.g., IDE plugins) - // to generate temporary files inside the base path - source = fileTree(layout.projectDirectory.dir("antlr")) { - include("**/*.g4") - } - - val pkgName = "com.strumenta.antlrkotlin.benchmarks.generated" - packageName = pkgName - - // We want visitors alongside listeners. - // The Kotlin target language is implicit, as is the file encoding (UTF-8) - arguments = listOf("-visitor") - - // Generated files are outputted inside build/generatedAntlr - val outDir = "generatedAntlr/${pkgName.replace(".", "/")}" - outputDirectory = layout.buildDirectory.dir(outDir).get().asFile - } - - withType> { - dependsOn(generateKotlinGrammarSource) - } - - // - // The source JAR tasks must explicitly depend on the grammar generation - // to avoid Gradle complaining and erroring out - // - - sourcesJar { - dependsOn(generateKotlinGrammarSource) - } - - kotlin.targets.configureEach { - if (publishable) { - named("${targetName}SourcesJar") { - dependsOn(generateKotlinGrammarSource) - } - } - } -} diff --git a/antlr-kotlin-tests/build.gradle.kts b/antlr-kotlin-tests/build.gradle.kts index c9f7a9af..cabed262 100644 --- a/antlr-kotlin-tests/build.gradle.kts +++ b/antlr-kotlin-tests/build.gradle.kts @@ -1,8 +1,5 @@ import com.strumenta.antlrkotlin.gradle.AntlrKotlinTask import com.strumenta.antlrkotlin.gradle.ext.targetsNative -import org.gradle.jvm.tasks.Jar -import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask -import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile plugins { id("strumenta.multiplatform") @@ -32,11 +29,32 @@ strumentaMultiplatform { } } +val generateKotlinGrammarSource = tasks.register("generateKotlinGrammarSource") { + dependsOn("cleanGenerateKotlinGrammarSource") + + // Only include *.g4 files. This allows tools (e.g., IDE plugins) + // to generate temporary files inside the base path + source = fileTree(layout.projectDirectory.dir("antlr")) { + include("**/*.g4") + } + + val pkgName = "com.strumenta.antlrkotlin.test.generated" + packageName = pkgName + + // We want visitors alongside listeners. + // The Kotlin target language is implicit, as is the file encoding (UTF-8) + arguments = listOf("-visitor") + + // Generated files are outputted inside build/generatedAntlr + val outDir = "generatedAntlr/${pkgName.replace(".", "/")}" + outputDirectory = layout.buildDirectory.dir(outDir).get().asFile +} + kotlin { sourceSets { commonMain { kotlin { - srcDir(layout.buildDirectory.dir("generatedAntlr")) + srcDir(generateKotlinGrammarSource) } dependencies { @@ -52,56 +70,3 @@ kotlin { } } } - -tasks { - // The TSQL grammar test fails because of a compiler error - // when targeting the JVM (MethodTooLargeException), even - // if we never test on the JVM. - // Here we simply filter out the generated TSQL Kotlin files - // from the compilation phase - withType { - exclude("**/test/generated/TSql*.kt") - } - - val generateKotlinGrammarSource = register("generateKotlinGrammarSource") { - dependsOn("cleanGenerateKotlinGrammarSource") - - // Only include *.g4 files. This allows tools (e.g., IDE plugins) - // to generate temporary files inside the base path - source = fileTree(layout.projectDirectory.dir("antlr")) { - include("**/*.g4") - } - - val pkgName = "com.strumenta.antlrkotlin.test.generated" - packageName = pkgName - - // We want visitors alongside listeners. - // The Kotlin target language is implicit, as is the file encoding (UTF-8) - arguments = listOf("-visitor") - - // Generated files are outputted inside build/generatedAntlr - val outDir = "generatedAntlr/${pkgName.replace(".", "/")}" - outputDirectory = layout.buildDirectory.dir(outDir).get().asFile - } - - withType> { - dependsOn(generateKotlinGrammarSource) - } - - // - // The source JAR tasks must explicitly depend on the grammar generation - // to avoid Gradle complaining and erroring out - // - - sourcesJar { - dependsOn(generateKotlinGrammarSource) - } - - kotlin.targets.configureEach { - if (publishable) { - named("${targetName}SourcesJar") { - dependsOn(generateKotlinGrammarSource) - } - } - } -} From c2ff27ded79b9adb6292627fb93a8977be3a2a20 Mon Sep 17 00:00:00 2001 From: Edoardo Luppi Date: Sun, 8 Jun 2025 20:16:03 +0200 Subject: [PATCH 2/3] test: move TSqlGrammarTest back to commonTest --- .../test/grammars/TSqlGrammarTest.kt} | 6 +--- .../test/grammars/TSqlGrammarNativeTest.kt | 36 ------------------- 2 files changed, 1 insertion(+), 41 deletions(-) rename antlr-kotlin-tests/src/{jsTest/kotlin/com/strumenta/antlrkotlin/test/grammars/TSqlGrammarJsTest.kt => commonTest/kotlin/com/strumenta/antlrkotlin/test/grammars/TSqlGrammarTest.kt} (79%) delete mode 100644 antlr-kotlin-tests/src/nativeTest/kotlin/com/strumenta/antlrkotlin/test/grammars/TSqlGrammarNativeTest.kt diff --git a/antlr-kotlin-tests/src/jsTest/kotlin/com/strumenta/antlrkotlin/test/grammars/TSqlGrammarJsTest.kt b/antlr-kotlin-tests/src/commonTest/kotlin/com/strumenta/antlrkotlin/test/grammars/TSqlGrammarTest.kt similarity index 79% rename from antlr-kotlin-tests/src/jsTest/kotlin/com/strumenta/antlrkotlin/test/grammars/TSqlGrammarJsTest.kt rename to antlr-kotlin-tests/src/commonTest/kotlin/com/strumenta/antlrkotlin/test/grammars/TSqlGrammarTest.kt index 62e6dbaa..cba50e57 100644 --- a/antlr-kotlin-tests/src/jsTest/kotlin/com/strumenta/antlrkotlin/test/grammars/TSqlGrammarJsTest.kt +++ b/antlr-kotlin-tests/src/commonTest/kotlin/com/strumenta/antlrkotlin/test/grammars/TSqlGrammarTest.kt @@ -11,12 +11,8 @@ import org.antlr.v4.kotlinruntime.CharStream import org.antlr.v4.kotlinruntime.ParserRuleContext import org.antlr.v4.kotlinruntime.TokenStream -// Note(Edoardo): move the test back to commonTest once -// the MethodTooLargeException has been solved on the JVM -// by the Kotlin team. -// FQN: org.jetbrains.org.objectweb.asm.MethodTooLargeException @Suppress("unused") -class TSqlGrammarJsTest : GrammarTest() { +class TSqlGrammarTest : GrammarTest() { override fun createLexer(input: CharStream): TSqlLexer = TSqlLexer(input) diff --git a/antlr-kotlin-tests/src/nativeTest/kotlin/com/strumenta/antlrkotlin/test/grammars/TSqlGrammarNativeTest.kt b/antlr-kotlin-tests/src/nativeTest/kotlin/com/strumenta/antlrkotlin/test/grammars/TSqlGrammarNativeTest.kt deleted file mode 100644 index 09ca08b7..00000000 --- a/antlr-kotlin-tests/src/nativeTest/kotlin/com/strumenta/antlrkotlin/test/grammars/TSqlGrammarNativeTest.kt +++ /dev/null @@ -1,36 +0,0 @@ -// This is needed as the IDE is not able to properly reference -// the TSqlParser class as it is too big -@file:Suppress("UNRESOLVED_REFERENCE") - -package com.strumenta.antlrkotlin.test.grammars - -import com.strumenta.antlrkotlin.test.GrammarTest -import com.strumenta.antlrkotlin.test.generated.TSqlLexer -import com.strumenta.antlrkotlin.test.generated.TSqlParser -import org.antlr.v4.kotlinruntime.CharStream -import org.antlr.v4.kotlinruntime.ParserRuleContext -import org.antlr.v4.kotlinruntime.TokenStream - -// Note(Edoardo): move the test back to commonTest once -// the MethodTooLargeException has been solved on the JVM -// by the Kotlin team. -// FQN: org.jetbrains.org.objectweb.asm.MethodTooLargeException -@Suppress("unused") -class TSqlGrammarNativeTest : GrammarTest() { - override fun createLexer(input: CharStream): TSqlLexer = - TSqlLexer(input) - - override fun createParser(input: TokenStream): TSqlParser = - TSqlParser(input) - - override fun setupTreeTestRuns(runs: MutableSet) { - runs += LispTestRun("tsql/hints.sql", "tsql/hints.sql.tree") - } - - override fun setupErrorsTestRuns(runs: MutableSet) { - runs += ErrorsTestRun("tsql/constants.sql", "tsql/constants.sql.errors") - } - - override fun getTree(parser: TSqlParser): ParserRuleContext = - parser.tsql_file() -} From 49fc4fdcefa8163858a2c29d95afaf0c4f146100 Mon Sep 17 00:00:00 2001 From: Edoardo Luppi Date: Sun, 8 Jun 2025 20:19:13 +0200 Subject: [PATCH 3/3] docs: update readme --- README.md | 59 +++++++++++++++++++++---------------------------------- 1 file changed, 22 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 73042e11..d7acf7db 100644 --- a/README.md +++ b/README.md @@ -120,24 +120,17 @@ To start using ANTLR Kotlin: Depending on `cleanGenerateKotlinGrammarSource` ensures the `.tokens` files are always fresh, and we do not end up with out-of-sync lexers and parsers. -5. Instruct the Kotlin compilation tasks to depend on the grammar generation. - - ```kotlin - tasks.withType> { - dependsOn(generateKotlinGrammarSource) - } - - ``` - -6. Register the `build/generatedAntlr` directory as part of the common source set. +5. Use the `generateKotlinGrammarSource` task to provide an additional source directory + to the `commonMain` source set. Gradle will then infer the task dependency automatically. ```kotlin kotlin { sourceSets { commonMain { kotlin { - srcDir(layout.buildDirectory.dir("generatedAntlr")) + srcDir(generateKotlinGrammarSource) } + ... } } } @@ -146,33 +139,25 @@ To start using ANTLR Kotlin: ## Benchmarks The [antlr-kotlin-benchmarks](./antlr-kotlin-benchmarks) module contains benchmarking code -targeting JVM, JS and WebAssembly. -The scenario has been adapted from [antlr4ng][2]. - -- JVM benchmarks use [kotlinx-benchmark][3], which under the hood uses JMH. - - To run benchmarks, use: - - ``` - ./gradlew :antlr-kotlin-benchmarks:jvmBenchmark - ``` - -- JS, WebAssembly and Native benchmarks cannot use kotlinx-benchmark currently. - Instead, they use a test case which re-uses the benchmark code. +for JVM, JS, WebAssembly and Native targets. - To run benchmarks, remove the `@Ignore` annotation on `ManualMySQLBenchmarks`, and use: - - ``` - ./gradlew :antlr-kotlin-benchmarks:jsTest - ``` - or - ``` - ./gradlew :antlr-kotlin-benchmarks:wasmJsTest - ``` - or - ``` - ./gradlew :antlr-kotlin-benchmarks:mingwX64Test - ``` +The benchmark scenario has been adapted from [antlr4ng][2]. +To run benchmarks, use: +``` +./gradlew :antlr-kotlin-benchmarks:jvmBenchmark +``` +or +``` +./gradlew :antlr-kotlin-benchmarks:jsBenchmark +``` +or +``` +./gradlew :antlr-kotlin-benchmarks:wasmJsBenchmark +``` +or +``` +./gradlew :antlr-kotlin-benchmarks:mingwX64Benchmark +``` ## Maven Central Publication