Skip to content

Commit

Permalink
feat(#3706): add one more error message
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Dec 23, 2024
1 parent 729fad7 commit 9918149
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
31 changes: 21 additions & 10 deletions eo-parser/src/main/java/org/eolang/parser/ParsingErrors.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.InputMismatchException;
import org.antlr.v4.runtime.LexerNoViableAltException;
import org.antlr.v4.runtime.NoViableAltException;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
import org.antlr.v4.runtime.Token;
Expand Down Expand Up @@ -84,21 +86,15 @@ public void syntaxError(
final String msg,
final RecognitionException error
) {
// @checkstyle MethodBodyCommentsCheck (20 lines)
// @todo #3332:30min Add more specific error messages.
// Currently we write just "error: no viable alternative at input" for all errors.
// It's better to use 'Recognizer<?, ?> recognizer' parameter of the current method
// to retrieve more specific error messages.
if (error instanceof NoViableAltException) {
final Token token = (Token) symbol;
final EoParser parser = (EoParser) recognizer;
final List<String> stack = parser.getRuleInvocationStack();
final String curr = stack.get(0);
final String rule = parser.getRuleInvocationStack().get(0);
final String[] names = parser.getRuleNames();
final String detailed;
if (names[EoParser.RULE_objects].equals(curr)) {
if (names[EoParser.RULE_objects].equals(rule)) {
detailed = "Invalid object declaration";
} else if (names[EoParser.RULE_metas].equals(curr)) {
} else if (names[EoParser.RULE_metas].equals(rule)) {
detailed = "Invalid meta declaration";
} else {
detailed = "no viable alternative at input";
Expand All @@ -120,6 +116,21 @@ public void syntaxError(
line
)
);
} else if (error instanceof InputMismatchException) {
System.out.println(error);
//todo
} else if (error instanceof LexerNoViableAltException) {
System.out.println(error);
//todo
} else if (Objects.isNull(error)) {
this.errors.add(
new ParsingException(
String.format(
"[%d:%d] %s: %s", line, position, "error", msg
),
line
)
);
} else {
this.errors.add(
new ParsingException(
Expand Down
10 changes: 10 additions & 0 deletions eo-parser/src/main/java/org/eolang/parser/ParsingException.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ public final class ParsingException extends RuntimeException {
*/
private final int place;

/**
* Ctor.
* @param msg Message
* @param line The place
*/
public ParsingException(final String msg, final int line) {
super(msg);
this.place = line;
}

/**
* Ctor.
* @param msg Message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ void checksTypoPacks(final String yaml) {
);
Assumptions.assumeTrue(story.map().get("skip") == null);
MatcherAssert.assertThat(
EoIndentLexerTest.TO_ADD_MESSAGE,
"We expect the error with correct line number was found",
XhtmlMatchers.xhtml(story.after().toString()),
XhtmlMatchers.hasXPaths("/program/errors/error/@line")
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
# SOFTWARE.
---
line: 4
# @todo #3706:30min Cryptic error message is returned
# 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}
input: |
# No comments.
[args] > one
Expand Down

0 comments on commit 9918149

Please sign in to comment.