Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Apr 28, 2024
2 parents 1344b70 + 755a830 commit 429a08e
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 95 deletions.
23 changes: 16 additions & 7 deletions eo-parser/src/main/antlr4/org/eolang/parser/Eo.g4
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ commentOptional
;

commentMandatory
: comment commentOptional
: comment+
;

// Object
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
;

Expand All @@ -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
;

Expand Down
126 changes: 79 additions & 47 deletions eo-parser/src/main/java/org/eolang/parser/XeEoListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<EoParser.CommentContext> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
^.@ > @
Expand All @@ -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
Expand Down
2 changes: 0 additions & 2 deletions eo-runtime/src/main/eo/org/eolang/cage.eo
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
# Object encaged by locator.
[locator] > encaged
$ > self

# Retrieved encaged object by locator.
[] > @ /?

# Encage new object by locator.
Expand Down
1 change: 1 addition & 0 deletions eo-runtime/src/main/eo/org/eolang/dataized.eo
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
# attribute.
# An example of usage:
# ```
# # Some 64+ characters comment should be here.
# [] > foo
# [] > @
# ^.inner.five > @
Expand Down
3 changes: 0 additions & 3 deletions eo-runtime/src/main/eo/org/eolang/go.eo
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
8
m.put m.id > [m]

# To.
[body] > to
try > @
body token
Expand All @@ -69,15 +68,13 @@
error e
true

# Token.
[] > token
# Backward jump.
error > backward
[]
&.^.^.to &.^.body > value
&.^.^.id > id

# Forward jump.
[res] > forward
error > @
[]
Expand Down
2 changes: 0 additions & 2 deletions eo-runtime/src/main/eo/org/eolang/malloc.eo
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 3 additions & 12 deletions eo-runtime/src/main/eo/org/eolang/negative-infinity.eo
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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. > @
Expand All @@ -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. > @
Expand All @@ -126,23 +120,20 @@
# 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.
is-nan num
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
Expand Down
Loading

0 comments on commit 429a08e

Please sign in to comment.