Skip to content

Commit 1fd0667

Browse files
rjernstsudo-suhas
authored andcommitted
Build: Rewrite antlr regeneration in gradle (elastic#23733)
This change ports the regeneration of antlr parser/lexer into gradle (but does still take advantage of ant calls where appropriate).
1 parent ed8d6f1 commit 1fd0667

File tree

2 files changed

+92
-174
lines changed

2 files changed

+92
-174
lines changed

modules/lang-painless/ant.xml

Lines changed: 0 additions & 157 deletions
This file was deleted.

modules/lang-painless/build.gradle

Lines changed: 92 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,6 @@ dependencyLicenses {
3333
mapping from: /asm-.*/, to: 'asm'
3434
}
3535

36-
// regeneration logic, comes in via ant right now
37-
// don't port it to gradle, it works fine.
38-
39-
/*
40-
configurations {
41-
regenerate
42-
}
43-
44-
dependencies {
45-
regenerate 'org.antlr:antlr4:4.5.1-1'
46-
}
47-
48-
// TODO: this is causing a resolve at configurationt time. we really should not be using ant anymore
49-
ant.references['regenerate.classpath'] = new Path(ant.project, configurations.regenerate.asPath)
50-
ant.importBuild 'ant.xml'
51-
*/
52-
5336
integTestCluster {
5437
setting 'script.max_compilations_per_minute', '1000'
5538
}
@@ -73,3 +56,95 @@ task generatePainlessApi(type: JavaExec) {
7356
classpath = sourceSets.test.runtimeClasspath
7457
args file('../../docs/reference/painless-api-reference')
7558
}
59+
60+
/**********************************************
61+
* Parser regeneration *
62+
**********************************************/
63+
64+
configurations {
65+
regenerate
66+
}
67+
68+
dependencies {
69+
regenerate 'org.antlr:antlr4:4.5.1-1'
70+
}
71+
72+
String grammarPath = 'src/main/antlr'
73+
String outputPath = 'src/main/java/org/elasticsearch/painless/antlr'
74+
75+
task cleanGenerated(type: Delete) {
76+
delete fileTree(grammarPath) {
77+
include '*.tokens'
78+
}
79+
delete fileTree(outputPath) {
80+
include 'Painless*.java'
81+
}
82+
}
83+
84+
task regenLexer(type: JavaExec) {
85+
dependsOn cleanGenerated
86+
main = 'org.antlr.v4.Tool'
87+
classpath = configurations.regenerate
88+
systemProperty 'file.encoding', 'UTF-8'
89+
systemProperty 'user.language', 'en'
90+
systemProperty 'user.country', 'US'
91+
systemProperty 'user.variant', ''
92+
args '-Werror',
93+
'-package', 'org.elasticsearch.painless.antlr',
94+
'-o', outputPath,
95+
"${file(grammarPath)}/PainlessLexer.g4"
96+
}
97+
98+
task regenParser(type: JavaExec) {
99+
dependsOn regenLexer
100+
main = 'org.antlr.v4.Tool'
101+
classpath = configurations.regenerate
102+
systemProperty 'file.encoding', 'UTF-8'
103+
systemProperty 'user.language', 'en'
104+
systemProperty 'user.country', 'US'
105+
systemProperty 'user.variant', ''
106+
args '-Werror',
107+
'-package', 'org.elasticsearch.painless.antlr',
108+
'-no-listener',
109+
'-visitor',
110+
// '-Xlog',
111+
'-o', outputPath,
112+
"${file(grammarPath)}/PainlessParser.g4"
113+
}
114+
115+
task regen {
116+
dependsOn regenParser
117+
doLast {
118+
// moves token files to grammar directory for use with IDE's
119+
ant.move(file: "${outputPath}/PainlessLexer.tokens", toDir: grammarPath)
120+
ant.move(file: "${outputPath}/PainlessParser.tokens", toDir: grammarPath)
121+
// make the generated classes package private
122+
ant.replaceregexp(match: 'public ((interface|class) \\QPainless\\E\\w+)',
123+
replace: '\\1',
124+
encoding: 'UTF-8') {
125+
fileset(dir: outputPath, includes: 'Painless*.java')
126+
}
127+
// make the lexer abstract
128+
ant.replaceregexp(match: '(class \\QPainless\\ELexer)',
129+
replace: 'abstract \\1',
130+
encoding: 'UTF-8') {
131+
fileset(dir: outputPath, includes: 'PainlessLexer.java')
132+
}
133+
// nuke timestamps/filenames in generated files
134+
ant.replaceregexp(match: '\\Q// Generated from \\E.*',
135+
replace: '\\/\\/ ANTLR GENERATED CODE: DO NOT EDIT',
136+
encoding: 'UTF-8') {
137+
fileset(dir: outputPath, includes: 'Painless*.java')
138+
}
139+
// remove tabs in antlr generated files
140+
ant.replaceregexp(match: '\t', flags: 'g', replace: ' ', encoding: 'UTF-8') {
141+
fileset(dir: outputPath, includes: 'Painless*.java')
142+
}
143+
// fix line endings
144+
ant.fixcrlf(srcdir: outputPath) {
145+
patternset(includes: 'Painless*.java')
146+
}
147+
}
148+
}
149+
150+

0 commit comments

Comments
 (0)