Skip to content

Commit

Permalink
Merge pull request #3761 from maxonfjvipon/bug/#3757/braces-in-phi
Browse files Browse the repository at this point in the history
bug(#3757): Conservative to-phi conversion
  • Loading branch information
yegor256 authored Dec 25, 2024
2 parents a753bc2 + a6cc1e8 commit b967900
Show file tree
Hide file tree
Showing 37 changed files with 200 additions and 82 deletions.
10 changes: 8 additions & 2 deletions eo-maven-plugin/src/main/java/org/eolang/maven/PhiMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ public final class PhiMojo extends SafeMojo {
)
private File phiOutputDir;

/**
* Print all the braces in sweet notation.
*/
@SuppressWarnings("PMD.ImmutableField")
private boolean conservative = true;

/**
* Convert to PHI without syntax sugar.
* @checkstyle MemberNameCheck (10 lines)
Expand Down Expand Up @@ -165,9 +171,9 @@ private String translated(final XML xml) throws ImpossibleToPhiTranslationExcept
final String phi;
try {
if (this.phiNoSugar) {
phi = xmir.toPhiNoSugar();
phi = xmir.toSaltyPhi();
} else {
phi = xmir.toPhi();
phi = xmir.toPhi(this.conservative);
}
} catch (final IndexOutOfBoundsException exception) {
throw new ImpossibleToPhiTranslationException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ public <T extends AbstractMojo> FakeMaven execute(final Class<T> mojo) throws IO
this.params.putIfAbsent("offline", false);
this.params.putIfAbsent("phiNoSugar", false);
this.params.putIfAbsent("phiSkipFailed", false);
this.params.putIfAbsent("conservative", false);
this.params.putIfAbsent(
"phiInputDir",
this.workspace.absolute(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,13 @@ void checksPhiPacksWithSugar(final String pack, @Mktmp final Path temp) throws E
new TextOf(
new FakeMaven(temp)
.withProgram(xtory.map().get("input").toString())
.with("conservative", xtory.map().get("conservative") != null)
.with("phiNoSugar", false)
.execute(new FakeMaven.Phi())
.result()
.get("target/phi/foo/x/main.phi")
).asString(),
Matchers.equalTo(xtory.map().get("with-sugar").toString())
Matchers.equalTo(xtory.map().get("sweet").toString())
);
}

Expand All @@ -219,7 +220,7 @@ void checksPhiPacksNoSugar(final String pack, @Mktmp final Path temp) throws Exc
.result()
.get("target/phi/foo/x/main.phi")
).asString(),
Matchers.equalTo(xtory.map().get("no-sugar").toString())
Matchers.equalTo(xtory.map().get("salty").toString())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ void checksUnphiPacks(final String pack, @Mktmp final Path temp) throws Exceptio
void convertsToXmirAndBack(final String pack, @Mktmp final Path temp) throws Exception {
final Xtory xtory = new XtSticky(new XtYaml(pack));
Assumptions.assumeTrue(xtory.map().get("skip") == null);
final String phi = xtory.map().get("with-sugar").toString();
final String phi = xtory.map().get("sweet").toString();
final String main = "target/phi/main.phi";
final Path path = Paths.get(main);
new HmBase(temp).save(phi, path);
Expand All @@ -244,6 +244,7 @@ void convertsToXmirAndBack(final String pack, @Mktmp final Path temp) throws Exc
final Path xmir = temp.resolve(String.format("target/%s/main.xmir", ParseMojo.DIR));
maven.foreignTojos().add("name").withXmir(xmir);
final Path result = maven
.with("conservative", xtory.map().get("conservative") != null)
.execute(OptimizeMojo.class)
.execute(PhiMojo.class)
.result()
Expand Down
34 changes: 26 additions & 8 deletions eo-parser/src/main/java/org/eolang/parser/Xmir.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
*/
@SuppressWarnings("PMD.TooManyMethods")
public final class Xmir implements XML {
/**
* Unhex transformation.
*/
private static final Shift UNHEX = new StUnhex();

/**
* Train of transformations that prepare XMIR for conversion to EO.
*/
Expand Down Expand Up @@ -175,7 +180,7 @@ public Collection<SAXParseException> validate(final XML xsd) {

/**
* Converts XMIR to EO.
* @return EO representation as {@code String}
* @return EO representation as {@link String}
*/
public String toEO() {
return this.converted(
Expand All @@ -185,7 +190,7 @@ public String toEO() {

/**
* Converts XMIR to EO, in reverse notation.
* @return EO representation as {@code String}
* @return EO representation as {@link String}
*/
public String toReversedEO() {
return this.converted(
Expand All @@ -195,14 +200,27 @@ public String toReversedEO() {

/**
* Converts XMIR to PHI.
* @return EO representation as {@code String}
* @return PHI representation as {@link String}
*/
public String toPhi() {
return this.toPhi(false);
}

/**
* Converts XMIR to PHI.
* @param conservative Add empty braces to formations or not
* @return PHI representation as {@link String}.
*/
public String toPhi(final boolean conservative) {
return this.converted(
new TrJoined<>(
Xmir.FOR_PHI,
new TrDefault<>(
new StUnhex(), new StClasspath("/org/eolang/parser/phi/to-phi.xsl")
Xmir.UNHEX,
new StClasspath(
"/org/eolang/parser/phi/to-phi.xsl",
String.format("conservative %b", conservative)
)
)
),
"program/phi/text()"
Expand All @@ -211,9 +229,9 @@ public String toPhi() {

/**
* Converts XMIR to PHI without any syntax sugar.
* @return EO representation as {@code String}
* @return PHI representation as {@link String}
*/
public String toPhiNoSugar() {
public String toSaltyPhi() {
return this.converted(
Xmir.FOR_PHI, "/org/eolang/parser/phi/to-phi-no-sugar.xsl", "program/phi/text()"
);
Expand All @@ -224,7 +242,7 @@ public String toPhiNoSugar() {
* @param train Train of transformations that prepares XMIR
* @param xsl Final XSL transformation
* @param xpath Xpath to retrieve the final result
* @return XMIR in other representation as {@code String}.
* @return XMIR in other representation as {@link String}.
*/
private String converted(final Train<Shift> train, final String xsl, final String xpath) {
return this.converted(new TrJoined<>(train.with(new StClasspath(xsl))), xpath);
Expand All @@ -234,7 +252,7 @@ private String converted(final Train<Shift> train, final String xsl, final Strin
* Converts XMIR.
* @param train Train of transformations that prepares XMIR
* @param xpath Xpath to retrieve the final result
* @return XMIR in other representation as {@code String}.
* @return XMIR in other representation as {@link String}.
*/
private String converted(final Train<Shift> train, final String xpath) {
return new Xsline(train).pass(this.xml).xpath(xpath).get(0);
Expand Down
11 changes: 8 additions & 3 deletions eo-parser/src/main/resources/org/eolang/parser/phi/to-phi.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ 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="to-phi" version="2.0">
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:eo="https://www.eolang.org" id="to-phi" version="2.0">
<xsl:import href="/org/eolang/parser/_funcs.xsl"/>
<xsl:output encoding="UTF-8" method="text"/>
<xsl:param name="conservative" as="xs:boolean"/>
<!-- Variables -->
<xsl:variable name="aliases" select="program/metas/meta/part[last()]"/>
<xsl:variable name="number-pattern" select="'^[0-9]+$'"/>
Expand Down Expand Up @@ -288,6 +289,10 @@ SOFTWARE.
<xsl:when test="$has-package">
<xsl:for-each select="$parts">
<xsl:value-of select="."/>
<xsl:if test="$conservative">
<xsl:value-of select="$clb"/>
<xsl:value-of select="$crb"/>
</xsl:if>
<xsl:value-of select="$arrow"/>
<xsl:value-of select="$lb"/>
<xsl:value-of select="eo:eol($tabs+position())"/>
Expand Down Expand Up @@ -511,7 +516,7 @@ SOFTWARE.
<xsl:variable name="name" select="eo:specials(@name, true())"/>
<xsl:if test="@name">
<xsl:value-of select="$name"/>
<xsl:if test="count(o[eo:void(.)])&gt;0">
<xsl:if test="$conservative or count(o[eo:void(.)])&gt;0">
<xsl:apply-templates select="." mode="inline-voids"/>
</xsl:if>
<xsl:value-of select="$arrow"/>
Expand Down Expand Up @@ -568,7 +573,7 @@ SOFTWARE.
<xsl:value-of select="$position - 1"/>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="eo:abstract(.) and o[eo:void(.)]">
<xsl:if test="eo:abstract(.) and ($conservative or o[eo:void(.)])">
<xsl:apply-templates select="." mode="inline-voids"/>
</xsl:if>
<xsl:value-of select="$arrow"/>
Expand Down
13 changes: 7 additions & 6 deletions eo-parser/src/test/java/org/eolang/parser/XmirTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,24 @@ void printsToEoReversed(final String pack) throws IOException {

@ParameterizedTest
@ClasspathSource(value = "org/eolang/parser/phi-packs", glob = "**.yaml")
void convertsToPhi(final String pack) throws IOException {
void convertsToSweetPhi(final String pack) throws IOException {
final Xtory xtory = new XtSticky(new XtYaml(pack));
final boolean conservative = xtory.map().containsKey("conservative");
MatcherAssert.assertThat(
"Result PHI should be equal to provided PHI with syntax sugar",
this.asXmir((String) xtory.map().get("input")).toPhi(),
Matchers.equalTo(xtory.map().get("with-sugar"))
this.asXmir((String) xtory.map().get("input")).toPhi(conservative),
Matchers.equalTo(xtory.map().get("sweet"))
);
}

@ParameterizedTest
@ClasspathSource(value = "org/eolang/parser/phi-packs", glob = "**.yaml")
void convertsToPhiNoSugar(final String pack) throws IOException {
void convertsToSaltyPhi(final String pack) throws IOException {
final Xtory xtory = new XtSticky(new XtYaml(pack));
MatcherAssert.assertThat(
"Result PHI should be equal to provided PHI with syntax sugar",
this.asXmir((String) xtory.map().get("input")).toPhiNoSugar(),
Matchers.equalTo(xtory.map().get("no-sugar"))
this.asXmir((String) xtory.map().get("input")).toSaltyPhi(),
Matchers.equalTo(xtory.map().get("salty"))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ input: |
length.
as-phi $
$.greater-than 0
with-sugar: |-
sweet: |-
{⟦
prints-itself ↦ ⟦
φ ↦ Φ̇.assert-that(Φ̇.as-phi(ξ).length, ξ.greater-than(0))
⟧}
no-sugar: |-
salty: |-
{
prints-itself ↦ ⟦
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,28 @@ input: |
# No comments.
[] > main /int
# No comments.
[] > outer
[x] > outer
# No comments.
[] > inner /bytes
with-sugar: |-
sweet: |-
{⟦
main ↦ ⟦
λ ⤍ Lmain
⟧,
outer ↦ ⟦
outer(x) ↦ ⟦
inner ↦ ⟦
λ ⤍ Louter_inner
⟧}
no-sugar: |-
salty: |-
{
main ↦ ⟦
λ ⤍ Lmain
⟧,
outer ↦ ⟦
x ↦ ∅,
inner ↦ ⟦
λ ⤍ Louter_inner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ input: |
x > first
[] >>
01- > b
with-sugar: |-
sweet: |-
{⟦
object ↦ ⟦
first ↦ Φ̇.x(ξ.auto-named-attr-at-4-6),
Expand All @@ -35,7 +35,7 @@ with-sugar: |-
⟧}
no-sugar: |-
salty: |-
{
object ↦ ⟦
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ input: |
w > @
5:five
"hello":3
with-sugar: |-
sweet: |-
{⟦
xyz ↦ Φ̇.x(
attr ↦ Φ̇.y,
Expand All @@ -38,7 +38,7 @@ with-sugar: |-
α3 ↦ "hello"
)
⟧}
no-sugar: |-
salty: |-
{
xyz ↦ Φ.org.eolang.x(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ input: |
01-.as-bytes.eq true
00-.as-bytes.eq false
with-sugar: |-
sweet: |-
{⟦
org ↦ ⟦
eolang ↦ ⟦
Expand Down Expand Up @@ -115,7 +115,7 @@ with-sugar: |-
λ ⤍ Package
⟧}
no-sugar: |-
salty: |-
{
org ↦ ⟦
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ input: |
# the current and the provided one,
# as a new sequence.
[b] > concat /bytes
with-sugar: |-
sweet: |-
{⟦
org ↦ ⟦
eolang ↦ ⟦
Expand Down Expand Up @@ -138,7 +138,7 @@ with-sugar: |-
λ ⤍ Package
⟧}
no-sugar: |-
salty: |-
{
org ↦ ⟦
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ input: |
# No comments.
[y] > main
x y > z
with-sugar: |-
sweet: |-
{⟦
foo ↦ ⟦
bar ↦ ⟦
Expand All @@ -39,7 +39,7 @@ with-sugar: |-
λ ⤍ Package
⟧}
no-sugar: |-
salty: |-
{
foo ↦ ⟦
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
input: |
5.plus 5 > ten
"Hello".concat "world" > greetings
with-sugar: |-
sweet: |-
{⟦
ten ↦ 5.plus(5),
greetings ↦ "Hello".concat("world")
⟧}
no-sugar: |-
salty: |-
{
ten ↦ Φ.org.eolang.number(
Expand Down
Loading

0 comments on commit b967900

Please sign in to comment.