Skip to content

Commit e15c746

Browse files
committed
A tool for rewriting the settings in KeY files to the new JSON-like format.
1 parent 7750e75 commit e15c746

4 files changed

Lines changed: 106 additions & 2 deletions

File tree

key.core/src/main/antlr4/KeYLexer.g4

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ lexer grammar KeYLexer;
4040
}
4141

4242
private Token tokenBackStorage = null;
43+
private boolean proofIsEOF = true;
44+
public void setProofIsEOF(boolean b) { proofIsEOF = b;}
45+
public boolean isProofIsEOF() { return proofIsEOF;}
46+
4347
@Override
4448
public void emit(Token token) {
4549
int MAX_K = 10;
@@ -51,7 +55,7 @@ lexer grammar KeYLexer;
5155
break;
5256
}
5357
}
54-
if(token.getType() == PROOF) {
58+
if(token.getType() == PROOF && isProofIsEOF()) {
5559
tokenBackStorage = super.emitEOF();
5660
//will later be overwritten the EOF token
5761
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/* This file is part of KeY - https://key-project.org
2+
* KeY is licensed under the GNU General Public License Version 2
3+
* SPDX-License-Identifier: GPL-2.0-only */
4+
package de.uka.ilkd.key;
5+
6+
import java.io.IOException;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
import java.nio.file.Paths;
10+
import java.util.Collections;
11+
import java.util.Iterator;
12+
13+
import de.uka.ilkd.key.nparser.KeYLexer;
14+
import de.uka.ilkd.key.nparser.ParsingFacade;
15+
import de.uka.ilkd.key.settings.ProofSettings;
16+
17+
import org.antlr.v4.runtime.CharStreams;
18+
import org.antlr.v4.runtime.Token;
19+
import org.antlr.v4.runtime.misc.ParseCancellationException;
20+
21+
/**
22+
* @author Alexander Weigl
23+
* @version 1 (4/6/25)
24+
*/
25+
public class RewriteSettings {
26+
public static void main(String[] args) throws IOException {
27+
if (args.length == 0) {
28+
args = new String[] {
29+
"key.core/src/test/resources/testcase/parser/MultipleRecursion/MultipleRecursion[MultipleRecursion__a()]_JML_normal_behavior_operation_contract_0.proof" };
30+
}
31+
32+
for (String arg : args) {
33+
var path = Paths.get(arg);
34+
var files = Files.isDirectory(
35+
path) ? Files.walk(path).filter(it -> Files.isRegularFile(it)
36+
&& (it.getFileName().toString().endsWith(".key") ||
37+
it.getFileName().toString().endsWith(".proof")))
38+
.toList()
39+
: Collections.singletonList(path);
40+
for (var file : files) {
41+
rewrite(file);
42+
}
43+
44+
}
45+
}
46+
47+
private static void rewrite(Path file) throws IOException {
48+
var lex = ParsingFacade.createLexer(file);
49+
lex.setProofIsEOF(false);
50+
var ctx = lex.getAllTokens();
51+
var output = new StringBuilder();
52+
53+
boolean hit = false;
54+
for (Iterator<? extends Token> iterator = ctx.iterator(); iterator.hasNext();) {
55+
var token = iterator.next();
56+
if (token.getType() == KeYLexer.KEYSETTINGS) {
57+
output.append(token.getText());
58+
59+
while (iterator.hasNext() && token.getType() != KeYLexer.STRING_LITERAL) {
60+
token = iterator.next();
61+
}
62+
63+
if (token.getType() != KeYLexer.STRING_LITERAL) {
64+
return;
65+
}
66+
67+
hit = true;
68+
69+
final var text = token.getText();
70+
var settings = new ProofSettings(ProofSettings.DEFAULT_SETTINGS);
71+
settings.loadSettingsFromPropertyString(text.substring(1, text.length() - 1));
72+
output.append(settings.settingsToString());
73+
74+
while (iterator.hasNext() && token.getType() != KeYLexer.RBRACE) {
75+
token = iterator.next();
76+
}
77+
} else {
78+
output.append(token.getText());
79+
}
80+
}
81+
82+
if (!hit) {
83+
System.err.printf("No settings in file %s found%n", file);
84+
return;
85+
}
86+
87+
try {
88+
ParsingFacade.parseFile(CharStreams.fromString(output.toString()));
89+
Files.writeString(file, output.toString());
90+
} catch (ParseCancellationException e) {
91+
System.err.printf("Error parsing after rewrite file %s: %s", file, e.getMessage());
92+
System.err.println(output);
93+
System.exit(1);
94+
}
95+
}
96+
}

key.core/src/main/java/de/uka/ilkd/key/settings/Configuration.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,10 @@ public ConfigurationWriter printIndent() {
466466
}
467467

468468
public ConfigurationWriter printComment(String comment) {
469+
if (comment == null || comment.isBlank()) {
470+
return this;
471+
}
472+
469473
if (comment.contains("\n")) {
470474
out.format("/* %s */\n", comment);
471475
} else {

key.core/src/main/java/de/uka/ilkd/key/settings/ProofSettings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public Configuration getConfiguration() {
136136
* Used by saveSettings() and settingsToString()
137137
*/
138138
public void settingsToStream(Writer out) {
139-
getConfiguration().save(out, "Proof-Settings-Config-File");
139+
getConfiguration().save(out, "");
140140
}
141141

142142
/**

0 commit comments

Comments
 (0)