-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
AntlrTask.java
318 lines (283 loc) · 9.78 KB
/
AntlrTask.java
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
* Copyright 2010 the original author or authors.
*
* Licensed 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.
*/
package org.gradle.api.plugins.antlr;
import org.gradle.api.NonNullApi;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.FileTree;
import org.gradle.api.file.FileType;
import org.gradle.api.file.ProjectLayout;
import org.gradle.api.file.SourceDirectorySet;
import org.gradle.api.plugins.antlr.internal.AntlrResult;
import org.gradle.api.plugins.antlr.internal.AntlrSourceGenerationException;
import org.gradle.api.plugins.antlr.internal.AntlrSpec;
import org.gradle.api.plugins.antlr.internal.AntlrSpecFactory;
import org.gradle.api.plugins.antlr.internal.AntlrWorkerManager;
import org.gradle.api.tasks.CacheableTask;
import org.gradle.api.tasks.Classpath;
import org.gradle.api.tasks.IgnoreEmptyDirectories;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.SkipWhenEmpty;
import org.gradle.api.tasks.SourceTask;
import org.gradle.api.tasks.TaskAction;
import org.gradle.internal.file.Deleter;
import org.gradle.process.internal.worker.WorkerProcessFactory;
import org.gradle.work.ChangeType;
import org.gradle.work.FileChange;
import org.gradle.work.InputChanges;
import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
/**
* Generates parsers from Antlr grammars.
*/
@NonNullApi
@CacheableTask
public abstract class AntlrTask extends SourceTask {
private boolean trace;
private boolean traceLexer;
private boolean traceParser;
private boolean traceTreeWalker;
private List<String> arguments = new ArrayList<>();
private FileCollection antlrClasspath;
private File outputDirectory;
private String maxHeapSize;
private FileCollection sourceSetDirectories;
private final FileCollection stableSources = getProject().files((Callable<Object>) this::getSource);
/**
* Specifies that all rules call {@code traceIn}/{@code traceOut}.
*/
@Input
public boolean isTrace() {
return trace;
}
public void setTrace(boolean trace) {
this.trace = trace;
}
/**
* Specifies that all lexer rules call {@code traceIn}/{@code traceOut}.
*/
@Input
public boolean isTraceLexer() {
return traceLexer;
}
public void setTraceLexer(boolean traceLexer) {
this.traceLexer = traceLexer;
}
/**
* Specifies that all parser rules call {@code traceIn}/{@code traceOut}.
*/
@Input
public boolean isTraceParser() {
return traceParser;
}
public void setTraceParser(boolean traceParser) {
this.traceParser = traceParser;
}
/**
* Specifies that all tree walker rules call {@code traceIn}/{@code traceOut}.
*/
@Input
public boolean isTraceTreeWalker() {
return traceTreeWalker;
}
public void setTraceTreeWalker(boolean traceTreeWalker) {
this.traceTreeWalker = traceTreeWalker;
}
/**
* The maximum heap size for the forked antlr process (ex: '1g').
*/
@Internal
public String getMaxHeapSize() {
return maxHeapSize;
}
public void setMaxHeapSize(String maxHeapSize) {
this.maxHeapSize = maxHeapSize;
}
public void setArguments(List<String> arguments) {
if (arguments != null) {
this.arguments = arguments;
}
}
/**
* List of command-line arguments passed to the antlr process
*
* @return The antlr command-line arguments
*/
@Input
public List<String> getArguments() {
return arguments;
}
/**
* Returns the directory to generate the parser source files into.
*
* @return The output directory.
*/
@OutputDirectory
public File getOutputDirectory() {
return outputDirectory;
}
/**
* Specifies the directory to generate the parser source files into.
*
* @param outputDirectory The output directory. Must not be null.
*/
public void setOutputDirectory(File outputDirectory) {
this.outputDirectory = outputDirectory;
}
/**
* Returns the classpath containing the Ant ANTLR task implementation.
*
* @return The Ant task implementation classpath.
*/
@Classpath
public FileCollection getAntlrClasspath() {
return antlrClasspath;
}
/**
* Specifies the classpath containing the Ant ANTLR task implementation.
*
* @param antlrClasspath The Ant task implementation classpath. Must not be null.
*/
protected void setAntlrClasspath(FileCollection antlrClasspath) {
this.antlrClasspath = antlrClasspath;
}
@Inject
protected WorkerProcessFactory getWorkerProcessBuilderFactory() {
throw new UnsupportedOperationException();
}
@Inject
protected ProjectLayout getProjectLayout() {
throw new UnsupportedOperationException();
}
/**
* Generate the parsers.
*
* @since 6.0
*/
@TaskAction
public void execute(InputChanges inputChanges) {
Set<File> grammarFiles = new HashSet<>();
FileCollection stableSources = getStableSources();
if (inputChanges.isIncremental()) {
boolean rebuildRequired = false;
for (FileChange fileChange : inputChanges.getFileChanges(stableSources)) {
if (fileChange.getFileType() == FileType.FILE) {
if (fileChange.getChangeType() == ChangeType.REMOVED) {
rebuildRequired = true;
break;
}
grammarFiles.add(fileChange.getFile());
}
}
if (rebuildRequired) {
try {
getDeleter().ensureEmptyDirectory(outputDirectory);
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
grammarFiles.addAll(stableSources.getFiles());
}
} else {
grammarFiles.addAll(stableSources.getFiles());
}
AntlrWorkerManager manager = new AntlrWorkerManager();
AntlrSpec spec = new AntlrSpecFactory().create(this, grammarFiles, sourceSetDirectories);
AntlrResult result = manager.runWorker(projectDir(), getWorkerProcessBuilderFactory(), getAntlrClasspath(), spec);
evaluate(result);
}
private void evaluate(AntlrResult result) {
int errorCount = result.getErrorCount();
if (errorCount < 0) {
throw new AntlrSourceGenerationException("There were errors during grammar generation", result.getException());
} else if (errorCount == 1) {
throw new AntlrSourceGenerationException("There was 1 error during grammar generation", result.getException());
} else if (errorCount > 1) {
throw new AntlrSourceGenerationException("There were "
+ errorCount
+ " errors during grammar generation", result.getException());
}
}
private File projectDir() {
return getProjectLayout().getProjectDirectory().getAsFile();
}
/**
* Sets the source for this task. Delegates to {@link #setSource(Object)}.
*
* If the source is of type {@link SourceDirectorySet}, then the relative path of each source grammar files
* is used to determine the relative output path of the generated source
* If the source is not of type {@link SourceDirectorySet}, then the generated source files end up
* flattened in the specified output directory.
*
* @param source The source.
* @since 4.0
*/
@Override
public void setSource(FileTree source) {
setSource((Object) source);
}
/**
* Sets the source for this task. Delegates to {@link SourceTask#setSource(Object)}.
*
* If the source is of type {@link SourceDirectorySet}, then the relative path of each source grammar files
* is used to determine the relative output path of the generated source
* If the source is not of type {@link SourceDirectorySet}, then the generated source files end up
* flattened in the specified output directory.
*
* @param source The source.
*/
@Override
public void setSource(Object source) {
super.setSource(source);
if (source instanceof SourceDirectorySet) {
this.sourceSetDirectories = ((SourceDirectorySet) source).getSourceDirectories();
}
}
/**
* {@inheritDoc}
*/
@Override
@Internal("tracked via stableSources")
public FileTree getSource() {
return super.getSource();
}
/**
* The sources for incremental change detection.
*
* @since 6.0
*/
@SkipWhenEmpty
@IgnoreEmptyDirectories
@PathSensitive(PathSensitivity.RELATIVE)
@InputFiles
protected FileCollection getStableSources() {
return stableSources;
}
@Inject
protected Deleter getDeleter() {
throw new UnsupportedOperationException("Decorator takes care of injection");
}
}