Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Jan 17, 2024
2 parents b91c80f + 797ff38 commit 3fae7a0
Show file tree
Hide file tree
Showing 12 changed files with 258 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.yaml.snakeyaml.Yaml;

/**
Expand Down Expand Up @@ -144,8 +145,12 @@ void convertsToXmirAndBack(final String pack, @TempDir final Path temp) throws E
);
}

@Test
void convertsValidXmirAndParsableEO(@TempDir final Path temp) throws IOException {
@ParameterizedTest
@CsvSource({"true", "false"})
void convertsValidXmirAndParsableEO(
final boolean reversed,
@TempDir final Path temp
) throws Exception {
final Map<String, Path> map = new FakeMaven(temp)
.withProgram(
"[args] > app",
Expand All @@ -154,6 +159,7 @@ void convertsValidXmirAndParsableEO(@TempDir final Path temp) throws IOException
)
.with("printSourcesDir", temp.resolve("target/1-parse").toFile())
.with("printOutputDir", temp.resolve("target/generated-sources").toFile())
.with("printReversed", reversed)
.execute(ParseMojo.class)
.execute(OptimizeMojo.class)
.execute(PhiMojo.class)
Expand Down
16 changes: 7 additions & 9 deletions eo-parser/src/main/antlr4/org/eolang/parser/Eo.g4
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,8 @@ vapplication
// Vertical application head
vapplicationHead
: applicable
| hmethodExtended
| hmethodExtendedVersioned
| vmethod
| vmethodVersioned
| hmethodOptional
| vmethodOptional
| versioned
;

Expand All @@ -193,6 +191,11 @@ vapplicationHeadNamed
: vapplicationHead oname?
;

// Vertical application head with binding
vapplicationHeadAs
: vapplicationHead as
;

// Vertical application arguments
vapplicationArgs
: EOL
Expand Down Expand Up @@ -245,11 +248,6 @@ vapplicationArgHapplicationUnbinded
: happlicationExtended oname?
;

// Vertical application head with binding
vapplicationHeadAs
: (applicable | hmethodOptional | versioned) as
;

// Vertical anonym object as argument of vertical application
vapplicationArgVanonymUnbinded
: attributes oname? formatees?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public final class XmirReversed extends XmirEnvelope {
/**
* XSL transformation that converts XMIR to EO with reversed methods.
*/
private static final String REVERSED = "/org/eolang/parser/xmir-to-eo.xsl";
private static final String REVERSED = "/org/eolang/parser/xmir-to-eo-reversed.xsl";

