From 6154f2381bb3a98a8873b0acbae0ecd268e7ebdd Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Sat, 27 Apr 2024 14:40:47 +0300 Subject: [PATCH 1/6] fix(#3152): grammar and packs --- .../src/main/antlr4/org/eolang/parser/Eo.g4 | 23 +++- .../java/org/eolang/parser/XeEoListener.java | 126 +++++++++++------- .../org/eolang/parser/packs/full-syntax.yaml | 7 +- 3 files changed, 96 insertions(+), 60 deletions(-) diff --git a/eo-parser/src/main/antlr4/org/eolang/parser/Eo.g4 b/eo-parser/src/main/antlr4/org/eolang/parser/Eo.g4 index be88cd0fbd..1b385e950f 100644 --- a/eo-parser/src/main/antlr4/org/eolang/parser/Eo.g4 +++ b/eo-parser/src/main/antlr4/org/eolang/parser/Eo.g4 @@ -36,7 +36,7 @@ commentOptional ; commentMandatory - : comment commentOptional + : comment+ ; // Object @@ -55,7 +55,16 @@ slave // Indeprendent objects that may have slaves (except atom) // Ends on the next line master - : commentMandatory (formation | (atom | hanonym oname) EOL) + : commentMandatory masterBody + ; + +subMaster + : commentOptional masterBody + ; + +masterBody + : formation + | (atom | hanonym oname) EOL ; // Just an object reference without name @@ -92,7 +101,7 @@ innersOrEol // No empty lines before "slave" // May be one empty line before "master" inners - : EOL TAB object (slave | EOL? master)* UNTAB + : EOL TAB (slave | subMaster) (slave | EOL? subMaster)* UNTAB ; // Attributes of an abstract object, atom or horizontal anonym object @@ -298,14 +307,14 @@ formationNameless // Formation with or without name formationNamedOrNameless - : commentMandatory formation + : commentOptional formation | formationNameless ; // Bound vertical anonym abstract object as argument of vertical application argument // Ends on the next line vapplicationArgVanonymBound - : commentMandatory formationBound + : commentOptional formationBound | formationBoundNameless ; @@ -323,12 +332,12 @@ vapplicationArgHanonymBoundBody // Horizontal anonym abstract object as argument of vertical application vapplicationArgHanonymBound - : commentMandatory vapplicationArgHanonymBoundBody oname + : commentOptional vapplicationArgHanonymBoundBody oname | vapplicationArgHanonymBoundBody ; vapplicationArgHanonymUnbound - : commentMandatory hanonym oname + : commentOptional hanonym oname | hanonym ; diff --git a/eo-parser/src/main/java/org/eolang/parser/XeEoListener.java b/eo-parser/src/main/java/org/eolang/parser/XeEoListener.java index 69dd90b911..d1d518f39a 100644 --- a/eo-parser/src/main/java/org/eolang/parser/XeEoListener.java +++ b/eo-parser/src/main/java/org/eolang/parser/XeEoListener.java @@ -30,6 +30,7 @@ import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.Iterator; +import java.util.List; import java.util.StringJoiner; import java.util.stream.Collectors; import org.antlr.v4.runtime.ParserRuleContext; @@ -232,7 +233,7 @@ public void exitComment(final EoParser.CommentContext ctx) { @Override public void enterCommentOptional(final EoParser.CommentOptionalContext ctx) { - // Nothing here + this.validateComment(ctx, ctx.comment()); } @Override @@ -242,52 +243,7 @@ public void exitCommentOptional(final EoParser.CommentOptionalContext ctx) { @Override public void enterCommentMandatory(final EoParser.CommentMandatoryContext ctx) { - if(this.tests) { - return; - } - final String comment = String.join( - "", - ctx.comment().COMMENTARY().getText().substring(1).trim(), - ctx.commentOptional().comment().stream().map( - context -> context.COMMENTARY().getText().substring(1).trim() - ).collect(Collectors.joining("")) - ); - final String length = String.format( - "Comment must be at least %d characters long", - XeEoListener.MIN_COMMENT_LENGTH - ); - final String warning = "warning"; - if (comment.isEmpty()) { - this.addError(ctx, "comment-length-check", warning, length); - } else { - if (comment.length() < XeEoListener.MIN_COMMENT_LENGTH) { - this.addError(ctx, "comment-length-check", warning, length); - } - if (comment.chars().anyMatch(chr -> chr < 32 || chr > 127)) { - this.addError( - ctx, - "comment-content-check", - warning, - "Comment must contain only ASCII printable characters: 0x20-0x7f" - ); - } - if (!Character.isUpperCase(comment.charAt(0))) { - this.addError( - ctx, - "comment-start-character-check", - warning, - "Comment must start with capital letter" - ); - } - if (comment.charAt(comment.length() - 1) != '.') { - this.addError( - ctx, - "comment-ending-check", - warning, - "Comment must end with dot" - ); - } - } + this.validateComment(ctx, ctx.comment()); } @Override @@ -325,6 +281,26 @@ public void exitMaster(final EoParser.MasterContext ctx) { // Nothing here } + @Override + public void enterSubMaster(final EoParser.SubMasterContext ctx) { + // Nothing here + } + + @Override + public void exitSubMaster(final EoParser.SubMasterContext ctx) { + // Nothing here + } + + @Override + public void enterMasterBody(final EoParser.MasterBodyContext ctx) { + // Nothing here + } + + @Override + public void exitMasterBody(final EoParser.MasterBodyContext ctx) { + // Nothing here + } + @Override public void enterJust(final EoParser.JustContext ctx) { // Nothing here @@ -1342,6 +1318,62 @@ private Objects startAbstract(final ParserRuleContext ctx) { return this.startObject(ctx).prop("abstract").leave(); } + /** + * Validate comment in front of abstract objects. + * @param ctx Context + * @param comments List of comment contexts + */ + private void validateComment( + final ParserRuleContext ctx, + final List comments + ) { + if(this.tests || comments.isEmpty()) { + return; + } + final String comment = String.join( + "", + comments.stream().map( + context -> context.COMMENTARY().getText().substring(1).trim() + ).collect(Collectors.joining("")) + ); + final String length = String.format( + "Comment must be at least %d characters long", + XeEoListener.MIN_COMMENT_LENGTH + ); + final String warning = "warning"; + if (comment.isEmpty()) { + this.addError(ctx, "comment-length-check", warning, length); + } else { + if (comment.length() < XeEoListener.MIN_COMMENT_LENGTH) { + this.addError(ctx, "comment-length-check", warning, length); + } + if (comment.chars().anyMatch(chr -> chr < 32 || chr > 127)) { + this.addError( + ctx, + "comment-content-check", + warning, + "Comment must contain only ASCII printable characters: 0x20-0x7f" + ); + } + if (!Character.isUpperCase(comment.charAt(0))) { + this.addError( + ctx, + "comment-start-character-check", + warning, + "Comment must start with capital letter" + ); + } + if (comment.charAt(comment.length() - 1) != '.') { + this.addError( + ctx, + "comment-ending-check", + warning, + "Comment must end with dot" + ); + } + } + } + /** * Add error to {@link XeEoListener#errors} directives. * @param ctx Context diff --git a/eo-parser/src/test/resources/org/eolang/parser/packs/full-syntax.yaml b/eo-parser/src/test/resources/org/eolang/parser/packs/full-syntax.yaml index eecdacbd76..acf40f177e 100644 --- a/eo-parser/src/test/resources/org/eolang/parser/packs/full-syntax.yaml +++ b/eo-parser/src/test/resources/org/eolang/parser/packs/full-syntax.yaml @@ -27,7 +27,7 @@ eo: | 500.43.@ > one - # This is just a simple string + # This is the default 64+ symbols comment in front of abstract object. "Hello, друг!" > hello! # This is the default 64+ symbols comment in front of abstract object. @@ -84,7 +84,6 @@ eo: | # This is the default 64+ symbols comment in front of abstract object. [] > obj "some" > @ - # This is the default 64+ symbols comment in front of abstract object. [] > foo ^.@ > @ @@ -104,12 +103,8 @@ eo: | # This is the default 64+ symbols comment in front of abstract object. [] > ooo - # This is one - # This is the default 64+ symbols comment in front of abstract object. [] > o-1 /? - # This is two - # This is the default 64+ symbols comment in front of abstract object. [] > o2 -2.4E3 > x From c0baf9415a6d7eff68281d7073db497b80afd1b8 Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Sat, 27 Apr 2024 14:48:35 +0300 Subject: [PATCH 2/6] fix(#3152): reduced amount of comments in runtime --- eo-runtime/src/main/eo/org/eolang/cage.eo | 2 -- eo-runtime/src/main/eo/org/eolang/dataized.eo | 1 + eo-runtime/src/main/eo/org/eolang/go.eo | 3 --- eo-runtime/src/main/eo/org/eolang/malloc.eo | 2 -- .../src/main/eo/org/eolang/negative-infinity.eo | 15 +++------------ .../src/main/eo/org/eolang/positive-infinity.eo | 14 ++------------ eo-runtime/src/main/eo/org/eolang/tuple.eo | 7 +++---- 7 files changed, 9 insertions(+), 35 deletions(-) diff --git a/eo-runtime/src/main/eo/org/eolang/cage.eo b/eo-runtime/src/main/eo/org/eolang/cage.eo index c42d43ab9b..051e216a2a 100644 --- a/eo-runtime/src/main/eo/org/eolang/cage.eo +++ b/eo-runtime/src/main/eo/org/eolang/cage.eo @@ -38,8 +38,6 @@ # Object encaged by locator. [locator] > encaged $ > self - - # Retrieved encaged object by locator. [] > @ /? # Encage new object by locator. diff --git a/eo-runtime/src/main/eo/org/eolang/dataized.eo b/eo-runtime/src/main/eo/org/eolang/dataized.eo index 4bae233f51..d430da0b65 100644 --- a/eo-runtime/src/main/eo/org/eolang/dataized.eo +++ b/eo-runtime/src/main/eo/org/eolang/dataized.eo @@ -38,6 +38,7 @@ # attribute. # An example of usage: # ``` +# # Some 64+ characters comment should be here. # [] > foo # [] > @ # ^.inner.five > @ diff --git a/eo-runtime/src/main/eo/org/eolang/go.eo b/eo-runtime/src/main/eo/org/eolang/go.eo index 52058f0a35..91cc9a9b57 100644 --- a/eo-runtime/src/main/eo/org/eolang/go.eo +++ b/eo-runtime/src/main/eo/org/eolang/go.eo @@ -58,7 +58,6 @@ 8 m.put m.id > [m] - # To. [body] > to try > @ body token @@ -69,7 +68,6 @@ error e true - # Token. [] > token # Backward jump. error > backward @@ -77,7 +75,6 @@ &.^.^.to &.^.body > value &.^.^.id > id - # Forward jump. [res] > forward error > @ [] diff --git a/eo-runtime/src/main/eo/org/eolang/malloc.eo b/eo-runtime/src/main/eo/org/eolang/malloc.eo index 030274d3f1..67904a99d8 100644 --- a/eo-runtime/src/main/eo/org/eolang/malloc.eo +++ b/eo-runtime/src/main/eo/org/eolang/malloc.eo @@ -85,8 +85,6 @@ # Allocates block in memory of given `size`. After allocation the `size` zero bytes bytes are # written into memory. [size scope] > of - # Allocates a memory block, initializes it, pass it to the `scope` and dataizes `scope` and - # at the end, even if the error is occurred, clears the block. [] > @ /bytes # Allocated block in memory that provides an API for writing and reading. diff --git a/eo-runtime/src/main/eo/org/eolang/negative-infinity.eo b/eo-runtime/src/main/eo/org/eolang/negative-infinity.eo index 5ef9bf6572..bf3c460746 100644 --- a/eo-runtime/src/main/eo/org/eolang/negative-infinity.eo +++ b/eo-runtime/src/main/eo/org/eolang/negative-infinity.eo @@ -63,20 +63,16 @@ [x] > times x > value! - # Tests if given number is greater than float or integer zero. [num] > is-num-gt-zero try > @ - [] - 0.lt num > @ + 0.lt num [e] 0.0.lt num > @ false - # Here we check if 'num' is nan by comparing it with nan bytes, because 'num' is cached bytes. [num] > is-nan num.eq nan.as-bytes > @ - # Tests if given number is equal to nan, float or integer zero. [num] > is-nan-or-zero or. > @ or. @@ -98,7 +94,6 @@ positive-infinity.as-bytes > pos-inf-as-bytes! x > value! - # Here we check if 'num' is nan by comparing it with nan bytes, because 'num' is cached bytes. [num] > is-nan num.eq nan.as-bytes > @ if. > @ @@ -113,7 +108,6 @@ x > value! negative-infinity > neg-inf-as-bytes! - # Here we check if 'num' is nan by comparing it with nan bytes, because 'num' is cached bytes. [num] > is-nan num.eq nan.as-bytes > @ if. > @ @@ -126,11 +120,10 @@ # Quotient of the division of $ by x [x] > div x > value! - # Here we check if 'num' is nan by comparing it with nan bytes, because 'num' is cached bytes. + [num] > is-nan num.eq nan.as-bytes > @ - # Tests if given number is equal to nan, positive or negative infinity. [num] > is-nan-or-infinite or. > @ or. @@ -138,11 +131,9 @@ num.eq positive-infinity num.eq negative-infinity - # Tests if given number is greater or equal to float or integer zero. [num] > is-num-gte-zero try > @ - [] - 0.lte num > @ + 0.lte num [e] 0.0.lte num > @ false diff --git a/eo-runtime/src/main/eo/org/eolang/positive-infinity.eo b/eo-runtime/src/main/eo/org/eolang/positive-infinity.eo index 8711d487f0..4de8879134 100644 --- a/eo-runtime/src/main/eo/org/eolang/positive-infinity.eo +++ b/eo-runtime/src/main/eo/org/eolang/positive-infinity.eo @@ -63,11 +63,9 @@ [x] > times x > value! - # Here we check if 'num' is nan by comparing it with nan bytes, because 'num' is cached bytes. [num] > is-nan num.eq nan.as-bytes > @ - # Tests if given number is equal to nan, float or integer zero. [num] > is-nan-or-zero or. > @ or. @@ -77,11 +75,9 @@ num.eq 0.0 num.eq 0 - # Tests if given number is greater than float or integer zero. [num] > is-num-gt-zero try > @ - [] - 0.lt num > @ + 0.lt num [e] 0.0.lt num > @ false @@ -98,7 +94,6 @@ x > value! negative-infinity > neg-inf-as-bytes! - # Here we check if 'num' is nan by comparing it with nan bytes, because 'num' is cached bytes. [num] > is-nan num.eq nan.as-bytes > @ if. > @ @@ -113,7 +108,6 @@ x > value! positive-infinity > pos-inf-as-bytes! - # Here we check if 'num' is nan by comparing it with nan bytes, because 'num' is cached bytes. [num] > is-nan num.eq nan.as-bytes > @ if. > @ @@ -127,11 +121,9 @@ [x] > div x > value! - # Here we check if 'num' is nan by comparing it with nan bytes, because 'num' is cached bytes. [num] > is-nan num.eq nan.as-bytes > @ - # Tests if given number is equal to nan, positive or negative infinity. [num] > is-nan-or-infinite or. > @ or. @@ -139,11 +131,9 @@ num.eq positive-infinity num.eq negative-infinity - # Tests if given number is greater or equal to float or integer zero. [num] > is-num-gte-zero try > @ - [] - 0.lte num > @ + 0.lte num [e] 0.0.lte num > @ false diff --git a/eo-runtime/src/main/eo/org/eolang/tuple.eo b/eo-runtime/src/main/eo/org/eolang/tuple.eo index dadbf2e5b2..7b96c4c09f 100644 --- a/eo-runtime/src/main/eo/org/eolang/tuple.eo +++ b/eo-runtime/src/main/eo/org/eolang/tuple.eo @@ -30,15 +30,15 @@ [head tail] > tuple # Empty tuple. [] > empty - # Take one element from the tuple, at the given position. + 0 > length + [i] > at error "Can't get an object from the empty tuple" > @ - # Take one element from the tuple, at the given position. + [x] > with tuple > @ tuple.empty x - 0 > length # Obtain the length of the tuple. [] > length @@ -60,7 +60,6 @@ error "Given index is out of tuple bounds" at-fast ^ len - # Fast recursive obtaining element from tuple. [tup len] > at-fast if. > @ (len.plus -1).gt ^.index From d11b4bc62ca13ffa0cb2b526ce3f57491ae91606 Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Sat, 27 Apr 2024 14:54:24 +0300 Subject: [PATCH 3/6] fix(#3152): space --- eo-runtime/src/main/eo/org/eolang/while.eo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eo-runtime/src/main/eo/org/eolang/while.eo b/eo-runtime/src/main/eo/org/eolang/while.eo index ec43c8a296..2ab9dfb9b3 100644 --- a/eo-runtime/src/main/eo/org/eolang/while.eo +++ b/eo-runtime/src/main/eo/org/eolang/while.eo @@ -46,7 +46,7 @@ if. > @ ^.condition.as-bool seq - * + * current ^.loop (index.plus 1) current From fc199727816e410113d27f361007ef2439660875 Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Sat, 27 Apr 2024 14:54:30 +0300 Subject: [PATCH 4/6] fix(#3152): back --- eo-runtime/src/main/eo/org/eolang/while.eo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eo-runtime/src/main/eo/org/eolang/while.eo b/eo-runtime/src/main/eo/org/eolang/while.eo index 2ab9dfb9b3..ec43c8a296 100644 --- a/eo-runtime/src/main/eo/org/eolang/while.eo +++ b/eo-runtime/src/main/eo/org/eolang/while.eo @@ -46,7 +46,7 @@ if. > @ ^.condition.as-bool seq - * + * current ^.loop (index.plus 1) current From 049e6718aa079587f0e211307350f1c15fc34479 Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Sat, 27 Apr 2024 16:34:13 +0300 Subject: [PATCH 5/6] fix(#3152): space --- eo-runtime/src/main/eo/org/eolang/while.eo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eo-runtime/src/main/eo/org/eolang/while.eo b/eo-runtime/src/main/eo/org/eolang/while.eo index ec43c8a296..8961cbff99 100644 --- a/eo-runtime/src/main/eo/org/eolang/while.eo +++ b/eo-runtime/src/main/eo/org/eolang/while.eo @@ -44,7 +44,7 @@ [index] > loop ^.body index > current if. > @ - ^.condition.as-bool + ^. condition.as-bool seq * current From 755a83020c60282d4250a751ca2d29f59892224f Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Sat, 27 Apr 2024 16:34:20 +0300 Subject: [PATCH 6/6] fix(#3152): back --- eo-runtime/src/main/eo/org/eolang/while.eo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eo-runtime/src/main/eo/org/eolang/while.eo b/eo-runtime/src/main/eo/org/eolang/while.eo index 8961cbff99..ec43c8a296 100644 --- a/eo-runtime/src/main/eo/org/eolang/while.eo +++ b/eo-runtime/src/main/eo/org/eolang/while.eo @@ -44,7 +44,7 @@ [index] > loop ^.body index > current if. > @ - ^. condition.as-bool + ^.condition.as-bool seq * current