From 3f81e85f9fc54fe6c08e1af7e0eadd1f8b23688d Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Thu, 12 Dec 2024 16:02:16 +0300 Subject: [PATCH 1/2] fix(#3646): fixed converting to phi for primitives --- .../main/java/org/eolang/parser/StUnhex.java | 25 ++-- .../resources/org/eolang/parser/add-refs.xsl | 11 +- .../java/org/eolang/parser/StUnhexTest.java | 4 +- ...o-bytes.yaml => no-ref-to-primitives.yaml} | 10 +- .../org/eolang/parser/phi-packs/nan-part.yaml | 46 +++++++ .../parser/phi-packs/negative-inf-part.yaml | 46 +++++++ .../eolang/parser/phi-packs/number-part.yaml | 65 ++++++++++ .../parser/phi-packs/positive-inf-part.yaml | 46 +++++++ .../eolang/parser/phi-packs/string-part.yaml | 117 ++++++++++++++++++ 9 files changed, 353 insertions(+), 17 deletions(-) rename eo-parser/src/test/resources/org/eolang/parser/eo-packs/{no-ref-to-bytes.yaml => no-ref-to-primitives.yaml} (93%) create mode 100644 eo-parser/src/test/resources/org/eolang/parser/phi-packs/nan-part.yaml create mode 100644 eo-parser/src/test/resources/org/eolang/parser/phi-packs/negative-inf-part.yaml create mode 100644 eo-parser/src/test/resources/org/eolang/parser/phi-packs/number-part.yaml create mode 100644 eo-parser/src/test/resources/org/eolang/parser/phi-packs/positive-inf-part.yaml create mode 100644 eo-parser/src/test/resources/org/eolang/parser/phi-packs/string-part.yaml diff --git a/eo-parser/src/main/java/org/eolang/parser/StUnhex.java b/eo-parser/src/main/java/org/eolang/parser/StUnhex.java index 14cc9cad1a..1d1ac89c57 100644 --- a/eo-parser/src/main/java/org/eolang/parser/StUnhex.java +++ b/eo-parser/src/main/java/org/eolang/parser/StUnhex.java @@ -51,13 +51,18 @@ public StUnhex() { StUnhex.class.getSimpleName(), new StXPath( StUnhex.xpath("number"), - xml -> StUnhex.append( - StUnhex.number( - StUnhex.buffer( - StUnhex.undash(xml.xpath("./o/text()").get(0)) - ).getDouble() - ) - ) + xml -> { + final double number = StUnhex.buffer( + StUnhex.undash(xml.xpath("./o/text()").get(0)) + ).getDouble(); + final Iterable dirs; + if (Double.isNaN(number) || Double.isInfinite(number)) { + dirs = new Directives().attr("skip", ""); + } else { + dirs = StUnhex.append(StUnhex.number(number)); + } + return dirs; + } ), new StXPath( StUnhex.xpath("string"), @@ -74,10 +79,6 @@ public StUnhex() { ) ) ) - ), - new StXPath( - "(//o[@data='bytes' and (@base='bytes' or @base='org.eolang.bytes') and empty(text())]/parent::o[(@base='string' or @base='org.eolang.string')])[1]", - xml -> new Directives().set("").attr("data", "string") ) ) ) @@ -147,7 +148,7 @@ private static String undash(final String txt) { */ private static String xpath(final String type) { return String.format( - "(//o[not(o) and string-length(normalize-space(text())) > 0 and (@base='bytes' or @base='org.eolang.bytes') and not(empty(text()))]/parent::o[(@base='%s' or @base='org.eolang.%1$s')])[1]", + "(//o[(@base='%s' or @base='org.eolang.%1$s') and(not(@skip)) and o[not(o) and string-length(normalize-space(text()))>0 and (@base='bytes' or @base='org.eolang.bytes')]])[1]", type ); } diff --git a/eo-parser/src/main/resources/org/eolang/parser/add-refs.xsl b/eo-parser/src/main/resources/org/eolang/parser/add-refs.xsl index e708e66174..e79bebae3f 100644 --- a/eo-parser/src/main/resources/org/eolang/parser/add-refs.xsl +++ b/eo-parser/src/main/resources/org/eolang/parser/add-refs.xsl @@ -39,10 +39,15 @@ SOFTWARE. --> - - + + bytes + string + number + + + - + diff --git a/eo-parser/src/test/java/org/eolang/parser/StUnhexTest.java b/eo-parser/src/test/java/org/eolang/parser/StUnhexTest.java index 3343a3b4b7..bbac7d0fee 100644 --- a/eo-parser/src/test/java/org/eolang/parser/StUnhexTest.java +++ b/eo-parser/src/test/java/org/eolang/parser/StUnhexTest.java @@ -58,7 +58,9 @@ void convertsMaxIntFromHexToEo() { "

FF-FF-FF-FF-FF-FF-FF-FF

" ) ), - XhtmlMatchers.hasXPaths("//o[text()='NaN']") + XhtmlMatchers.hasXPaths( + "//o[@base='number' and @skip and o[@base='org.eolang.bytes' and text()!='']]" + ) ); } diff --git a/eo-parser/src/test/resources/org/eolang/parser/eo-packs/no-ref-to-bytes.yaml b/eo-parser/src/test/resources/org/eolang/parser/eo-packs/no-ref-to-primitives.yaml similarity index 93% rename from eo-parser/src/test/resources/org/eolang/parser/eo-packs/no-ref-to-bytes.yaml rename to eo-parser/src/test/resources/org/eolang/parser/eo-packs/no-ref-to-primitives.yaml index 0e3c844d98..a9e4c37b27 100644 --- a/eo-parser/src/test/resources/org/eolang/parser/eo-packs/no-ref-to-bytes.yaml +++ b/eo-parser/src/test/resources/org/eolang/parser/eo-packs/no-ref-to-primitives.yaml @@ -28,6 +28,14 @@ asserts: input: | +package org.eolang - # No comments. + # Bytes. [] > bytes $.eq 01- > yes + + # String. + [] > string + string > x + + # Number. + [] > number + number > x diff --git a/eo-parser/src/test/resources/org/eolang/parser/phi-packs/nan-part.yaml b/eo-parser/src/test/resources/org/eolang/parser/phi-packs/nan-part.yaml new file mode 100644 index 0000000000..d2eb4525ee --- /dev/null +++ b/eo-parser/src/test/resources/org/eolang/parser/phi-packs/nan-part.yaml @@ -0,0 +1,46 @@ +# 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. +--- +input: | + # Nan. + [] > nan + number 7F-F8-00-00-00-00-00-00 > @ +with-sugar: |- + { + ⟦ + nan ↦ ⟦ + φ ↦ Φ̇.number(Φ̇.bytes(⟦ Δ ⤍ 7F-F8-00-00-00-00-00-00 ⟧)) + ⟧ + ⟧ + } +no-sugar: |- + { + ⟦ + nan ↦ ⟦ + φ ↦ Φ.org.eolang.number( + α0 ↦ Φ.org.eolang.bytes( + α0 ↦ ⟦ Δ ⤍ 7F-F8-00-00-00-00-00-00 ⟧ + ) + ) + ⟧ + ⟧ + } diff --git a/eo-parser/src/test/resources/org/eolang/parser/phi-packs/negative-inf-part.yaml b/eo-parser/src/test/resources/org/eolang/parser/phi-packs/negative-inf-part.yaml new file mode 100644 index 0000000000..00cffa85b5 --- /dev/null +++ b/eo-parser/src/test/resources/org/eolang/parser/phi-packs/negative-inf-part.yaml @@ -0,0 +1,46 @@ +# 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. +--- +input: | + # Foo. + [] > negative-infinity + number FF-F0-00-00-00-00-00-00 > @ +with-sugar: |- + { + ⟦ + negative-infinity ↦ ⟦ + φ ↦ Φ̇.number(Φ̇.bytes(⟦ Δ ⤍ FF-F0-00-00-00-00-00-00 ⟧)) + ⟧ + ⟧ + } +no-sugar: |- + { + ⟦ + negative-infinity ↦ ⟦ + φ ↦ Φ.org.eolang.number( + α0 ↦ Φ.org.eolang.bytes( + α0 ↦ ⟦ Δ ⤍ FF-F0-00-00-00-00-00-00 ⟧ + ) + ) + ⟧ + ⟧ + } diff --git a/eo-parser/src/test/resources/org/eolang/parser/phi-packs/number-part.yaml b/eo-parser/src/test/resources/org/eolang/parser/phi-packs/number-part.yaml new file mode 100644 index 0000000000..c4d1a3ec56 --- /dev/null +++ b/eo-parser/src/test/resources/org/eolang/parser/phi-packs/number-part.yaml @@ -0,0 +1,65 @@ +# 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. +--- +input: | + +package org.eolang + +alias org.eolang.txt.sprintf + + # The `number` object is an abstraction of a 64-bit floating-point + [as-bytes] > number + $.times -1 > neg +with-sugar: |- + { + ⟦ + org ↦ ⟦ + eolang ↦ ⟦ + number ↦ ⟦ + as-bytes ↦ ∅, + neg ↦ ξ.times(-1) + ⟧, + λ ⤍ Package + ⟧, + λ ⤍ Package + ⟧ + ⟧ + } +no-sugar: |- + { + ⟦ + org ↦ ⟦ + eolang ↦ ⟦ + number ↦ ⟦ + as-bytes ↦ ∅, + neg ↦ ξ.times( + α0 ↦ Φ.org.eolang.number( + α0 ↦ Φ.org.eolang.bytes( + α0 ↦ ⟦ Δ ⤍ BF-F0-00-00-00-00-00-00 ⟧ + ) + ) + ) + ⟧, + λ ⤍ Package + ⟧, + λ ⤍ Package + ⟧ + ⟧ + } \ No newline at end of file diff --git a/eo-parser/src/test/resources/org/eolang/parser/phi-packs/positive-inf-part.yaml b/eo-parser/src/test/resources/org/eolang/parser/phi-packs/positive-inf-part.yaml new file mode 100644 index 0000000000..033f272e4a --- /dev/null +++ b/eo-parser/src/test/resources/org/eolang/parser/phi-packs/positive-inf-part.yaml @@ -0,0 +1,46 @@ +# 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. +--- +input: | + # Foo. + [] > positive-infinity + number 7F-F0-00-00-00-00-00-00 > @ +with-sugar: |- + { + ⟦ + positive-infinity ↦ ⟦ + φ ↦ Φ̇.number(Φ̇.bytes(⟦ Δ ⤍ 7F-F0-00-00-00-00-00-00 ⟧)) + ⟧ + ⟧ + } +no-sugar: |- + { + ⟦ + positive-infinity ↦ ⟦ + φ ↦ Φ.org.eolang.number( + α0 ↦ Φ.org.eolang.bytes( + α0 ↦ ⟦ Δ ⤍ 7F-F0-00-00-00-00-00-00 ⟧ + ) + ) + ⟧ + ⟧ + } diff --git a/eo-parser/src/test/resources/org/eolang/parser/phi-packs/string-part.yaml b/eo-parser/src/test/resources/org/eolang/parser/phi-packs/string-part.yaml new file mode 100644 index 0000000000..3b6568341f --- /dev/null +++ b/eo-parser/src/test/resources/org/eolang/parser/phi-packs/string-part.yaml @@ -0,0 +1,117 @@ +# 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. +--- +input: | + +package org.eolang + +alias org.eolang.txt.sprintf + + # String. + [as-bytes] > string + # Calculates the length of the string. + [] > length + # The object checks if `index` + `char-size` is out of string bounds. + # If not - the new recursion iteration is started with `index` increased by `char-size` and + # `accum` increased by 1. + [index char-size len] > increase-length + if. > @ + (index.plus char-size).gt ^.size + error + sprintf + "Expected %d byte character at %d index, but there are not enough bytes for it: %x" + * char-size index ^.^.as-bytes +with-sugar: |- + { + ⟦ + org ↦ ⟦ + eolang ↦ ⟦ + string ↦ ⟦ + as-bytes ↦ ∅, + length ↦ ⟦ + increase-length ↦ ⟦ + index ↦ ∅, + char-size ↦ ∅, + len ↦ ∅, + φ ↦ ξ.index.plus(ξ.char-size).gt(ξ.ρ.size).if( + Φ̇.error( + Φ̇.txt.sprintf( + "Expected %d byte character at %d index, but there are not enough bytes for it: %x", + Φ̇.tuple( + Φ̇.tuple(Φ̇.tuple(Φ̇.tuple.empty, ξ.char-size), ξ.index), ξ.ρ.ρ.as-bytes + ) + ) + ) + ) + ⟧ + ⟧ + ⟧, + λ ⤍ Package + ⟧, + λ ⤍ Package + ⟧ + ⟧ + } +no-sugar: |- + { + ⟦ + org ↦ ⟦ + eolang ↦ ⟦ + string ↦ ⟦ + as-bytes ↦ ∅, + length ↦ ⟦ + increase-length ↦ ⟦ + index ↦ ∅, + char-size ↦ ∅, + len ↦ ∅, + φ ↦ ξ.index.plus( + α0 ↦ ξ.char-size + ).gt( + α0 ↦ ξ.ρ.size + ).if( + α0 ↦ Φ.org.eolang.error( + α0 ↦ Φ.org.eolang.txt.sprintf( + α0 ↦ Φ.org.eolang.string( + α0 ↦ Φ.org.eolang.bytes( + α0 ↦ ⟦ Δ ⤍ 45-78-70-65-63-74-65-64-20-25-64-20-62-79-74-65-20-63-68-61-72-61-63-74-65-72-20-61-74-20-25-64-20-69-6E-64-65-78-2C-20-62-75-74-20-74-68-65-72-65-20-61-72-65-20-6E-6F-74-20-65-6E-6F-75-67-68-20-62-79-74-65-73-20-66-6F-72-20-69-74-3A-20-25-78 ⟧ + ) + ), + α1 ↦ Φ.org.eolang.tuple( + α0 ↦ Φ.org.eolang.tuple( + α0 ↦ Φ.org.eolang.tuple( + α0 ↦ Φ.org.eolang.tuple.empty, + α1 ↦ ξ.char-size + ), + α1 ↦ ξ.index + ), + α1 ↦ ξ.ρ.ρ.as-bytes + ) + ) + ) + ) + ⟧ + ⟧ + ⟧, + λ ⤍ Package + ⟧, + λ ⤍ Package + ⟧ + ⟧ + } From 2790ee7824a1502f153530b0c7e01358bcd2a7a3 Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Thu, 12 Dec 2024 16:07:37 +0300 Subject: [PATCH 2/2] fix(#3646): yamlint --- .../org/eolang/parser/eo-packs/no-ref-to-primitives.yaml | 2 +- .../test/resources/org/eolang/parser/phi-packs/number-part.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/eo-parser/src/test/resources/org/eolang/parser/eo-packs/no-ref-to-primitives.yaml b/eo-parser/src/test/resources/org/eolang/parser/eo-packs/no-ref-to-primitives.yaml index a9e4c37b27..22ff10a545 100644 --- a/eo-parser/src/test/resources/org/eolang/parser/eo-packs/no-ref-to-primitives.yaml +++ b/eo-parser/src/test/resources/org/eolang/parser/eo-packs/no-ref-to-primitives.yaml @@ -31,7 +31,7 @@ input: | # Bytes. [] > bytes $.eq 01- > yes - + # String. [] > string string > x diff --git a/eo-parser/src/test/resources/org/eolang/parser/phi-packs/number-part.yaml b/eo-parser/src/test/resources/org/eolang/parser/phi-packs/number-part.yaml index c4d1a3ec56..19937bd564 100644 --- a/eo-parser/src/test/resources/org/eolang/parser/phi-packs/number-part.yaml +++ b/eo-parser/src/test/resources/org/eolang/parser/phi-packs/number-part.yaml @@ -62,4 +62,4 @@ no-sugar: |- λ ⤍ Package ⟧ ⟧ - } \ No newline at end of file + }