Skip to content

Commit

Permalink
Merge pull request #3762 from volodya-lombrozo/3756_error_msgs
Browse files Browse the repository at this point in the history
feat(#3756): Use a Single Format for All Error Messages
  • Loading branch information
yegor256 authored Dec 26, 2024
2 parents db7d303 + 829f334 commit d5de9b0
Show file tree
Hide file tree
Showing 20 changed files with 127 additions and 76 deletions.
47 changes: 11 additions & 36 deletions eo-parser/src/main/java/org/eolang/parser/EoParserErrors.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.InputMismatchException;
import org.antlr.v4.runtime.NoViableAltException;
Expand Down Expand Up @@ -87,6 +86,7 @@ public void syntaxError(
)
);
}
final List<String> msgs = new ArrayList<>(0);
if (error instanceof NoViableAltException || error instanceof InputMismatchException) {
final Token token = (Token) symbol;
final Parser parser = (Parser) recognizer;
Expand All @@ -102,44 +102,19 @@ public void syntaxError(
} else {
detailed = "no viable alternative at input";
}
this.errors.add(
new ParsingException(
String.format(
"[%d:%d] %s: %s:%n%s",
line, position,
"error",
detailed,
new UnderlinedMessage(
this.lines.line(line),
position,
Math.max(token.getStopIndex() - token.getStartIndex(), 1)
).formatted()
),
error,
line
)
);
} else if (Objects.isNull(error)) {
this.errors.add(
new ParsingException(
String.format(
"[%d:%d] %s: %s", line, position, "error", msg
),
line
)
msgs.add(new MsgLocated(line, position, detailed).formatted());
msgs.add(
new MsgUnderlined(
this.lines.line(line),
position,
Math.max(token.getStopIndex() - token.getStartIndex(), 1)
).formatted()
);
} else {
this.errors.add(
new ParsingException(
String.format(
"[%d:%d] %s: \"%s\"",
line, position, msg, this.lines.line(line)
),
error,
line
)
);
msgs.add(new MsgLocated(line, position, msg).formatted());
msgs.add(this.lines.line(line));
}
this.errors.add(new ParsingException(error, line, msgs));
}

@Override
Expand Down
8 changes: 3 additions & 5 deletions eo-parser/src/main/java/org/eolang/parser/GeneralErrors.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,10 @@ public void syntaxError(
) {
this.errors.add(
new ParsingException(
String.format(
"[%d:%d] %s: \"%s\"",
line, position, msg, this.lines.line(line)
),
error,
line
line,
new MsgLocated(line, position, msg).formatted(),
this.lines.line(line)
)
);
}
Expand Down
66 changes: 66 additions & 0 deletions eo-parser/src/main/java/org/eolang/parser/MsgLocated.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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.
*/
package org.eolang.parser;

