-
Notifications
You must be signed in to change notification settings - Fork 42
Support for TextBlockLiterals #3796
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wadoon
wants to merge
1
commit into
main
Choose a base branch
from
weigl/textblock
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...re/src/main/java/de/uka/ilkd/key/java/transformations/pipeline/SimpleJavaTransformer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* This file is part of KeY - https://key-project.org | ||
| * KeY is licensed under the GNU General Public License Version 2 | ||
| * SPDX-License-Identifier: GPL-2.0-only */ | ||
| package de.uka.ilkd.key.java.transformations.pipeline; | ||
|
|
||
|
|
||
| import com.github.javaparser.ast.CompilationUnit; | ||
| import com.github.javaparser.ast.Node; | ||
| import com.github.javaparser.ast.body.TypeDeclaration; | ||
| import org.jspecify.annotations.NullMarked; | ||
|
|
||
| /** | ||
| * A transformation of Java ASTs in an early loading stage. | ||
| * <p> | ||
| * A {@link SimpleJavaTransformer} defines a transformation on mutable {@link Node}. | ||
| * As an entry an transformer receives the compilation unit. Traversal of it is left to the | ||
| * implementation of the transformer. The code should be transformed in-place. Creation of new | ||
| * {@link CompilationUnit} | ||
| * is not possible, but the creation of new {@link TypeDeclaration} | ||
| * | ||
| * Implementation should be stateless as instances are reused accross {@link CompilationUnit}s but | ||
| * not across | ||
| * loading requests. | ||
| * | ||
| * @author weigl | ||
| */ | ||
| @NullMarked | ||
| public interface SimpleJavaTransformer { | ||
| default void apply(TypeDeclaration<?> td) { | ||
| } | ||
|
|
||
| default void apply(CompilationUnit cu) { | ||
| for (TypeDeclaration<?> type : cu.getTypes()) { | ||
| apply(type); | ||
| for (var m : type.getMembers()) { | ||
| if (m instanceof TypeDeclaration<?> ty) { | ||
| apply(ty); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
26 changes: 26 additions & 0 deletions
26
...ore/src/main/java/de/uka/ilkd/key/java/transformations/pipeline/TextblockTransformer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* This file is part of KeY - https://key-project.org | ||
| * KeY is licensed under the GNU General Public License Version 2 | ||
| * SPDX-License-Identifier: GPL-2.0-only */ | ||
| package de.uka.ilkd.key.java.transformations.pipeline; | ||
|
|
||
| import com.github.javaparser.ast.body.TypeDeclaration; | ||
| import com.github.javaparser.ast.expr.StringLiteralExpr; | ||
| import com.github.javaparser.ast.expr.TextBlockLiteralExpr; | ||
|
|
||
| /** | ||
| * | ||
| * @author Alexander Weigl | ||
| * @version 1 (3/31/26) | ||
| */ | ||
| public class TextblockTransformer implements SimpleJavaTransformer { | ||
| @Override | ||
| public void apply(TypeDeclaration<?> td) { | ||
| for (var textblock : td.findAll(TextBlockLiteralExpr.class)) { | ||
| var s = textblock.getValue().stripIndent().translateEscapes() | ||
| .replace("\\\n", "") | ||
| .replace("\"", "\\\""); | ||
| var literal = new StringLiteralExpr(s); | ||
| textblock.replace(literal); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...src/test/java/de/uka/ilkd/key/java/transformations/pipeline/TextblockTransformerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* This file is part of KeY - https://key-project.org | ||
| * KeY is licensed under the GNU General Public License Version 2 | ||
| * SPDX-License-Identifier: GPL-2.0-only */ | ||
| package de.uka.ilkd.key.java.transformations.pipeline; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
|
|
||
| import org.key_project.util.java.IOUtil; | ||
|
|
||
| import com.github.javaparser.JavaParser; | ||
| import com.github.javaparser.ParseResult; | ||
| import com.github.javaparser.ParserConfiguration; | ||
| import com.github.javaparser.ast.CompilationUnit; | ||
| import com.google.common.truth.Truth; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.fail; | ||
|
|
||
| /** | ||
| * | ||
| * @author Alexander Weigl | ||
| * @version 1 (3/31/26) | ||
| */ | ||
| class TextblockTransformerTest { | ||
| @Test | ||
| void transform() throws IOException { | ||
| final InputStream expected = getClass().getResourceAsStream("Textblock.expected.java"); | ||
| final InputStream input = getClass().getResourceAsStream("Textblock.java"); | ||
| assertNotNull(input); | ||
| assertNotNull(expected); | ||
|
|
||
| JavaParser parser = new JavaParser(); | ||
| parser.getParserConfiguration().setLanguageLevel(ParserConfiguration.LanguageLevel.RAW); | ||
|
|
||
| ParseResult<CompilationUnit> result = parser.parse(input); | ||
| if (!result.isSuccessful()) { | ||
| result.getProblems().forEach(System.err::println); | ||
| fail("Parsing of fixture failed."); | ||
| } | ||
|
|
||
|
|
||
| CompilationUnit cu = result.getResult().get(); | ||
|
|
||
| TextblockTransformer transformer = new TextblockTransformer(); | ||
| transformer.apply(cu); | ||
|
|
||
| String expectedJava = IOUtil.readFrom(expected); | ||
| Truth.assertThat(cu.toString()).isEqualTo(expectedJava); | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
.../src/test/resources/de/uka/ilkd/key/java/transformations/pipeline/Textblock.expected.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| public class Textblock { | ||
|
|
||
| // Expected One large string w/o newlines | ||
| String text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."; | ||
|
|
||
| // Expected: strip indentation and new lines. Four spaces before <body>. | ||
| String html = "<html>\n <body>\n <p>Hello, world</p>\n </body>\n</html>\n"; | ||
|
|
||
| // Expected: String with newlines and escaped quotes. | ||
| String query = "SELECT \"EMP_ID\", \"LAST_NAME\" FROM \"EMPLOYEE_TB\"\nWHERE \"CITY\" = 'INDIANAPOLIS'\nORDER BY \"EMP_ID\", \"LAST_NAME\";\n"; | ||
|
|
||
| Object obj = ("function hello() {\n print('\"Hello, world\"');\n}") + "hello();\n"; | ||
|
|
||
| //expected "line 1\nlines 2\nline 3\n" | ||
| String simple = "line 1\nline 2\nline 3\n"; | ||
|
|
||
| //Expected: newlines and escaped quotes | ||
| String code = "String text = \"\"\"\n A text block inside a text block\n\"\"\";\n"; | ||
|
|
||
| String abc = (" 1 \"\n 2 \"\"\n 3 \"\"\"\n 4 \"\"\"\"\n 5 \"\"\"\"\"\n 6 \"\"\"\"\"\"\n 7 \"\"\"\"\"\"\"\n 8 \"\"\"\"\"\"\"\"\n 9 \"\"\"\"\"\"\"\"\"\n 10 \"\"\"\"\"\"\"\"\"\"\n 11 \"\"\"\"\"\"\"\"\"\"\"\n 12 \"\"\"\"\"\"\"\"\"\"\"\"\n"); | ||
|
|
||
| String text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."; | ||
|
|
||
| String colors = "red \ngreen \nblue \n"; | ||
|
|
||
| // attention trailing whitespaces! | ||
| String colors = "red\ngreen\nblue\n"; | ||
| } |
83 changes: 83 additions & 0 deletions
83
key.core/src/test/resources/de/uka/ilkd/key/java/transformations/pipeline/Textblock.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| public class Textblock { | ||
|
|
||
| // Expected One large string w/o newlines | ||
| String text = """ | ||
| Lorem ipsum dolor sit amet, consectetur adipiscing \ | ||
| elit, sed do eiusmod tempor incididunt ut labore \ | ||
| et dolore magna aliqua.\ | ||
| """; | ||
|
|
||
| // Expected: strip indentation and new lines. Four spaces before <body>. | ||
| String html = """ | ||
| <html> | ||
| <body> | ||
| <p>Hello, world</p> | ||
| </body> | ||
| </html> | ||
| """; | ||
|
|
||
| // Expected: String with newlines and escaped quotes. | ||
| String query = """ | ||
| SELECT "EMP_ID", "LAST_NAME" FROM "EMPLOYEE_TB" | ||
| WHERE "CITY" = 'INDIANAPOLIS' | ||
| ORDER BY "EMP_ID", "LAST_NAME"; | ||
| """; | ||
|
|
||
| Object obj = (""" | ||
| function hello() { | ||
| print('"Hello, world"'); | ||
| }""")+ | ||
| """ | ||
| hello(); | ||
| """; | ||
|
|
||
| //expected "line 1\nlines 2\nline 3\n" | ||
| String simple = """ | ||
| line 1 | ||
| line 2 | ||
| line 3 | ||
| """; | ||
|
|
||
| //Expected: newlines and escaped quotes | ||
| String code = | ||
| """ | ||
| String text = \""" | ||
| A text block inside a text block | ||
| \"""; | ||
| """; | ||
|
|
||
| String abc = (""" | ||
| 1 " | ||
| 2 "" | ||
| 3 ""\" | ||
| 4 ""\"" | ||
| 5 ""\""" | ||
| 6 ""\"""\" | ||
| 7 ""\"""\"" | ||
| 8 ""\"""\""" | ||
| 9 ""\"""\"""\" | ||
| 10 ""\"""\"""\"" | ||
| 11 ""\"""\"""\""" | ||
| 12 ""\"""\"""\"""\" | ||
| """); | ||
|
|
||
| String text = """ | ||
| Lorem ipsum dolor sit amet, consectetur adipiscing \ | ||
| elit, sed do eiusmod tempor incididunt ut labore \ | ||
| et dolore magna aliqua.\ | ||
| """; | ||
|
|
||
| String colors = """ | ||
| red \s | ||
| green\s | ||
| blue \s | ||
| """; | ||
|
|
||
| // attention trailing whitespaces! | ||
| String colors = """ | ||
| red | ||
| green | ||
| blue | ||
| """; | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would
TypeDeclarationTransformerbe a better name?Simpleis not really expressive