Skip to content

Commit

Permalink
fix(#2778): pretty phi
Browse files Browse the repository at this point in the history
  • Loading branch information
maxonfjvipon committed Jan 16, 2024
1 parent 9d2277b commit 2072903
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 21 deletions.
75 changes: 61 additions & 14 deletions eo-maven-plugin/src/main/resources/org/eolang/maven/phi/to-phi.xsl
Expand Up @@ -22,7 +22,7 @@ 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.
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:eo="https://www.eolang.org" id="to-phi" version="2.0">
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:eo="https://www.eolang.org" xmlns:xs="http://www.w3.org/2001/XMLSchema" id="to-phi" version="2.0">
<xsl:output encoding="UTF-8" method="text"/>
<xsl:import href="/org/eolang/parser/_datas.xsl"/>
<!-- Variables -->
Expand Down Expand Up @@ -74,13 +74,15 @@ SOFTWARE.
<select>∅</select>
</xsl:variable>
<!-- Functions -->
<!-- ADD XI OR NOT -->
<xsl:function name="eo:add-xi">
<xsl:param name="add"/>
<xsl:if test="$add">
<xsl:value-of select="$xi"/>
<xsl:text>.</xsl:text>
</xsl:if>
</xsl:function>
<!-- SPECIAL CHARACTERS -->
<xsl:function name="eo:specials">
<xsl:param name="n"/>
<xsl:param name="is-name"/>
Expand Down Expand Up @@ -139,6 +141,7 @@ SOFTWARE.
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<!-- TOKENIZE BYTES -->
<xsl:function name="eo:bytes">
<xsl:param name="bts"/>
<xsl:choose>
Expand All @@ -159,46 +162,71 @@ SOFTWARE.
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<!-- COMMA WITH SPACE -->
<xsl:function name="eo:comma">
<xsl:param name="pos"/>
<xsl:param name="tabs"/>
<xsl:if test="$pos&gt;1">
<xsl:text>, </xsl:text>
<xsl:text>,</xsl:text>
<xsl:value-of select="eo:eol($tabs)"/>
</xsl:if>
</xsl:function>
<!-- EOL WITH INDENTATION -->
<xsl:function name="eo:eol">
<xsl:param name="tabs"/>
<xsl:value-of select="'&#10;'"/>
<xsl:for-each select="1 to $tabs">
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:function>
<!-- Program -->
<xsl:template match="program">
<phi>
<xsl:text>{</xsl:text>
<xsl:variable name="tabs" select="1"/>
<xsl:value-of select="eo:eol($tabs)"/>
<xsl:variable name="has-package" select="metas/meta/head[text()='package']"/>
<xsl:variable name="parts" select="tokenize(metas/meta[head[text()='package']]/tail[1], '\.')"/>
<xsl:variable name="package" select="metas/meta[head[text()='package']]/tail[1]"/>
<xsl:variable name="parts" select="tokenize($package,'\.')"/>
<xsl:variable name="length" select="string-length($package)-string-length(replace($package,'\.',''))"/>
<xsl:choose>
<xsl:when test="$has-package">
<xsl:for-each select="$parts">
<xsl:value-of select="."/>
<xsl:value-of select="$arrow"/>
<xsl:value-of select="$lb"/>
<xsl:value-of select="eo:eol($tabs+position())"/>
</xsl:for-each>
<xsl:apply-templates select="objects"/>
<xsl:apply-templates select="objects">
<xsl:with-param name="tabs" select="$tabs + $length + 1"/>
</xsl:apply-templates>
<xsl:for-each select="$parts">
<xsl:value-of select="eo:comma(2)"/>
<xsl:value-of select="eo:comma(2, $tabs + $length + 2 - position())"/>
<xsl:value-of select="$lambda"/>
<xsl:value-of select="$dashed-arrow"/>
<xsl:text>Package</xsl:text>
<xsl:value-of select="eo:eol($tabs + $length + 1 - position())"/>
<xsl:value-of select="$rb"/>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="objects"/>
<xsl:apply-templates select="objects">
<xsl:with-param name="tabs" select="$tabs"/>
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="eo:eol(0)"/>
<xsl:text>}</xsl:text>
</phi>
</xsl:template>
<!-- Objects -->
<xsl:template match="objects">
<xsl:param name="tabs"/>
<xsl:for-each select="o">
<xsl:value-of select="eo:comma(position())"/>
<xsl:apply-templates select="."/>
<xsl:value-of select="eo:comma(position(), $tabs)"/>
<xsl:apply-templates select=".">
<xsl:with-param name="tabs" select="$tabs"/>
</xsl:apply-templates>
</xsl:for-each>
</xsl:template>
<!-- Free attribute -->
Expand Down Expand Up @@ -233,6 +261,7 @@ SOFTWARE.
</xsl:template>
<!-- Just object -->
<xsl:template match="o[@base]">
<xsl:param name="tabs"/>
<xsl:if test="@name">
<xsl:value-of select="eo:specials(@name, true())"/>
<xsl:value-of select="$arrow"/>
Expand All @@ -254,25 +283,33 @@ SOFTWARE.
</xsl:choose>
<xsl:if test="count(o)&gt;0">
<xsl:text>(</xsl:text>
<xsl:value-of select="eo:eol($tabs+1)"/>
<xsl:for-each select="o">
<xsl:apply-templates select="." mode="application">
<xsl:with-param name="position" select="position()"/>
<xsl:with-param name="tabs" select="$tabs+1"/>
</xsl:apply-templates>
</xsl:for-each>
<xsl:value-of select="eo:eol($tabs)"/>
<xsl:text>)</xsl:text>
</xsl:if>
</xsl:when>
<!-- Method -->
<xsl:otherwise>
<xsl:apply-templates select="o[position()=1]"/>
<xsl:apply-templates select="o[position()=1]">
<xsl:with-param name="tabs" select="$tabs"/>
</xsl:apply-templates>
<xsl:value-of select="eo:specials(@base, true())"/>
<xsl:if test="count(o)&gt;1">
<xsl:text>(</xsl:text>
<xsl:value-of select="eo:eol($tabs+1)"/>
<xsl:for-each select="o[position()!=1]">
<xsl:apply-templates select="." mode="application">
<xsl:with-param name="position" select="position()"/>
<xsl:with-param name="tabs" select="$tabs+1"/>
</xsl:apply-templates>
</xsl:for-each>
<xsl:value-of select="eo:eol($tabs)"/>
<xsl:text>)</xsl:text>
</xsl:if>
</xsl:otherwise>
Expand All @@ -286,37 +323,45 @@ SOFTWARE.
</xsl:message>
</xsl:if>
<xsl:text>(</xsl:text>
<xsl:value-of select="eo:eol($tabs+1)"/>
<xsl:value-of select="$delta"/>
<xsl:value-of select="$dashed-arrow"/>
<xsl:value-of select="eo:bytes(.)"/>
<xsl:value-of select="eo:eol($tabs)"/>
<xsl:text>)</xsl:text>
</xsl:if>
</xsl:template>
<!-- Formation -->
<xsl:template match="o[not(@base) and (@abstract or @atom)]">
<xsl:param name="tabs"/>
<xsl:if test="@name">
<xsl:value-of select="eo:specials(@name, true())"/>
<xsl:value-of select="$arrow"/>
</xsl:if>
<xsl:value-of select="$lb"/>
<xsl:value-of select="eo:eol($tabs+1)"/>
<xsl:if test="@atom">
<xsl:value-of select="$lambda"/>
<xsl:value-of select="$dashed-arrow"/>
<xsl:text>Lambda</xsl:text>
<xsl:if test="count(o)&gt;0">
<xsl:text>, </xsl:text>
<xsl:value-of select="eo:comma(2, $tabs+1)"/>
</xsl:if>
</xsl:if>
<xsl:for-each select="o">
<xsl:value-of select="eo:comma(position())"/>
<xsl:apply-templates select="."/>
<xsl:value-of select="eo:comma(position(), $tabs+1)"/>
<xsl:apply-templates select=".">
<xsl:with-param name="tabs" select="$tabs+1"/>
</xsl:apply-templates>
</xsl:for-each>
<xsl:value-of select="eo:eol($tabs)"/>
<xsl:value-of select="$rb"/>
</xsl:template>
<!-- Application -->
<xsl:template match="o" mode="application">
<xsl:param name="tabs"/>
<xsl:param name="position" select="1"/>
<xsl:value-of select="eo:comma($position)"/>
<xsl:value-of select="eo:comma($position, $tabs)"/>
<xsl:choose>
<xsl:when test="@as">
<xsl:if test="matches(@as,'^[0-9][1-9]*$')">
Expand All @@ -330,7 +375,9 @@ SOFTWARE.
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="$arrow"/>
<xsl:apply-templates select="."/>
<xsl:apply-templates select=".">
<xsl:with-param name="tabs" select="$tabs"/>
</xsl:apply-templates>
</xsl:template>
<!-- Ignore other elements -->
<xsl:template match="node()|@*">
Expand Down
23 changes: 16 additions & 7 deletions eo-maven-plugin/src/test/java/org/eolang/maven/PhiMojoTest.java
Expand Up @@ -64,18 +64,27 @@ void createsFiles(@TempDir final Path temp) throws Exception {
@ClasspathSource(value = "org/eolang/maven/phi", glob = "**.yaml")
void checksPhiPacks(final String pack, @TempDir final Path temp) throws Exception {
final Map<String, Object> map = new Yaml().load(pack);
String phi = new TextOf(
new FakeMaven(temp)
.withProgram(map.get("eo").toString())
.execute(new FakeMaven.Phi())
.result()
.get("target/phi/foo/x/main.phi")
).asString();
System.out.println(phi);
MatcherAssert.assertThat(
String.format(
"Result phi expression should be equal to %s, but it doesn't",
map.get("phi").toString()
),
new TextOf(
new FakeMaven(temp)
.withProgram(map.get("eo").toString())
.execute(new FakeMaven.Phi())
.result()
.get("target/phi/foo/x/main.phi")
).asString(),
phi,
// new TextOf(
// new FakeMaven(temp)
// .withProgram(map.get("eo").toString())
// .execute(new FakeMaven.Phi())
// .result()
// .get("target/phi/foo/x/main.phi")
// ).asString(),
Matchers.equalTo(map.get("phi").toString())
);
}
Expand Down

0 comments on commit 2072903

Please sign in to comment.