-
Notifications
You must be signed in to change notification settings - Fork 9
/
javacc.gradle
296 lines (255 loc) · 10.9 KB
/
javacc.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import java.nio.charset.Charset
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
* 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.
*/
// This adds javacc generation support.
configure(rootProject) {
configurations {
javacc
}
dependencies {
javacc "net.java.dev.javacc:javacc:7.0.4"
}
repositories {
mavenCentral()
}
task javacc() {
description "Regenerate sources for corresponding javacc grammar files."
group "generation"
dependsOn allprojects.collect { prj -> prj.tasks.withType(JavaCCTask) }
}
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") }
String original = path.getText("UTF-8")
String modified = normalizeEols.apply(original)
modified = modify.apply(modified)
modified = normalizeEols.apply(modified)
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 ->
modifyFile(file, { text ->
return text.replace(
"implemetation",
"implementation");
})
}
generatedFiles.each { file ->
modifyFile(file, { text ->
// Normalize EOLs and tabs (EOLs are a side-effect of modifyFile).
text = text.replace("\t", " ");
text = text.replaceAll("JavaCC - OriginalChecksum=[^*]+", "(filtered)")
text = text.replace("StringBuffer", "StringBuilder")
return text
})
}
generatedFiles.matching({ include "*TokenManager.java" }).each { file ->
modifyFile(file, { text ->
// Remove redundant imports.
text = text.replaceAll(
/(?m)^import .+/,
"")
// Add CharStream imports.
text = text.replaceAll(
/package (.+)/,
'''
package $1
import org.apache.lucene.queryparser.charstream.CharStream;
'''.trim())
// Eliminates redundant cast message.
text = text.replace(
"int hiByte = (int)(curChar >> 8);",
"int hiByte = curChar >> 8;")
// Access to forbidden APIs.
text = text.replace(
"public java.io.PrintStream debugStream = System.out;",
"// (debugStream omitted).")
text = text.replace(
"public void setDebugStream(java.io.PrintStream ds) { debugStream = ds; }",
"// (setDebugStream omitted).")
text = text.replace(
"public class QueryParserTokenManager ",
'@SuppressWarnings("unused") public class QueryParserTokenManager ')
text = text.replace(
"public class StandardSyntaxParserTokenManager ",
'@SuppressWarnings("unused") public class StandardSyntaxParserTokenManager ')
return text
})
}
}
configure(project(":zulia-query-parser")) {
task javaccParserZuliaFlexibleInternal(type: JavaCCTask) {
description "Regenerate flexible query parser from ZuliaSyntaxParser.jj"
group "generation"
javaccFile = file('src/main/java/io/zulia/server/search/queryparser/parser/ZuliaSyntaxParser.jj')
afterGenerate << commonCleanups
afterGenerate << { FileTree generatedFiles ->
generatedFiles.matching { include "ParseException.java" }.each { file ->
modifyFile(file, { text ->
// Modify constructor.
text = text.replace(
"class ParseException extends Exception",
"class ParseException extends QueryNodeParseException")
// Modify imports.
text = text.replace(
"package io.zulia.server.search.queryparser.parser;", '''\
package io.zulia.server.search.queryparser.parser;
import org.apache.lucene.queryparser.flexible.core.QueryNodeParseException;
import org.apache.lucene.queryparser.flexible.core.messages.QueryParserMessages;
import org.apache.lucene.queryparser.flexible.messages.Message;
import org.apache.lucene.queryparser.flexible.messages.MessageImpl;
''')
// Modify constructors and code bits
text = text.replaceAll(
/(?s)[ ]*public ParseException\(Token currentTokenVal[^}]+[}]/, '''\
public ParseException(Token currentTokenVal,
int[][] expectedTokenSequencesVal, String[] tokenImageVal)
{
super(new MessageImpl(QueryParserMessages.INVALID_SYNTAX, initialise(
currentTokenVal, expectedTokenSequencesVal, tokenImageVal)));
this.currentToken = currentTokenVal;
this.expectedTokenSequences = expectedTokenSequencesVal;
this.tokenImage = tokenImageVal;
}
''')
text = text.replaceAll(
/(?s)[ ]*public ParseException\(String message\)[^}]+[}]/, '''\
public ParseException(Message message)
{
super(message);
}
''')
text = text.replaceAll(
/(?s)[ ]*public ParseException\(\)[^}]+[}]/, '''\
public ParseException()
{
super(new MessageImpl(QueryParserMessages.INVALID_SYNTAX, "Error"));
}
''')
return text
})
}
generatedFiles.matching { include "ZuliaSyntaxParser.java" }.each { file ->
modifyFile(file, { text ->
// Remove redundant cast
text = text.replace(
"new java.util.ArrayList<int[]>",
"new java.util.ArrayList<>")
text = text.replace(
"new ArrayList<QueryNode>()",
"new ArrayList<>()")
text = text.replace(
"Collections.<QueryNode> singletonList",
"Collections.singletonList")
text = text.replace(
"public class StandardSyntaxParser ",
'@SuppressWarnings({"unused","null"}) public class StandardSyntaxParser ')
return text
})
}
}
}
task javacc() {
description "Regenerate query parsers (javacc syntax definitions)."
group "generation"
}
}
// We always regenerate, no need to declare outputs.
class JavaCCTask extends DefaultTask {
@InputFile
File javaccFile
/**
* Apply closures to all generated files before they're copied back
* to mainline code.
*/
// A subtle bug here is that this makes it not an input... should be a list of replacements instead?
@Internal
List<Closure<FileTree>> afterGenerate = new ArrayList<>()
@OutputFiles
List<File> getGeneratedSources() {
// Return the list of generated files.
def baseDir = javaccFile.parentFile
def baseName = javaccFile.name.replace(".jj", "")
return [
project.file("${baseDir}/${baseName}.java"),
project.file("${baseDir}/${baseName}Constants.java"),
project.file("${baseDir}/${baseName}TokenManager.java"),
project.file("${baseDir}/ParseException.java"),
project.file("${baseDir}/Token.java"),
project.file("${baseDir}/TokenMgrError.java")
]
}
JavaCCTask() {
dependsOn(project.rootProject.configurations.javacc)
}
@TaskAction
def generate() {
if (!javaccFile || !javaccFile.exists()) {
throw new GradleException("Input file does not exist: ${javaccFile}")
}
// Run javacc generation into temporary folder so that we know all the generated files
// and can post-process them easily.
def tempDir = this.getTemporaryDir()
tempDir.mkdirs()
project.delete project.fileTree(tempDir, { include: "**/*.java" })
def targetDir = javaccFile.parentFile
logger.lifecycle("Recompiling JavaCC: ${project.rootDir.relativePath(javaccFile)}")
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=${tempDir}",
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}")
}
// Make sure we don't have warnings.
if (output.toString(Charset.defaultCharset()).contains("Warning:")) {
throw new GradleException("JavaCC emitted warnings for ${javaccFile}, here is the compilation output:\n${output}")
}
// Apply any custom modifications.
def generatedFiles = project.fileTree(tempDir)
afterGenerate.each { closure ->
closure.call(generatedFiles)
}
// Copy back to mainline sources.
project.copy {
from tempDir
into targetDir
// We don't need CharStream interface as we redirect to our own.
exclude "CharStream.java"
}
}
}