Skip to content

Commit

Permalink
fix(#2758): xmir
Browse files Browse the repository at this point in the history
  • Loading branch information
maxonfjvipon committed Jan 15, 2024
1 parent ba51a0d commit 6bff264
Show file tree
Hide file tree
Showing 11 changed files with 316 additions and 116 deletions.
Expand Up @@ -39,6 +39,7 @@
import org.eolang.maven.util.Home;
import org.eolang.maven.util.Walk;
import org.eolang.parser.XMIR;
import org.eolang.parser.xmir.Xmir;

/**
* Print XMIR to EO.
Expand Down Expand Up @@ -84,7 +85,7 @@ void exec() throws IOException {
this.printSourcesDir.toPath().relativize(source).toString()
.replace(".xmir", ".eo")
);
home.save(new XMIR(new TextOf(source)).toEO(), relative);
home.save(new Xmir(new TextOf(source)).toEO(), relative);
Logger.info(
this,
"Printed: %s => %s", source, this.printOutputDir.toPath().resolve(relative)
Expand Down
3 changes: 2 additions & 1 deletion eo-parser/src/main/java/org/eolang/parser/StEoLogged.java
Expand Up @@ -27,6 +27,7 @@
import com.jcabi.xml.XML;
import com.yegor256.xsline.Shift;
import java.util.function.Consumer;
import org.eolang.parser.xmir.Xmir;

/**
* Shift that logs the EO representation of the XML before throwing an exception.
Expand Down Expand Up @@ -78,7 +79,7 @@ public XML apply(final int position, final XML xml) {
this.logger.accept(
String.format(
"Eo representation of the parsed xml: %n%s",
new XMIR(xml).toEO()
new Xmir.Default(xml).toEO()
)
);
throw new IllegalStateException(
Expand Down
111 changes: 0 additions & 111 deletions eo-parser/src/main/java/org/eolang/parser/XMIR.java

This file was deleted.

Expand Up @@ -37,6 +37,7 @@
import org.apache.commons.text.StringEscapeUtils;
import org.cactoos.iterable.Mapped;
import org.cactoos.text.Joined;
import org.eolang.parser.xmir.XmirInfo;
import org.xembly.Directive;
import org.xembly.Directives;

Expand Down
Expand Up @@ -36,6 +36,7 @@
import org.antlr.v4.runtime.tree.ErrorNode;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.cactoos.list.ListOf;
import org.eolang.parser.xmir.XmirInfo;
import org.xembly.Directive;
import org.xembly.Directives;

Expand Down
154 changes: 154 additions & 0 deletions eo-parser/src/main/java/org/eolang/parser/xmir/Xmir.java
@@ -0,0 +1,154 @@
/*
* 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.
*/
package org.eolang.parser.xmir;

import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import com.yegor256.xsline.TrClasspath;
import com.yegor256.xsline.TrDefault;
import com.yegor256.xsline.TrJoined;
import com.yegor256.xsline.Xsline;
import org.cactoos.Text;
import org.cactoos.scalar.Unchecked;
import org.eolang.parser.StUnhex;

/**
* Prints XMIR to EO.
*
* @since 0.35.0
*/
public interface Xmir {
/**
* Converts XMIR to EO.
* @return EO representation as {@code String}
*/
String toEO();

/**
* Default Xmir that prints EO with strait vertical methods.
*
* This class will help you turn XMIR (XML document) into EOLANG
* plain text source code. It's as simple as this:
*
* <pre> String eo = new Xmir.Default(xml).toEO();</pre>
*
* Here, the {@code xml} is a {@code String} or an instance
* of {@code XML} from the jcabi-xml package.
* @link <a href="https://xml.jcabi.com">xml.jcabi.com</a>
* @since 0.35.5
*/
final class Default implements Xmir {
/**
* Wrap method calls XSL.
*/
private static final String WRAP = "/org/eolang/parser/wrap-method-calls.xsl";

/**
* Default xmir-to-eo XSL transformation.
*/
private static final String TO_EO = "/org/eolang/parser/xmir-to-eo.xsl";

/**
* The XML content.
*/
private final Unchecked<String> content;

/**
* XSL transformation sheets.
*/
private final String[] sheets;

/**
* Ctor.
* @param src The source
*/
public Default(final String src) {
this(new XMLDocument(src));
}

/**
* Ctor.
* @param src The source
*/
public Default(final Text src) {
this(new Unchecked<>(src::asString), Xmir.Default.TO_EO);
}

/**
* Ctor.
* @param src The source
* @param xsl To-EO transformation
*/
public Default(final Text src, final String xsl) {
this(new Unchecked<>(src::asString), xsl);
}

/**
* Ctor.
* @param src The source
*/
public Default(final XML src, final String xsl) {
this(new Unchecked<>(src::toString), xsl);
}

/**
* Ctor.
* @param src The source
*/
public Default(final XML src) {
this(new Unchecked<>(src::toString), Xmir.Default.TO_EO);
}

/**
* Ctor.
* @param src The source
* @param xsl To-EO transformation
*/
Default(final Unchecked<String> src, final String xsl) {
this(src, new String[] { Xmir.Default.WRAP, xsl });
}

/**
* Ctor.
* @param src The source
*/
private Default(final Unchecked<String> src, final String[] sheets) {
this.content = src;
this.sheets = sheets;
}

@Override
public String toEO() {
return new Xsline(
new TrJoined<>(
new TrDefault<>(new StUnhex()),
new TrClasspath<>(this.sheets).back()
)
)
.pass(new XMLDocument(this.content.value()))
.xpath("eo/text()")
.get(0);
}
}
}
48 changes: 48 additions & 0 deletions eo-parser/src/main/java/org/eolang/parser/xmir/XmirEnvelope.java
@@ -0,0 +1,48 @@
/*
* 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.
*/
package org.eolang.parser.xmir;

/**
* Envelope for {@link Xmir}.
* @since 0.35.0
*/
class XmirEnvelope implements Xmir {
/**
* Original XMIR.
*/
private final Xmir origin;

/**
* Ctor.
* @param xmir Original XMIR
*/
XmirEnvelope(final Xmir xmir) {
this.origin = xmir;
}

@Override
public String toEO() {
return this.origin.toEO();
}
}
Expand Up @@ -21,7 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.parser;
package org.eolang.parser.xmir;

/**
* Info about XMIR.
Expand Down

0 comments on commit 6bff264

Please sign in to comment.