Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Dec 7, 2023
2 parents 3553322 + 76bbb56 commit eafd274
Show file tree
Hide file tree
Showing 24 changed files with 1,489 additions and 535 deletions.
2 changes: 1 addition & 1 deletion eo-maven-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ binary code consists of a few high-level steps, which must be done
one after another:

* **Parsing**.
It's done by the `org.eolang.parser.Syntax` class in the `eo-parser` module. It takes
It's done by the `org.eolang.parser.EoSyntax` class in the `eo-parser` module. It takes
the source code in a plain text format and parses into XML document,
using [ANTLR4](https://www.antlr.org/) and [Xembly](https://www.xembly.org).
The output of the parser you can find in the `target/eo/parse` directory.
Expand Down
17 changes: 7 additions & 10 deletions eo-maven-plugin/src/main/java/org/eolang/maven/ParseMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.jcabi.log.Logger;
import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
Expand All @@ -38,7 +37,6 @@
import org.cactoos.Scalar;
import org.cactoos.experimental.Threads;
import org.cactoos.io.InputOf;
import org.cactoos.io.OutputTo;
import org.cactoos.iterable.Filtered;
import org.cactoos.iterable.Mapped;
import org.cactoos.number.SumOf;
Expand All @@ -48,7 +46,7 @@
import org.eolang.maven.footprint.FtDefault;
import org.eolang.maven.tojos.ForeignTojo;
import org.eolang.maven.util.Rel;
import org.eolang.parser.Syntax;
import org.eolang.parser.EoSyntax;
import org.xembly.Directives;
import org.xembly.Xembler;

Expand Down Expand Up @@ -154,19 +152,18 @@ private void parse(final ForeignTojo tojo) throws IOException {
name,
"xmir",
() -> {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
new Syntax(
name,
new InputOf(source),
new OutputTo(baos)
).parse();
final String parsed = new XMLDocument(
new Xembler(
new Directives().xpath("/program").attr(
"source",
source.toAbsolutePath()
)
).applyQuietly(new XMLDocument(baos.toByteArray()).node())
).applyQuietly(
new EoSyntax(
name,
new InputOf(source)
).parsed().node()
)
).toString();
Logger.debug(
this,
Expand Down
108 changes: 108 additions & 0 deletions eo-maven-plugin/src/main/java/org/eolang/maven/UnphiMojo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* 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.maven;

import com.jcabi.log.Logger;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.cactoos.experimental.Threads;
import org.cactoos.iterable.Mapped;
import org.cactoos.number.SumOf;
import org.cactoos.text.TextOf;
import org.eolang.maven.util.HmBase;
import org.eolang.maven.util.Home;
import org.eolang.maven.util.Walk;
import org.eolang.parser.PhiSyntax;

/**
* Read PHI files and parse them to the XMIR.
* @since 0.34.0
*/
@Mojo(
name = "phi-to-xmir",
defaultPhase = LifecyclePhase.PROCESS_SOURCES,
threadSafe = true
)
public final class UnphiMojo extends SafeMojo {
/**
* The directory where to take phi files for parsing from.
* @checkstyle MemberNameCheck (10 lines)
*/
@Parameter(
property = "unphiInputDir",
required = true,
defaultValue = "${project.build.directory}/eo/phi"
)
private File unphiInputDir;

/**
* The directory where to save xmir files to.
* @checkstyle MemberNameCheck (10 lines)
*/
@Parameter(
property = "unphiOutputDir",
required = true,
defaultValue = "${project.build.directory}/eo/1-parse"
)
private File unphiOutputDir;

@Override
public void exec() {
final Home home = new HmBase(this.unphiOutputDir);
final int count = new SumOf(
new Threads<>(
Runtime.getRuntime().availableProcessors(),
new Mapped<>(
phi -> () -> {
final Path relative = Paths.get(
this.unphiInputDir.toPath().relativize(phi).toString().replace(
String.format(".%s", PhiMojo.EXT),
String.format(".%s", TranspileMojo.EXT)
)
);
home.save(
new PhiSyntax(
phi.getFileName().toString(),
new TextOf(phi)
).parsed().toString(),
relative
);
Logger.info(
this,
"Parsed to xmir: %s -> %s",
phi, this.unphiOutputDir.toPath().resolve(relative)
);
return 1;
},
new Walk(this.unphiInputDir.toPath())
)
)
).intValue();
Logger.info(this, "Parsed %d phi files to xmir", count);
}
}
2 changes: 1 addition & 1 deletion eo-maven-plugin/src/main/resources/log4j.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ log4j.appender.CONSOLE.layout.ConversionPattern=%d{HH:mm:ss} [%color{%p}] %c: %m

log4j.logger.org.eolang=INFO
log4j.logger.com.yegor256.xsline=INFO
log4j.logger.org.eolang.parser.Syntax=INFO
log4j.logger.org.eolang.parser.EoSyntax=INFO
log4j.logger.org.eolang.parser.Scenario=INFO
log4j.logger.org.eolang.parser.XMIRTest=INFO
log4j.logger.org.eolang.maven.SodgMojo=TRACE
10 changes: 10 additions & 0 deletions eo-maven-plugin/src/test/java/org/eolang/maven/FakeMaven.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,16 @@ public <T extends AbstractMojo> FakeMaven execute(final Class<T> mojo) throws IO
"phiOutputDir",
this.workspace.absolute(Paths.get("target/phi")).toFile()
);
this.params.putIfAbsent(
"unphiInputDir",
this.workspace.absolute(Paths.get("target/phi")).toFile()
);
this.params.putIfAbsent(
"unphiOutputDir",
this.workspace.absolute(
Paths.get(String.format("target/%s", ParseMojo.DIR))
).toFile()
);
}
final Moja<T> moja = new Moja<>(mojo);
for (final Map.Entry<String, ?> entry : this.allowedParams(mojo).entrySet()) {
Expand Down
20 changes: 8 additions & 12 deletions eo-maven-plugin/src/test/java/org/eolang/maven/JavaFilesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,16 @@
*/
package org.eolang.maven;

import com.jcabi.xml.XMLDocument;
import com.jcabi.xml.XML;
import com.yegor256.xsline.Xsline;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.cactoos.io.InputOf;
import org.cactoos.io.OutputTo;
import org.cactoos.io.ResourceOf;
import org.cactoos.text.TextOf;
import org.eolang.maven.util.HmBase;
import org.eolang.parser.Syntax;
import org.eolang.parser.EoSyntax;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -50,17 +49,14 @@ class JavaFilesTest {
/**
* Parsed eo program from resources.
*/
private XMLDocument xmir;
private XML xmir;

@BeforeEach
void setUp() throws Exception {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
new Syntax(
void setUp() throws IOException {
this.xmir = new EoSyntax(
"sum.eo",
new ResourceOf("org/eolang/maven/sum.eo"),
new OutputTo(baos)
).parse();
this.xmir = new XMLDocument(baos.toByteArray());
new ResourceOf("org/eolang/maven/sum.eo")
).parsed();
}

@Test
Expand Down
99 changes: 99 additions & 0 deletions eo-maven-plugin/src/test/java/org/eolang/maven/UnphiMojoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* 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.maven;

import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.cactoos.list.ListOf;
import org.cactoos.text.TextOf;
import org.eolang.jucs.ClasspathSource;
import org.eolang.maven.util.HmBase;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.yaml.snakeyaml.Yaml;

/**
* Test cases for {@link UnphiMojo}.
* @since 0.34.0
* @todo #2642:30min Create test packs for {@link UnphiMojo}. UnphiMojo seems to work correctly.
* We need to create yaml packs and enable {@link UnphiMojoTest#checksUnphiPacks(String, Path)}
* and make sure all of them are passed. Don't forget to remove the puzzle.
*/
class UnphiMojoTest {
@Test
void createsFile(@TempDir final Path temp) throws Exception {
new HmBase(temp).save(
"{std ↦ Φ.org.eolang.io.stdout, y ↦ Φ.org.eolang.x}",
Paths.get("target/phi/std.phi")
);
MatcherAssert.assertThat(
String.format(
"There should be file with .%s extension after parsing phi to XMIR, but there isn't",
TranspileMojo.EXT
),
new FakeMaven(temp)
.execute(UnphiMojo.class)
.result(),
Matchers.hasKey(String.format("target/%s/std.xmir", ParseMojo.DIR))
);
}

@ParameterizedTest
@Disabled
@ClasspathSource(value = "org/eolang/maven/unphi", glob = "**.yaml")
void checksUnphiPacks(final String pack, @TempDir final Path temp) throws Exception {
final Map<String, Object> map = new Yaml().load(pack);
final String phi = map.get("phi").toString();
new HmBase(temp).save(phi, Paths.get("target/phi/main.phi"));
final List<String> failures = new ListOf<>();
new FakeMaven(temp).execute(UnphiMojo.class);
for (final String xpath : (Iterable<String>) map.get("tests")) {
final List<XML> nodes = new XMLDocument(
new TextOf(
Paths.get(String.format("target/%s/main.xmir", ParseMojo.DIR))
).asString()
).nodes(xpath);
if (nodes.isEmpty()) {
failures.add(xpath);
}
}
MatcherAssert.assertThat(
String.format(
"Failed to parse phi expression: %s; failed tests: %s",
phi, Arrays.toString(failures.toArray())
),
failures,
Matchers.empty()
);
}
}
2 changes: 1 addition & 1 deletion eo-maven-plugin/src/test/resources/log4j.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ log4j.appender.CONSOLE.layout.ConversionPattern=%d{HH:mm:ss} [%p] %c: %m%n

log4j.logger.org.eolang=INFO
log4j.logger.com.jcabi.log.VerboseProcess=INFO
log4j.logger.org.eolang.parser.Syntax=INFO
log4j.logger.org.eolang.parser.EoSyntax=INFO
log4j.logger.org.eolang.parser.Program=INFO
log4j.logger.org.eolang.maven.SodgMojo=INFO
log4j.logger.org.eolang.maven.SodgMojoTest=INFO
Expand Down
2 changes: 1 addition & 1 deletion eo-parser/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ SOFTWARE.
<artifactId>qulice-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>checkstyle:/src/main/java/org/eolang/parser/XeListener.java</exclude>
<exclude>checkstyle:/src/main/java/org/eolang/parser/XeEoListener.java</exclude>
</excludes>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
grammar Program;
grammar Eo;

tokens { TAB, UNTAB }

Expand Down

1 comment on commit eafd274

@0pdd
Copy link

@0pdd 0pdd commented on eafd274 Dec 7, 2023

Choose a reason for hiding this comment

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

Puzzle 2642-02dbe2cd discovered in eo-maven-plugin/src/test/java/org/eolang/maven/UnphiMojoTest.java) and submitted as #2682. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.