Skip to content
Snippets Groups Projects
Commit ffd1205c authored by Johannes Mey's avatar Johannes Mey
Browse files

remove own options package and reuse the one from JastAdd

parent 54af6737
No related branches found
No related tags found
No related merge requests found
package org.jastadd.relast.compiler; package org.jastadd.relast.compiler;
import beaver.Parser; import beaver.Parser;
import org.jastadd.option.ArgumentParser;
import org.jastadd.option.Option;
import org.jastadd.option.ValueOption;
import org.jastadd.relast.ast.GrammarFile; import org.jastadd.relast.ast.GrammarFile;
import org.jastadd.relast.ast.Program; import org.jastadd.relast.ast.Program;
import org.jastadd.relast.compiler.options.CommandLine;
import org.jastadd.relast.compiler.options.CommandLine.CommandLineException;
import org.jastadd.relast.compiler.options.Option;
import org.jastadd.relast.compiler.options.StringOption;
import org.jastadd.relast.parser.RelAstParser; import org.jastadd.relast.parser.RelAstParser;
import org.jastadd.relast.scanner.RelAstScanner; import org.jastadd.relast.scanner.RelAstScanner;
...@@ -18,15 +17,16 @@ import java.nio.file.Files; ...@@ -18,15 +17,16 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
public class Compiler { public class Compiler {
private StringOption optionOutputDir; private ValueOption optionOutputDir;
private StringOption optionInputDir; private ValueOption optionInputDir;
private ArrayList<Option<?>> options; private ArrayList<Option<?>> options;
private CommandLine commandLine; private ArgumentParser commandLine;
public Compiler() { public Compiler() {
options = new ArrayList<>(); options = new ArrayList<>();
...@@ -36,21 +36,22 @@ public class Compiler { ...@@ -36,21 +36,22 @@ public class Compiler {
public static void main(String[] args) { public static void main(String[] args) {
try { try {
new Compiler().run(args); new Compiler().run(args);
} catch (CommandLineException | CompilerException e) { } catch (CompilerException e) {
System.err.println(e.getMessage()); System.err.println(e.getMessage());
System.exit(-1); System.exit(-1);
} }
} }
public int run(String[] args) throws CommandLineException, CompilerException { public int run(String[] args) throws CompilerException {
options = new ArrayList<>(); options = new ArrayList<>();
addOptions(); addOptions();
commandLine = new CommandLine(options); commandLine = new ArgumentParser();
commandLine.parse(args); commandLine.addOptions(options);
commandLine.parseArgs(args, System.err);
Path inputPath; Path inputPath;
if (optionInputDir.isSet()) { if (optionInputDir.isMatched()) {
inputPath = Paths.get(optionInputDir.getValue()); inputPath = Paths.get(optionInputDir.value());
} else { } else {
inputPath = Paths.get("."); inputPath = Paths.get(".");
printMessage("No input dir is set. Assuming current directory '" + inputPath.toAbsolutePath().toString() + "'."); printMessage("No input dir is set. Assuming current directory '" + inputPath.toAbsolutePath().toString() + "'.");
...@@ -65,8 +66,8 @@ public class Compiler { ...@@ -65,8 +66,8 @@ public class Compiler {
} }
Path outputPath; // should not be used, but otherwise there is a compiler warning Path outputPath; // should not be used, but otherwise there is a compiler warning
if (optionOutputDir.isSet()) { if (optionOutputDir.isMatched()) {
outputPath = Paths.get(optionOutputDir.getValue()); outputPath = Paths.get(optionOutputDir.value());
} else { } else {
outputPath = Paths.get("./gen/"); outputPath = Paths.get("./gen/");
printMessage("No output dir is set. Assuming '" + outputPath.toAbsolutePath().toString() + "'."); printMessage("No output dir is set. Assuming '" + outputPath.toAbsolutePath().toString() + "'.");
...@@ -78,7 +79,8 @@ public class Compiler { ...@@ -78,7 +79,8 @@ public class Compiler {
printMessage("Running RelAST Preprocessor"); printMessage("Running RelAST Preprocessor");
List<String> otherArgs = commandLine.getArguments(); // TODO treat unknown options appropriately
List<String> otherArgs = Collections.emptyList();
if (!otherArgs.isEmpty()) { if (!otherArgs.isEmpty()) {
printMessage("Unsupported arguments will be ignored: " + otherArgs); printMessage("Unsupported arguments will be ignored: " + otherArgs);
} }
...@@ -111,8 +113,8 @@ public class Compiler { ...@@ -111,8 +113,8 @@ public class Compiler {
} }
private void addOptions() { private void addOptions() {
optionOutputDir = addOption(new StringOption("outputDir", "target directory for the generated files.")); optionOutputDir = addOption(new ValueOption("outputDir", "target directory for the generated files."));
optionInputDir = addOption(new StringOption("inputDir", "input directory.")); optionInputDir = addOption(new ValueOption("inputDir", "input directory."));
} }
private <OptionType extends Option<?>> OptionType addOption(OptionType option) { private <OptionType extends Option<?>> OptionType addOption(OptionType option) {
...@@ -152,7 +154,7 @@ public class Compiler { ...@@ -152,7 +154,7 @@ public class Compiler {
System.err.println(); System.err.println();
System.err.println("Usage: java -jar relast.jar [--option1] [--option2=value] ... <filename1> <filename2> ... "); System.err.println("Usage: java -jar relast.jar [--option1] [--option2=value] ... <filename1> <filename2> ... ");
System.err.println("Options:"); System.err.println("Options:");
System.err.print(commandLine.printOptionHelp()); commandLine.printHelp(System.err);
return 1; return 1;
} }
......
package org.jastadd.relast.compiler.options;
import java.util.*;
public class CommandLine {
private final Collection<Option<?>> options;
private final Map<String, Option<?>> mapping;
private final List<String> arguments;
public CommandLine(Collection<Option<?>> options) {
this.options = options;
this.mapping = new HashMap<>();
for (Option<?> option : options) {
mapping.put(option.getName(), option);
}
this.arguments = new ArrayList<>();
}
public void parse(String[] args) throws CommandLineException {
int i = 0;
while (i < args.length) {
if (args[i].startsWith(Option.PREFIX)) {
int argumentIndex = args[i].indexOf("=");
String name;
String argument = null;
if (argumentIndex > 0) {
name = args[i].substring(2, argumentIndex);
argument = args[i].substring(argumentIndex + 1);
} else {
name = args[i].substring(2);
}
Option<?> option = mapping.get(name);
if (option == null) {
throw new CommandLineException("Option " + Option.PREFIX + name + " not found");
}
match(option, argument);
} else {
arguments.add(args[i]);
}
i++;
}
}
public void match(Option<?> option, String argument) throws CommandLineException {
try {
switch (option.hasArgument()) {
case NO:
if (argument == null) {
option.match(null);
} else {
throw new CommandLineException("Option " + option + " is not allowed to have an argument");
}
break;
case OPTIONAL:
option.match(argument);
break;
case YES:
if (argument != null) {
option.match(argument);
} else {
throw new CommandLineException("Option " + option + " requires an argument");
}
break;
}
} catch (Option.IllegalMatchException e) {
throw new CommandLineException("Invalid value for option " + option + ": " + e.getMessage());
}
}
public List<String> getArguments() {
return arguments;
}
public String printOptionHelp() {
StringBuilder sb = new StringBuilder();
int longestOption = 0;
for (Option<?> option : options) {
if (longestOption < option.getName().length()) {
longestOption = option.getName().length();
}
}
for (Option<?> option : new TreeSet<>(options)) {
String s = String.format(" %s%-" + (longestOption + 6) + "s %s%n",
Option.PREFIX, option.getName(), option.getDescription());
sb.append(s);
}
return sb.toString();
}
public static class CommandLineException extends Exception {
private static final long serialVersionUID = 1L;
public CommandLineException(String message) {
super(message);
}
}
}
package org.jastadd.relast.compiler.options;
import java.util.Collection;
import java.util.TreeSet;
public class EnumOption extends Option<String> {
private final TreeSet<String> allowedValues;
private final String defaultValue;
private String value;
private boolean isSet;
public EnumOption(String name, String description, Collection<String> allowedValues, String defaultValue) {
super(name, description);
this.allowedValues = new TreeSet<>(allowedValues);
this.defaultValue = defaultValue;
this.value = defaultValue;
this.isSet = false;
}
public boolean addAllowedValue(String allowedValue) {
return allowedValues.add(allowedValue);
}
@Override
public String getValue() {
return value;
}
@Override
public Option.HasArgument hasArgument() {
return Option.HasArgument.OPTIONAL;
}
@Override
public void match(String argument) throws IllegalMatchException {
if (argument == null) {
isSet = true;
value = defaultValue;
} else if (allowedValues.contains(argument)) {
isSet = true;
value = argument;
} else {
throw new IllegalMatchException(argument
+ " is not allowed, allowed values are " + allowedValues);
}
}
@Override
public boolean isSet() {
return isSet;
}
@Override
public String getDescription() {
String allowedValuesStr = allowedValues.toString();
allowedValuesStr = allowedValuesStr.substring(1);
allowedValuesStr = allowedValuesStr.substring(0, allowedValuesStr.length() - 1);
return super.getDescription() + " (allowed values: " + allowedValuesStr + ")";
}
}
package org.jastadd.relast.compiler.options;
public class FlagOption extends Option<Boolean> {
private boolean value;
public FlagOption(String name, String description) {
super(name, description);
value = false;
}
@Override
public Boolean getValue() {
return value;
}
@Override
public Option.HasArgument hasArgument() {
return Option.HasArgument.NO;
}
@Override
public void match(String string) throws IllegalMatchException {
value = true;
}
@Override
public boolean isSet() {
return getValue();
}
}
package org.jastadd.relast.compiler.options;
abstract public class Option<ValueType> implements Comparable<Option<?>> {
public final static String PREFIX = "--";
private final String name;
private final String description;
public Option(String name, String description) {
this.name = name;
this.description = description;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
@Override
public int compareTo(Option<?> o) {
return name.compareTo(o.name);
}
@Override
public boolean equals(Object other) {
if (other instanceof Option) {
return compareTo((Option<?>) other) == 0;
}
return false;
}
@Override
public String toString() {
return PREFIX + name;
}
abstract public boolean isSet();
abstract public ValueType getValue();
abstract public HasArgument hasArgument();
abstract public void match(String input) throws IllegalMatchException;
public enum HasArgument {
NO,
OPTIONAL,
YES
}
public static class IllegalMatchException extends Exception {
private static final long serialVersionUID = 1L;
public IllegalMatchException(String message) {
super(message);
}
}
}
package org.jastadd.relast.compiler.options;
public class StringOption extends Option<String> {
private String value;
private boolean isSet;
public StringOption(String name, String description) {
this(name, description, "");
}
public StringOption(String name, String description, String defaultValue) {
super(name, description);
value = defaultValue;
isSet = false;
}
@Override
public String getValue() {
return value;
}
@Override
public Option.HasArgument hasArgument() {
return Option.HasArgument.YES;
}
@Override
public void match(String value) {
this.value = value;
isSet = true;
}
@Override
public boolean isSet() {
return isSet;
}
}
package org.jastadd.ros2rag.tests; package org.jastadd.ros2rag.tests;
import org.jastadd.relast.compiler.Compiler; import org.jastadd.relast.compiler.Compiler;
import org.jastadd.relast.compiler.options.CommandLine;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.io.File; import java.io.File;
...@@ -12,7 +11,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; ...@@ -12,7 +11,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
public class RelAstTest { public class RelAstTest {
void transform(String inputDir, String outputDir) throws CommandLine.CommandLineException, Compiler.CompilerException { void transform(String inputDir, String outputDir) throws Compiler.CompilerException {
System.out.println("Running test in directory '" + Paths.get(".").toAbsolutePath() + "'."); System.out.println("Running test in directory '" + Paths.get(".").toAbsolutePath() + "'.");
assertTrue(Paths.get(inputDir).toFile().exists(), "input directory does not exist"); assertTrue(Paths.get(inputDir).toFile().exists(), "input directory does not exist");
...@@ -37,7 +36,7 @@ public class RelAstTest { ...@@ -37,7 +36,7 @@ public class RelAstTest {
} }
@Test @Test
void transformMinimalExample() throws CommandLine.CommandLineException, Compiler.CompilerException { void transformMinimalExample() throws Compiler.CompilerException {
transform("src/test/resources/in", "src/test/resources/out"); transform("src/test/resources/in", "src/test/resources/out");
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment