Skip to content

Commit

Permalink
fix(#3624): parser
Browse files Browse the repository at this point in the history
  • Loading branch information
maxonfjvipon committed Dec 10, 2024
1 parent cfabb44 commit c25aa01
Show file tree
Hide file tree
Showing 40 changed files with 385 additions and 162 deletions.
11 changes: 6 additions & 5 deletions eo-parser/src/main/java/org/eolang/parser/EoSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
* @checkstyle ClassFanOutComplexityCheck (500 lines)
*/
public final class EoSyntax implements Syntax {

/**
* The name of the EO program being parsed, usually the name of
* <tt>.eo</tt> file itself. This name will be present in the
Expand Down Expand Up @@ -117,10 +116,12 @@ public XML parsed() throws IOException {
parser.addErrorListener(spy);
final XeEoListener xel = new XeEoListener(this.name);
new ParseTreeWalker().walk(xel, parser.program());
final XML dom = new XMLDocument(
new Xembler(
new Directives(xel).append(spy)
).domQuietly()
final XML dom = Syntax.CANONICAL.pass(
new XMLDocument(
new Xembler(
new Directives(xel).append(spy)
).domQuietly()
)
);
if (spy.size() == 0) {
Logger.debug(
Expand Down
10 changes: 6 additions & 4 deletions eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,12 @@ public XML parsed() throws IOException {
parser.removeErrorListeners();
parser.addErrorListener(spy);
new ParseTreeWalker().walk(xel, parser.program());
final XML dom = new XMLDocument(
new Xembler(
new Directives(xel).append(spy).append(this.extra)
).domQuietly()
final XML dom = Syntax.CANONICAL.pass(
new XMLDocument(
new Xembler(
new Directives(xel).append(spy).append(this.extra)
).domQuietly()
)
);
if (spy.size() == 0) {
Logger.debug(this, "Input of PHI calculus compiled, no errors");
Expand Down
6 changes: 6 additions & 0 deletions eo-parser/src/main/java/org/eolang/parser/Syntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@
package org.eolang.parser;

import com.jcabi.xml.XML;
import com.yegor256.xsline.Xsline;
import java.io.IOException;

/**
* Syntax parser, to XMIR, using ANTLR4.
* @since 0.34.0
*/
public interface Syntax {
/**
* Canonical Xsline.
*/
Xsline CANONICAL = new Xsline(new TrCanonical());

/**
* Parse it to XML.
* @return Parsed XML
Expand Down
76 changes: 76 additions & 0 deletions eo-parser/src/main/java/org/eolang/parser/TrCanonical.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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.parser;

import com.yegor256.xsline.StClasspath;
import com.yegor256.xsline.StEndless;
import com.yegor256.xsline.TrClasspath;
import com.yegor256.xsline.TrDefault;
import com.yegor256.xsline.TrEnvelope;
import com.yegor256.xsline.TrFast;
import com.yegor256.xsline.TrJoined;
import com.yegor256.xsline.TrLambda;
import com.yegor256.xsline.TrLogged;
import java.util.logging.Level;

/**
* Train of XSL shifts that turn XMIR into canonical one.
*
* @since 0.48
*/
public final class TrCanonical extends TrEnvelope {
/**
* Ctor.
*/
public TrCanonical() {
super(
new TrStepped(
new TrFast(
new TrLambda(
new TrLogged(
new TrJoined<>(
new TrDefault<>(
new StEndless(
new StClasspath(
"/org/eolang/parser/stars-to-tuples.xsl"
)
)
),
new TrClasspath<>(
"/org/eolang/parser/wrap-method-calls.xsl",
"/org/eolang/parser/const-to-dataized.xsl"
).back()
),
TrCanonical.class,
Level.FINEST
),
StEoLogged::new
),
TrCanonical.class,
500L
)
)
);
}
}
58 changes: 15 additions & 43 deletions eo-parser/src/main/java/org/eolang/parser/TrParsing.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,9 @@
*/
package org.eolang.parser;

import com.jcabi.xml.ClasspathSources;
import com.jcabi.xml.XSL;
import com.jcabi.xml.XSLDocument;
import com.yegor256.xsline.StEndless;
import com.yegor256.xsline.StLambda;
import com.yegor256.xsline.TrClasspath;
import com.yegor256.xsline.TrDefault;
import com.yegor256.xsline.TrEnvelope;
import com.yegor256.xsline.TrFast;
import com.yegor256.xsline.TrJoined;
import com.yegor256.xsline.TrLambda;
import com.yegor256.xsline.TrLogged;
import java.util.logging.Level;
Expand All @@ -43,15 +36,6 @@
* @since 0.1
*/
public final class TrParsing extends TrEnvelope {

/**
* Transform stars to tuples.
*/
private static final XSL TUPLES = new XSLDocument(
TrParsing.class.getResourceAsStream("stars-to-tuples.xsl"),
"stars-to-tuples.xsl"
).with(new ClasspathSources(TrParsing.class));

/**
* Ctor.
*/
Expand All @@ -61,33 +45,21 @@ public TrParsing() {
new TrFast(
new TrLambda(
new TrLogged(
new TrJoined<>(
new TrDefault<>(
new StEndless(
new StLambda(
"stars-to-tuples",
TrParsing.TUPLES::transform
)
)
),
new TrClasspath<>(
"/org/eolang/parser/cti-adds-errors.xsl",
"/org/eolang/parser/add-refs.xsl",
"/org/eolang/parser/wrap-method-calls.xsl",
"/org/eolang/parser/expand-qqs.xsl",
"/org/eolang/parser/add-probes.xsl",
"/org/eolang/parser/vars-float-up.xsl",
"/org/eolang/parser/add-refs.xsl",
"/org/eolang/parser/expand-aliases.xsl",
"/org/eolang/parser/resolve-aliases.xsl",
"/org/eolang/parser/add-refs.xsl",
"/org/eolang/parser/add-default-package.xsl",
"/org/eolang/parser/explicit-data.xsl",
"/org/eolang/parser/const-to-dataized.xsl",
"/org/eolang/parser/set-locators.xsl",
"/org/eolang/parser/clean-up.xsl"
).back()
),
new TrClasspath<>(
"/org/eolang/parser/cti-adds-errors.xsl",
"/org/eolang/parser/add-refs.xsl",
"/org/eolang/parser/expand-qqs.xsl",
"/org/eolang/parser/add-probes.xsl",
"/org/eolang/parser/vars-float-up.xsl",
"/org/eolang/parser/add-refs.xsl",
"/org/eolang/parser/expand-aliases.xsl",
"/org/eolang/parser/resolve-aliases.xsl",
"/org/eolang/parser/add-refs.xsl",
"/org/eolang/parser/add-default-package.xsl",
"/org/eolang/parser/explicit-data.xsl",
"/org/eolang/parser/set-locators.xsl",
"/org/eolang/parser/clean-up.xsl"
).back(),
TrParsing.class,
Level.FINEST
),
Expand Down
6 changes: 5 additions & 1 deletion eo-parser/src/main/java/org/eolang/parser/Xmir.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.yegor256.xsline.Shift;
import com.yegor256.xsline.StAfter;
import com.yegor256.xsline.StClasspath;
import com.yegor256.xsline.StEndless;
import com.yegor256.xsline.StLambda;
import com.yegor256.xsline.TrDefault;
import com.yegor256.xsline.TrLambda;
Expand Down Expand Up @@ -59,9 +60,12 @@ public final class Xmir {
private static final Train<Shift> TRAIN = new TrLogged(
new TrLambda(
new TrDefault<>(
new StEndless(
new StClasspath("/org/eolang/parser/tuples-to-stars.xsl")
),
new StClasspath("/org/eolang/parser/explicit-data.xsl"),
new StClasspath("/org/eolang/parser/dataized-to-const.xsl"),
new StUnhex(),
new StClasspath("/org/eolang/parser/wrap-method-calls.xsl"),
new StClasspath("/org/eolang/parser/wrap-data.xsl")
),
shift -> new StAfter(
Expand Down
3 changes: 0 additions & 3 deletions eo-parser/src/main/resources/XMIR.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,6 @@ SOFTWARE.
now. We will remove them later, when we will have a separate
XSD for the parser output (maybe).
-->
<xs:attribute name="star" type="empty"/>
<xs:attribute name="method" type="empty"/>
<xs:attribute name="const" type="empty"/>
</xs:complexType>
<xs:complexType name="error">
<xs:annotation>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,16 @@ SOFTWARE.
<xsl:param name="name"/>
<xsl:param name="scope"/>
<xsl:param name="counter"/>
<xsl:variable name="unique" select="concat($name, '-', $counter)"/>
<xsl:variable name="unique">
<xsl:choose>
<xsl:when test="$counter=0">
<xsl:value-of select="$name"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat($name, '-', $counter)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:choose>
<xsl:when test="$scope[o[@name=$unique]]">
<xsl:value-of select="eo:unique-name($name, $scope, $counter + 1)"/>
Expand All @@ -62,12 +71,15 @@ SOFTWARE.
</xsl:for-each>
<xsl:if test="eo:abstract(.)">
<xsl:attribute name="name">
<xsl:value-of select="eo:unique-name(@name, ./parent::o, 1)"/>
<xsl:value-of select="eo:unique-name(@name, ./parent::o, 0)"/>
</xsl:attribute>
</xsl:if>
<xsl:for-each select="o">
<xsl:apply-templates select="."/>
</xsl:for-each>
<xsl:if test="eo:has-data(.)">
<xsl:value-of select="."/>
</xsl:if>
</xsl:element>
</xsl:element>
</xsl:element>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:eo="https://www.eolang.org" id="dataized-to-const" version="2.0">
<!--
Performs the reverse operation of "/org/eolang/parser/const-to-dataized.xsl"
-->
<xsl:import href="/org/eolang/parser/_funcs.xsl"/>
<xsl:output encoding="UTF-8" method="xml"/>
<xsl:template match="o[@base='.as-bytes' and o[position()=1 and @base='org.eolang.dataized']]">
<xsl:variable name="argument" select="o[position()=1]/o[1]"/>
<xsl:choose>
<xsl:when test="exists($argument) and not(exists($argument/@name))">
<xsl:element name="o">
<xsl:for-each select="$argument/@*">
<xsl:attribute name="{name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<xsl:attribute name="name" select="@name"/>
<xsl:attribute name="const"/>
<xsl:for-each select="$argument/o">
<xsl:copy-of select="."/>
</xsl:for-each>
<xsl:if test="eo:has-data($argument)">
<xsl:value-of select="$argument"/>
</xsl:if>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
23 changes: 23 additions & 0 deletions eo-parser/src/main/resources/org/eolang/parser/stars-to-tuples.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,29 @@ 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" id="stars-to-tuples" version="2.0">
<!--
Converts such XMIR with @star attributes:
<o star="" base="tuple">
<o base="1".../>
<o base="2".../>
<o base="3".../>
</o>
Into the next one without @star:
<o base="tuple">
<o base="tuple">
<o base="tuple">
<o base="tuple">
<o base=".emtpy" method=""/>

Check warning on line 41 in eo-parser/src/main/resources/org/eolang/parser/stars-to-tuples.xsl

View workflow job for this annotation

GitHub Actions / typos

"emtpy" should be "empty".
<o base="1"/>
</o>
<o base="2"/>
</o>
<o base="3"/>
</o>
-->
<xsl:output encoding="UTF-8" method="xml"/>
<xsl:template match="o[@star]">
<xsl:choose>
Expand Down
Loading

0 comments on commit c25aa01

Please sign in to comment.