-
Notifications
You must be signed in to change notification settings - Fork 3
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
Unroll PHI/UNPHI Transformations #720
Merged
rultor
merged 19 commits into
objectionary:master
from
volodya-lombrozo:705_enable_phi_unphi
Sep 24, 2024
Merged
Changes from 14 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
f9fdcd2
feat(#705): add CanonicalXmir
volodya-lombrozo 766aa0d
feat(#705): setup all the steps to transfrom canonical XMIR back to p…
volodya-lombrozo 75d6567
feat(#705): add XSL transformations to make unroll
volodya-lombrozo 71438bb
feat(#705): fix all the code offences
volodya-lombrozo 5727ab4
Merge branch 'master' into 705_enable_phi_unphi
volodya-lombrozo b4d21f3
feat(#705): add Equals and HashCode annotations to all domain classes
volodya-lombrozo 97c89e5
feat(#705): use Lists in BytecodeClass implementation
volodya-lombrozo a6f7c9b
feat(#705): add one more puzzle to enable CanonicalXmirTest
volodya-lombrozo 945be17
feat(#705): add UnrollMojo
volodya-lombrozo fa02c15
feat(#705): update 'phi-unphi' integration test
volodya-lombrozo 13d6aa4
feat(#705): 'phi-unphi' integration test: use unrolled xmir for assem…
volodya-lombrozo a0a31c9
feat(#705): don't remove files from the integration test
volodya-lombrozo a0434a5
feat(#705): remove redundant lines
volodya-lombrozo a117ad1
feat(#705): update roll-data.xsl transformation according with this c…
volodya-lombrozo 8ca4f7b
feat(#705): add file name for CanonicalXmir
volodya-lombrozo 7827d4c
feat(#705): fix all the code offences
volodya-lombrozo f40b5d5
feat(#705): fix xml offences in transformations
volodya-lombrozo 85a1d26
feat(#705): add Shift type for TrJoined
volodya-lombrozo 23abedb
feat(#705): remove the puzzle for #705 issue
volodya-lombrozo 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 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 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 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,134 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2016-2024 Objectionary.com | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included | ||
* in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package org.eolang.jeo; | ||
|
||
import com.jcabi.log.Logger; | ||
import com.jcabi.xml.XMLDocument; | ||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.stream.Stream; | ||
import org.apache.maven.plugin.AbstractMojo; | ||
import org.apache.maven.plugin.MojoFailureException; | ||
import org.apache.maven.plugins.annotations.LifecyclePhase; | ||
import org.apache.maven.plugins.annotations.Mojo; | ||
import org.apache.maven.plugins.annotations.Parameter; | ||
import org.eolang.jeo.representation.CanonicalXmir; | ||
|
||
/** | ||
* This mojo unrolls all the changes made by PHI/UNPHI transformations. | ||
* In other words, it makes XMIR understandable by jeo-maven-plugin after PHI/UNPHI transformations. | ||
* @since 0.6 | ||
*/ | ||
@Mojo(name = "unroll-phi", defaultPhase = LifecyclePhase.PROCESS_CLASSES) | ||
public final class UnrollMojo extends AbstractMojo { | ||
|
||
/** | ||
* Source directory. | ||
* | ||
* @since 0.6 | ||
* @checkstyle MemberNameCheck (6 lines) | ||
*/ | ||
@Parameter( | ||
property = "jeo.unroll-phi.sourcesDir", | ||
defaultValue = "${project.build.directory}/generated-sources/jeo-xmir" | ||
) | ||
private File sourcesDir; | ||
|
||
/** | ||
* Target directory. | ||
* | ||
* @since 0.6 | ||
* @checkstyle MemberNameCheck (6 lines) | ||
*/ | ||
@Parameter( | ||
property = "jeo.unroll-phi.outputDir", | ||
defaultValue = "${project.build.directory}/generated-sources/jeo-unrolled" | ||
) | ||
private File outputDir; | ||
|
||
@Override | ||
public void execute() throws MojoFailureException { | ||
try (Stream<Path> xmirs = Files.walk(this.sourcesDir.toPath())) { | ||
xmirs.filter(UnrollMojo::isXmir).forEach(this::unroll); | ||
} catch (final IOException exception) { | ||
throw new MojoFailureException( | ||
String.format( | ||
"Failed to unroll XMIR files from '%s' directory to '%s'", | ||
this.sourcesDir, | ||
this.outputDir | ||
), | ||
exception | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Unrolls the XMIR file. | ||
* @param path The path to the XMIR file. | ||
*/ | ||
private void unroll(final Path path) { | ||
try { | ||
final Path output = this.outputDir.toPath() | ||
.resolve(this.sourcesDir.toPath().relativize(path)); | ||
Logger.info(this, "Unrolling XMIR file '%s' to '%s'", path, output); | ||
Files.createDirectories(output.getParent()); | ||
Files.write( | ||
output, | ||
new CanonicalXmir(new XMLDocument(path)) | ||
.plain() | ||
.toString() | ||
.getBytes(StandardCharsets.UTF_8) | ||
); | ||
} catch (final FileNotFoundException exception) { | ||
throw new IllegalStateException( | ||
String.format( | ||
"Failed to unroll XMIR file '%s', most probably the file does not exist", | ||
path | ||
), | ||
exception | ||
); | ||
} catch (final IOException exception) { | ||
throw new IllegalStateException( | ||
String.format( | ||
"Failed to unroll XMIR file '%s', most probably an I/O error occurred", | ||
path | ||
), | ||
exception | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if the file is an XMIR file. | ||
* @param path The path to the file. | ||
* @return True if the file is an XMIR file, false otherwise. | ||
*/ | ||
private static boolean isXmir(final Path path) { | ||
return Files.isRegularFile(path) && path.toString().endsWith(".xmir"); | ||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
src/main/java/org/eolang/jeo/representation/CanonicalXmir.java
This file contains 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,120 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2016-2024 Objectionary.com | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included | ||
* in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package org.eolang.jeo.representation; | ||
|
||
import com.jcabi.xml.XML; | ||
import com.yegor256.xsline.StClasspath; | ||
import com.yegor256.xsline.StEndless; | ||
import com.yegor256.xsline.TrClasspath; | ||
import com.yegor256.xsline.TrDefault; | ||
import com.yegor256.xsline.TrJoined; | ||
import com.yegor256.xsline.Xsline; | ||
import java.io.IOException; | ||
import org.cactoos.io.InputOf; | ||
import org.eolang.parser.EoSyntax; | ||
import org.eolang.parser.xmir.Xmir; | ||
|
||
/** | ||
* Canonical XMIR. | ||
* The purpose of this class is to restore the original XMIR format after PHI/UNPHI transformations. | ||
* You can read more about it | ||
* <a href="https://github.com/objectionary/eo/issues/3373#issuecomment-2361337359">here</a>. | ||
* @since 0.6 | ||
*/ | ||
public final class CanonicalXmir { | ||
|
||
/** | ||
* Canonical XMIR after "phi/unphi" transformations. | ||
*/ | ||
private final XML canonical; | ||
|
||
/** | ||
* Constructor. | ||
* @param canonical Significantly modified XMIR after "phi/unphi". | ||
*/ | ||
public CanonicalXmir(final XML canonical) { | ||
this.canonical = canonical; | ||
} | ||
|
||
/** | ||
* Convert canonical XMIR to plain XMIR. | ||
* @return Plain XMIR. | ||
*/ | ||
public XML plain() { | ||
try { | ||
return CanonicalXmir.unroll(CanonicalXmir.parse(this.toEo())); | ||
} catch (final IOException exception) { | ||
throw new IllegalStateException( | ||
"Can't parse the canonical XMIR", | ||
exception | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Unroll all the changes made by the "phi/unphi" transformations. | ||
* @param parsed Parsed XMIR. | ||
* @return Unrolled XMIR. | ||
*/ | ||
private static XML unroll(final XML parsed) { | ||
return new Xsline( | ||
new TrJoined<>( | ||
new TrClasspath<>( | ||
"/org/eolang/parser/wrap-method-calls.xsl" | ||
).back(), | ||
new TrDefault<>( | ||
new StEndless( | ||
new StClasspath( | ||
"/org/eolang/parser/roll-bases.xsl" | ||
) | ||
) | ||
), | ||
new TrClasspath<>( | ||
"/org/eolang/parser/add-refs.xsl", | ||
"/org/eolang/parser/vars-float-down.xsl", | ||
"/org/eolang/parser/roll-data.xsl" | ||
).back() | ||
) | ||
).pass(parsed); | ||
} | ||
|
||
/** | ||
* Convert XMIR to EO. | ||
* @return EO. | ||
*/ | ||
private String toEo() { | ||
return new Xmir.Default(this.canonical).toEO(); | ||
} | ||
|
||
/** | ||
* Parse XMIR. | ||
* @param eoprog Eo program. | ||
* @return Parsed XMIR. | ||
* @throws IOException If fails. | ||
*/ | ||
private static XML parse(final String eoprog) throws IOException { | ||
return new EoSyntax("name", new InputOf(eoprog)).parsed(); | ||
} | ||
|
||
} |
This file contains 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
Oops, something went wrong.
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.
@volodya-lombrozo I think this is the problem of objectionary/eo#3373 (comment)
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.
@maxonfjvipon Please, read this issue objectionary/eo#3380 it's a bug in
eo
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.
@volodya-lombrozo I think the default constructor won't help you here because the
name
should taken from somewhere. Can't you set it manually from the file name?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.
@maxonfjvipon Please, extend this issue with your knowledge about file names objectionary/eo#3380