diff --git a/eo-parser/src/main/java/org/eolang/parser/ParsingErrors.java b/eo-parser/src/main/java/org/eolang/parser/ParsingErrors.java index 5462963964..faed677aad 100644 --- a/eo-parser/src/main/java/org/eolang/parser/ParsingErrors.java +++ b/eo-parser/src/main/java/org/eolang/parser/ParsingErrors.java @@ -30,7 +30,6 @@ 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.RecognitionException; import org.antlr.v4.runtime.Recognizer; @@ -46,6 +45,10 @@ * Accumulates all parsing errors. * * @since 0.30.0 + * @todo #3706:30min Split {@link ParsingErrors} into two classes. + * Currently we use the same {@link ParsingErrors} class to accumulate all the parsing errors + * despite their origin. This class should be split into two classes: one for parsing errors + * {@link ParserErrors} and another for lexer errors {@link LexerErrors}. */ final class ParsingErrors extends BaseErrorListener implements Iterable { @@ -86,7 +89,7 @@ public void syntaxError( final String msg, final RecognitionException error ) { - if (error instanceof NoViableAltException) { + if (error instanceof NoViableAltException || error instanceof InputMismatchException) { final Token token = (Token) symbol; final EoParser parser = (EoParser) recognizer; final String rule = parser.getRuleInvocationStack().get(0); @@ -96,6 +99,8 @@ public void syntaxError( detailed = "Invalid object declaration"; } else if (names[EoParser.RULE_metas].equals(rule)) { detailed = "Invalid meta declaration"; + } else if (names[EoParser.RULE_program].equals(rule)) { + detailed = "Invalid program declaration"; } else { detailed = "no viable alternative at input"; } @@ -116,12 +121,6 @@ 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( diff --git a/eo-parser/src/test/resources/org/eolang/parser/eo-typos/empty-line-between-metas.yaml b/eo-parser/src/test/resources/org/eolang/parser/eo-typos/empty-line-between-metas.yaml index b7e940de21..73f0384a54 100644 --- a/eo-parser/src/test/resources/org/eolang/parser/eo-typos/empty-line-between-metas.yaml +++ b/eo-parser/src/test/resources/org/eolang/parser/eo-typos/empty-line-between-metas.yaml @@ -21,6 +21,10 @@ # SOFTWARE. --- line: 3 +message: |- + [3:0] error: Invalid object declaration: + +meta other + ^^^^^^^^^^ input: | +meta smth diff --git a/eo-parser/src/test/resources/org/eolang/parser/eo-typos/not-empty-atoms.yaml b/eo-parser/src/test/resources/org/eolang/parser/eo-typos/not-empty-atoms.yaml index ccf738023e..db8dd4a130 100644 --- a/eo-parser/src/test/resources/org/eolang/parser/eo-typos/not-empty-atoms.yaml +++ b/eo-parser/src/test/resources/org/eolang/parser/eo-typos/not-empty-atoms.yaml @@ -21,6 +21,10 @@ # SOFTWARE. --- line: 4 +message: |- + [4:-1] error: Invalid program declaration: + [] > inner + ^^^^^^^^^^^^ input: | # No comments. [] > test /int diff --git a/eo-parser/src/test/resources/org/eolang/parser/eo-typos/redundant-parentheses/simple-application-named.yaml b/eo-parser/src/test/resources/org/eolang/parser/eo-typos/redundant-parentheses/simple-application-named.yaml index df2a1fdcd8..410ff63447 100644 --- a/eo-parser/src/test/resources/org/eolang/parser/eo-typos/redundant-parentheses/simple-application-named.yaml +++ b/eo-parser/src/test/resources/org/eolang/parser/eo-typos/redundant-parentheses/simple-application-named.yaml @@ -21,6 +21,10 @@ # SOFTWARE. --- line: 3 +message: |- + [3:-1] error: Invalid program declaration: + EOF + ^^^ input: | 1.add 1 > x (1.add 1) > y diff --git a/eo-parser/src/test/resources/org/eolang/parser/eo-typos/redundant-parentheses/simple-application.yaml b/eo-parser/src/test/resources/org/eolang/parser/eo-typos/redundant-parentheses/simple-application.yaml index df8c0c874b..f7c3882170 100644 --- a/eo-parser/src/test/resources/org/eolang/parser/eo-typos/redundant-parentheses/simple-application.yaml +++ b/eo-parser/src/test/resources/org/eolang/parser/eo-typos/redundant-parentheses/simple-application.yaml @@ -21,6 +21,15 @@ # SOFTWARE. --- line: 3 +# @todo #3706:30min Unreadable error message if program declaration is invalid. +# Improve error message for the case when a program declaration is invalid. +# The error message should be more informative and should point to the exact +# 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: + EOF + ^^^ input: | 1.add 1 > x (1.add 1) diff --git a/eo-parser/src/test/resources/org/eolang/parser/eo-typos/vmethod-after-happlication.yaml b/eo-parser/src/test/resources/org/eolang/parser/eo-typos/vmethod-after-happlication.yaml index 6aa84a0103..fcd162a8a6 100644 --- a/eo-parser/src/test/resources/org/eolang/parser/eo-typos/vmethod-after-happlication.yaml +++ b/eo-parser/src/test/resources/org/eolang/parser/eo-typos/vmethod-after-happlication.yaml @@ -21,6 +21,10 @@ # SOFTWARE. --- line: 2 +message: |- + [2:0] error: Invalid program declaration: + .z + ^ input: | x y .z diff --git a/eo-parser/src/test/resources/org/eolang/parser/eo-typos/vmethod-after-hmethod.yaml b/eo-parser/src/test/resources/org/eolang/parser/eo-typos/vmethod-after-hmethod.yaml index 5105fbeb22..d76fb3a8b8 100644 --- a/eo-parser/src/test/resources/org/eolang/parser/eo-typos/vmethod-after-hmethod.yaml +++ b/eo-parser/src/test/resources/org/eolang/parser/eo-typos/vmethod-after-hmethod.yaml @@ -21,6 +21,10 @@ # SOFTWARE. --- line: 2 +message: |- + [2:0] error: Invalid program declaration: + .z + ^ input: | x.y .z