/**
* Ctor.
Expand Down
177 changes: 177 additions & 0 deletions eo-parser/src/main/resources/org/eolang/parser/xmir-to-eo-reversed.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License (MIT)
Copyright (c) 2016-2023 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="xmir-to-eo-reversed" version="2.0">
<!--
This one maps XMIR to EO original syntax in reversed notation.
It's used in XmirReversed.java class.
-->
<xsl:import href="/org/eolang/parser/_funcs.xsl"/>
<xsl:variable name="eol" select="'&#10;'"/>
<xsl:output method="text" encoding="UTF-8"/>
<!-- PROGRAM -->
<xsl:template match="program">
<eo>
<xsl:apply-templates select="license"/>
<xsl:apply-templates select="metas[meta]"/>
<xsl:apply-templates select="objects"/>
</eo>
</xsl:template>
<!-- LICENCE -->
<xsl:template match="license">
<xsl:for-each select="tokenize(., $eol)">
<xsl:text># </xsl:text>
<xsl:value-of select="."/>
<xsl:value-of select="$eol"/>
</xsl:for-each>
<xsl:if test="text()">
<xsl:value-of select="$eol"/>
</xsl:if>
</xsl:template>
<!-- METAS -->
<xsl:template match="metas">
<xsl:apply-templates select="meta"/>
<xsl:value-of select="$eol"/>
</xsl:template>
<!-- META -->
<xsl:template match="meta">
<xsl:text>+</xsl:text>
<xsl:value-of select="head"/>
<xsl:if test="not(empty(tail/text()))">
<xsl:text> </xsl:text>
<xsl:value-of select="tail"/>
</xsl:if>
<xsl:value-of select="$eol"/>
</xsl:template>
<!-- OBJECTS -->
<xsl:template match="objects">
<xsl:apply-templates select="o"/>
</xsl:template>
<!-- OBJECT, NOT FREE ATTRIBUTE -->
<xsl:template match="o[not(eo:attr(.))]">
<xsl:param name="indent" select="''"/>
<!--IF NOT THE FIRST TOP OBJECT -->
<xsl:if test="position()&gt;1 and parent::objects">
<xsl:value-of select="$eol"/>
</xsl:if>
<xsl:value-of select="$indent"/>
<xsl:apply-templates select="." mode="head">
<xsl:with-param name="indent" select="$indent"/>
</xsl:apply-templates>
<xsl:apply-templates select="." mode="tail"/>
<xsl:value-of select="$eol"/>
<xsl:apply-templates select="o[not(eo:attr(.))]">
<xsl:with-param name="indent" select="concat(' ', $indent)"/>
</xsl:apply-templates>
</xsl:template>
<!-- BASED -->
<xsl:template match="o[not(@data) and @base]" mode="head">
<xsl:choose>
<xsl:when test="starts-with(@base,'.')">
<xsl:value-of select="substring(@base,2)"/>
<xsl:text>.</xsl:text>
</xsl:when>
<!-- NOT OPTIMIZED TUPLE -->
<xsl:when test="@star">
<xsl:text>*</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="@base"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- ABSTRACT OR ATOM -->
<xsl:template match="o[not(@data) and not(@base)]" mode="head">
<xsl:text>[</xsl:text>
<xsl:for-each select="o[eo:attr(.)]">
<xsl:if test="position()&gt;1">
<xsl:text> </xsl:text>
</xsl:if>
<xsl:value-of select="@name"/>
</xsl:for-each>
<xsl:text>]</xsl:text>
</xsl:template>
<!-- TAIL: SUFFIX, NAME, CONST, COPY, ATOM -->
<xsl:template match="o" mode="tail">
<xsl:if test="@copy">
<xsl:text>'</xsl:text>
</xsl:if>
<xsl:if test="@as">
<xsl:text>:</xsl:text>
<xsl:value-of select="@as"/>
</xsl:if>
<xsl:if test="@name">
<xsl:text> &gt; </xsl:text>
<xsl:value-of select="@name"/>
<xsl:if test="@const">
<xsl:text>!</xsl:text>
</xsl:if>
<xsl:if test="@atom">
<xsl:text> /</xsl:text>
<xsl:value-of select="@atom"/>
</xsl:if>
</xsl:if>
</xsl:template>
<!-- DATA -->
<xsl:template match="o[@data]" mode="head">
<xsl:param name="indent"/>
<xsl:choose>
<xsl:when test="@data='string'">
<xsl:text>"""</xsl:text>
<xsl:value-of select="$eol"/>
<xsl:if test="not(empty(text()))">
<xsl:value-of select="$indent"/>
<xsl:value-of select="text()"/>
<xsl:value-of select="$eol"/>
</xsl:if>
<xsl:value-of select="$indent"/>
<xsl:text>"""</xsl:text>
</xsl:when>
<xsl:when test="@data='bool' or @data='int' or @data='float'">
<xsl:value-of select="text()"/>
</xsl:when>
<xsl:when test="@data='bytes'">
<xsl:choose>
<xsl:when test="empty(text())">
<xsl:text>--</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="replace(text(), ' ', '-')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:message terminate="yes">
<xsl:text>Invalid data attribute: </xsl:text>
<xsl:value-of select="@data"/>
</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
54 changes: 35 additions & 19 deletions eo-parser/src/main/resources/org/eolang/parser/xmir-to-eo.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ SOFTWARE.
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:eo="https://www.eolang.org" id="xmir-to-eo" version="2.0">
<!--
This one maps XMIR to EO original syntax in reversed notation.
It's used in XmirReversed.java class.
This one maps XMIR to EO original syntax in strait notation.
It's used in Xmir.java class.
-->
<xsl:import href="/org/eolang/parser/_funcs.xsl"/>
<xsl:variable name="eol" select="'&#10;'"/>
Expand Down Expand Up @@ -71,27 +71,43 @@ SOFTWARE.
<!-- OBJECT, NOT FREE ATTRIBUTE -->
<xsl:template match="o[not(eo:attr(.))]">
<xsl:param name="indent" select="''"/>
<!--IF NOT THE FIRST TOP OBJECT -->
<xsl:if test="position()&gt;1 and parent::objects">
<xsl:value-of select="$eol"/>
</xsl:if>
<xsl:value-of select="$indent"/>
<xsl:apply-templates select="." mode="head">
<xsl:with-param name="indent" select="$indent"/>
</xsl:apply-templates>
<xsl:apply-templates select="." mode="tail"/>
<xsl:value-of select="$eol"/>
<xsl:apply-templates select="o[not(eo:attr(.))]">
<xsl:with-param name="indent" select="concat(' ', $indent)"/>
</xsl:apply-templates>
<xsl:choose>
<!-- METHOD -->
<xsl:when test="starts-with(@base,'.')">
<xsl:apply-templates select="o[position()=1]">
<xsl:with-param name="indent" select="$indent"/>
</xsl:apply-templates>
<xsl:value-of select="$indent"/>
<xsl:apply-templates select="." mode="head">
<xsl:with-param name="indent" select="$indent"/>
</xsl:apply-templates>
<xsl:apply-templates select="." mode="tail"/>
<xsl:value-of select="$eol"/>
<xsl:apply-templates select="o[position()&gt;1 and not(eo:attr(.))]">
<xsl:with-param name="indent" select="concat(' ', $indent)"/>
</xsl:apply-templates>
</xsl:when>
<!-- NOT METHOD -->
<xsl:otherwise>
<!--IF NOT THE FIRST TOP OBJECT -->
<xsl:if test="position()&gt;1 and parent::objects">
<xsl:value-of select="$eol"/>
</xsl:if>
<xsl:value-of select="$indent"/>
<xsl:apply-templates select="." mode="head">
<xsl:with-param name="indent" select="$indent"/>
</xsl:apply-templates>
<xsl:apply-templates select="." mode="tail"/>
<xsl:value-of select="$eol"/>
<xsl:apply-templates select="o[not(eo:attr(.))]">
<xsl:with-param name="indent" select="concat(' ', $indent)"/>
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- BASED -->
<xsl:template match="o[not(@data) and @base]" mode="head">
<xsl:choose>
<xsl:when test="starts-with(@base,'.')">
<xsl:value-of select="substring(@base,2)"/>
<xsl:text>.</xsl:text>
</xsl:when>
<!-- NOT OPTIMIZED TUPLE -->
<xsl:when test="@star">
<xsl:text>*</xsl:text>
Expand Down
12 changes: 0 additions & 12 deletions eo-parser/src/test/java/org/eolang/parser/xmir/XmirTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.eolang.parser.EoSyntax;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.params.ParameterizedTest;
import org.yaml.snakeyaml.Yaml;

Expand All @@ -45,18 +44,7 @@
* @checkstyle AbbreviationAsWordInNameCheck (500 lines)
*/
final class XmirTest {
/**
* Convert EO to xmir and back and compare.
* @param pack EO pack.
* @throws Exception If fails.
* @todo #2758:30min Implement printing from XMIR to EO in strait notation
* (where method starts on the next line). It should be done via XSL which
* should be used in {@link Xmir.Default} object. The next disabled test
* shows that such implementation does not work now:
* {@link XmirTest#printsStrait(String)}
*/
@ParameterizedTest
@Disabled
@ClasspathSource(value = "org/eolang/parser/samples/", glob = "**.yaml")
void printsStrait(final String pack) throws IOException {
final Map<String, Object> map = new Yaml().load(pack);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,18 @@ eo: |
.b:0
c
.d
.e:1
.e:1
Q
.org
.eolang
.io
.stdout > third
Q
.org
.eolang
.string:0
Q
.org
.eolang
.bytes
00-42-23
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ strait: |
[args] > main
QQ
.io
stdout > @
.stdout > @
args
.at
args
Expand Down

1 comment on commit 3fae7a0

@0pdd
Copy link

@0pdd 0pdd commented on 3fae7a0 Jan 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 2758-623c133e disappeared from eo-parser/src/test/java/org/eolang/parser/xmir/XmirTest.java), that's why I closed #2787. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.