Skip to content

Commit

Permalink
fix(#2778): PhiMojo with train
Browse files Browse the repository at this point in the history
  • Loading branch information
maxonfjvipon committed Jan 16, 2024
1 parent 37633ac commit ec87153
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
40 changes: 22 additions & 18 deletions eo-maven-plugin/src/main/java/org/eolang/maven/PhiMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,28 @@
package org.eolang.maven;

import com.jcabi.log.Logger;
import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import com.jcabi.xml.XSLDocument;
import com.yegor256.xsline.Shift;
import com.yegor256.xsline.StClasspath;
import com.yegor256.xsline.Train;
import com.yegor256.xsline.Xsline;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.cactoos.Text;
import org.cactoos.experimental.Threads;
import org.cactoos.io.ResourceOf;
import org.cactoos.iterable.Mapped;
import org.cactoos.number.SumOf;
import org.cactoos.text.Sticky;
import org.cactoos.text.TextOf;
import org.eolang.maven.util.HmBase;
import org.eolang.maven.util.Home;
import org.eolang.maven.util.Rel;
import org.eolang.maven.util.Walk;
import org.eolang.parser.ParsingTrain;
import org.eolang.parser.Schema;

/**
* Read XMIR files and translate them to the phi-calculus expression.
Expand All @@ -55,18 +58,14 @@
)
public final class PhiMojo extends SafeMojo {
/**
* Extension of the file where we put phi-calculus expression (.phi).
* Transformations train.
*/
public static final String EXT = "phi";
private static final Train<Shift> TRAIN = new ParsingTrain();

/**
* Translation to phi.
* Extension of the file where we put phi-calculus expression (.phi).
*/
private static final Text TRANSLATION = new Sticky(
new TextOf(
new ResourceOf("org/eolang/maven/phi/to-phi.xsl")
)
);
public static final String EXT = "phi";

/**
* The directory where to take xmir files for translation from.
Expand Down Expand Up @@ -98,13 +97,17 @@ public void exec() {
Runtime.getRuntime().availableProcessors(),
new Mapped<>(
xmir -> () -> {
final XML xml = new XMLDocument(
new TextOf(xmir).asString()
);
new Schema(xml).check();
final Path relative = Paths.get(
this.phiInputDir.toPath().relativize(xmir).toString().replace(
String.format(".%s", TranspileMojo.EXT),
String.format(".%s", PhiMojo.EXT)
)
);
home.save(PhiMojo.translated(new TextOf(xmir)), relative);
home.save(PhiMojo.translated(xml), relative);
Logger.info(
this,
"Translated to phi: %s -> %s",
Expand Down Expand Up @@ -133,11 +136,12 @@ count, new Rel(this.phiInputDir), new Rel(this.phiOutputDir)
* Translate given xmir to phi calculus expression.
* @param xmir Text of xmir
* @return Translated xmir
* @throws Exception If fail to translate
*/
private static String translated(final Text xmir) throws Exception {
return new XSLDocument(PhiMojo.TRANSLATION.asString()).applyTo(
new XMLDocument(xmir.asString())
);
private static String translated(final XML xmir) {
return new Xsline(
PhiMojo.TRAIN.with(
new StClasspath("/org/eolang/maven/phi/to-phi.xsl")
)
).pass(xmir).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ SOFTWARE.
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:eo="https://www.eolang.org" id="to-phi" version="2.0">
<xsl:output encoding="UTF-8" method="text"/>
<xsl:import href="/org/eolang/parser/_datas.xsl"/>
<!-- Variables -->
<xsl:variable name="aliases" select="program/metas/meta/part[last()]"/>
<xsl:variable name="xi">
Expand Down Expand Up @@ -276,6 +277,12 @@ SOFTWARE.
</xsl:choose>
<!-- Data -->
<xsl:if test="@data">
<xsl:if test="$literal-objects/text()!=concat('org.eolang.',@data)">
<xsl:message terminate="yes">
<xsl:text>Invalid value in data attribute: </xsl:text>
<xsl:value-of select="@data"/>
</xsl:message>
</xsl:if>
<xsl:text>(</xsl:text>
<xsl:value-of select="$delta"/>
<xsl:value-of select="$dashed-arrow"/>
Expand Down

0 comments on commit ec87153

Please sign in to comment.