Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#3744): Human-Readable Message for Prohibited Comment #3802

9 changes: 9 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 @@ -263,16 +263,24 @@ vapplicationArgsReversed

// Arguments of vertical application
// Must either all bound or all unbound
// Comments between vertical arguments are prohibited
// Ends on the next line
vapplicationArgsSpecific
: vapplicationArgBound+
| vapplicationArgUnbound+
;

// Prohibited comment between vertical arguments
// If this rule is matched, the parser will throw an error
prohibitedComment
: comment
;

// Vertical application arguments with bindings
vapplicationArgBound
: vapplicationArgBoundCurrent EOL
| vapplicationArgBoundNext
| prohibitedComment+
;

// Vertical application arguments with bindings
Expand All @@ -296,6 +304,7 @@ vapplicationArgBoundNext
vapplicationArgUnbound
: vapplicationArgUnboundCurrent EOL
| vapplicationArgUnboundNext
| prohibitedComment+
;

// Vertical application arguments without bindings
Expand Down
46 changes: 44 additions & 2 deletions eo-parser/src/main/java/org/eolang/parser/XeEoListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,18 @@ public void exitVapplicationArgs(final EoParser.VapplicationArgsContext ctx) {
// Nothing here
}

@Override
public void enterProhibitedComment(final EoParser.ProhibitedCommentContext ctx) {
this.errors.add(
XeEoListener.error(ctx.comment().COMMENTARY(), "Comment here is prohibited")
);
}

@Override
public void exitProhibitedComment(final EoParser.ProhibitedCommentContext ctx) {
// Nothing here
}

@Override
public void enterVapplicationArgsReversed(final EoParser.VapplicationArgsReversedContext ctx) {
// Nothing here
Expand Down Expand Up @@ -1256,7 +1268,7 @@ private static String trimMargin(final String text, final int indent) {
* Create parsing exception from given context.
* @param ctx Context
* @param msg Error message
* @return Parsing exception from current context
* @return Parsing exception from the current context
*/
private static ParsingException error(final ParserRuleContext ctx, final String msg) {
return new ParsingException(
Expand All @@ -1274,13 +1286,43 @@ private static ParsingException error(final ParserRuleContext ctx, final String
);
}

/**
* Create parsing exception from given terminal node.
* @param terminal Terminal node
* @param msg Error message
* @return Parsing exception from the current terminal node
*/
private static ParsingException error(final TerminalNode terminal, final String msg) {
return new ParsingException(
terminal.getSymbol().getLine(),
new MsgLocated(
terminal.getSymbol().getLine(),
terminal.getSymbol().getCharPositionInLine(),
msg
).formatted(),
new MsgUnderlined(
XeEoListener.line(terminal.getSymbol()),
terminal.getSymbol().getCharPositionInLine(),
terminal.getText().length()
).formatted()
);
}

/**
* Get line from context.
* @param ctx Context
* @return Line
*/
private static String line(final ParserRuleContext ctx) {
final Token token = ctx.start;
return XeEoListener.line(ctx.start);
}

/**
* Get line from token.
* @param token Token
* @return Line
*/
private static String line(final Token token) {
final int number = token.getLine();
final String[] lines = token.getInputStream().toString().split("\n");
if (number > 0 && number <= lines.length) {
Expand Down
8 changes: 4 additions & 4 deletions eo-parser/src/test/java/org/eolang/parser/EoSyntaxTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,10 @@ void checksTypoPacks(final String yaml) {
if (story.map().containsKey(msg)) {
MatcherAssert.assertThat(
XhtmlMatchers.xhtml(story.after()).toString(),
story.after()
.xpath("/program/errors/error[1]/text()")
.get(0)
.replaceAll("\r", ""),
String.join(
"\n",
story.after().xpath("/program/errors/error/text()")
).replaceAll("\r", ""),
Matchers.equalTo(story.map().get(msg).toString())
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
---
line: 5
# @todo #3706:30min The error message doesn't highlight the exact position of the
# comment. The error message should be updated to point to the exact position
# of the comment.
message: |
[5:12] error: 'Invalid object declaration'
sprintwf
line: 4
message: |-
[4:4] error: 'Comment here is prohibited'
# a comment here is prohibited
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
input: |
# No comments
[args] > app
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# The MIT License (MIT)
#
# Copyright (c) 2016-2025 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.
---
line: 4
message: |-
[4:4] error: 'Comment here is prohibited'
# The first comment is prohibited
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[5:4] error: 'Comment here is prohibited'
# The second comment is prohibited
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[7:6] error: 'Comment here is prohibited'
# The third comment is prohibited
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
input: |
# No comments
[args] > app
stdout > @
# The first comment is prohibited
# The second comment is prohibited
sprintwf
# The third comment is prohibited
"hello"
Loading