/**
* Error message that includes the location of the error.
* @since 0.50
*/
final class MsgLocated {

/**
* The line where the error occurred.
*/
private final int line;

/**
* The position in the line where the error occurred.
*/
private final int position;

/**
* The error message.
*/
private final String message;

/**
* Ctor.
* @param line The line where the error occurred.
* @param position The position in the line where the error occurred.
* @param message The error message.
*/
MsgLocated(final int line, final int position, final String message) {
this.line = line;
this.position = position;
this.message = message;
}

/**
* Formats the error message.
* @return The formatted error message.
*/
String formatted() {
return String.format("[%d:%d] error: '%s'", this.line, this.position, this.message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,8 @@
* }
* </p>
* @since 0.50
* @todo #3332:30min Add more decorators for the error message.
* For example, {@link GeneralErrors} currently contains logic related to the message formatting.
* It's better to create a separate class for this purpose.
*/
final class UnderlinedMessage {
final class MsgUnderlined {

/**
* The message.
Expand All @@ -67,7 +64,7 @@ final class UnderlinedMessage {
* @param from The position from which to start underlining.
* @param length The length of the underline.
*/
UnderlinedMessage(final String origin, final int from, final int length) {
MsgUnderlined(final String origin, final int from, final int length) {
this.origin = origin;
this.from = from;
this.length = length;
Expand All @@ -94,12 +91,12 @@ private String underline() {
if (this.origin.isEmpty() || this.length <= 0 || this.from >= this.origin.length()) {
result = "";
} else if (this.from < 0) {
result = UnderlinedMessage.repeat("^", this.origin.length());
result = MsgUnderlined.repeat("^", this.origin.length());
} else {
result = String.format(
"%s%s",
UnderlinedMessage.repeat(" ", this.from),
UnderlinedMessage.repeat("^", Math.min(this.length, this.origin.length()))
MsgUnderlined.repeat(" ", this.from),
MsgUnderlined.repeat("^", Math.min(this.length, this.origin.length()))
);
}
return result;
Expand Down
27 changes: 21 additions & 6 deletions eo-parser/src/main/java/org/eolang/parser/ParsingException.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
package org.eolang.parser;

import java.util.List;

/**
* When parsing fails.
*
Expand All @@ -42,20 +44,34 @@ public final class ParsingException extends RuntimeException {

/**
* Ctor.
* @param msg Message
* @param cause Cause of failure
* @param line The place
* @param msgs Messages
*/
public ParsingException(final String msg, final int line) {
this(msg, null, line);
ParsingException(final Exception cause, final int line, final String... msgs) {
this(cause, line, List.of(msgs));
}

/**
* Ctor.
* @param msg Message
*
* @param cause The cause
* @param line The place
* @param msgs Messages
* @since 0.1
*/
ParsingException(final Exception cause, final int line, final List<String> msgs) {
this(cause, line, String.join("\n", msgs));
}

/**
* Ctor.
*
* @param cause Cause of failure
* @param line The place
* @param msg Message
*/
public ParsingException(final String msg, final Exception cause, final int line) {
ParsingException(final Exception cause, final int line, final String msg) {
super(msg, cause);
this.place = line;
}
Expand All @@ -67,5 +83,4 @@ public ParsingException(final String msg, final Exception cause, final int line)
public int line() {
return this.place;
}

}
5 changes: 2 additions & 3 deletions eo-parser/src/main/java/org/eolang/parser/XePhiListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,8 @@ public void enterDeltaBinding(final PhiParser.DeltaBindingContext ctx) {
&& !"bytes".equals(this.attributes.peek())
) {
throw new ParsingException(
"It's impossible to represent Δ ⤍ ∅ binding in EO",
new IllegalStateException(),
ctx.getStart().getLine()
new IllegalStateException(), ctx.getStart().getLine(),
"It's impossible to represent Δ ⤍ ∅ binding in EO"
);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,25 @@
import org.junit.jupiter.params.provider.MethodSource;

/**
* Test case for {@link UnderlinedMessage}.
* Test case for {@link MsgUnderlined}.
* @since 0.50
* @checkstyle ParameterNumberCheck (500 lines)
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
final class UnderlinedMessageTest {
final class MsgUnderlinedTest {

@ParameterizedTest
@MethodSource("examples")
void addsUndeline(final String input, final int from, final int length, final String expected) {
MatcherAssert.assertThat(
"We expect the message to be highlighted with underline characters",
new UnderlinedMessage(input, from, length).formatted(),
new MsgUnderlined(input, from, length).formatted(),
Matchers.equalTo(expected)
);
}

/**
* Test cases for {@link UnderlinedMessageTest#addsUndeline}.
* Test cases for {@link MsgUnderlinedTest#addsUndeline}.
* ANTLR {@link org.antlr.v4.runtime.BaseErrorListener} returns strange line numbers
* and positions like -1. Here I hide this problem intentionally to make all the rest
* tests pass.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
---
line: 2
message: >-
[2:4] error: Invalid object declaration:
[2:4] error: 'Invalid object declaration'
y:^
^
input: |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
---
line: 2
message: |-
[2:7] error: Invalid object declaration:
[2:7] error: 'Invalid object declaration'
[] > a [] > b [] > c [] > d hello world
^
input: |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ line: 5
# comment. The error message should be updated to point to the exact position
# of the comment.
message: |
[5:12] error: Invalid object declaration:
[5:12] error: 'Invalid object declaration'
sprintwf
input: |
# No comments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ line: 4
# Cryptic error message is returned when there are two empty lines between metas.
# The error message should be more informative and should highlight the exact location
# of the error.
message: |-
[4:0] error: extraneous input '\n' expecting {COMMENTARY, 'Q', 'QQ', '*', '$', '[', '(', '@', '^', BYTES, STRING, INT, FLOAT, HEX, NAME, TEXT}
message: |
[4:0] error: 'extraneous input '\n' expecting {COMMENTARY, 'Q', 'QQ', '*', '$', '[', '(', '@', '^', BYTES, STRING, INT, FLOAT, HEX, NAME, TEXT}'
input: |
# No comments.
[args] > one
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
---
line: 1
message: |-
[1:10] error: Invalid meta declaration:
[1:10] error: 'Invalid meta declaration'
+meta with spaces
^
input: |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
---
line: 3
message: |-
[3:0] error: Invalid object declaration:
[3:0] error: 'Invalid object declaration'
+meta other
^^^^^^^^^^
input: |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
---
line: 3
message: |-
[3:0] error: Invalid meta declaration:
[3:0] error: 'Invalid meta declaration'
[] > main
^
input: |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
---
line: 4
message: |-
[4:-1] error: Invalid program declaration:
[4:-1] error: 'Invalid program declaration'
[] > inner
^^^^^^^^^^^^
input: |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
---
line: 3
message: |-
[3:-1] error: Invalid program declaration:
[3:-1] error: 'Invalid program declaration'
EOF
^^^
input: |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ line: 3
# place in the input where the error occurred. Moreover it should be clear
# what to do to fix the error. 'simple-application-named.yaml' has the same issue.
message: |-
[3:-1] error: Invalid program declaration:
[3:-1] error: 'Invalid program declaration'
EOF
^^^
input: |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
---
line: 5
message: >-
[5:2] error: Invalid object declaration:
[5:2] error: 'Invalid object declaration'
*
^
input: |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
---
line: 2
message: |-
[2:0] error: Invalid program declaration:
[2:0] error: 'Invalid program declaration'
.z
^
input: |
Expand Down
Loading

1 comment on commit d5de9b0

@0pdd
Copy link

@0pdd 0pdd commented on d5de9b0 Dec 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 3332-6fa7c070 disappeared from eo-parser/src/main/java/org/eolang/parser/UnderlinedMessage.java), that's why I closed #3756. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.