Skip to content

Commit a800acf

Browse files
committed
bug(#3999): hexing
1 parent 35aea80 commit a800acf

8 files changed

Lines changed: 162 additions & 39 deletions

File tree

eo-parser/src/main/java/org/eolang/parser/EoSyntax.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import com.jcabi.xml.XMLDocument;
1010
import com.yegor256.xsline.Shift;
1111
import com.yegor256.xsline.TrClasspath;
12+
import com.yegor256.xsline.TrDefault;
13+
import com.yegor256.xsline.TrJoined;
1214
import com.yegor256.xsline.Train;
1315
import com.yegor256.xsline.Xsline;
1416
import java.io.IOException;
@@ -78,22 +80,25 @@ public final class EoSyntax implements Syntax {
7880
*/
7981
private static final Function<XML, XML> CANONICAL = new Xsline(
8082
new TrFull(
81-
new TrClasspath<>(
82-
"/org/eolang/parser/parse/move-voids-up.xsl",
83-
"/org/eolang/parser/parse/validate-before-stars.xsl",
84-
"/org/eolang/parser/parse/resolve-before-stars.xsl",
85-
"/org/eolang/parser/parse/wrap-method-calls.xsl",
86-
"/org/eolang/parser/parse/const-to-dataized.xsl",
87-
"/org/eolang/parser/parse/stars-to-tuples.xsl",
88-
"/org/eolang/parser/parse/vars-float-up.xsl",
89-
"/org/eolang/parser/parse/build-fqns.xsl",
90-
"/org/eolang/parser/parse/expand-qqs.xsl",
91-
"/org/eolang/parser/parse/expand-aliases.xsl",
92-
"/org/eolang/parser/parse/resolve-aliases.xsl",
93-
"/org/eolang/parser/parse/add-default-package.xsl",
94-
"/org/eolang/parser/parse/explicit-data.xsl",
95-
"/org/eolang/parser/parse/roll-bases.xsl"
96-
).back()
83+
new TrJoined<>(
84+
new TrClasspath<>(
85+
"/org/eolang/parser/parse/move-voids-up.xsl",
86+
"/org/eolang/parser/parse/validate-before-stars.xsl",
87+
"/org/eolang/parser/parse/resolve-before-stars.xsl",
88+
"/org/eolang/parser/parse/wrap-method-calls.xsl",
89+
"/org/eolang/parser/parse/const-to-dataized.xsl",
90+
"/org/eolang/parser/parse/stars-to-tuples.xsl",
91+
"/org/eolang/parser/parse/vars-float-up.xsl",
92+
"/org/eolang/parser/parse/build-fqns.xsl",
93+
"/org/eolang/parser/parse/expand-qqs.xsl",
94+
"/org/eolang/parser/parse/expand-aliases.xsl",
95+
"/org/eolang/parser/parse/resolve-aliases.xsl",
96+
"/org/eolang/parser/parse/add-default-package.xsl",
97+
"/org/eolang/parser/parse/explicit-data.xsl",
98+
"/org/eolang/parser/parse/roll-bases.xsl"
99+
).back(),
100+
new TrDefault<>(new StHex())
101+
)
97102
)
98103
)::pass;
99104

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* SPDX-FileCopyrightText: Copyright (c) 2016-2025 Objectionary.com
3+
* SPDX-License-Identifier: MIT
4+
*/
5+
package org.eolang.parser;
6+
7+
import com.yegor256.xsline.Shift;
8+
import com.yegor256.xsline.StEnvelope;
9+
import java.nio.ByteBuffer;
10+
import org.w3c.dom.Element;
11+
import org.w3c.dom.Node;
12+
13+
/**
14+
* This {@link Shift} turns regular data (numbers) inside XMIR.
15+
* into hexadecimal representation.
16+
*
17+
* @since 0.54
18+
*/
19+
final class StHex extends StEnvelope {
20+
/**
21+
* Ctor.
22+
*/
23+
StHex() {
24+
super(
25+
new StXnav(
26+
"st-hex",
27+
"//o[@hex]",
28+
xnav -> {
29+
final Node node = xnav.node();
30+
node.setTextContent(
31+
new BytesToHex(
32+
ByteBuffer
33+
.allocate(Double.BYTES)
34+
.putDouble(Double.parseDouble(xnav.text().orElseThrow()))
35+
.array()
36+
).get()
37+
);
38+
((Element) node).removeAttribute("hex");
39+
}
40+
)
41+
);
42+
}
43+
}

eo-parser/src/main/java/org/eolang/parser/StXnav.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*
1818
* @since 0.53.0
1919
*/
20-
public final class StXnav implements Shift {
20+
final class StXnav implements Shift {
2121
/**
2222
* XPath to search for.
2323
*/
@@ -28,19 +28,35 @@ public final class StXnav implements Shift {
2828
*/
2929
private final Consumer<Xnav> fun;
3030

31+
/**
32+
* UID.
33+
*/
34+
private final String identifier;
35+
36+
/**
37+
* Ctor.
38+
* @param path The XPath
39+
* @param func The function
40+
*/
41+
StXnav(final String path, final Consumer<Xnav> func) {
42+
this("st-xnav", path, func);
43+
}
44+
3145
/**
3246
* Ctor.
47+
* @param identifier UID
3348
* @param path The XPath
3449
* @param func The function
3550
*/
36-
public StXnav(final String path, final Consumer<Xnav> func) {
51+
StXnav(final String identifier, final String path, final Consumer<Xnav> func) {
52+
this.identifier = identifier;
3753
this.xpath = path;
3854
this.fun = func;
3955
}
4056

4157
@Override
4258
public String uid() {
43-
return this.getClass().getSimpleName();
59+
return this.identifier;
4460
}
4561

4662
@Override

eo-parser/src/main/resources/org/eolang/parser/parse/stars-to-tuples.xsl

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,24 @@
3939
</xsl:element>
4040
</xsl:variable>
4141
<xsl:element name="o">
42-
<xsl:attribute name="base" select="'.with'"/>
42+
<xsl:attribute name="base" select="'Q.org.eolang.tuple'"/>
4343
<xsl:apply-templates select="@* except (@star | @base)"/>
4444
<xsl:apply-templates select="$nested"/>
4545
<xsl:apply-templates select="o[last()]"/>
46+
<xsl:element name="o">
47+
<xsl:attribute name="base" select="'Q.org.eolang.number'"/>
48+
<xsl:element name="o">
49+
<xsl:attribute name="base" select="'Q.org.eolang.bytes'"/>
50+
<xsl:attribute name="hex"/>
51+
<xsl:value-of select="count(o)"/>
52+
</xsl:element>
53+
</xsl:element>
4654
</xsl:element>
4755
</xsl:when>
4856
<xsl:otherwise>
4957
<xsl:element name="o">
50-
<xsl:attribute name="base" select="'.empty'"/>
58+
<xsl:attribute name="base" select="'Q.org.eolang.tuple.empty'"/>
5159
<xsl:apply-templates select="@* except (@star | @base)"/>
52-
<xsl:element name="o">
53-
<xsl:attribute name="base" select="'Q.org.eolang.tuple'"/>
54-
</xsl:element>
5560
</xsl:element>
5661
</xsl:otherwise>
5762
</xsl:choose>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* SPDX-FileCopyrightText: Copyright (c) 2016-2025 Objectionary.com
3+
* SPDX-License-Identifier: MIT
4+
*/
5+
package org.eolang.parser;
6+
7+
import com.jcabi.matchers.XhtmlMatchers;
8+
import com.jcabi.xml.XMLDocument;
9+
import com.yegor256.xsline.Xsline;
10+
import org.hamcrest.MatcherAssert;
11+
import org.hamcrest.Matchers;
12+
import org.junit.jupiter.params.ParameterizedTest;
13+
import org.junit.jupiter.params.provider.CsvSource;
14+
15+
/**
16+
* Test case for {@link StHex}.
17+
*
18+
* @since 0.54.0
19+
*/
20+
final class StHexTest {
21+
@ParameterizedTest
22+
@CsvSource({
23+
"1, 3F-F0-00-00-00-00-00-00",
24+
"2, 40-00-00-00-00-00-00-00",
25+
"3, 40-08-00-00-00-00-00-00",
26+
"10, 40-24-00-00-00-00-00-00",
27+
})
28+
void convertsNumberToHex(final String number, final String bytes) {
29+
MatcherAssert.assertThat(
30+
String.format(
31+
"StHex must convert number '%s' to '%s' bytes and remove @hex attribute",
32+
number, bytes
33+
),
34+
new Xsline(new StHex()).pass(
35+
new XMLDocument(
36+
String.format(
37+
"<p><o base='Q.org.eolang.number'><o base='Q.org.eolang.bytes' hex=''>%s</o></o></p>",
38+
number
39+
)
40+
)
41+
),
42+
Matchers.allOf(
43+
XhtmlMatchers.hasXPath(String.format("//o[text()='%s']", bytes)),
44+
XhtmlMatchers.hasXPath("/p[not(//o[@hex])]")
45+
)
46+
);
47+
}
48+
}

eo-parser/src/test/resources/org/eolang/parser/eo-packs/parse/explicit-data.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ asserts:
1616
- //o[@base='Q.org.eolang.bytes' and @name='fourth' and not(o) and text()='11-21']
1717
- //o[@base='.string' and @name='str' and count(o)=2 and o[last() and @base='Q.org.eolang.bytes' and text()='48-65-79']]
1818
- //o[@base='Q.org.eolang.bytes' and @name='bt' and count(o)=0 and text()='A2-']
19-
- //o[@base='.with']/o[@base='.with']/o[@base='.with']/o[@base='Q.org.eolang.number']/o[@base='Q.org.eolang.bytes' and text()='3F-F0-00-00-00-00-00-00']
19+
- //o[@base='Q.org.eolang.tuple']/o[@base='Q.org.eolang.tuple']/o[@base='Q.org.eolang.tuple']/o[@base='Q.org.eolang.number']/o[@base='Q.org.eolang.bytes' and text()='3F-F0-00-00-00-00-00-00']
2020
input: |
2121
42 > first
2222
number 11-21 > bts

eo-parser/src/test/resources/org/eolang/parser/eo-packs/parse/roll-bases.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ asserts:
2121
- //o[@base='$.two.a' and @name='one']
2222
- //o[@base='$.three.b' and @name='two']
2323
- //o[@base='Q.org.eolang.c' and @name='three']
24-
- //o[@base='.with']/o[@base='Q.org.eolang.tuple.empty.with' and o[@base='Q.org.eolang.number']]
24+
- //o[@base='Q.org.eolang.tuple']/o[@base='Q.org.eolang.tuple' and o[@base='Q.org.eolang.tuple.empty'] and o[@base='Q.org.eolang.number']]
2525
- //o[@base='Q.org.eolang.true.if' and count(o)=2]
2626
- //o[@base='.if' and count(o)=3]/o[1][@base='$.x']/o[1][@base='Q.org.eolang.y']
2727
- //o[@base='.and']/o[1][@base='.eq']/o[1][@base='.as-bytes']/o[@base='Q.org.eolang.bytes' and text()!='']

eo-parser/src/test/resources/org/eolang/parser/eo-packs/parse/stars-to-tuples.yaml

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,32 @@ asserts:
88
- //objects[count(o)=7]
99
- //objects[count(//o[@star])=0]
1010
# first
11-
- //o[@base='.empty' and @line=1 and @name='xs']/o[@base='Q.org.eolang.tuple' and not(@name)]
11+
- //o[@base='Q.org.eolang.tuple.empty' and @line=1 and @name='xs']
1212
# second
13-
- //o[@base='.with' and @line=2 and @name='xl']/o[@base='.empty']/o[@base='Q.org.eolang.tuple']
14-
- //o[@base='.with' and @line=2 and @name='xl']/o[@base='Q.org.eolang.number']
13+
- //o[@base='Q.org.eolang.tuple' and @line=2 and @name='xl']/o[1][@base='Q.org.eolang.tuple.empty']
14+
- //o[@base='Q.org.eolang.tuple' and @line=2 and @name='xl']/o[2][@base='Q.org.eolang.number']
15+
- //o[@base='Q.org.eolang.tuple' and @line=2 and @name='xl']/o[3][@base='Q.org.eolang.number']/o[1][@hex and text()='1']
1516
# third
16-
- //o[@base='.with' and @line=3 and not(@name)]/o[@base='.with']/o[@base='.empty']/o[@base='Q.org.eolang.tuple']
17-
- //o[@base='.with' and @line=3 and not(@name)]/o[@base='.with']/o[@base='Q.org.eolang.number']
18-
- //o[@base='.with' and @line=3 and not(@name)]/o[@base='Q.org.eolang.number']
17+
- //o[@base='Q.org.eolang.tuple' and @line=3]/o[1][@base='Q.org.eolang.tuple']/o[1][@base='Q.org.eolang.tuple.empty']
18+
- //o[@base='Q.org.eolang.tuple' and @line=3]/o[1][@base='Q.org.eolang.tuple']/o[2][@base='Q.org.eolang.number']
19+
- //o[@base='Q.org.eolang.tuple' and @line=3]/o[1][@base='Q.org.eolang.tuple']/o[3][@base='Q.org.eolang.number']/o[1][@hex and text()='1']
20+
- //o[@base='Q.org.eolang.tuple' and @line=3]/o[2][@base='Q.org.eolang.number']
21+
- //o[@base='Q.org.eolang.tuple' and @line=3]/o[3][@base='Q.org.eolang.number']/o[1][@hex and text()='2']
1922
# fourth
20-
- //o[@base='.empty' and @line=4]/o[@base='.empty']/o[@base='Q.org.eolang.tuple']
23+
- //o[@base='.empty' and @line=4]/o[@base='Q.org.eolang.tuple.empty']
2124
# fifth
22-
- //o[@base='.with' and @line=5 and not(@name)]/o[@base='.with']/o[@base='.with']/o[@base='.empty']/o[@base='Q.org.eolang.tuple']
23-
- //o[@base='.with' and @line=5 and not(@name)]/o[@base='.with']/o[@base='.with']/o[@base='.c']
24-
- //o[@base='.with' and @line=5 and not(@name)]/o[@base='.with']/o[@base='.f']
25-
- //o[@base='.with' and @line=5 and not(@name)]/o[@base='.i']
25+
- //o[@base='Q.org.eolang.tuple' and @line=5]/o[1][@base='Q.org.eolang.tuple']/o[1][@base='Q.org.eolang.tuple']/o[1][@base='Q.org.eolang.tuple.empty']
26+
- //o[@base='Q.org.eolang.tuple' and @line=5]/o[1][@base='Q.org.eolang.tuple']/o[1][@base='Q.org.eolang.tuple']/o[2][@base='.c']
27+
- //o[@base='Q.org.eolang.tuple' and @line=5]/o[1][@base='Q.org.eolang.tuple']/o[1][@base='Q.org.eolang.tuple']/o[3][@base='Q.org.eolang.number']/o[@hex and text()='1']
28+
- //o[@base='Q.org.eolang.tuple' and @line=5]/o[1][@base='Q.org.eolang.tuple']/o[2][@base='.f']
29+
- //o[@base='Q.org.eolang.tuple' and @line=5]/o[1][@base='Q.org.eolang.tuple']/o[3][@base='Q.org.eolang.number']/o[@hex and text()='2']
30+
- //o[@base='Q.org.eolang.tuple' and @line=5]/o[2][@base='.i']
31+
- //o[@base='Q.org.eolang.tuple' and @line=5]/o[3][@base='Q.org.eolang.number']/o[@hex and text()='3']
2632
# sixth
27-
- //o[@base='.elements']/o[@base='arr']/o[@base='.empty']/o[@base='Q.org.eolang.tuple']
33+
- //o[@base='.elements']/o[@base='arr']/o[@base='Q.org.eolang.tuple.empty']
2834
# seventh
2935
- //o[@base='.reduced']/o[@base='list']
30-
- //o[@base='.reduced']/o[@base='.empty']/o[@base='Q.org.eolang.tuple']
36+
- //o[@base='.reduced']/o[@base='Q.org.eolang.tuple.empty']
3137
- //o[@base='.reduced']/o[not(@base)]/o[@base='∅' and @name='x']
3238
input: |
3339
* > xs

0 commit comments

Comments
 (0)