forked from vbmacher/cup-maven-plugin
/
CupParserGenerator.java
211 lines (183 loc) · 7.89 KB
/
CupParserGenerator.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
/*
* Copyright (c) 2012-2016, Peter Jakubčo
*
* KISS, DRY, YAGNI
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package com.github.vbmacher.cup;
import java_cup.Main;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
/**
* Wrapper for calling CUP.
*/
public class CupParserGenerator {
private static final String PACKAGE = "package";
private static final String CLASS = "class";
private final Log log;
private final CupParameters cupParameters;
private final File cupDefinition;
private static class CupFileFilter implements DirectoryStream.Filter<Path> {
@Override
public boolean accept(Path entry) throws IOException {
File file = entry.toFile();
return file.isFile() && file.getName().toLowerCase().endsWith(".cup");
}
}
/**
* Creates new instance.
*
* @param log Maven plug-in logger
* @param cupParameters Parameters used for CUP parser generation
*/
public CupParserGenerator(Log log, CupParameters cupParameters) {
this.log = Objects.requireNonNull(log);
this.cupParameters = Objects.requireNonNull(cupParameters);
this.cupDefinition = cupParameters.getCupDefinition();
}
/**
* Generate parser(s) using CUP library.
*
* @throws MojoExecutionException When something goes wrong
*/
public void process() throws MojoExecutionException {
if (cupDefinition.isDirectory()) {
parseCupDirectory();
} else {
parseCupFile(cupDefinition);
}
}
private void parseCupDirectory() throws MojoExecutionException {
log.debug("Processing directory: " + cupDefinition.getAbsolutePath());
try (DirectoryStream<Path> stream = Files.newDirectoryStream(cupDefinition.toPath(), new CupFileFilter())) {
for (Path cupFile : stream) {
parseCupFile(cupFile.toFile());
}
} catch (IOException e) {
throw new MojoExecutionException("Could not process directory " + cupDefinition.getName(), e);
}
}
private void parseCupFile(File cupFile) throws MojoExecutionException {
log.debug("Processing CUP file " + cupFile.getName());
String packageName = determinePackageName(cupFile);
String className = determineClassName(cupFile);
verifyParameters(cupFile, packageName, className);
File outputDirectory = cupParameters.getOutputDirectory();
File outputFile = prepareOutputFile(packageName, className, outputDirectory);
if (sourceNeedsProcessing(cupFile, outputFile)) {
List<String> params = cupParameters.prepareCommandLine(cupFile, outputFile);
try {
Main.main(params.toArray(new String[0]));
log.info(" generated " + outputFile);
String symbolsName = cupParameters.getSymbolsName();
if (symbolsName != null) {
File symFile = new File(outputDirectory, getOutputFilename(packageName, symbolsName));
log.info(" generated " + symFile);
}
} catch (Exception e) {
throw new MojoExecutionException("Could not process CUP file " + cupFile.getAbsolutePath(), e);
}
}
}
private File prepareOutputFile(String packageName, String className, File outputDirectory) {
File generatedFile = new File(outputDirectory, getOutputFilename(packageName, className));
if (generatedFile.getParentFile() != null) {
generatedFile.getParentFile().mkdirs();
}
return generatedFile;
}
private boolean sourceNeedsProcessing(File cupFile, File generatedFile) {
long staleMillis = cupParameters.getStaleMillis();
if (cupFile.lastModified() - generatedFile.lastModified() <= staleMillis) {
log.info(" " + generatedFile.getName() + " is up to date.");
log.debug("StaleMillis = " + staleMillis + "ms");
return false;
}
return true;
}
private String determinePackageName(File cupFile) throws MojoExecutionException {
String globalPackageName = cupParameters.getPackageName();
if (globalPackageName == null) {
try {
return readCupEntity(PACKAGE, cupFile);
} catch (IOException e) {
throw new MojoExecutionException("Could not read package name from CUP file " + cupFile.getName(), e);
}
}
return globalPackageName;
}
private String determineClassName(File cupFile) throws MojoExecutionException {
String globalClassName = cupParameters.getClassName();
if (globalClassName == null) {
try {
return readCupEntity(CLASS, cupFile);
} catch (IOException e) {
throw new MojoExecutionException("Could not read class name from CUP file " + cupFile.getName(), e);
}
}
return globalClassName;
}
private String readCupEntity(String entityName, File cupFile) throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(cupFile))) {
String line;
while ((line = reader.readLine()) != null) {
String trimmed = line.trim();
if (trimmed.startsWith(entityName)) {
int end = trimmed.indexOf(';');
if (end > 0) {
return line.substring(entityName.length(), end).trim();
}
}
}
return null;
}
}
private String getOutputFilename(String packageName, String className) {
String packageDirectory = packageName.replace('.', File.separatorChar);
if (!packageDirectory.isEmpty()) {
packageDirectory += File.separatorChar;
}
return packageDirectory + className + ".java";
}
private void verifyParameters(File cupFile, String packageName, String className) throws MojoExecutionException {
if (cupFile == null) {
throw new MojoExecutionException(
"<cupDefinition> is empty. Please define input file with <cupDefinition>input.cup</cupDefinition>"
);
}
if (!cupFile.isFile()) {
throw new MojoExecutionException("Input file does not exist: " + cupFile.getAbsolutePath());
}
if (packageName == null) {
throw new MojoExecutionException("Package name is not defined");
}
if (className == null) {
throw new MojoExecutionException("Class name is not defined");
}
if (cupParameters.getOutputDirectory() == null) {
throw new MojoExecutionException("Output directory is not defined");
}
}
}