From e25dac085fec077cf27e8c79a6f0796059678f9f Mon Sep 17 00:00:00 2001 From: Dawid Weiss Date: Wed, 29 Jan 2020 17:01:34 +0100 Subject: [PATCH] LUCENE-9134: this adds initial javacc support (without follow-up tweaks required to make the sources identical as those generated by ant). --- build.gradle | 4 +- gradle/generation/javacc.gradle | 117 ++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 gradle/generation/javacc.gradle diff --git a/build.gradle b/build.gradle index fc5d96542a5..5db7b0218ee 100644 --- a/build.gradle +++ b/build.gradle @@ -40,7 +40,8 @@ ext { // https://github.com/palantir/gradle-consistent-versions/issues/383 scriptDepVersions = [ "apache-rat": "0.11", - "jflex": "1.7.0" + "javacc": "5.0", + "jflex": "1.7.0", ] } @@ -81,6 +82,7 @@ apply from: file('gradle/validation/owasp-dependency-check.gradle') // Source or data regeneration tasks apply from: file('gradle/generation/jflex.gradle') +apply from: file('gradle/generation/javacc.gradle') // Additional development aids. apply from: file('gradle/maven/maven-local.gradle') diff --git a/gradle/generation/javacc.gradle b/gradle/generation/javacc.gradle new file mode 100644 index 00000000000..85f06099356 --- /dev/null +++ b/gradle/generation/javacc.gradle @@ -0,0 +1,117 @@ + +// This adds javacc generation support. + +configure(rootProject) { + configurations { + javacc + } + + dependencies { + javacc "net.java.dev.javacc:javacc:${scriptDepVersions['javacc']}" + } + + task javacc() { + description "Regenerate sources for corresponding javacc grammar files." + group "generation" + + dependsOn ":lucene:queryparser:javaccParserClassic" + dependsOn ":lucene:queryparser:javaccParserSurround" + dependsOn ":lucene:queryparser:javaccParserFlexible" + } +} + +// We always regenerate, no need to declare outputs. +class JavaCCTask extends DefaultTask { + @Input + File javaccFile + + JavaCCTask() { + dependsOn(project.rootProject.configurations.javacc) + } + + @TaskAction + def generate() { + if (!javaccFile || !javaccFile.exists()) { + throw new RuntimeException("JavaCC input file does not exist: ${javaccFile}") + } + + // Remove previous files so we can regenerate them. javacc doesn't want to overwrite + // locally modified files. + def parentDir = javaccFile.parentFile + def toDelete = project.fileTree(parentDir, { + include "**/*.java" + }).findAll { file -> file.getText("UTF-8").contains("Generated By:JavaCC") } + project.delete(toDelete) + + logger.lifecycle("Regenerating JavaCC:\n from: ${javaccFile}\n to: ${parentDir}") + + def output = new ByteArrayOutputStream() + def result = project.javaexec { + classpath { + project.rootProject.configurations.javacc + } + + ignoreExitValue = true + standardOutput = output + errorOutput = output + + main = "org.javacc.parser.Main" + args += [ + "-OUTPUT_DIRECTORY=${parentDir}", + javaccFile + ] + } + + // Unless we request verbose logging, don't emit javacc output. + if (result.exitValue != 0) { + throw new GradleException("JavaCC failed to compile ${javaccFile}, here is the compilation output:\n${output}") + } + } +} + + +configure(project(":lucene:queryparser")) { + task javaccParserClassic(type: JavaCCTask) { + description "Regenerate classic query parser from java CC.java" + group "generation" + + javaccFile = file('src/java/org/apache/lucene/queryparser/classic/QueryParser.jj') + def parent = javaccFile.parentFile.toString() // I'll need this later. + + doLast { + // There'll be a lot of cleanup in here to get precommits and builds to pass, but as long as we don't + // run the javacc task in master and check the regenerated files in, we'll be OK. + // As soon as we merge this into master I can fill this in. + } + } +} + +configure(project(":lucene:queryparser")) { + task javaccParserSurround(type: JavaCCTask) { + description "Regenerate surround query parser from java CC.java" + group "generation" + + javaccFile = file('src/java/org/apache/lucene/queryparser/surround/parser/QueryParser.jj') + + doLast { + // There'll be a lot of cleanup in here to get precommits and builds to pass, but as long as we don't + // run the javacc task in master and check the regenerated files in, we'll be OK. + // As soon as we merge this into master I can fill this in. + } + } +} + +configure(project(":lucene:queryparser")) { + task javaccParserFlexible(type: JavaCCTask) { + description "Regenerate Flexible query parser from java CC.java" + group "generation" + + javaccFile = file('src/java/org/apache/lucene/queryparser/flexible/standard/parser/StandardSyntaxParser.jj') + + doLast { + // There'll be a lot of cleanup in here to get precommits and builds to pass, but as long as we don't + // run the javacc task in master and check the regenerated files in, we'll be OK. + // As soon as we merge this into master I can fill this in. + } + } +} \ No newline at end of file