Skip to content

Commit

Permalink
bug(#3768): phi can be void
Browse files Browse the repository at this point in the history
  • Loading branch information
maxonfjvipon committed Dec 27, 2024
1 parent fa89ddc commit ef4c37c
Show file tree
Hide file tree
Showing 21 changed files with 154 additions and 493 deletions.
1 change: 1 addition & 0 deletions eo-parser/src/main/antlr4/org/eolang/parser/Eo.g4
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ voids

// Void attribute
void: NAME
| PHI
;

// Type of atom
Expand Down
1 change: 1 addition & 0 deletions eo-parser/src/main/java/org/eolang/parser/TrCanonical.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public TrCanonical() {
new TrLogged(
new TrJoined<>(
new TrClasspath<>(
"/org/eolang/parser/move-voids-up.xsl",
"/org/eolang/parser/validate-before-stars.xsl",
"/org/eolang/parser/resolve-before-star.xsl"
).back(),
Expand Down
14 changes: 13 additions & 1 deletion eo-parser/src/main/java/org/eolang/parser/XeEoListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,19 @@ public void exitVoids(final EoParser.VoidsContext ctx) {

@Override
public void enterVoid(final EoParser.VoidContext ctx) {
this.startObject(ctx).prop("name", ctx.NAME().getText()).prop("base", "∅");
this.startObject(ctx);
final String name;
if (ctx.NAME() != null) {
name = ctx.NAME().getText();
} else if (ctx.PHI() != null) {
name = ctx.PHI().getText();
} else {
name = "";
}
if (!name.isEmpty()) {
this.objects.prop("name", name);
}
this.objects.prop("base", "∅");
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion eo-parser/src/main/java/org/eolang/parser/Xmir.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public String toPhi(final boolean conservative) {
*/
public String toSaltyPhi() {
return this.converted(
Xmir.FOR_PHI, "/org/eolang/parser/phi/to-phi-no-sugar.xsl", "program/phi/text()"
Xmir.FOR_PHI, "/org/eolang/parser/phi/to-salty-phi.xsl", "program/phi/text()"
);
}

Expand Down
53 changes: 53 additions & 0 deletions eo-parser/src/main/resources/org/eolang/parser/move-voids-up.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License (MIT)
Copyright (c) 2016-2024 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.
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:eo="https://www.eolang.org" id="move-voids-up" version="2.0">
<!--
Move void attributes on top
-->
<xsl:import href="/org/eolang/parser/_funcs.xsl"/>
<xsl:output encoding="UTF-8" method="xml"/>
<xsl:template match="o[count(o)&gt;0]">
<xsl:apply-templates select="." mode="with-attrs"/>
</xsl:template>
<xsl:template match="o[count(o[eo:void(.)])&gt;0 and count(o[not(eo:void(.))])&gt;0]" mode="with-attrs">
<xsl:apply-templates select="." mode="with-diff-attrs"/>
</xsl:template>
<xsl:template match="o[o[not(eo:void(.))]/following-sibling::o[eo:void(.)]]" mode="with-diff-attrs">
<xsl:element name="o">
<xsl:apply-templates select="@*"/>
<xsl:for-each select="o[eo:void(.)]">
<xsl:copy-of select="."/>
</xsl:for-each>
<xsl:for-each select="o[not(eo:void(.))]">
<xsl:apply-templates select="."/>
</xsl:for-each>
</xsl:element>
</xsl:template>
<xsl:template match="node()|@*" mode="#all">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
4 changes: 2 additions & 2 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 @@ -342,7 +342,7 @@ SOFTWARE.
</xsl:template>
<!-- Void attribute -->
<xsl:template match="o[eo:void(.)]">
<xsl:value-of select="@name"/>
<xsl:value-of select="eo:specials(@name, true())"/>
<xsl:value-of select="$arrow"/>
<xsl:value-of select="$empty"/>
</xsl:template>
Expand All @@ -354,7 +354,7 @@ SOFTWARE.
<xsl:value-of select="eo:comma(position(), -1)"/>
<xsl:value-of select="$space"/>
</xsl:if>
<xsl:value-of select="@name"/>
<xsl:value-of select="eo:specials(@name, true())"/>
</xsl:for-each>
<xsl:value-of select="$crb"/>
</xsl:template>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ 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-no-sugar" version="2.0">
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:eo="https://www.eolang.org" id="to-salty-phi" version="2.0">
<xsl:import href="/org/eolang/parser/_funcs.xsl"/>
<xsl:output encoding="UTF-8" method="text"/>
<!-- Variables -->
Expand Down Expand Up @@ -211,8 +211,8 @@ SOFTWARE.
</xsl:for-each>
</xsl:template>
<!-- Void attribute -->
<xsl:template match="o[@base=$empty]">
<xsl:value-of select="./@name"/>
<xsl:template match="o[eo:void(.)]">
<xsl:value-of select="eo:specials(@name, true())"/>
<xsl:value-of select="$arrow"/>
<xsl:value-of select="$empty"/>
</xsl:template>
Expand Down Expand Up @@ -241,7 +241,7 @@ SOFTWARE.
</xsl:choose>
</xsl:template>
<!-- Just object -->
<xsl:template match="o[@base and @base!=$empty]">
<xsl:template match="o[@base and not(eo:void(.))]">
<xsl:param name="tabs"/>
<xsl:param name="package"/>
<xsl:if test="@name">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ input: |
# No comments.
[tt a] > atom /int
# This unit test is supposed to check the functionality of the corresponding object.
[] > with-void-phi
[@] > x
"Hello" > hello
x 5 > five
and. > @
(five.plus 5).eq 10
five.hello.eq "Hello"
# This is very good object
# No comments.
[x] > first
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,18 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
---
file: org/eolang/snippets/parenting.eo
out:
- ".*123.*"
args: [ "org.eolang.snippets.parenting", "123" ]
eo: |
+package org.eolang.snippets
+alias org.eolang.io.stdout
sheets: [ ]
asserts:
- /program[not(errors)]
- /program/objects/o[@name='foo' and o[@name='x']/following-sibling::o[@name='@']]
- /program/objects/o[@name='bar' and o[@name='x']/following-sibling::o[@name='@']/o[@name='y']/following-sibling::o[@name='@']]
input: |
# No comments.
[args] > parenting
"The code snippet that checks parent's args" > description
[x] > app
5 > y
# Foo.
"Hello" > [x] > foo
# No comments.
[] > take
^.args.at 0 > @
stdout > @
take
# Bar.
z > [y] > [x] > bar
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
---
out:
- ".*Hello, Jeff!.*"
file: org/eolang/snippets/simple-horizontal.eo
args: [ "org.eolang.snippets.simple-horizontal" ]
eo: |
+package org.eolang.snippets
+alias org.eolang.io.stdout
# No comments.
[args] (stdout "Hello, Jeff!" > @) > simple-horizontal
input: |-
# Foo.
[@] > foo
sweet: |-
{⟦
foo(φ) ↦ ⟦⟧
⟧}
salty: |-
{
foo ↦ ⟦
φ ↦ ∅
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,7 @@
inline-voids(text, α0, φ) ↦ ⟦ ⟧,
empty-inline-voids() ↦ ⟦
num ↦ 42
⟧,
with-void-phi-sweet(φ) ↦ ⟦⟧,
with-void-phi-salty ↦ ⟦ φ ↦ ∅ ⟧
⟧}
4 changes: 3 additions & 1 deletion eo-runtime/src/main/java/org/eolang/PhDefault.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ public class PhDefault implements Phi, Cloneable {
/**
* Attribute name matcher.
*/
private static final Pattern SORTABLE = Pattern.compile("^[a-z].*$");
private static final Pattern SORTABLE = Pattern.compile(
String.format("^([a-z].*)|%s$", Attr.PHI)
);

/**
* Attributes nesting level.
Expand Down
22 changes: 22 additions & 0 deletions eo-runtime/src/test/eo/org/eolang/runtime-tests.eo
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,28 @@
build 2 > @
9

# This unit test is supposed to check the functionality of the corresponding object.
[] > with-void-phi
[@] > x
"Hello" > hello
x 5 > five
and. > @
(five.plus 5).eq 10
five.hello.eq "Hello"

# This unit test is supposed to check the functionality of the corresponding object.
[] (seq (* (five.eq 5) true) > @) (5 > five) > complex-horizontal

# This unit test is supposed to check the functionality of the corresponding object.
[] > vertical-bound-method
eq. > @
if.
true
"second":1
"first"
.as-bytes:0
"first"

# Nesting blah test.
[] > nesting-blah-test
blah0 > @
Expand Down
Loading

0 comments on commit ef4c37c

Please sign in to comment.