/
Main.java
350 lines (313 loc) · 12 KB
/
Main.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* JFlex 1.7.1-SNAPSHOT *
* Copyright (C) 1998-2018 Gerwin Klein <lsf@jflex.de> *
* All rights reserved. *
* *
* License: BSD *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package jflex;
import static jflex.core.Options.setEncoding;
import static jflex.core.Options.unused_warning;
import static jflex.core.Out.error;
import static jflex.l10n.ErrorMessages.NO_ENCODING;
import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import jflex.base.Build;
import jflex.core.LexGenerator;
import jflex.core.Options;
import jflex.core.Out;
import jflex.core.unicode.UnicodeProperties;
import jflex.exceptions.GeneratorException;
import jflex.exceptions.SilentExit;
import jflex.gui.MainFrame;
import jflex.l10n.ErrorMessages;
/**
* This is the command-line interface.
*
* <p>It is responsible for parsing the commandline, getting input files, starting up the GUI if
* necessary, etc. and invokes {@link LexGenerator} accordingly.
*
* @author Gerwin Klein
* @author Régis Décamps
* @version JFlex 1.7.1-SNAPSHOT
*/
public class Main {
/**
* parseOptions.
*
* @param argv an array of {@link java.lang.String} objects.
* @return a {@link java.util.List} object.
* @throws SilentExit if any.
*/
private static List<File> parseOptions(String argv[]) throws SilentExit {
List<File> files = new ArrayList<>();
for (int i = 0; i < argv.length; i++) {
if (Objects.equals(argv[i], "-d")
|| Objects.equals(argv[i], "--outdir")) { // $NON-NLS-1$ //$NON-NLS-2$
if (++i >= argv.length) {
Out.error(ErrorMessages.NO_DIRECTORY);
throw new GeneratorException();
}
Options.setDir(argv[i]);
continue;
}
if (Objects.equals(argv[i], "--skel")
|| Objects.equals(argv[i], "-skel")) { // $NON-NLS-1$ //$NON-NLS-2$
if (++i >= argv.length) {
Out.error(ErrorMessages.NO_SKEL_FILE);
throw new GeneratorException();
}
Options.setSkeleton(new File(argv[i]));
continue;
}
if (Objects.equals(argv[i], "--encoding")) {
if (++i >= argv.length) {
error(NO_ENCODING);
throw new GeneratorException();
}
setEncoding(argv[i]);
continue;
}
if (Objects.equals(argv[i], "-jlex")
|| Objects.equals(argv[i], "--jlex")) { // $NON-NLS-1$ //$NON-NLS-2$
Options.jlex = true;
continue;
}
if (Objects.equals(argv[i], "-v")
|| Objects.equals(argv[i], "--verbose")
|| Objects.equals(argv[i], "-verbose")) { // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
Options.verbose = true;
Options.progress = true;
Options.unused_warning = true;
continue;
}
if (Objects.equals(argv[i], "-q")
|| Objects.equals(argv[i], "--quiet")
|| Objects.equals(argv[i], "-quiet")) { // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
Options.verbose = false;
Options.progress = false;
Options.unused_warning = false;
continue;
}
if (Objects.equals(argv[i], "--warn-unused")) { // $NON-NLS-1$
unused_warning = true;
continue;
}
if (Objects.equals(argv[i], "--no-warn-unused")) { // $NON-NLS-1$
unused_warning = false;
continue;
}
if (Objects.equals(argv[i], "--dump")
|| Objects.equals(argv[i], "-dump")) { // $NON-NLS-1$ //$NON-NLS-2$
Options.dump = true;
continue;
}
if (Objects.equals(argv[i], "--time")
|| Objects.equals(argv[i], "-time")) { // $NON-NLS-1$ //$NON-NLS-2$
Options.time = true;
continue;
}
if (Objects.equals(argv[i], "--version")
|| Objects.equals(argv[i], "-version")) { // $NON-NLS-1$ //$NON-NLS-2$
Out.println(ErrorMessages.THIS_IS_JFLEX, Build.VERSION);
throw new SilentExit(0);
}
if (Objects.equals(argv[i], "--dot")
|| Objects.equals(argv[i], "-dot")) { // $NON-NLS-1$ //$NON-NLS-2$
Options.dot = true;
continue;
}
if (Objects.equals(argv[i], "--help")
|| Objects.equals(argv[i], "-h")
|| Objects.equals(argv[i], "/h")) { // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
printUsage();
throw new SilentExit(0);
}
if (Objects.equals(argv[i], "--info")
|| Objects.equals(argv[i], "-info")) { // $NON-NLS-1$ //$NON-NLS-2$
Out.printSystemInfo();
throw new SilentExit(0);
}
if (Objects.equals(argv[i], "--nomin")
|| Objects.equals(argv[i], "-nomin")) { // $NON-NLS-1$ //$NON-NLS-2$
Options.no_minimize = true;
continue;
}
if (Objects.equals(argv[i], "--pack")
|| Objects.equals(argv[i], "-pack")) { // $NON-NLS-1$ //$NON-NLS-2$
/* no-op - pack is the only generation method */
continue;
}
if (Objects.equals(argv[i], "--nobak")
|| Objects.equals(argv[i], "-nobak")) { // $NON-NLS-1$ //$NON-NLS-2$
Options.no_backup = true;
continue;
}
if (Objects.equals(argv[i], "--legacydot")
|| Objects.equals(argv[i], "-legacydot")) { // $NON-NLS-1$ //$NON-NLS-2$
Options.legacy_dot = true;
continue;
}
if (Objects.equals(argv[i], "--uniprops")
|| Objects.equals(argv[i], "-uniprops")) { // $NON-NLS-1$ //$NON-NLS-2$
if (++i >= argv.length) {
Out.error(
ErrorMessages.PROPS_ARG_REQUIRES_UNICODE_VERSION, UnicodeProperties.UNICODE_VERSIONS);
throw new GeneratorException();
}
String unicodeVersion = argv[i];
try {
printUnicodePropertyValuesAndAliases(unicodeVersion);
} catch (UnicodeProperties.UnsupportedUnicodeVersionException e) {
Out.error(
ErrorMessages.UNSUPPORTED_UNICODE_VERSION_SUPPORTED_ARE,
UnicodeProperties.UNICODE_VERSIONS);
throw new GeneratorException();
}
throw new SilentExit();
}
if (argv[i].startsWith("-")) { // $NON-NLS-1$
Out.error(ErrorMessages.UNKNOWN_COMMANDLINE, argv[i]);
printUsage();
throw new SilentExit();
}
// if argv[i] is not an option, try to read it as file
File f = new File(argv[i]);
if (f.isFile() && f.canRead()) files.add(f);
else {
Out.error("Sorry, couldn't open \"" + f + "\""); // $NON-NLS-2$
throw new GeneratorException();
}
}
return files;
}
/**
* Prints one Unicode property value per line, along with its aliases, if any, for the given
* unicodeVersion.
*
* @param unicodeVersion The Unicode version to print property values and aliases for
* @throws UnicodeProperties.UnsupportedUnicodeVersionException if unicodeVersion is not supported
*/
private static void printUnicodePropertyValuesAndAliases(String unicodeVersion)
throws UnicodeProperties.UnsupportedUnicodeVersionException {
Pattern versionPattern = Pattern.compile("(\\d+)(?:\\.(\\d+))?(?:\\.\\d+)?");
Matcher matcher = versionPattern.matcher(unicodeVersion);
if (!matcher.matches()) {
throw new UnicodeProperties.UnsupportedUnicodeVersionException();
}
String underscoreVersion =
matcher.group(1) + (null == matcher.group(2) ? "_0" : "_" + matcher.group(2));
String[] propertyValues;
String[] propertyValueAliases;
try {
Class<?> clazz = Class.forName("jflex.unicode.data.Unicode_" + underscoreVersion);
Field field = clazz.getField("propertyValues");
propertyValues = (String[]) field.get(null);
field = clazz.getField("propertyValueAliases");
propertyValueAliases = (String[]) field.get(null);
} catch (Exception e) {
throw new UnicodeProperties.UnsupportedUnicodeVersionException();
}
SortedMap<String, SortedSet<String>> propertyValuesToAliases = new TreeMap<>();
for (String value : propertyValues) {
propertyValuesToAliases.put(value, new TreeSet<String>());
}
for (int i = 0; i < propertyValueAliases.length - 1; i += 2) {
String alias = propertyValueAliases[i];
String value = propertyValueAliases[i + 1];
SortedSet<String> aliases = propertyValuesToAliases.get(value);
if (null == aliases) {
aliases = new TreeSet<>();
propertyValuesToAliases.put(value, aliases);
}
aliases.add(alias);
}
for (Map.Entry<String, SortedSet<String>> entry : propertyValuesToAliases.entrySet()) {
String value = entry.getKey();
SortedSet<String> aliases = entry.getValue();
Out.print(value);
if (aliases.size() > 0) {
for (String alias : aliases) {
Out.print(", " + alias);
}
}
Out.println("");
}
}
/** Prints the cli usage on stdout. */
private static void printUsage() {
Out.println(""); // $NON-NLS-1$
Out.println("Usage: jflex <options> <input-files>");
Out.println("");
Out.println("Where <options> can be one or more of");
Out.println("-d <directory> write generated file to <directory>");
Out.println("--skel <file> use external skeleton <file>");
Out.println("--encoding <name> use <name> as input/output encoding");
Out.println("--pack set default code generation method (default)");
Out.println("--jlex strict JLex compatibility");
Out.println("--legacydot dot (.) metachar matches [^\\n] instead of");
Out.println(" [^\\n\\r\\u000B\\u000C\\u0085\\u2028\\u2029]");
Out.println("--nomin skip minimization step");
Out.println("--nobak don't create backup files");
Out.println("--dump display transition tables");
Out.println("--dot write graphviz .dot files for the generated automata (alpha)");
Out.println("--verbose");
Out.println("-v display generation progress messages (default)");
Out.println("--quiet");
Out.println("-q display errors only");
Out.println("--time display generation time statistics");
Out.println("--version print the version number of this copy of jflex");
Out.println("--info print system + JDK information");
Out.println("--uniprops <ver> print all supported properties for Unicode version <ver>");
Out.println("--help");
Out.println("-h print this message");
Out.println("");
Out.println(ErrorMessages.THIS_IS_JFLEX, Build.VERSION);
Out.println("Have a nice day!");
}
/**
* generate.
*
* @param argv an array of {@link java.lang.String} objects.
* @throws SilentExit if any.
*/
public static void generate(String argv[]) throws SilentExit {
List<File> files = parseOptions(argv);
if (files.size() > 0) {
for (File file : files) {
LexGenerator.generate(file);
}
} else {
new MainFrame();
}
}
/**
* Starts the generation process with the files in {@code argv} or pops up a window to choose a
* file, when {@code argv} doesn't have any file entries.
*
* @param argv the commandline.
*/
public static void main(String argv[]) {
try {
generate(argv);
} catch (GeneratorException e) {
Out.statistics();
System.exit(1);
} catch (SilentExit e) {
System.exit(e.exitCode());
}
}
// Only CLI, not meant for instanciation.
private Main() {}
}