Skip to content

Commit

Permalink
bug(#3383): phi object
Browse files Browse the repository at this point in the history
  • Loading branch information
maxonfjvipon committed Jan 22, 2025
1 parent 0c08a1d commit b95780a
Show file tree
Hide file tree
Showing 13 changed files with 243 additions and 222 deletions.
87 changes: 3 additions & 84 deletions eo-maven-plugin/src/main/java/org/eolang/maven/UnphiMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -62,19 +52,6 @@
threadSafe = true
)
public final class UnphiMojo extends SafeMojo {

/**
* Unphi transformations.
*/
private static final Train<Shift> 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)
Expand Down Expand Up @@ -109,8 +86,7 @@ public final class UnphiMojo extends SafeMojo {
public void exec() {
final List<String> errors = new CopyOnWriteArrayList<>();
final Home home = new HmBase(this.unphiOutputDir);
final Iterable<Directive> metas = new UnphiMojo.Metas(this.unphiMetas);
final Xsline xsline = new Xsline(this.measured(UnphiMojo.TRANSFORMATIONS));
final Iterable<Directive> metas = new Phi.Metas(this.unphiMetas);
final long start = System.currentTimeMillis();
final int count = new Threaded<>(
new Walk(this.unphiInputDir.toPath()),
Expand All @@ -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,
Expand Down Expand Up @@ -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<Directive> {
/**
* Package meta.
*/
private static final String PACKAGE = "package";

/**
* Ctor.
* @param metas Metas to append
*/
Metas(final Iterable<String> 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
)
)
);
}
}
}
20 changes: 19 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 @@ -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;
Expand All @@ -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
* <tt>.eo</tt> file itself. This name will be present in the
Expand Down Expand Up @@ -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))
Expand Down
196 changes: 196 additions & 0 deletions eo-parser/src/main/java/org/eolang/parser/Phi.java
Original file line number Diff line number Diff line change
@@ -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<String> name;

/**
* Phi source code.
*/
private final Text source;

/**
* Set of metas that are have to be added to result XMIR.
*/
private final Iterable<Directive> 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<String> 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<String> 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<Directive> 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<Directive> 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<String> name, final Path file, final Iterable<Directive> 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<String> name, final Text source, final Iterable<Directive> 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<Directive> {
/**
* Package meta.
*/
private static final String PACKAGE = "package";

/**
* Ctor.
* @param metas Metas to append
*/
public Metas(final Iterable<String> 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
)
)
);
}
}
}
Loading

0 comments on commit b95780a

Please sign in to comment.