Skip to content

Commit

Permalink
Merge pull request #3767 from maxonfjvipon/bug/#3619/sugar-for-tuples
Browse files Browse the repository at this point in the history
bug(#3619): added sugar for compact tuples
  • Loading branch information
yegor256 authored Dec 26, 2024
2 parents d818680 + 12f0d08 commit fa89ddc
Show file tree
Hide file tree
Showing 22 changed files with 378 additions and 151 deletions.
6 changes: 6 additions & 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,12 @@ vapplicationHead
: applicable
| hmethod
| vmethod
| compactArray
;

// Compact arrays
compactArray
: NAME SPACE STAR INT?
;

// Vertical application head with optional name
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
70 changes: 55 additions & 15 deletions eo-parser/src/main/java/org/eolang/parser/XeEoListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,38 @@ public void exitVapplicationHead(final EoParser.VapplicationHeadContext ctx) {
// Nothing here
}

@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'
|| Integer.parseInt(num) < 0
) {
this.errors.add(
XeEoListener.error(
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())
.prop("before-star", count);
}

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

@Override
public void enterVapplicationHeadNamed(final EoParser.VapplicationHeadNamedContext ctx) {
// Nothing here
Expand Down Expand Up @@ -1054,21 +1086,7 @@ public void enterAs(final EoParser.AsContext ctx) {
} else {
final int index = Integer.parseInt(ctx.INT().getText());
if (index < 0) {
this.errors.add(
new ParsingException(
ctx.getStart().getLine(),
new MsgLocated(
ctx.getStart().getLine(),
ctx.getStart().getCharPositionInLine(),
"Object binding can't be negative"
).formatted(),
new MsgUnderlined(
XeEoListener.line(ctx),
ctx.getStart().getCharPositionInLine(),
ctx.getText().length()
).formatted()
)
);
this.errors.add(XeEoListener.error(ctx, "Object binding can't be negative"));
}
has = String.format("α%d", index);
}
Expand Down Expand Up @@ -1222,6 +1240,28 @@ private static String trimMargin(final String text, final int indent) {
return res.toString();
}

/**
* Create parsing exception from given context.
* @param ctx Context
* @param msg Error message
* @return Parsing exception from current context
*/
private static ParsingException error(final ParserRuleContext ctx, final String msg) {
return new ParsingException(
ctx.getStart().getLine(),
new MsgLocated(
ctx.getStart().getLine(),
ctx.getStart().getCharPositionInLine(),
msg
).formatted(),
new MsgUnderlined(
XeEoListener.line(ctx),
ctx.getStart().getCharPositionInLine(),
ctx.getText().length()
).formatted()
);
}

/**
* Get line from context.
* @param ctx Context
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
Loading

0 comments on commit fa89ddc

Please sign in to comment.