Skip to content
Snippets Groups Projects
Commit 26fe2558 authored by Jan Gruteser's avatar Jan Gruteser
Browse files

simplify file creation in Translator

parent 2e7d2fca
No related branches found
No related tags found
No related merge requests found
Pipeline #144922 passed
......@@ -39,6 +39,8 @@ import java.util.List;
import java.util.stream.Collectors;
public class Translator implements TranslationGlobals {
private static final String GENERATED_BY_TLA2B_HEADER = "/*@ generated by TLA2B ";
private String parentPath;
private File moduleFile;
private File configFile;
......@@ -106,14 +108,11 @@ public class Translator implements TranslationGlobals {
dir.mkdirs();
dir.deleteOnExit();
try {
moduleFile = new File("temp/" + moduleName + ".tla");
moduleFile.createNewFile();
try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(moduleFile.toPath()), StandardCharsets.UTF_8))) {
try (BufferedWriter out = Files.newBufferedWriter(moduleFile.toPath(), StandardCharsets.UTF_8)) {
out.write(moduleString);
}
} catch (IOException e) {
e.printStackTrace();
System.err.println("Error while writing file '" + moduleFile.getAbsolutePath() + "':\n" + e.getMessage());
}
}
......@@ -121,14 +120,10 @@ public class Translator implements TranslationGlobals {
modelConfig = null;
if (configString != null) {
configFile = new File("temp/" + moduleName + ".cfg");
try {
configFile.createNewFile();
try (BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(Files.newOutputStream(configFile.toPath()), StandardCharsets.UTF_8))) {
try (BufferedWriter out = Files.newBufferedWriter(configFile.toPath(), StandardCharsets.UTF_8)) {
out.write(configString);
}
} catch (IOException e) {
e.printStackTrace();
System.err.println("Error while writing file '" + configFile.getAbsolutePath() + "':\n" + e.getMessage());
}
}
}
......@@ -210,44 +205,26 @@ public class Translator implements TranslationGlobals {
}
public void createProbFile() {
String fileName = FileUtils.removeExtension(moduleFile.getAbsolutePath());
fileName = fileName + ".prob";
File probFile;
probFile = new File(fileName);
try {
probFile.createNewFile();
} catch (IOException e) {
System.err.printf("Could not create File %s.%n", probFile.getName());
System.exit(-1);
}
File probFile = new File(FileUtils.removeExtension(moduleFile.getAbsolutePath()) + ".prob");
BParser bParser = new BParser();
try {
try (BufferedWriter outWriter = Files.newBufferedWriter(probFile.toPath(), StandardCharsets.UTF_8)) {
bParser.getDefinitions().addDefinitions(getBDefinitions());
final RecursiveMachineLoader rml = parseAllMachines(getBAST(), getModuleFile(), bParser);
PrintWriter outWriter = new PrintWriter(probFile, "UTF-8");
rml.printAsProlog(new PrologTermOutput(outWriter, false));
outWriter.close();
System.out.println(probFile.getAbsolutePath() + " created.");
} catch (BCompoundException | FileNotFoundException | PreParseException | UnsupportedEncodingException e) {
} catch (BCompoundException | IOException | PreParseException e) {
System.err.println(e.getMessage());
System.exit(-1);
}
}
public void createMachineFile() {
String bFile = FileUtils.removeExtension(moduleFile.getAbsolutePath()) + "_tla.txt";
File machineFile = new File(bFile);
File machineFile = new File(FileUtils.removeExtension(moduleFile.getAbsolutePath()) + "_tla.txt");
if (machineFile.exists()) {
try {
BufferedReader in = new BufferedReader(new FileReader(machineFile));
try (BufferedReader in = new BufferedReader(new FileReader(machineFile))) {
String firstLine = in.readLine();
in.close();
if (firstLine != null && !firstLine.startsWith("/*@ generated by TLA2B ")) {
if (firstLine != null && !firstLine.startsWith(GENERATED_BY_TLA2B_HEADER)) {
System.err.println("Error: File " + machineFile.getName() + " already exists"
+ " and was not generated by TLA2B.\n" + "Delete or move this file.");
System.exit(-1);
......@@ -258,28 +235,16 @@ public class Translator implements TranslationGlobals {
}
}
try {
machineFile.createNewFile();
} catch (IOException e) {
System.err.printf("Could not create File %s.%n", machineFile.getName());
System.exit(-1);
}
PrettyPrinter pp = new PrettyPrinter();
pp.setRenaming(new SuffixIdentifierRenaming());
getBAST().apply(pp);
String result = "/*@ generated by TLA2B " + VERSION_NUMBER + " */\n" + pp.getPrettyPrint();
try {
try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(machineFile.toPath()), StandardCharsets.UTF_8))) {
out.write(result);
}
try (BufferedWriter out = Files.newBufferedWriter(machineFile.toPath(), StandardCharsets.UTF_8)) {
out.write(GENERATED_BY_TLA2B_HEADER + VERSION_NUMBER + " */\n" + pp.getPrettyPrint());
System.out.println("B-Machine " + machineFile.getAbsolutePath() + " created.");
} catch (IOException e) {
System.err.println("Error while creating file '" + machineFile.getAbsolutePath() + "'.");
System.err.println("Error while creating file '" + machineFile.getAbsolutePath() + "':\n" + e.getMessage());
System.exit(-1);
}
}
public RecursiveMachineLoader parseAllMachines(final Start ast, final File f, final BParser bparser) throws BCompoundException {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment