Skip to content

Commit

Permalink
bug(#3619): Sugar for tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
maxonfjvipon committed Dec 26, 2024
1 parent 8c39600 commit e06261a
Show file tree
Hide file tree
Showing 22 changed files with 326 additions and 159 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 @@ -231,6 +231,7 @@ vapplicationHead
: applicable
| hmethod
| vmethod
| compactArray
;

// Compact arrays
Expand Down
9 changes: 8 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 @@ -68,12 +68,19 @@ public final class EoSyntax implements Syntax {
* Ctor.
*
* @param ipt The EO program to parse
* @since 0.40.0
*/
public EoSyntax(final Input ipt) {
this("unknown", ipt);
}

/**
* Ctor.
* @param ipt The EO program to parse
*/
public EoSyntax(final String ipt) {
this("unknown", ipt);
}

/**
* Ctor.
*
Expand Down
4 changes: 4 additions & 0 deletions eo-parser/src/main/java/org/eolang/parser/TrCanonical.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public TrCanonical() {
new TrLambda(
new TrLogged(
new TrJoined<>(
new TrClasspath<>(
"/org/eolang/parser/validate-before-stars.xsl",
"/org/eolang/parser/resolve-before-star.xsl"
).back(),
new TrDefault<>(
new StEndless(
new StClasspath(
Expand Down
31 changes: 8 additions & 23 deletions eo-parser/src/main/java/org/eolang/parser/XeEoListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -524,47 +524,32 @@ public void exitVapplicationHead(final EoParser.VapplicationHeadContext ctx) {

@Override
public void enterCompactArray(final EoParser.CompactArrayContext ctx) {
final int count;
if (ctx.INT() != null) {
final String num = ctx.INT().getText();
if (num.charAt(0) == '+'
|| num.charAt(0) == '-'
|| (num.length() > 1 && num.charAt(0) == '0')
|| num.length() > 1 && num.charAt(0) == '0'
|| Integer.parseInt(num) < 0
) {
this.errors.put(
ctx,
"Index after '*' must be a positive integer without leading zero or arithmetic signs"
);
}
final int number = Integer.parseInt(num);
count = Math.max(number, 0);
} else {
count = 0;
}
this.startObject(ctx)
.prop("base", ctx.NAME().getText())
.start(
ctx.getStart().getLine(),
ctx.getStart().getCharPositionInLine() + ctx.NAME().getText().length() + 1
)
.prop("base", "tuple")
.prop("star");

// if (ctx.STAR() != null) {
// base = "tuple";
// this.objects.prop("star");
// } else if (ctx.NAME() != null) {
// base = ctx.NAME().getText();
// } else if (ctx.PHI() != null) {
// base = "@";
// } else {
// base = "";
// }
// if (!base.isEmpty()) {
// this.objects.prop("base", base);
// }
this.objects.leave();
.prop("before-star", count);
}

@Override
public void exitCompactArray(final EoParser.CompactArrayContext ctx) {
// Nothing here
this.objects.leave();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?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:xs="http://www.w3.org/2001/XMLSchema" id="resolve-before-star" version="2.0">
<!--
Converts such XMIR with @before-star attributes:
<o base="seq" before-star="2">
<o base="1".../>
<o base="2".../>
<o base="3".../>
<o base="4".../>
</o>
Into the next one:
<o base="seq">
<o base="1".../>
<o base="2".../>
<o base="tuple" star="">
<o base="3".../>
<o base="4".../>
</o>
</o>
-->
<xsl:output encoding="UTF-8" method="xml"/>
<xsl:template match="o[@before-star]">
<xsl:variable name="before" select="@before-star" as="xs:int"/>
<xsl:element name="o">
<xsl:for-each select="@* except @before-star">
<xsl:attribute name="{name()}" select="."/>
</xsl:for-each>
<xsl:for-each select="o[position() &lt;= $before]">
<xsl:apply-templates select="."/>
</xsl:for-each>
<xsl:element name="o">
<xsl:attribute name="base" select="'tuple'"/>
<xsl:attribute name="star"/>
<xsl:for-each select="o[position() &gt; $before]">
<xsl:apply-templates select="."/>
</xsl:for-each>
</xsl:element>
</xsl:element>
</xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?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:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" id="validate-before-stars" version="2.0">
<!--
Checks if index after '*' in compact array syntax is more than amount of arguments.
Correct:
```
sprintf *1
"Hello, %s"
"world"
```
Incorrect:
```
sprintf *3
"Hello, %s"
"world"
```
-->
<xsl:output encoding="UTF-8" method="xml"/>
<xsl:template match="/program">
<xsl:copy>
<xsl:apply-templates select="(node() except errors)|@*"/>
<xsl:variable name="errors" as="element()*">
<xsl:for-each select="//o[@before-star &gt; count(o)]">
<xsl:element name="error">
<xsl:attribute name="check" select="'validate-before-stars'"/>
<xsl:attribute name="line" select="if (@line) then @line else 0"/>
<xsl:attribute name="severity" select="'error'"/>
<xsl:text>Index after '*' (</xsl:text>
<xsl:value-of select="@before-star"/>
<xsl:text>) must be less than amount arguments (</xsl:text>
<xsl:value-of select="count(o)"/>
<xsl:text>)</xsl:text>
</xsl:element>
</xsl:for-each>
</xsl:variable>
<xsl:if test="not(empty($errors)) or exists(/program/errors)">
<errors>
<xsl:apply-templates select="/program/errors/error"/>
<xsl:copy-of select="$errors"/>
</errors>
</xsl:if>
</xsl:copy>
</xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
3 changes: 1 addition & 2 deletions eo-parser/src/test/java/org/eolang/parser/TrParsingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import com.jcabi.xml.XMLDocument;
import com.yegor256.xsline.TrClasspath;
import com.yegor256.xsline.Xsline;
import org.cactoos.io.InputOf;
import org.eolang.jucs.ClasspathSource;
import org.eolang.xax.XtSticky;
import org.eolang.xax.XtStrict;
Expand Down Expand Up @@ -92,7 +91,7 @@ void parsesPacks(final String yaml) {
yaml,
eo -> new EoSyntax(
"scenario",
new InputOf(String.format("%s\n", eo))
String.format("%s\n", eo)
).parsed(),
new TrParsing().empty()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ input: |
# This is very good object
# No comments.
[x] > first
sprintf *1 > xyz
"Hello, %s"
"Jeff"
x > @
second > hello
$.plus.@ 5 > i
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# 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.
---
sheets: [ ]
asserts:
- /program/errors/error[@line='3' and @severity='error']
- /program/objects//o[@name='first' and count(o)=1]/o[@base='tuple' and count(o)=2]
- /program/objects//o[@name='second' and count(o)=2]/o[position()=2 and @base='tuple' and count(o)=2]
- /program/objects//o[@name='third' and count(o)=3]/o[position()=3 and @base='tuple' and count(o)=2]
- /program/objects//o[@name='fourth' and count(o)=2]/o[@base='sprintf' and count(o)=1]/o[@base='tuple' and count(o)=2]
- /program/objects[count(//o[@before-star])=0]
input: |
# No comments.
[] > foo
sprintf *2 > with-error
x
sprintf * > first
x
y
sprintf *1 > second
x
y
z
sprintf *2 > third
x
y
z
sprintf *1 > fourth
sprintf *
x
y
z
2 changes: 1 addition & 1 deletion eo-runtime/src/main/eo/org/eolang/bytes.eo
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
error
sprintf
"Can't convert non 8 length bytes to a number, bytes are %x"
*
* ^

# Equals to another object.
[b] > eq /org.eolang.bool
Expand Down
25 changes: 11 additions & 14 deletions eo-runtime/src/main/eo/org/eolang/fs/dir.eo
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@
if. > @
^.exists
^
seq
*
mkdir
^
seq *
mkdir
^

# Makes a directory together with all required
# parent directories.
Expand All @@ -64,10 +63,9 @@
walked.length > len!
if. > @
^.exists
seq
*
rec-delete walked 0
^
seq *
rec-delete walked 0
^
^

# Deletes files and directories in current directory recursively.
Expand All @@ -79,12 +77,11 @@
if. > @
^.len.eq index
true
seq
*
tup.tail.deleted.exists
^.rec-delete
tup.head
index.plus 1
seq *
tup.tail.deleted.exists
^.rec-delete
tup.head
index.plus 1

# Creates an empty temporary file in the current directory.
[] > tmpfile
Expand Down
Loading

0 comments on commit e06261a

Please sign in to comment.