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(#3756): Use a Single Format for All Error Messages #3762

Merged
merged 5 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 LocationMessage(line, position, detailed).formatted());
msgs.add(
new UnderlinedMessage(
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 LocationMessage(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 LocationMessage(line, position, msg).formatted(),
this.lines.line(line)
)
);
}
Expand Down
66 changes: 66 additions & 0 deletions eo-parser/src/main/java/org/eolang/parser/LocationMessage.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 LocationMessage {
yegor256 marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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.
*/
LocationMessage(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);
}
}
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@
* }
* </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 {

Expand Down
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 @@ -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
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