|
| 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 | +} |
0 commit comments