Skip to content

Commit

Permalink
LUCENE-9654: Expressions module gramar antlr code regeneration (#269)
Browse files Browse the repository at this point in the history
  • Loading branch information
dweiss authored Aug 27, 2021
1 parent 3b3f960 commit e470535
Show file tree
Hide file tree
Showing 10 changed files with 1,134 additions and 621 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ apply from: file('gradle/generation/nori.gradle')
apply from: file('gradle/generation/icu.gradle')
apply from: file('gradle/generation/javacc.gradle')
apply from: file('gradle/generation/forUtil.gradle')
apply from: file('gradle/generation/antlr.gradle')

apply from: file('gradle/datasets/external-datasets.gradle')

Expand Down
88 changes: 88 additions & 0 deletions gradle/generation/antlr.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import java.nio.file.Files

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/

configure(project(":lucene:expressions")) {
configurations {
antlr
}

dependencies {
antlr "org.antlr:antlr4"
}

task generateAntlrInternal() {
description "Regenerate Javascript.g4"
group "generation"

dependsOn configurations.antlr

def generatedPatterns = [
"**/JavascriptBaseVisitor.java",
"**/JavascriptLexer.java",
"**/JavascriptParser.java",
"**/JavascriptVisitor.java",
]

def sourceDir = "src/java/org/apache/lucene/expressions/js"
def inputFiles = file("$sourceDir/Javascript.g4")
def tempOutput = file("$buildDir/antlr")
def outputFiles = fileTree(dir: sourceDir, includes: generatedPatterns)

inputs.files inputFiles
outputs.files outputFiles

doFirst {
project.delete tempOutput

project.javaexec {
main = "org.antlr.v4.Tool"
classpath = configurations.antlr

ignoreExitValue false
args = [
"-no-listener",
"-visitor",
"-package", "org.apache.lucene.expressions.js",
"-o", tempOutput,
inputFiles
]
}

def generatedFiles = fileTree(dir: tempOutput, includes: generatedPatterns)
generatedFiles.each { file ->
modifyFile(file, { text ->
text = text.replaceAll("public ((interface|class) Javascript\\w+)", "\$1")
text = text.replaceAll("// Generated from .*", "// ANTLR GENERATED CODE: DO NOT EDIT")
return text
})
}

project.copy {
from tempOutput
into sourceDir
include generatedPatterns
}
}
}

def generateAntlr = wrapWithPersistentChecksums(
generateAntlrInternal, [ andThenTasks: ["spotlessJava", "spotlessJavaApply"] ])

regenerate.dependsOn generateAntlr
}
14 changes: 0 additions & 14 deletions gradle/generation/javacc.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,6 @@ configure(rootProject) {
}
}

/**
* Utility function to read a file, apply changes to its content and write it back.
*/
def modifyFile = { File path, Function<String, String> modify ->
Function<String, String> normalizeEols = { text -> text.replace("\r\n", "\n") }
modify = normalizeEols.andThen(modify).andThen(normalizeEols)

String original = path.getText("UTF-8")
String modified = modify.apply(original)
if (!original.equals(modified)) {
path.write(modified, "UTF-8")
}
}

def commonCleanups = { FileTree generatedFiles ->
// This is a minor typo in a comment that nonetheless people have hand-corrected in the past.
generatedFiles.matching({ include "CharStream.java" }).each {file ->
Expand Down
21 changes: 21 additions & 0 deletions gradle/generation/regenerate.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import org.apache.commons.codec.digest.DigestUtils

import java.util.function.Function

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
Expand Down Expand Up @@ -62,6 +64,24 @@ def computeChecksummedEntries = { Task sourceTask ->
return allEntries
}

configure(rootProject) {
ext {
/**
* Utility function to read a file, apply changes to its content and write it back.
*/
modifyFile = { File path, Function<String, String> modify ->
Function<String, String> normalizeEols = { text -> text.replace("\r\n", "\n") }
modify = normalizeEols.andThen(modify).andThen(normalizeEols)

String original = path.getText("UTF-8")
String modified = modify.apply(original)
if (!original.equals(modified)) {
path.write(modified, "UTF-8")
}
}
}
}

configure([
project(":lucene:analysis:common"),
project(":lucene:analysis:icu"),
Expand All @@ -70,6 +90,7 @@ configure([
project(":lucene:backward-codecs"),
project(":lucene:core"),
project(":lucene:queryparser"),
project(":lucene:expressions"),
]) {
task regenerate() {
description "Rerun any code or static data generation tasks."
Expand Down
3 changes: 0 additions & 3 deletions gradle/validation/spotless.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ configure(project(":lucene").subprojects) { prj ->
break

case ":lucene:expressions":
targetExclude "**/JavascriptLexer.java",
"**/JavascriptParser.java",
"**/JavascriptVisitor.java"
break

case ":lucene:queryparser":
Expand Down
7 changes: 7 additions & 0 deletions lucene/expressions/src/generated/checksums/generateAntlr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"lucene/expressions/src/java/org/apache/lucene/expressions/js/Javascript.g4": "818e89aae0b6c7601051802013898c128fe7c1ba",
"lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptBaseVisitor.java": "45e3c7093f3e485a07be507efbdefc5e3d112576",
"lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptLexer.java": "f0a53549fb8329dd4181f836d1778417de08550e",
"lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptParser.java": "ca9d587e6e98436647370ee5b7c82ae07243ee50",
"lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptVisitor.java": "ebf033dc72e63203e5d4d85fd57114dd973482dc"
}
Loading

0 comments on commit e470535

Please sign in to comment.