Skip to content
Snippets Groups Projects
Commit 2850be7f authored by René Schöne's avatar René Schöne
Browse files

import command line messages

- fix handling of absolute input files
- see #5
parent 86d0d611
No related branches found
No related tags found
No related merge requests found
...@@ -43,6 +43,7 @@ public class Mustache { ...@@ -43,6 +43,7 @@ public class Mustache {
Template template = handlebars.compile(templateFileName); Template template = handlebars.compile(templateFileName);
try (Writer w = new FileWriter(outputFileName)) { try (Writer w = new FileWriter(outputFileName)) {
System.out.println("Writing " + outputFileName);
template.apply(context, w); template.apply(context, w);
w.flush(); w.flush();
} }
......
...@@ -55,14 +55,14 @@ public abstract class RelAstProcessor extends AbstractCompiler { ...@@ -55,14 +55,14 @@ public abstract class RelAstProcessor extends AbstractCompiler {
inputBasePath = Paths.get(optionInputBaseDir.value()).toAbsolutePath(); inputBasePath = Paths.get(optionInputBaseDir.value()).toAbsolutePath();
} else { } else {
inputBasePath = Paths.get(".").toAbsolutePath(); inputBasePath = Paths.get(".").toAbsolutePath();
printMessage("No input base dir is set. Assuming current directory '" + inputBasePath.toAbsolutePath().toString() + "'."); printMessage("No input base dir is set. Assuming current directory '" + inputBasePath.toAbsolutePath() + "'.");
} }
if (!inputBasePath.toFile().exists()) { if (!inputBasePath.toFile().exists()) {
printMessage("Input path '" + inputBasePath.toAbsolutePath().toString() + "' does not exist. Exiting..."); printMessage("Input path '" + inputBasePath.toAbsolutePath() + "' does not exist. Exiting...");
System.exit(-1); System.exit(-1);
} else if (!inputBasePath.toFile().isDirectory()) { } else if (!inputBasePath.toFile().isDirectory()) {
printMessage("Input path '" + inputBasePath.toAbsolutePath().toString() + "' is not a directory. Exiting..."); printMessage("Input path '" + inputBasePath.toAbsolutePath() + "' is not a directory. Exiting...");
System.exit(-1); System.exit(-1);
} }
...@@ -74,29 +74,29 @@ public abstract class RelAstProcessor extends AbstractCompiler { ...@@ -74,29 +74,29 @@ public abstract class RelAstProcessor extends AbstractCompiler {
} }
if (outputBasePath.toFile().exists() && !outputBasePath.toFile().isDirectory()) { if (outputBasePath.toFile().exists() && !outputBasePath.toFile().isDirectory()) {
printMessage("Output path '" + inputBasePath.toAbsolutePath().toString() + "' exists, but is not a directory. Exiting..."); printMessage("Output path '" + inputBasePath.toAbsolutePath() + "' exists, but is not a directory. Exiting...");
} }
printMessage("Running " + getName()); printMessage("Running " + getName());
// gather all files // gather all files
Collection<Path> inputFiles = new ArrayList<>(); Collection<Path> inputFiles = new ArrayList<>();
getConfiguration().getFiles().forEach(name -> relativizeFileName(inputBasePath, Paths.get(name)).ifPresent(inputFiles::add)); getConfiguration().getFiles().forEach(name -> checkFileName(inputBasePath, Paths.get(name)).ifPresent(inputFiles::add));
Program program = parseProgram(inputFiles); Program program = parseProgram(inputBasePath, inputFiles);
return processGrammar(program, inputBasePath, outputBasePath); return processGrammar(program, inputBasePath, outputBasePath);
} }
protected abstract int processGrammar(Program program, Path inputBasePath, Path outputBasePath) throws CompilerException; protected abstract int processGrammar(Program program, Path inputBasePath, Path outputBasePath) throws CompilerException;
private Optional<Path> relativizeFileName(Path inputBasePath, Path filePath) { private Optional<Path> checkFileName(Path inputBasePath, Path filePath) {
if (filePath.isAbsolute()) { if (filePath.isAbsolute()) {
if (filePath.startsWith(inputBasePath)) { if (filePath.normalize().startsWith(inputBasePath.normalize())) {
return Optional.of(filePath.relativize(inputBasePath)); return Optional.of(filePath);
} else { } else {
printMessage("Path '" + filePath + "' is not contained in the base path '" + inputBasePath + "'."); printMessage("Path '" + filePath + "' is not contained in the base path '" + inputBasePath + "', ignoring it.");
return Optional.empty(); return Optional.empty();
} }
} else { } else {
...@@ -118,7 +118,7 @@ public abstract class RelAstProcessor extends AbstractCompiler { ...@@ -118,7 +118,7 @@ public abstract class RelAstProcessor extends AbstractCompiler {
} }
} }
private Program parseProgram(Collection<Path> inputFiles) { private Program parseProgram(Path inputBasePath, Collection<Path> inputFiles) {
Program program = new Program(); Program program = new Program();
RelAstParser parser = new RelAstParser(); RelAstParser parser = new RelAstParser();
...@@ -128,7 +128,7 @@ public abstract class RelAstProcessor extends AbstractCompiler { ...@@ -128,7 +128,7 @@ public abstract class RelAstProcessor extends AbstractCompiler {
try (BufferedReader reader = Files.newBufferedReader(path)) { try (BufferedReader reader = Files.newBufferedReader(path)) {
RelAstScanner scanner = new RelAstScanner(reader); RelAstScanner scanner = new RelAstScanner(reader);
GrammarFile inputGrammar = (GrammarFile) parser.parse(scanner); GrammarFile inputGrammar = (GrammarFile) parser.parse(scanner);
inputGrammar.setFileName(path.toString()); inputGrammar.setFileName(inputBasePath.relativize(path).toString());
program.addGrammarFile(inputGrammar); program.addGrammarFile(inputGrammar);
} catch (IOException | beaver.Parser.Exception e) { } catch (IOException | beaver.Parser.Exception e) {
printMessage("Could not parse grammar file " + path); printMessage("Could not parse grammar file " + path);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment