Skip to content

Commit

Permalink
bug(objectionary#3845): first try
Browse files Browse the repository at this point in the history
  • Loading branch information
maxonfjvipon committed Jan 24, 2025
1 parent 53ec85a commit 87f7994
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 6 deletions.
3 changes: 2 additions & 1 deletion eo-parser/src/main/java/org/eolang/parser/EoSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public final class EoSyntax implements Syntax {
"/org/eolang/parser/parse/resolve-before-star.xsl",
"/org/eolang/parser/parse/wrap-method-calls.xsl",
"/org/eolang/parser/parse/const-to-dataized.xsl",
"/org/eolang/parser/parse/stars-to-tuples.xsl"
"/org/eolang/parser/parse/stars-to-tuples.xsl",
"/org/eolang/parser/parse/roll-bases.xsl"
).back()
)
);
Expand Down
148 changes: 148 additions & 0 deletions eo-parser/src/main/resources/org/eolang/parser/parse/roll-bases.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License (MIT)
Copyright (c) 2016-2025 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.
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:eo="https://www.eolang.org" id="roll-bases" version="2.0">
<!--
This XSL rolls ONE reversed object dispatch into single base.
To get all the dispatches rolled up you need to apply the transformation
multiple times until it makes no effect.
- <o base=".org">
<o base="Q"/> => <o base="org"/>
</o>
- <o base=".instructions">
<o base="$"/> => <o base="instructions"/>
</o>
- <o base=".eolang">
<o base="org"/> => <o base="org.eolang"/>
</o>
- <o base=".seq">
<o base="org.eolang"/> => <o base="org.eolang.seq"/>
</o>
-->
<xsl:output encoding="UTF-8" method="xml"/>
<xsl:import href="/org/eolang/parser/_funcs.xsl"/>
<xsl:template match="o[starts-with(@base, '.')]">
<xsl:variable name="first" select="o[1]"/>
<xsl:choose>
<!--
.x
.y
Process .y
-->
<xsl:when test="starts-with($first/@base, '.')">
<xsl:variable name="argument">
<xsl:apply-templates select="$first"/>
</xsl:variable>
<xsl:choose>
<!--
Left as
.x
.y
Copy as is
-->
<xsl:when test="starts-with($argument/@base, '.')">
<xsl:element name="o">
<xsl:apply-templates select="@*"/>
<xsl:copy-of select="$argument"/>
<xsl:apply-templates select="o[position()&gt;1]"/>
</xsl:element>
</xsl:when>
<!--
Changed to
.x
z.y
Try to roll again
-->
<xsl:otherwise>
<xsl:apply-templates select="." mode="roll">
<xsl:with-param name="arg" select="$argument"/>
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<!--
.x
y
-->
<xsl:otherwise>
<xsl:apply-templates select="." mode="roll">
<xsl:with-param name="arg" select="$first"/>
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- Try to roll @base with first child -->
<xsl:template match="o" mode="roll">
<xsl:param name="arg"/>
<xsl:choose>
<!--
.x
y
z
No rolling because of argument
-->
<xsl:when test="$arg/o">
<xsl:element name="o">
<xsl:apply-templates select="@*"/>
<xsl:copy-of select="$arg"/>
<xsl:apply-templates select="o[position()&gt;1]"/>
<xsl:if test="eo:has-data(.)">
<xsl:value-of select="."/>
</xsl:if>
</xsl:element>
</xsl:when>
<!--
.x
y
Roll into y.x
-->
<xsl:otherwise>
<xsl:element name="o">
<xsl:apply-templates select="@* except @base"/>
<xsl:attribute name="base">
<xsl:choose>
<xsl:when test="$arg/@base='$' or $arg/@base='Q'">
<xsl:value-of select="substring(@base, 2)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat($arg/@base, @base)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:apply-templates select="o[position()&gt;1]"/>
<xsl:if test="eo:has-data(.)">
<xsl:value-of select="."/>
</xsl:if>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- Default copying -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ SOFTWARE.
Into the next one without @star:
<o base="tuple">
<o base="tuple">
<o base="tuple">
<o base="tuple">
<o base=".empty" method=""/>
<o base=".with">
<o base=".with">
<o base=".with">
<o base=".empty">
<o base="tuple"/>
</o>
<o base="1"/>
</o>
<o base="2"/>
Expand Down

0 comments on commit 87f7994

Please sign in to comment.