From b95780a3f6458ac87c83c0a1a4b310bcba935593 Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Wed, 22 Jan 2025 13:12:28 +0300 Subject: [PATCH] bug(#3383): phi object --- .../main/java/org/eolang/maven/UnphiMojo.java | 87 +------- .../main/java/org/eolang/parser/EoSyntax.java | 20 +- .../src/main/java/org/eolang/parser/Phi.java | 196 ++++++++++++++++++ .../java/org/eolang/parser/PhiSyntax.java | 19 +- .../main/java/org/eolang/parser/Syntax.java | 6 - .../java/org/eolang/parser/TrCanonical.java | 52 ----- .../src/main/java/org/eolang/parser/Xmir.java | 1 - .../eolang/parser/phi/incorrect-inners.xsl | 67 ------ .../parser/phi/wrap-default-package.xsl | 4 +- .../org/eolang/parser/print/wrap-data.xsl | 6 +- .../parser}/unphi/atoms-with-bound-attrs.xsl | 7 +- .../eolang/parser}/unphi/normalize-bytes.xsl | 0 .../org/eolang/parser}/unphi/wrap-bytes.xsl | 0 13 files changed, 243 insertions(+), 222 deletions(-) create mode 100644 eo-parser/src/main/java/org/eolang/parser/Phi.java delete mode 100644 eo-parser/src/main/java/org/eolang/parser/TrCanonical.java delete mode 100644 eo-parser/src/main/resources/org/eolang/parser/phi/incorrect-inners.xsl rename {eo-maven-plugin/src/main/resources/org/eolang/maven => eo-parser/src/main/resources/org/eolang/parser}/unphi/atoms-with-bound-attrs.xsl (92%) rename {eo-maven-plugin/src/main/resources/org/eolang/maven => eo-parser/src/main/resources/org/eolang/parser}/unphi/normalize-bytes.xsl (100%) rename {eo-maven-plugin/src/main/resources/org/eolang/maven => eo-parser/src/main/resources/org/eolang/parser}/unphi/wrap-bytes.xsl (100%) diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/UnphiMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/UnphiMojo.java index 1c2d32a74b..a05d7b5ab6 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/UnphiMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/UnphiMojo.java @@ -25,10 +25,6 @@ import com.jcabi.log.Logger; import com.jcabi.xml.XML; -import com.yegor256.xsline.Shift; -import com.yegor256.xsline.TrClasspath; -import com.yegor256.xsline.Train; -import com.yegor256.xsline.Xsline; import java.io.File; import java.nio.file.Path; import java.nio.file.Paths; @@ -38,19 +34,13 @@ import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; -import org.cactoos.iterable.IterableEnvelope; -import org.cactoos.iterable.Joined; -import org.cactoos.iterable.Mapped; import org.cactoos.set.SetOf; -import org.cactoos.text.TextOf; import org.eolang.maven.util.HmBase; import org.eolang.maven.util.Home; import org.eolang.maven.util.Threaded; import org.eolang.maven.util.Walk; -import org.eolang.parser.PhiSyntax; -import org.eolang.parser.TrFull; +import org.eolang.parser.Phi; import org.xembly.Directive; -import org.xembly.Directives; /** * Read PHI files and parse them to the XMIR. @@ -62,19 +52,6 @@ threadSafe = true ) public final class UnphiMojo extends SafeMojo { - - /** - * Unphi transformations. - */ - private static final Train TRANSFORMATIONS = new TrFull( - new TrClasspath<>( - "/org/eolang/maven/unphi/wrap-bytes.xsl", - "/org/eolang/maven/unphi/normalize-bytes.xsl", - "/org/eolang/parser/parse/wrap-method-calls.xsl", - "/org/eolang/maven/unphi/atoms-with-bound-attrs.xsl" - ).back() - ); - /** * The directory where to take phi files for parsing from. * @checkstyle MemberNameCheck (10 lines) @@ -109,8 +86,7 @@ public final class UnphiMojo extends SafeMojo { public void exec() { final List errors = new CopyOnWriteArrayList<>(); final Home home = new HmBase(this.unphiOutputDir); - final Iterable metas = new UnphiMojo.Metas(this.unphiMetas); - final Xsline xsline = new Xsline(this.measured(UnphiMojo.TRANSFORMATIONS)); + final Iterable metas = new Phi.Metas(this.unphiMetas); final long start = System.currentTimeMillis(); final int count = new Threaded<>( new Walk(this.unphiInputDir.toPath()), @@ -122,13 +98,7 @@ public void exec() { String.format(".%s", AssembleMojo.XMIR) ) ); - final XML result = xsline.pass( - new PhiSyntax( - phi.getFileName().toString().replace(".phi", ""), - new TextOf(phi), - metas - ).parsed() - ); + final XML result = new Phi(phi, metas).unphi(); home.save(result.toString(), xmir); Logger.debug( this, @@ -163,55 +133,4 @@ public void exec() { ); } } - - /** - * Accumulates all metas that should be attached to unphied XMIR. - * +package meta is prohibited since it's converted to special object - * with "λ -> Package" binding. - * @since 0.36.0 - */ - private static class Metas extends IterableEnvelope { - /** - * Package meta. - */ - private static final String PACKAGE = "package"; - - /** - * Ctor. - * @param metas Metas to append - */ - Metas(final Iterable metas) { - super( - new Joined<>( - new Mapped<>( - meta -> { - final String[] pair = meta.split(" ", 2); - final String head = pair[0].substring(1); - if (UnphiMojo.Metas.PACKAGE.equals(head)) { - throw new IllegalStateException( - "+package meta is prohibited for attaching to unphied XMIR" - ); - } - final Directives dirs = new Directives() - .xpath("/program") - .addIf("metas") - .add("meta") - .add("head").set(head).up() - .add("tail"); - if (pair.length > 1) { - dirs.set(pair[1].trim()).up(); - for (final String part : pair[1].trim().split(" ")) { - dirs.add("part").set(part).up(); - } - } else { - dirs.up(); - } - return dirs.up(); - }, - metas - ) - ) - ); - } - } } diff --git a/eo-parser/src/main/java/org/eolang/parser/EoSyntax.java b/eo-parser/src/main/java/org/eolang/parser/EoSyntax.java index 207baacbc7..c63dc3ce56 100644 --- a/eo-parser/src/main/java/org/eolang/parser/EoSyntax.java +++ b/eo-parser/src/main/java/org/eolang/parser/EoSyntax.java @@ -26,6 +26,8 @@ import com.jcabi.log.Logger; import com.jcabi.xml.XML; import com.jcabi.xml.XMLDocument; +import com.yegor256.xsline.TrClasspath; +import com.yegor256.xsline.Xsline; import java.io.IOException; import java.util.List; import org.antlr.v4.runtime.CommonTokenStream; @@ -50,6 +52,22 @@ * @checkstyle ClassFanOutComplexityCheck (500 lines) */ public final class EoSyntax implements Syntax { + /** + * Set of optimizations that builds canonical XMIR from parsed EO. + */ + private static final Xsline CANONICAL = new Xsline( + new TrFull( + new TrClasspath<>( + "/org/eolang/parser/parse/move-voids-up.xsl", + "/org/eolang/parser/parse/validate-before-stars.xsl", + "/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" + ).back() + ) + ); + /** * The name of the EO program being parsed, usually the name of * .eo file itself. This name will be present in the @@ -126,7 +144,7 @@ public XML parsed() throws IOException { parser.addErrorListener(eospy); final XeEoListener xel = new XeEoListener(this.name); new ParseTreeWalker().walk(xel, parser.program()); - final XML dom = Syntax.CANONICAL.pass( + final XML dom = EoSyntax.CANONICAL.pass( new XMLDocument( new Xembler( new Directives(xel).append(new DrErrors(spy)).append(new DrErrors(eospy)) diff --git a/eo-parser/src/main/java/org/eolang/parser/Phi.java b/eo-parser/src/main/java/org/eolang/parser/Phi.java new file mode 100644 index 0000000000..82aa67aa34 --- /dev/null +++ b/eo-parser/src/main/java/org/eolang/parser/Phi.java @@ -0,0 +1,196 @@ +/* + * 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. + */ +package org.eolang.parser; + +import com.jcabi.xml.XML; +import java.io.IOException; +import java.nio.file.Path; +import java.util.Set; +import java.util.function.Supplier; +import org.cactoos.Text; +import org.cactoos.iterable.IterableEnvelope; +import org.cactoos.iterable.Joined; +import org.cactoos.iterable.Mapped; +import org.cactoos.text.TextOf; +import org.xembly.Directive; +import org.xembly.Directives; + +/** + * Phi expression. + * @since 0.51.0 + */ +public class Phi { + /** + * Program name. + */ + private final Supplier name; + + /** + * Phi source code. + */ + private final Text source; + + /** + * Set of metas that are have to be added to result XMIR. + */ + private final Iterable metas; + + /** + * Ctor. + * @param file Source file + */ + public Phi(final Path file) { + this(file, new Directives()); + } + + /** + * Ctor. + * @param file Source file + * @param metas Set of metas to add to result XMIR + */ + public Phi(final Path file, final Set metas) { + this(file, new Phi.Metas(metas)); + } + + /** + * Ctor. + * @param source PHI source code + */ + public Phi(final Text source) { + this(source, new Directives()); + } + + /** + * Ctor. + * @param source PHI source code + * @param metas Set of metas to add to result XMIR + */ + public Phi(final Text source, final Set metas) { + this(source, new Phi.Metas(metas)); + } + + /** + * Ctor. + * @param file Source file + * @param metas Extra metas to add after parsing + */ + public Phi(final Path file, final Iterable metas) { + this( + () -> file.getFileName().toString().replace(".phi", ""), + file, + metas + ); + } + + /** + * Ctor. + * @param source PHI source code + * @param metas Extra metas to add after parsing + */ + public Phi(final Text source, final Iterable metas) { + this(() -> "unknown", source, metas); + } + + /** + * Ctor. + * @param name Program name + * @param file Source file + * @param metas Extra metas to add after parsing + */ + public Phi(final Supplier name, final Path file, final Iterable metas) { + this(name, new TextOf(file), metas); + } + + /** + * Ctor. + * @param name Program name + * @param source PHI source code + * @param metas Extra metas to add after parsing + */ + public Phi(final Supplier name, final Text source, final Iterable metas) { + this.name = name; + this.source = source; + this.metas = metas; + } + + /** + * Parse PHI expression to XMIR. + * @return Parsed PHI to XMIR. + * @throws IOException If fails to parse + */ + public XML unphi() throws IOException { + return new PhiSyntax(this.name.get(), this.source, this.metas).parsed(); + } + + /** + * Accumulates all metas that should be attached to unphied XMIR. + * +package meta is prohibited since it's converted to special object + * with "λ -> Package" binding. + * @since 0.36.0 + */ + public static class Metas extends IterableEnvelope { + /** + * Package meta. + */ + private static final String PACKAGE = "package"; + + /** + * Ctor. + * @param metas Metas to append + */ + public Metas(final Iterable metas) { + super( + new Joined<>( + new Mapped<>( + meta -> { + final String[] pair = meta.split(" ", 2); + final String head = pair[0].substring(1); + if (Phi.Metas.PACKAGE.equals(head)) { + throw new IllegalStateException( + "+package meta is prohibited for attaching to unphied XMIR" + ); + } + final Directives dirs = new Directives() + .xpath("/program") + .addIf("metas") + .add("meta") + .add("head").set(head).up() + .add("tail"); + if (pair.length > 1) { + dirs.set(pair[1].trim()).up(); + for (final String part : pair[1].trim().split(" ")) { + dirs.add("part").set(part).up(); + } + } else { + dirs.up(); + } + return dirs.up(); + }, + metas + ) + ) + ); + } + } +} diff --git a/eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java b/eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java index ea0abeb250..76b2e14a37 100644 --- a/eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java +++ b/eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java @@ -26,6 +26,8 @@ import com.jcabi.log.Logger; import com.jcabi.xml.XML; import com.jcabi.xml.XMLDocument; +import com.yegor256.xsline.TrClasspath; +import com.yegor256.xsline.Xsline; import java.io.IOException; import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.CommonTokenStream; @@ -43,6 +45,21 @@ * @since 0.34.0 */ public final class PhiSyntax implements Syntax { + /** + * Set of optimizations that builds canonical XMIR from parsed PHI. + */ + private static final Xsline CANONICAL = new Xsline( + new TrFull( + new TrClasspath<>( + "/org/eolang/parser/parse/move-voids-up.xsl", + "/org/eolang/parser/parse/wrap-method-calls.xsl", + "/org/eolang/parser/unphi/wrap-bytes.xsl", + "/org/eolang/parser/unphi/normalize-bytes.xsl", + "/org/eolang/parser/unphi/atoms-with-bound-attrs.xsl" + ).back() + ) + ); + /** * Name of the program. */ @@ -106,7 +123,7 @@ public XML parsed() throws IOException { parser.removeErrorListeners(); parser.addErrorListener(spy); new ParseTreeWalker().walk(xel, parser.program()); - final XML dom = Syntax.CANONICAL.pass( + final XML dom = PhiSyntax.CANONICAL.pass( new XMLDocument( new Xembler( new Directives(xel).append(new DrErrors(spy)).append(this.extra) diff --git a/eo-parser/src/main/java/org/eolang/parser/Syntax.java b/eo-parser/src/main/java/org/eolang/parser/Syntax.java index 5797974319..073c7f7006 100644 --- a/eo-parser/src/main/java/org/eolang/parser/Syntax.java +++ b/eo-parser/src/main/java/org/eolang/parser/Syntax.java @@ -24,7 +24,6 @@ package org.eolang.parser; import com.jcabi.xml.XML; -import com.yegor256.xsline.Xsline; import java.io.IOException; /** @@ -32,11 +31,6 @@ * @since 0.34.0 */ interface Syntax { - /** - * Canonical Xsline. - */ - Xsline CANONICAL = new Xsline(new TrCanonical()); - /** * Parse it to XML. * @return Parsed XML diff --git a/eo-parser/src/main/java/org/eolang/parser/TrCanonical.java b/eo-parser/src/main/java/org/eolang/parser/TrCanonical.java deleted file mode 100644 index d1bb8379b7..0000000000 --- a/eo-parser/src/main/java/org/eolang/parser/TrCanonical.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * 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. - */ -package org.eolang.parser; - -import com.yegor256.xsline.TrClasspath; -import com.yegor256.xsline.TrEnvelope; - -/** - * Train of XSL shifts that turn XMIR into canonical one. - * - * @since 0.48 - */ -final class TrCanonical extends TrEnvelope { - /** - * Ctor. - */ - TrCanonical() { - super( - new TrFull( - new TrClasspath<>( - "/org/eolang/parser/parse/move-voids-up.xsl", - "/org/eolang/parser/parse/validate-before-stars.xsl", - "/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" - ).back() - ) - ); - } -} diff --git a/eo-parser/src/main/java/org/eolang/parser/Xmir.java b/eo-parser/src/main/java/org/eolang/parser/Xmir.java index ec77874650..933d47b72f 100644 --- a/eo-parser/src/main/java/org/eolang/parser/Xmir.java +++ b/eo-parser/src/main/java/org/eolang/parser/Xmir.java @@ -88,7 +88,6 @@ public final class Xmir implements XML { "/org/eolang/parser/shake/resolve-aliases.xsl", "/org/eolang/parser/shake/add-default-package.xsl", "/org/eolang/parser/shake/explicit-data.xsl", - "/org/eolang/parser/phi/incorrect-inners.xsl", "/org/eolang/parser/phi/wrap-default-package.xsl" ).back() ); diff --git a/eo-parser/src/main/resources/org/eolang/parser/phi/incorrect-inners.xsl b/eo-parser/src/main/resources/org/eolang/parser/phi/incorrect-inners.xsl deleted file mode 100644 index b5846441df..0000000000 --- a/eo-parser/src/main/resources/org/eolang/parser/phi/incorrect-inners.xsl +++ /dev/null @@ -1,67 +0,0 @@ - - - - - - - - - - - - - incorrect-inners - - - - - - - - No line - - - - - critical - - Element can't have child elements if it does not have 'base' attribute or if it isn't abstract - - . Name of the object - - - - - - - - - - - - - diff --git a/eo-parser/src/main/resources/org/eolang/parser/phi/wrap-default-package.xsl b/eo-parser/src/main/resources/org/eolang/parser/phi/wrap-default-package.xsl index 0cb3040b19..064b44fd41 100644 --- a/eo-parser/src/main/resources/org/eolang/parser/phi/wrap-default-package.xsl +++ b/eo-parser/src/main/resources/org/eolang/parser/phi/wrap-default-package.xsl @@ -40,9 +40,7 @@ SOFTWARE. - - - + org.eolang diff --git a/eo-parser/src/main/resources/org/eolang/parser/print/wrap-data.xsl b/eo-parser/src/main/resources/org/eolang/parser/print/wrap-data.xsl index 6e52c2c245..c8ec1445fb 100644 --- a/eo-parser/src/main/resources/org/eolang/parser/print/wrap-data.xsl +++ b/eo-parser/src/main/resources/org/eolang/parser/print/wrap-data.xsl @@ -46,11 +46,7 @@ SOFTWARE. - - - - - + diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/unphi/atoms-with-bound-attrs.xsl b/eo-parser/src/main/resources/org/eolang/parser/unphi/atoms-with-bound-attrs.xsl similarity index 92% rename from eo-maven-plugin/src/main/resources/org/eolang/maven/unphi/atoms-with-bound-attrs.xsl rename to eo-parser/src/main/resources/org/eolang/parser/unphi/atoms-with-bound-attrs.xsl index 370881a0e7..d727d0b70f 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/unphi/atoms-with-bound-attrs.xsl +++ b/eo-parser/src/main/resources/org/eolang/parser/unphi/atoms-with-bound-attrs.xsl @@ -38,7 +38,10 @@ SOFTWARE. --> - + + + + @@ -72,7 +75,7 @@ SOFTWARE. - + diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/unphi/normalize-bytes.xsl b/eo-parser/src/main/resources/org/eolang/parser/unphi/normalize-bytes.xsl similarity index 100% rename from eo-maven-plugin/src/main/resources/org/eolang/maven/unphi/normalize-bytes.xsl rename to eo-parser/src/main/resources/org/eolang/parser/unphi/normalize-bytes.xsl diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/unphi/wrap-bytes.xsl b/eo-parser/src/main/resources/org/eolang/parser/unphi/wrap-bytes.xsl similarity index 100% rename from eo-maven-plugin/src/main/resources/org/eolang/maven/unphi/wrap-bytes.xsl rename to eo-parser/src/main/resources/org/eolang/parser/unphi/wrap-bytes.xsl