Skip to content

Commit d615015

Browse files
committed
try to fix the formatting
1 parent b98271f commit d615015

File tree

6 files changed

+46
-23
lines changed

6 files changed

+46
-23
lines changed

key.format/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ This module provide a pretty-printer / formatting utility for KeY files.
55
Usage:
66

77
```
8-
./gradlew :key.format:format
8+
./gradlew :key.format:formatKey
99
```
1010

1111
which formats the rule base of KeY.

key.format/build.gradle

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,11 @@ tasks.register('formatKey', JavaExec) {
2929
workingDir(rootDir)
3030
classpath = sourceSets.main.runtimeClasspath
3131
args("format", "key.core/src/main/resources/de/uka/ilkd/key/proof/rules")
32+
}
33+
34+
tasks.register('formatKeyCheck', JavaExec) {
35+
mainClass = mainClassName
36+
workingDir(rootDir)
37+
classpath = sourceSets.main.runtimeClasspath
38+
args("check", "key.core/src/main/resources/de/uka/ilkd/key/proof/rules")
3239
}

key.format/src/main/java/de/uka/ilkd/key/nparser/format/ExpressionVisitor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ public Void visitTerminal(TerminalNode node) {
120120
token == KeYLexer.AVOID ||
121121
token == KeYLexer.EXISTS ||
122122
token == KeYLexer.FORALL ||
123-
token == KeYLexer.SEMI)) {
123+
token == KeYLexer.SEMI)
124+
|| token==KeYLexer.IFEX
125+
|| token==KeYLexer.IDENT) {
124126
output.spaceBeforeNext();
125127
}
126128

key.format/src/main/java/de/uka/ilkd/key/nparser/format/KeyFileFormatter.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ public Void visitTerminal(TerminalNode node) {
383383
if (token == KeYLexer.DOC_COMMENT) {
384384
processIndentationInMLComment(text, output);
385385
} else {
386-
output.token(text);
386+
output.token(node.getSymbol());
387387
}
388388

389389
if (isLBrace) {
@@ -412,12 +412,9 @@ private static int countNLs(String text) {
412412
*/
413413
public static String format(CharStream stream) {
414414
// weigl: Not necessary is handled within the lexer
415-
// var in = CharStreams.fromString(text.replaceAll("\\r\\n?", "\n"));
415+
//var in = CharStreams.fromString(text.replaceAll("\\r\\n?", "\n"));
416416

417417
var lexer = ParsingFacade.createLexer(stream);
418-
// weigl: Should not be necessary
419-
// lexer.setTokenFactory(new CommonTokenFactory(true));
420-
421418
CommonTokenStream tokens = new CommonTokenStream(lexer);
422419
tokens.fill();
423420

key.format/src/main/java/de/uka/ilkd/key/nparser/format/KeyFormatFacade.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
import java.io.IOException;
1212
import java.nio.file.Files;
1313
import java.nio.file.Path;
14+
import java.sql.SQLOutput;
1415
import java.util.ArrayList;
1516
import java.util.List;
1617
import java.util.concurrent.Callable;
1718

19+
import de.uka.ilkd.key.util.KeYConstants;
1820
import de.uka.ilkd.key.util.parsing.SyntaxErrorReporter;
1921

2022
import org.antlr.v4.runtime.CharStreams;
@@ -51,7 +53,7 @@ public class KeyFormatFacade {
5153
public static boolean checkFile(Path file) throws IOException {
5254
var formatted = format(CharStreams.fromPath(file));
5355
var content = Files.readString(file);
54-
return !content.equals(formatted);
56+
return content.equals(formatted);
5557
}
5658

5759

@@ -97,10 +99,12 @@ private static class Format implements Callable<Integer> {
9799

98100
@Override
99101
public Integer call() throws IOException {
102+
System.out.println("Formatting ...");
100103
var files = expandPaths(paths);
101104

102105
for (var file : files) {
103106
try {
107+
System.out.printf("Formatting %s\n",file);
104108
formatSingleFileTo(file);
105109
} catch (SyntaxErrorReporter.ParserException e) {
106110
LOGGER.error("{} | Parser error", file, e);
@@ -131,13 +135,19 @@ private static class Check implements Callable<Integer> {
131135

132136
@Override
133137
public Integer call() throws IOException {
138+
System.out.println("Checking ... ");
134139
var valid = true;
135140
var files = expandPaths(paths);
136141
for (Path file : files) {
137142
try {
138-
if (checkFile(file)) {
139-
valid = false;
140-
LOGGER.warn("{} | File is not formatted properly", file);
143+
System.out.printf("Checking file %s", file);
144+
final var b = checkFile(file);
145+
valid &= b;
146+
if (b) {
147+
System.out.printf(" ... ok\n");
148+
} else {
149+
System.out.printf(" ... not ok\n");
150+
formatSingleFile(file, file.getParent().resolve(file.getFileName().toString() + ".formatted"));
141151
}
142152
} catch (SyntaxErrorReporter.ParserException e) {
143153
LOGGER.error("{} | Syntax errors in file", file, e);
@@ -152,6 +162,7 @@ private static class Cmd {
152162
}
153163

154164
public static void main(String[] args) {
165+
System.out.println("KeY formatter. " + KeYConstants.VERSION);
155166
int exitCode = new CommandLine(new Cmd())
156167
.addSubcommand(new Format())
157168
.addSubcommand(new Check())

key.format/src/main/java/de/uka/ilkd/key/nparser/format/Output.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
* SPDX-License-Identifier: GPL-2.0-only */
44
package de.uka.ilkd.key.nparser.format;
55

6+
import de.uka.ilkd.key.nparser.KeYLexer;
7+
import org.antlr.v4.runtime.Token;
8+
69
/**
710
* Output class for managing formatted output with indentation.
8-
*
11+
* <p>
912
* This class provides methods to manage and format output with proper
1013
* indentation and spacing. It supports adding tokens, characters, and
1114
* handling new lines and indentation levels.
@@ -63,26 +66,29 @@ public void noSpaceBeforeNext() {
6366
this.spaceBeforeNextToken = false;
6467
}
6568

66-
/**
67-
* Add a token to the output. Respects whitespace before token.
68-
*
69-
* @param value a string value
70-
*/
71-
public void token(String value) {
69+
70+
public void token(Token value) {
7271
checkBeforeToken();
73-
output.append(value);
72+
if (lastTokenId == KeYLexer.IDENT && value.getType() == KeYLexer.IDENT) {
73+
spaceBeforeNext();
74+
}
75+
output.append(value.getText());
76+
lastTokenId = value.getType();
7477
}
7578

7679
/**
77-
* Add a character to the output. Respects whitespace before token.
80+
* Add a token to the output. Respects whitespace before token.
7881
*
79-
* @param value a char value
82+
* @param text a string value
8083
*/
81-
public void token(char value) {
84+
public void token(String text) {
8285
checkBeforeToken();
83-
output.append(value);
86+
output.append(text);
87+
lastTokenId = 0;
8488
}
8589

90+
private int lastTokenId;
91+
8692
/**
8793
* Increases the indentation level.
8894
*/

0 commit comments

Comments
 (0)