From 9b44b6b09538e97cab8420c1ec58ca1cb7b1435a Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Tue, 24 Dec 2024 10:56:40 +0300 Subject: [PATCH 01/11] feat(#3742): create a separate package for errors --- .../main/java/org/eolang/parser/EoSyntax.java | 1 + .../java/org/eolang/parser/PhiSyntax.java | 1 + .../eolang/parser/errors/EoParserErrors.java | 12 ++++ .../eolang/parser/errors/ErrorDirectives.java | 43 ++++++++++++++ .../java/org/eolang/parser/errors/Lines.java | 35 +++++++++++ .../parser/{ => errors}/ParsingErrors.java | 59 ++++--------------- .../{ => errors}/UnderlinedMessage.java | 4 +- .../{ => errors}/UnderlinedMessageTest.java | 2 +- 8 files changed, 106 insertions(+), 51 deletions(-) create mode 100644 eo-parser/src/main/java/org/eolang/parser/errors/EoParserErrors.java create mode 100644 eo-parser/src/main/java/org/eolang/parser/errors/ErrorDirectives.java create mode 100644 eo-parser/src/main/java/org/eolang/parser/errors/Lines.java rename eo-parser/src/main/java/org/eolang/parser/{ => errors}/ParsingErrors.java (70%) rename eo-parser/src/main/java/org/eolang/parser/{ => errors}/UnderlinedMessage.java (95%) rename eo-parser/src/test/java/org/eolang/parser/{ => errors}/UnderlinedMessageTest.java (98%) diff --git a/eo-parser/src/main/java/org/eolang/parser/EoSyntax.java b/eo-parser/src/main/java/org/eolang/parser/EoSyntax.java index 548324ea7b..eaa3f250cf 100644 --- a/eo-parser/src/main/java/org/eolang/parser/EoSyntax.java +++ b/eo-parser/src/main/java/org/eolang/parser/EoSyntax.java @@ -38,6 +38,7 @@ import org.cactoos.text.Joined; import org.cactoos.text.Split; import org.cactoos.text.TextOf; +import org.eolang.parser.errors.ParsingErrors; import org.xembly.Directives; import org.xembly.Xembler; diff --git a/eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java b/eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java index e249f5f39a..1923be8107 100644 --- a/eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java +++ b/eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java @@ -32,6 +32,7 @@ import org.antlr.v4.runtime.tree.ParseTreeWalker; import org.cactoos.Text; import org.cactoos.io.InputStreamOf; +import org.eolang.parser.errors.ParsingErrors; import org.xembly.Directive; import org.xembly.Directives; import org.xembly.Xembler; diff --git a/eo-parser/src/main/java/org/eolang/parser/errors/EoParserErrors.java b/eo-parser/src/main/java/org/eolang/parser/errors/EoParserErrors.java new file mode 100644 index 0000000000..9fc382a49d --- /dev/null +++ b/eo-parser/src/main/java/org/eolang/parser/errors/EoParserErrors.java @@ -0,0 +1,12 @@ +package org.eolang.parser.errors; + +import java.util.Iterator; +import org.antlr.v4.runtime.BaseErrorListener; +import org.xembly.Directive; + +public final class EoParserErrors extends BaseErrorListener implements Iterable { + @Override + public Iterator iterator() { + return null; + } +} diff --git a/eo-parser/src/main/java/org/eolang/parser/errors/ErrorDirectives.java b/eo-parser/src/main/java/org/eolang/parser/errors/ErrorDirectives.java new file mode 100644 index 0000000000..41eb299287 --- /dev/null +++ b/eo-parser/src/main/java/org/eolang/parser/errors/ErrorDirectives.java @@ -0,0 +1,43 @@ +package org.eolang.parser.errors; + +import java.util.Iterator; +import java.util.List; +import org.cactoos.iterable.Mapped; +import org.eolang.parser.ParsingException; +import org.xembly.Directive; +import org.xembly.Directives; + +public final class ErrorDirectives implements Iterable { + + /** + * Errors accumulated. + */ + private final List errors; + + /** + * Ctor. + * @param errors The errors. + */ + public ErrorDirectives(final List errors) { + this.errors = errors; + } + + @Override + public Iterator iterator() { + return new org.cactoos.iterable.Joined<>( + new Mapped>( + error -> new Directives() + .xpath("/program") + .strict(1) + .addIf("errors") + .strict(1) + .add("error") + .attr("check", "eo-parser") + .attr("line", error.line()) + .attr("severity", "critical") + .set(error.getMessage()), + this.errors + ) + ).iterator(); + } +} diff --git a/eo-parser/src/main/java/org/eolang/parser/errors/Lines.java b/eo-parser/src/main/java/org/eolang/parser/errors/Lines.java new file mode 100644 index 0000000000..ca327d425d --- /dev/null +++ b/eo-parser/src/main/java/org/eolang/parser/errors/Lines.java @@ -0,0 +1,35 @@ +package org.eolang.parser.errors; + +import java.util.List; +import java.util.Optional; +import org.cactoos.Text; +import org.cactoos.text.UncheckedText; + +final class Lines { + + /** + * The source. + */ + private final List lines; + + Lines(final List lines) { + this.lines = lines; + } + + /** + * Get the line by number. + * @param number The line number. + * @return The line. + */ + Optional line(final int number) { + final Optional result; + if (number < 1 || number > this.lines.size()) { + result = Optional.empty(); + } else { + result = Optional.ofNullable(this.lines.get(number - 1)) + .map(UncheckedText::new) + .map(UncheckedText::asString); + } + return result; + } +} diff --git a/eo-parser/src/main/java/org/eolang/parser/ParsingErrors.java b/eo-parser/src/main/java/org/eolang/parser/errors/ParsingErrors.java similarity index 70% rename from eo-parser/src/main/java/org/eolang/parser/ParsingErrors.java rename to eo-parser/src/main/java/org/eolang/parser/errors/ParsingErrors.java index ee8ee8573f..786ad35a43 100644 --- a/eo-parser/src/main/java/org/eolang/parser/ParsingErrors.java +++ b/eo-parser/src/main/java/org/eolang/parser/errors/ParsingErrors.java @@ -21,13 +21,12 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.parser; +package org.eolang.parser.errors; 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.NoViableAltException; @@ -36,22 +35,17 @@ import org.antlr.v4.runtime.Recognizer; import org.antlr.v4.runtime.Token; import org.cactoos.Text; -import org.cactoos.iterable.Mapped; import org.cactoos.list.ListOf; -import org.cactoos.text.UncheckedText; +import org.eolang.parser.EoParser; +import org.eolang.parser.ParsingException; import org.xembly.Directive; -import org.xembly.Directives; /** * 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 { +public final class ParsingErrors extends BaseErrorListener implements Iterable { /** * Errors accumulated. @@ -61,13 +55,13 @@ final class ParsingErrors extends BaseErrorListener implements Iterable lines; + private final Lines lines; /** * Ctor. * @param lines The source in lines */ - ParsingErrors(final Text... lines) { + public ParsingErrors(final Text... lines) { this(new ListOf<>(lines)); } @@ -75,9 +69,9 @@ final class ParsingErrors extends BaseErrorListener implements Iterable src) { + public ParsingErrors(final List src) { this.errors = new LinkedList<>(); - this.lines = src; + this.lines = new Lines(src); } // @checkstyle ParameterNumberCheck (10 lines) @@ -113,7 +107,7 @@ public void syntaxError( "error", detailed, new UnderlinedMessage( - this.line(line).orElse("EOF"), + this.lines.line(line).orElse("EOF"), position, Math.max(token.getStopIndex() - token.getStartIndex(), 1) ).formatted() @@ -136,7 +130,7 @@ public void syntaxError( new ParsingException( String.format( "[%d:%d] %s: \"%s\"", - line, position, msg, this.line(line).orElse("EOF") + line, position, msg, this.lines.line(line).orElse("EOF") ), error, line @@ -147,21 +141,7 @@ public void syntaxError( @Override public Iterator iterator() { - return new org.cactoos.iterable.Joined<>( - new Mapped>( - error -> new Directives() - .xpath("/program") - .strict(1) - .addIf("errors") - .strict(1) - .add("error") - .attr("check", "eo-parser") - .attr("line", error.line()) - .attr("severity", "critical") - .set(error.getMessage()), - this.errors - ) - ).iterator(); + return new ErrorDirectives(this.errors).iterator(); } /** @@ -171,21 +151,4 @@ public Iterator iterator() { public int size() { return this.errors.size(); } - - /** - * Get the line by number. - * @param number The line number. - * @return The line. - */ - private Optional line(final int number) { - final Optional result; - if (number < 1 || number > this.lines.size()) { - result = Optional.empty(); - } else { - result = Optional.ofNullable(this.lines.get(number - 1)) - .map(UncheckedText::new) - .map(UncheckedText::asString); - } - return result; - } } diff --git a/eo-parser/src/main/java/org/eolang/parser/UnderlinedMessage.java b/eo-parser/src/main/java/org/eolang/parser/errors/UnderlinedMessage.java similarity index 95% rename from eo-parser/src/main/java/org/eolang/parser/UnderlinedMessage.java rename to eo-parser/src/main/java/org/eolang/parser/errors/UnderlinedMessage.java index 1e90d53df3..8523a9254f 100644 --- a/eo-parser/src/main/java/org/eolang/parser/UnderlinedMessage.java +++ b/eo-parser/src/main/java/org/eolang/parser/errors/UnderlinedMessage.java @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.parser; +package org.eolang.parser.errors; import java.util.Collections; @@ -41,7 +41,7 @@ *

* @since 0.50 * @todo #3332:30min Add more decorators for the error message. - * For example, {@link ParsingErrors} currently contains logic related to the message formatting. + * For example, {@link org.eolang.parser.ParsingErrors} currently contains logic related to the message formatting. * It's better to create a separate class for this purpose. */ final class UnderlinedMessage { diff --git a/eo-parser/src/test/java/org/eolang/parser/UnderlinedMessageTest.java b/eo-parser/src/test/java/org/eolang/parser/errors/UnderlinedMessageTest.java similarity index 98% rename from eo-parser/src/test/java/org/eolang/parser/UnderlinedMessageTest.java rename to eo-parser/src/test/java/org/eolang/parser/errors/UnderlinedMessageTest.java index f7b2215c3d..92ff121f98 100644 --- a/eo-parser/src/test/java/org/eolang/parser/UnderlinedMessageTest.java +++ b/eo-parser/src/test/java/org/eolang/parser/errors/UnderlinedMessageTest.java @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.parser; +package org.eolang.parser.errors; import java.util.stream.Stream; import org.hamcrest.MatcherAssert; From 179d1ef760d15e138c47ffe62708144d951b6e37 Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Tue, 24 Dec 2024 11:13:01 +0300 Subject: [PATCH 02/11] feat(#3742): use EoParserErrors --- .../main/java/org/eolang/parser/EoSyntax.java | 10 +- .../eolang/parser/errors/EoParserErrors.java | 150 +++++++++++++++++- .../eolang/parser/errors/ErrorDirectives.java | 31 +++- .../java/org/eolang/parser/errors/Lines.java | 35 +++- .../eolang/parser/errors/ParsingErrors.java | 84 +++------- .../eolang/parser/errors/package-info.java | 28 ++++ 6 files changed, 267 insertions(+), 71 deletions(-) create mode 100644 eo-parser/src/main/java/org/eolang/parser/errors/package-info.java diff --git a/eo-parser/src/main/java/org/eolang/parser/EoSyntax.java b/eo-parser/src/main/java/org/eolang/parser/EoSyntax.java index eaa3f250cf..301c34309c 100644 --- a/eo-parser/src/main/java/org/eolang/parser/EoSyntax.java +++ b/eo-parser/src/main/java/org/eolang/parser/EoSyntax.java @@ -38,6 +38,7 @@ import org.cactoos.text.Joined; import org.cactoos.text.Split; import org.cactoos.text.TextOf; +import org.eolang.parser.errors.EoParserErrors; import org.eolang.parser.errors.ParsingErrors; import org.xembly.Directives; import org.xembly.Xembler; @@ -114,17 +115,18 @@ public XML parsed() throws IOException { new CommonTokenStream(lexer) ); parser.removeErrorListeners(); - parser.addErrorListener(spy); + final EoParserErrors eospy = new EoParserErrors(lines); + parser.addErrorListener(eospy); final XeEoListener xel = new XeEoListener(this.name); new ParseTreeWalker().walk(xel, parser.program()); final XML dom = Syntax.CANONICAL.pass( new XMLDocument( new Xembler( - new Directives(xel).append(spy) + new Directives(xel).append(spy).append(eospy) ).domQuietly() ) ); - if (spy.size() == 0) { + if (spy.size() + eospy.size() == 0) { Logger.debug( this, "The %s program of %d EO lines compiled, no errors", @@ -133,7 +135,7 @@ public XML parsed() throws IOException { } else { Logger.debug( this, "The %s program of %d EO lines compiled with %d error(s)", - this.name, lines.size(), spy.size() + this.name, lines.size(), spy.size() + eospy.size() ); } return dom; diff --git a/eo-parser/src/main/java/org/eolang/parser/errors/EoParserErrors.java b/eo-parser/src/main/java/org/eolang/parser/errors/EoParserErrors.java index 9fc382a49d..d0f22ff7cf 100644 --- a/eo-parser/src/main/java/org/eolang/parser/errors/EoParserErrors.java +++ b/eo-parser/src/main/java/org/eolang/parser/errors/EoParserErrors.java @@ -1,12 +1,160 @@ +/* + * 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.errors; +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; +import org.antlr.v4.runtime.Parser; +import org.antlr.v4.runtime.RecognitionException; +import org.antlr.v4.runtime.Recognizer; +import org.antlr.v4.runtime.Token; +import org.cactoos.Text; +import org.eolang.parser.EoParser; +import org.eolang.parser.ParsingException; import org.xembly.Directive; +/** + * Accumulates all parsing errors related to EO parser. + * @since 0.50 + */ public final class EoParserErrors extends BaseErrorListener implements Iterable { + + /** + * Errors accumulated. + */ + private final List errors; + + /** + * The source. + */ + private final Lines lines; + + /** + * Ctor. + * @param src The source in lines + */ + public EoParserErrors(final List src) { + this(new ArrayList<>(0), new Lines(src)); + } + + /** + * Ctor. + * @param errors Errors accumulated + * @param lines The source in lines + */ + private EoParserErrors(final List errors, final Lines lines) { + this.errors = errors; + this.lines = lines; + } + + // @checkstyle ParameterNumberCheck (10 lines) + @Override + public void syntaxError( + final Recognizer recognizer, + final Object symbol, + final int line, + final int position, + final String msg, + final RecognitionException error + ) { + if (!Parser.class.isInstance(recognizer)) { + throw new IllegalArgumentException( + String.format( + "Illegal usage of %s, please use it to recognize only EO parser errors", this + ) + ); + } + if (error instanceof NoViableAltException || error instanceof InputMismatchException) { + final Token token = (Token) symbol; + final Parser parser = (Parser) recognizer; + final String rule = parser.getRuleInvocationStack().get(0); + final String[] names = parser.getRuleNames(); + final String detailed; + if (names[EoParser.RULE_objects].equals(rule)) { + 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"; + } + 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 + ) + ); + } else { + this.errors.add( + new ParsingException( + String.format( + "[%d:%d] %s: \"%s\"", + line, position, msg, this.lines.line(line) + ), + error, + line + ) + ); + } + } + @Override public Iterator iterator() { - return null; + return new ErrorDirectives(this.errors).iterator(); + } + + /** + * The number of errors. + * @return The number of errors. + */ + public int size() { + return this.errors.size(); } } diff --git a/eo-parser/src/main/java/org/eolang/parser/errors/ErrorDirectives.java b/eo-parser/src/main/java/org/eolang/parser/errors/ErrorDirectives.java index 41eb299287..b7d79fe763 100644 --- a/eo-parser/src/main/java/org/eolang/parser/errors/ErrorDirectives.java +++ b/eo-parser/src/main/java/org/eolang/parser/errors/ErrorDirectives.java @@ -1,3 +1,26 @@ +/* + * 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.errors; import java.util.Iterator; @@ -7,7 +30,11 @@ import org.xembly.Directive; import org.xembly.Directives; -public final class ErrorDirectives implements Iterable { +/** + * Error directives. + * @since 0.50 + */ +final class ErrorDirectives implements Iterable { /** * Errors accumulated. @@ -18,7 +45,7 @@ public final class ErrorDirectives implements Iterable { * Ctor. * @param errors The errors. */ - public ErrorDirectives(final List errors) { + ErrorDirectives(final List errors) { this.errors = errors; } diff --git a/eo-parser/src/main/java/org/eolang/parser/errors/Lines.java b/eo-parser/src/main/java/org/eolang/parser/errors/Lines.java index ca327d425d..b2a94926eb 100644 --- a/eo-parser/src/main/java/org/eolang/parser/errors/Lines.java +++ b/eo-parser/src/main/java/org/eolang/parser/errors/Lines.java @@ -1,3 +1,26 @@ +/* + * 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.errors; import java.util.List; @@ -5,6 +28,10 @@ import org.cactoos.Text; import org.cactoos.text.UncheckedText; +/** + * The source in lines. + * @since 0.50 + */ final class Lines { /** @@ -12,6 +39,10 @@ final class Lines { */ private final List lines; + /** + * Ctor. + * @param lines The source in lines + */ Lines(final List lines) { this.lines = lines; } @@ -21,7 +52,7 @@ final class Lines { * @param number The line number. * @return The line. */ - Optional line(final int number) { + String line(final int number) { final Optional result; if (number < 1 || number > this.lines.size()) { result = Optional.empty(); @@ -30,6 +61,6 @@ Optional line(final int number) { .map(UncheckedText::new) .map(UncheckedText::asString); } - return result; + return result.orElse("EOF"); } } diff --git a/eo-parser/src/main/java/org/eolang/parser/errors/ParsingErrors.java b/eo-parser/src/main/java/org/eolang/parser/errors/ParsingErrors.java index 786ad35a43..e0e0d82a23 100644 --- a/eo-parser/src/main/java/org/eolang/parser/errors/ParsingErrors.java +++ b/eo-parser/src/main/java/org/eolang/parser/errors/ParsingErrors.java @@ -23,20 +23,14 @@ */ package org.eolang.parser.errors; +import java.util.ArrayList; import java.util.Iterator; -import java.util.LinkedList; 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; -import org.antlr.v4.runtime.Parser; import org.antlr.v4.runtime.RecognitionException; import org.antlr.v4.runtime.Recognizer; -import org.antlr.v4.runtime.Token; import org.cactoos.Text; import org.cactoos.list.ListOf; -import org.eolang.parser.EoParser; import org.eolang.parser.ParsingException; import org.xembly.Directive; @@ -70,8 +64,17 @@ public ParsingErrors(final Text... lines) { * @param src The source in lines */ public ParsingErrors(final List src) { - this.errors = new LinkedList<>(); - this.lines = new Lines(src); + this(new ArrayList<>(0), new Lines(src)); + } + + /** + * Ctor. + * @param errors Errors accumulated + * @param lines The source in lines + */ + private ParsingErrors(final List errors, final Lines lines) { + this.errors = errors; + this.lines = lines; } // @checkstyle ParameterNumberCheck (10 lines) @@ -84,59 +87,16 @@ public void syntaxError( final String msg, final RecognitionException error ) { - if (error instanceof NoViableAltException || error instanceof InputMismatchException) { - final Token token = (Token) symbol; - final Parser parser = (Parser) recognizer; - final String rule = parser.getRuleInvocationStack().get(0); - final String[] names = parser.getRuleNames(); - final String detailed; - if (names[EoParser.RULE_objects].equals(rule)) { - 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"; - } - this.errors.add( - new ParsingException( - String.format( - "[%d:%d] %s: %s:%n%s", - line, position, - "error", - detailed, - new UnderlinedMessage( - this.lines.line(line).orElse("EOF"), - 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 - ) - ); - } else { - this.errors.add( - new ParsingException( - String.format( - "[%d:%d] %s: \"%s\"", - line, position, msg, this.lines.line(line).orElse("EOF") - ), - error, - line - ) - ); - } + this.errors.add( + new ParsingException( + String.format( + "[%d:%d] %s: \"%s\"", + line, position, msg, this.lines.line(line) + ), + error, + line + ) + ); } @Override diff --git a/eo-parser/src/main/java/org/eolang/parser/errors/package-info.java b/eo-parser/src/main/java/org/eolang/parser/errors/package-info.java new file mode 100644 index 0000000000..fb73a69d54 --- /dev/null +++ b/eo-parser/src/main/java/org/eolang/parser/errors/package-info.java @@ -0,0 +1,28 @@ +/* + * 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. + */ +/** + * This package contains all the classes related to errors that can be found in the parsing process. + * @since 0.50 + */ +package org.eolang.parser.errors; \ No newline at end of file From b26f3d5955d6f761da06a976e169864fd471882b Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Tue, 24 Dec 2024 11:19:42 +0300 Subject: [PATCH 03/11] feat(#3742): fix all the code offences --- .../java/org/eolang/parser/errors/Lines.java | 8 +++--- .../parser/errors/UnderlinedMessage.java | 2 +- .../eolang/parser/errors/package-info.java | 2 +- .../eolang/parser/errors/package-info.java | 28 +++++++++++++++++++ 4 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 eo-parser/src/test/java/org/eolang/parser/errors/package-info.java diff --git a/eo-parser/src/main/java/org/eolang/parser/errors/Lines.java b/eo-parser/src/main/java/org/eolang/parser/errors/Lines.java index b2a94926eb..71ebeaa522 100644 --- a/eo-parser/src/main/java/org/eolang/parser/errors/Lines.java +++ b/eo-parser/src/main/java/org/eolang/parser/errors/Lines.java @@ -37,14 +37,14 @@ final class Lines { /** * The source. */ - private final List lines; + private final List source; /** * Ctor. * @param lines The source in lines */ Lines(final List lines) { - this.lines = lines; + this.source = lines; } /** @@ -54,10 +54,10 @@ final class Lines { */ String line(final int number) { final Optional result; - if (number < 1 || number > this.lines.size()) { + if (number < 1 || number > this.source.size()) { result = Optional.empty(); } else { - result = Optional.ofNullable(this.lines.get(number - 1)) + result = Optional.ofNullable(this.source.get(number - 1)) .map(UncheckedText::new) .map(UncheckedText::asString); } diff --git a/eo-parser/src/main/java/org/eolang/parser/errors/UnderlinedMessage.java b/eo-parser/src/main/java/org/eolang/parser/errors/UnderlinedMessage.java index 8523a9254f..75182aa61f 100644 --- a/eo-parser/src/main/java/org/eolang/parser/errors/UnderlinedMessage.java +++ b/eo-parser/src/main/java/org/eolang/parser/errors/UnderlinedMessage.java @@ -41,7 +41,7 @@ *

* @since 0.50 * @todo #3332:30min Add more decorators for the error message. - * For example, {@link org.eolang.parser.ParsingErrors} currently contains logic related to the message formatting. + * For example, {@link ParsingErrors} currently contains logic related to the message formatting. * It's better to create a separate class for this purpose. */ final class UnderlinedMessage { diff --git a/eo-parser/src/main/java/org/eolang/parser/errors/package-info.java b/eo-parser/src/main/java/org/eolang/parser/errors/package-info.java index fb73a69d54..11ca16d5c4 100644 --- a/eo-parser/src/main/java/org/eolang/parser/errors/package-info.java +++ b/eo-parser/src/main/java/org/eolang/parser/errors/package-info.java @@ -25,4 +25,4 @@ * This package contains all the classes related to errors that can be found in the parsing process. * @since 0.50 */ -package org.eolang.parser.errors; \ No newline at end of file +package org.eolang.parser.errors; diff --git a/eo-parser/src/test/java/org/eolang/parser/errors/package-info.java b/eo-parser/src/test/java/org/eolang/parser/errors/package-info.java new file mode 100644 index 0000000000..c082286b79 --- /dev/null +++ b/eo-parser/src/test/java/org/eolang/parser/errors/package-info.java @@ -0,0 +1,28 @@ +/* + * 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. + */ +/** + * Contains tests for the error handling in the parser. + * @since 0.50 + */ +package org.eolang.parser.errors; From 02f86f79edd064921d38e094dca56d3c38c8f2d6 Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Tue, 24 Dec 2024 11:36:49 +0300 Subject: [PATCH 04/11] feat(#3742): shake methods --- .../eolang/parser/errors/EoParserErrors.java | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/eo-parser/src/main/java/org/eolang/parser/errors/EoParserErrors.java b/eo-parser/src/main/java/org/eolang/parser/errors/EoParserErrors.java index d0f22ff7cf..6c4aad966d 100644 --- a/eo-parser/src/main/java/org/eolang/parser/errors/EoParserErrors.java +++ b/eo-parser/src/main/java/org/eolang/parser/errors/EoParserErrors.java @@ -73,6 +73,19 @@ private EoParserErrors(final List errors, final Lines lines) { this.lines = lines; } + /** + * The number of errors. + * @return The number of errors. + */ + public int size() { + return this.errors.size(); + } + + @Override + public Iterator iterator() { + return new ErrorDirectives(this.errors).iterator(); + } + // @checkstyle ParameterNumberCheck (10 lines) @Override public void syntaxError( @@ -144,17 +157,4 @@ public void syntaxError( ); } } - - @Override - public Iterator iterator() { - return new ErrorDirectives(this.errors).iterator(); - } - - /** - * The number of errors. - * @return The number of errors. - */ - public int size() { - return this.errors.size(); - } } From 3bf80e0f198e691e071a976691e59a838884331d Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Tue, 24 Dec 2024 15:15:21 +0300 Subject: [PATCH 05/11] feat(#3742): ErrorDirectives -> DrErrors --- .../parser/errors/{ErrorDirectives.java => DrErrors.java} | 4 ++-- .../main/java/org/eolang/parser/errors/EoParserErrors.java | 2 +- .../src/main/java/org/eolang/parser/errors/ParsingErrors.java | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) rename eo-parser/src/main/java/org/eolang/parser/errors/{ErrorDirectives.java => DrErrors.java} (94%) diff --git a/eo-parser/src/main/java/org/eolang/parser/errors/ErrorDirectives.java b/eo-parser/src/main/java/org/eolang/parser/errors/DrErrors.java similarity index 94% rename from eo-parser/src/main/java/org/eolang/parser/errors/ErrorDirectives.java rename to eo-parser/src/main/java/org/eolang/parser/errors/DrErrors.java index b7d79fe763..beed3dc822 100644 --- a/eo-parser/src/main/java/org/eolang/parser/errors/ErrorDirectives.java +++ b/eo-parser/src/main/java/org/eolang/parser/errors/DrErrors.java @@ -34,7 +34,7 @@ * Error directives. * @since 0.50 */ -final class ErrorDirectives implements Iterable { +final class DrErrors implements Iterable { /** * Errors accumulated. @@ -45,7 +45,7 @@ final class ErrorDirectives implements Iterable { * Ctor. * @param errors The errors. */ - ErrorDirectives(final List errors) { + DrErrors(final List errors) { this.errors = errors; } diff --git a/eo-parser/src/main/java/org/eolang/parser/errors/EoParserErrors.java b/eo-parser/src/main/java/org/eolang/parser/errors/EoParserErrors.java index 6c4aad966d..7fc1117e4c 100644 --- a/eo-parser/src/main/java/org/eolang/parser/errors/EoParserErrors.java +++ b/eo-parser/src/main/java/org/eolang/parser/errors/EoParserErrors.java @@ -83,7 +83,7 @@ public int size() { @Override public Iterator iterator() { - return new ErrorDirectives(this.errors).iterator(); + return new DrErrors(this.errors).iterator(); } // @checkstyle ParameterNumberCheck (10 lines) diff --git a/eo-parser/src/main/java/org/eolang/parser/errors/ParsingErrors.java b/eo-parser/src/main/java/org/eolang/parser/errors/ParsingErrors.java index e0e0d82a23..a3c9f3985a 100644 --- a/eo-parser/src/main/java/org/eolang/parser/errors/ParsingErrors.java +++ b/eo-parser/src/main/java/org/eolang/parser/errors/ParsingErrors.java @@ -101,7 +101,7 @@ public void syntaxError( @Override public Iterator iterator() { - return new ErrorDirectives(this.errors).iterator(); + return new DrErrors(this.errors).iterator(); } /** From 57b9a5cbce7f231f0f80f51baae5a2acd7d5b99e Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Tue, 24 Dec 2024 15:17:01 +0300 Subject: [PATCH 06/11] feat(#3742): add Dr- prefix --- eo-parser/src/main/java/org/eolang/parser/EoSyntax.java | 8 ++++---- eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java | 4 ++-- .../errors/{EoParserErrors.java => DrEoParserErrors.java} | 6 +++--- .../errors/{ParsingErrors.java => DrParsingErrors.java} | 8 ++++---- .../java/org/eolang/parser/errors/UnderlinedMessage.java | 2 +- 5 files changed, 14 insertions(+), 14 deletions(-) rename eo-parser/src/main/java/org/eolang/parser/errors/{EoParserErrors.java => DrEoParserErrors.java} (95%) rename eo-parser/src/main/java/org/eolang/parser/errors/{ParsingErrors.java => DrParsingErrors.java} (91%) diff --git a/eo-parser/src/main/java/org/eolang/parser/EoSyntax.java b/eo-parser/src/main/java/org/eolang/parser/EoSyntax.java index 301c34309c..f902da37cc 100644 --- a/eo-parser/src/main/java/org/eolang/parser/EoSyntax.java +++ b/eo-parser/src/main/java/org/eolang/parser/EoSyntax.java @@ -38,8 +38,8 @@ import org.cactoos.text.Joined; import org.cactoos.text.Split; import org.cactoos.text.TextOf; -import org.eolang.parser.errors.EoParserErrors; -import org.eolang.parser.errors.ParsingErrors; +import org.eolang.parser.errors.DrEoParserErrors; +import org.eolang.parser.errors.DrParsingErrors; import org.xembly.Directives; import org.xembly.Xembler; @@ -107,7 +107,7 @@ public EoSyntax(final String nme, final Input ipt) { */ public XML parsed() throws IOException { final List lines = this.lines(); - final ParsingErrors spy = new ParsingErrors(lines); + final DrParsingErrors spy = new DrParsingErrors(lines); final EoLexer lexer = new EoIndentLexer(this.normalize()); lexer.removeErrorListeners(); lexer.addErrorListener(spy); @@ -115,7 +115,7 @@ public XML parsed() throws IOException { new CommonTokenStream(lexer) ); parser.removeErrorListeners(); - final EoParserErrors eospy = new EoParserErrors(lines); + final DrEoParserErrors eospy = new DrEoParserErrors(lines); parser.addErrorListener(eospy); final XeEoListener xel = new XeEoListener(this.name); new ParseTreeWalker().walk(xel, parser.program()); diff --git a/eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java b/eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java index 1923be8107..62ab106d10 100644 --- a/eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java +++ b/eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java @@ -32,7 +32,7 @@ import org.antlr.v4.runtime.tree.ParseTreeWalker; import org.cactoos.Text; import org.cactoos.io.InputStreamOf; -import org.eolang.parser.errors.ParsingErrors; +import org.eolang.parser.errors.DrParsingErrors; import org.xembly.Directive; import org.xembly.Directives; import org.xembly.Xembler; @@ -92,7 +92,7 @@ public PhiSyntax( @Override public XML parsed() throws IOException { final XePhiListener xel = new XePhiListener(this.name); - final ParsingErrors spy = new ParsingErrors(this.input); + final DrParsingErrors spy = new DrParsingErrors(this.input); final PhiLexer lexer = new PhiLexer( CharStreams.fromStream( new InputStreamOf(this.input) diff --git a/eo-parser/src/main/java/org/eolang/parser/errors/EoParserErrors.java b/eo-parser/src/main/java/org/eolang/parser/errors/DrEoParserErrors.java similarity index 95% rename from eo-parser/src/main/java/org/eolang/parser/errors/EoParserErrors.java rename to eo-parser/src/main/java/org/eolang/parser/errors/DrEoParserErrors.java index 7fc1117e4c..378e4f2fa9 100644 --- a/eo-parser/src/main/java/org/eolang/parser/errors/EoParserErrors.java +++ b/eo-parser/src/main/java/org/eolang/parser/errors/DrEoParserErrors.java @@ -43,7 +43,7 @@ * Accumulates all parsing errors related to EO parser. * @since 0.50 */ -public final class EoParserErrors extends BaseErrorListener implements Iterable { +public final class DrEoParserErrors extends BaseErrorListener implements Iterable { /** * Errors accumulated. @@ -59,7 +59,7 @@ public final class EoParserErrors extends BaseErrorListener implements Iterable< * Ctor. * @param src The source in lines */ - public EoParserErrors(final List src) { + public DrEoParserErrors(final List src) { this(new ArrayList<>(0), new Lines(src)); } @@ -68,7 +68,7 @@ public EoParserErrors(final List src) { * @param errors Errors accumulated * @param lines The source in lines */ - private EoParserErrors(final List errors, final Lines lines) { + private DrEoParserErrors(final List errors, final Lines lines) { this.errors = errors; this.lines = lines; } diff --git a/eo-parser/src/main/java/org/eolang/parser/errors/ParsingErrors.java b/eo-parser/src/main/java/org/eolang/parser/errors/DrParsingErrors.java similarity index 91% rename from eo-parser/src/main/java/org/eolang/parser/errors/ParsingErrors.java rename to eo-parser/src/main/java/org/eolang/parser/errors/DrParsingErrors.java index a3c9f3985a..6cffbd2322 100644 --- a/eo-parser/src/main/java/org/eolang/parser/errors/ParsingErrors.java +++ b/eo-parser/src/main/java/org/eolang/parser/errors/DrParsingErrors.java @@ -39,7 +39,7 @@ * * @since 0.30.0 */ -public final class ParsingErrors extends BaseErrorListener implements Iterable { +public final class DrParsingErrors extends BaseErrorListener implements Iterable { /** * Errors accumulated. @@ -55,7 +55,7 @@ public final class ParsingErrors extends BaseErrorListener implements Iterable(lines)); } @@ -63,7 +63,7 @@ public ParsingErrors(final Text... lines) { * Ctor. * @param src The source in lines */ - public ParsingErrors(final List src) { + public DrParsingErrors(final List src) { this(new ArrayList<>(0), new Lines(src)); } @@ -72,7 +72,7 @@ public ParsingErrors(final List src) { * @param errors Errors accumulated * @param lines The source in lines */ - private ParsingErrors(final List errors, final Lines lines) { + private DrParsingErrors(final List errors, final Lines lines) { this.errors = errors; this.lines = lines; } diff --git a/eo-parser/src/main/java/org/eolang/parser/errors/UnderlinedMessage.java b/eo-parser/src/main/java/org/eolang/parser/errors/UnderlinedMessage.java index 75182aa61f..64a6943bd9 100644 --- a/eo-parser/src/main/java/org/eolang/parser/errors/UnderlinedMessage.java +++ b/eo-parser/src/main/java/org/eolang/parser/errors/UnderlinedMessage.java @@ -41,7 +41,7 @@ *

* @since 0.50 * @todo #3332:30min Add more decorators for the error message. - * For example, {@link ParsingErrors} currently contains logic related to the message formatting. + * For example, {@link DrParsingErrors} currently contains logic related to the message formatting. * It's better to create a separate class for this purpose. */ final class UnderlinedMessage { From 86a209167cfcfa721630ae9988068483c0ec924a Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Tue, 24 Dec 2024 17:08:26 +0300 Subject: [PATCH 07/11] feat(#3742): remove redundant package --- .../parser/{errors => }/DrEoParserErrors.java | 8 ++---- .../eolang/parser/{errors => }/DrErrors.java | 3 +- .../parser/{errors => }/DrParsingErrors.java | 9 +++--- .../main/java/org/eolang/parser/EoSyntax.java | 2 -- .../org/eolang/parser/{errors => }/Lines.java | 2 +- .../java/org/eolang/parser/PhiSyntax.java | 1 - .../{errors => }/UnderlinedMessage.java | 2 +- .../eolang/parser/errors/package-info.java | 28 ------------------- .../{errors => }/UnderlinedMessageTest.java | 3 +- .../eolang/parser/errors/package-info.java | 28 ------------------- 10 files changed, 12 insertions(+), 74 deletions(-) rename eo-parser/src/main/java/org/eolang/parser/{errors => }/DrEoParserErrors.java (95%) rename eo-parser/src/main/java/org/eolang/parser/{errors => }/DrErrors.java (96%) rename eo-parser/src/main/java/org/eolang/parser/{errors => }/DrParsingErrors.java (91%) rename eo-parser/src/main/java/org/eolang/parser/{errors => }/Lines.java (98%) rename eo-parser/src/main/java/org/eolang/parser/{errors => }/UnderlinedMessage.java (99%) delete mode 100644 eo-parser/src/main/java/org/eolang/parser/errors/package-info.java rename eo-parser/src/test/java/org/eolang/parser/{errors => }/UnderlinedMessageTest.java (97%) delete mode 100644 eo-parser/src/test/java/org/eolang/parser/errors/package-info.java diff --git a/eo-parser/src/main/java/org/eolang/parser/errors/DrEoParserErrors.java b/eo-parser/src/main/java/org/eolang/parser/DrEoParserErrors.java similarity index 95% rename from eo-parser/src/main/java/org/eolang/parser/errors/DrEoParserErrors.java rename to eo-parser/src/main/java/org/eolang/parser/DrEoParserErrors.java index 378e4f2fa9..fc9cc2a206 100644 --- a/eo-parser/src/main/java/org/eolang/parser/errors/DrEoParserErrors.java +++ b/eo-parser/src/main/java/org/eolang/parser/DrEoParserErrors.java @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.parser.errors; +package org.eolang.parser; import java.util.ArrayList; import java.util.Iterator; @@ -35,15 +35,13 @@ import org.antlr.v4.runtime.Recognizer; import org.antlr.v4.runtime.Token; import org.cactoos.Text; -import org.eolang.parser.EoParser; -import org.eolang.parser.ParsingException; import org.xembly.Directive; /** * Accumulates all parsing errors related to EO parser. * @since 0.50 */ -public final class DrEoParserErrors extends BaseErrorListener implements Iterable { +final class DrEoParserErrors extends BaseErrorListener implements Iterable { /** * Errors accumulated. @@ -59,7 +57,7 @@ public final class DrEoParserErrors extends BaseErrorListener implements Iterabl * Ctor. * @param src The source in lines */ - public DrEoParserErrors(final List src) { + DrEoParserErrors(final List src) { this(new ArrayList<>(0), new Lines(src)); } diff --git a/eo-parser/src/main/java/org/eolang/parser/errors/DrErrors.java b/eo-parser/src/main/java/org/eolang/parser/DrErrors.java similarity index 96% rename from eo-parser/src/main/java/org/eolang/parser/errors/DrErrors.java rename to eo-parser/src/main/java/org/eolang/parser/DrErrors.java index beed3dc822..4859b0e4ec 100644 --- a/eo-parser/src/main/java/org/eolang/parser/errors/DrErrors.java +++ b/eo-parser/src/main/java/org/eolang/parser/DrErrors.java @@ -21,12 +21,11 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.parser.errors; +package org.eolang.parser; import java.util.Iterator; import java.util.List; import org.cactoos.iterable.Mapped; -import org.eolang.parser.ParsingException; import org.xembly.Directive; import org.xembly.Directives; diff --git a/eo-parser/src/main/java/org/eolang/parser/errors/DrParsingErrors.java b/eo-parser/src/main/java/org/eolang/parser/DrParsingErrors.java similarity index 91% rename from eo-parser/src/main/java/org/eolang/parser/errors/DrParsingErrors.java rename to eo-parser/src/main/java/org/eolang/parser/DrParsingErrors.java index 6cffbd2322..e7fce89da3 100644 --- a/eo-parser/src/main/java/org/eolang/parser/errors/DrParsingErrors.java +++ b/eo-parser/src/main/java/org/eolang/parser/DrParsingErrors.java @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.parser.errors; +package org.eolang.parser; import java.util.ArrayList; import java.util.Iterator; @@ -31,7 +31,6 @@ import org.antlr.v4.runtime.Recognizer; import org.cactoos.Text; import org.cactoos.list.ListOf; -import org.eolang.parser.ParsingException; import org.xembly.Directive; /** @@ -39,7 +38,7 @@ * * @since 0.30.0 */ -public final class DrParsingErrors extends BaseErrorListener implements Iterable { +final class DrParsingErrors extends BaseErrorListener implements Iterable { /** * Errors accumulated. @@ -55,7 +54,7 @@ public final class DrParsingErrors extends BaseErrorListener implements Iterable * Ctor. * @param lines The source in lines */ - public DrParsingErrors(final Text... lines) { + DrParsingErrors(final Text... lines) { this(new ListOf<>(lines)); } @@ -63,7 +62,7 @@ public DrParsingErrors(final Text... lines) { * Ctor. * @param src The source in lines */ - public DrParsingErrors(final List src) { + DrParsingErrors(final List src) { this(new ArrayList<>(0), new Lines(src)); } diff --git a/eo-parser/src/main/java/org/eolang/parser/EoSyntax.java b/eo-parser/src/main/java/org/eolang/parser/EoSyntax.java index f902da37cc..b56dcca36b 100644 --- a/eo-parser/src/main/java/org/eolang/parser/EoSyntax.java +++ b/eo-parser/src/main/java/org/eolang/parser/EoSyntax.java @@ -38,8 +38,6 @@ import org.cactoos.text.Joined; import org.cactoos.text.Split; import org.cactoos.text.TextOf; -import org.eolang.parser.errors.DrEoParserErrors; -import org.eolang.parser.errors.DrParsingErrors; import org.xembly.Directives; import org.xembly.Xembler; diff --git a/eo-parser/src/main/java/org/eolang/parser/errors/Lines.java b/eo-parser/src/main/java/org/eolang/parser/Lines.java similarity index 98% rename from eo-parser/src/main/java/org/eolang/parser/errors/Lines.java rename to eo-parser/src/main/java/org/eolang/parser/Lines.java index 71ebeaa522..64dbfc2731 100644 --- a/eo-parser/src/main/java/org/eolang/parser/errors/Lines.java +++ b/eo-parser/src/main/java/org/eolang/parser/Lines.java @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.parser.errors; +package org.eolang.parser; import java.util.List; import java.util.Optional; diff --git a/eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java b/eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java index 62ab106d10..0ec73009d5 100644 --- a/eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java +++ b/eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java @@ -32,7 +32,6 @@ import org.antlr.v4.runtime.tree.ParseTreeWalker; import org.cactoos.Text; import org.cactoos.io.InputStreamOf; -import org.eolang.parser.errors.DrParsingErrors; import org.xembly.Directive; import org.xembly.Directives; import org.xembly.Xembler; diff --git a/eo-parser/src/main/java/org/eolang/parser/errors/UnderlinedMessage.java b/eo-parser/src/main/java/org/eolang/parser/UnderlinedMessage.java similarity index 99% rename from eo-parser/src/main/java/org/eolang/parser/errors/UnderlinedMessage.java rename to eo-parser/src/main/java/org/eolang/parser/UnderlinedMessage.java index 64a6943bd9..08ddf2b511 100644 --- a/eo-parser/src/main/java/org/eolang/parser/errors/UnderlinedMessage.java +++ b/eo-parser/src/main/java/org/eolang/parser/UnderlinedMessage.java @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.parser.errors; +package org.eolang.parser; import java.util.Collections; diff --git a/eo-parser/src/main/java/org/eolang/parser/errors/package-info.java b/eo-parser/src/main/java/org/eolang/parser/errors/package-info.java deleted file mode 100644 index 11ca16d5c4..0000000000 --- a/eo-parser/src/main/java/org/eolang/parser/errors/package-info.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * 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. - */ -/** - * This package contains all the classes related to errors that can be found in the parsing process. - * @since 0.50 - */ -package org.eolang.parser.errors; diff --git a/eo-parser/src/test/java/org/eolang/parser/errors/UnderlinedMessageTest.java b/eo-parser/src/test/java/org/eolang/parser/UnderlinedMessageTest.java similarity index 97% rename from eo-parser/src/test/java/org/eolang/parser/errors/UnderlinedMessageTest.java rename to eo-parser/src/test/java/org/eolang/parser/UnderlinedMessageTest.java index 92ff121f98..a6c2a6da46 100644 --- a/eo-parser/src/test/java/org/eolang/parser/errors/UnderlinedMessageTest.java +++ b/eo-parser/src/test/java/org/eolang/parser/UnderlinedMessageTest.java @@ -21,9 +21,10 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.parser.errors; +package org.eolang.parser; import java.util.stream.Stream; +import org.eolang.parser.errors.UnderlinedMessage; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.params.ParameterizedTest; diff --git a/eo-parser/src/test/java/org/eolang/parser/errors/package-info.java b/eo-parser/src/test/java/org/eolang/parser/errors/package-info.java deleted file mode 100644 index c082286b79..0000000000 --- a/eo-parser/src/test/java/org/eolang/parser/errors/package-info.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * 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. - */ -/** - * Contains tests for the error handling in the parser. - * @since 0.50 - */ -package org.eolang.parser.errors; From dc92fa4fb81eed9404cf822614fda70e2000da95 Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Tue, 24 Dec 2024 17:13:21 +0300 Subject: [PATCH 08/11] feat(#3742): rename classes back --- ...oParserErrors.java => EoParserErrors.java} | 16 ++++++++------ .../main/java/org/eolang/parser/EoSyntax.java | 6 ++--- ...rParsingErrors.java => GeneralErrors.java} | 22 ++++++++++--------- .../java/org/eolang/parser/PhiSyntax.java | 2 +- .../org/eolang/parser/UnderlinedMessage.java | 2 +- .../eolang/parser/UnderlinedMessageTest.java | 1 - 6 files changed, 26 insertions(+), 23 deletions(-) rename eo-parser/src/main/java/org/eolang/parser/{DrEoParserErrors.java => EoParserErrors.java} (93%) rename eo-parser/src/main/java/org/eolang/parser/{DrParsingErrors.java => GeneralErrors.java} (87%) diff --git a/eo-parser/src/main/java/org/eolang/parser/DrEoParserErrors.java b/eo-parser/src/main/java/org/eolang/parser/EoParserErrors.java similarity index 93% rename from eo-parser/src/main/java/org/eolang/parser/DrEoParserErrors.java rename to eo-parser/src/main/java/org/eolang/parser/EoParserErrors.java index fc9cc2a206..6947bdd204 100644 --- a/eo-parser/src/main/java/org/eolang/parser/DrEoParserErrors.java +++ b/eo-parser/src/main/java/org/eolang/parser/EoParserErrors.java @@ -24,7 +24,6 @@ package org.eolang.parser; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; import java.util.Objects; import org.antlr.v4.runtime.BaseErrorListener; @@ -41,7 +40,7 @@ * Accumulates all parsing errors related to EO parser. * @since 0.50 */ -final class DrEoParserErrors extends BaseErrorListener implements Iterable { +final class EoParserErrors extends BaseErrorListener { /** * Errors accumulated. @@ -57,7 +56,7 @@ final class DrEoParserErrors extends BaseErrorListener implements Iterable src) { + EoParserErrors(final List src) { this(new ArrayList<>(0), new Lines(src)); } @@ -66,7 +65,7 @@ final class DrEoParserErrors extends BaseErrorListener implements Iterable errors, final Lines lines) { + private EoParserErrors(final List errors, final Lines lines) { this.errors = errors; this.lines = lines; } @@ -79,9 +78,12 @@ public int size() { return this.errors.size(); } - @Override - public Iterator iterator() { - return new DrErrors(this.errors).iterator(); + /** + * All errors accumulated as directives. + * @return The errors. + */ + public Iterable directives() { + return new DrErrors(this.errors); } // @checkstyle ParameterNumberCheck (10 lines) diff --git a/eo-parser/src/main/java/org/eolang/parser/EoSyntax.java b/eo-parser/src/main/java/org/eolang/parser/EoSyntax.java index b56dcca36b..e9b81129d4 100644 --- a/eo-parser/src/main/java/org/eolang/parser/EoSyntax.java +++ b/eo-parser/src/main/java/org/eolang/parser/EoSyntax.java @@ -105,7 +105,7 @@ public EoSyntax(final String nme, final Input ipt) { */ public XML parsed() throws IOException { final List lines = this.lines(); - final DrParsingErrors spy = new DrParsingErrors(lines); + final GeneralErrors spy = new GeneralErrors(lines); final EoLexer lexer = new EoIndentLexer(this.normalize()); lexer.removeErrorListeners(); lexer.addErrorListener(spy); @@ -113,14 +113,14 @@ public XML parsed() throws IOException { new CommonTokenStream(lexer) ); parser.removeErrorListeners(); - final DrEoParserErrors eospy = new DrEoParserErrors(lines); + final EoParserErrors eospy = new EoParserErrors(lines); parser.addErrorListener(eospy); final XeEoListener xel = new XeEoListener(this.name); new ParseTreeWalker().walk(xel, parser.program()); final XML dom = Syntax.CANONICAL.pass( new XMLDocument( new Xembler( - new Directives(xel).append(spy).append(eospy) + new Directives(xel).append(spy.directives()).append(eospy.directives()) ).domQuietly() ) ); diff --git a/eo-parser/src/main/java/org/eolang/parser/DrParsingErrors.java b/eo-parser/src/main/java/org/eolang/parser/GeneralErrors.java similarity index 87% rename from eo-parser/src/main/java/org/eolang/parser/DrParsingErrors.java rename to eo-parser/src/main/java/org/eolang/parser/GeneralErrors.java index e7fce89da3..ed1e383243 100644 --- a/eo-parser/src/main/java/org/eolang/parser/DrParsingErrors.java +++ b/eo-parser/src/main/java/org/eolang/parser/GeneralErrors.java @@ -24,7 +24,6 @@ package org.eolang.parser; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; import org.antlr.v4.runtime.BaseErrorListener; import org.antlr.v4.runtime.RecognitionException; @@ -38,7 +37,7 @@ * * @since 0.30.0 */ -final class DrParsingErrors extends BaseErrorListener implements Iterable { +final class GeneralErrors extends BaseErrorListener { /** * Errors accumulated. @@ -54,7 +53,7 @@ final class DrParsingErrors extends BaseErrorListener implements Iterable(lines)); } @@ -62,7 +61,7 @@ final class DrParsingErrors extends BaseErrorListener implements Iterable src) { + GeneralErrors(final List src) { this(new ArrayList<>(0), new Lines(src)); } @@ -71,7 +70,7 @@ final class DrParsingErrors extends BaseErrorListener implements Iterable errors, final Lines lines) { + private GeneralErrors(final List errors, final Lines lines) { this.errors = errors; this.lines = lines; } @@ -98,11 +97,6 @@ public void syntaxError( ); } - @Override - public Iterator iterator() { - return new DrErrors(this.errors).iterator(); - } - /** * How many errors? * @return Count of errors accumulated @@ -110,4 +104,12 @@ public Iterator iterator() { public int size() { return this.errors.size(); } + + /** + * All errors accumulated as directives. + * @return The errors + */ + public Iterable directives() { + return new DrErrors(this.errors); + } } diff --git a/eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java b/eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java index 0ec73009d5..e5f7ff98cf 100644 --- a/eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java +++ b/eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java @@ -91,7 +91,7 @@ public PhiSyntax( @Override public XML parsed() throws IOException { final XePhiListener xel = new XePhiListener(this.name); - final DrParsingErrors spy = new DrParsingErrors(this.input); + final GeneralErrors spy = new GeneralErrors(this.input); final PhiLexer lexer = new PhiLexer( CharStreams.fromStream( new InputStreamOf(this.input) diff --git a/eo-parser/src/main/java/org/eolang/parser/UnderlinedMessage.java b/eo-parser/src/main/java/org/eolang/parser/UnderlinedMessage.java index 08ddf2b511..a6f3a3ce2b 100644 --- a/eo-parser/src/main/java/org/eolang/parser/UnderlinedMessage.java +++ b/eo-parser/src/main/java/org/eolang/parser/UnderlinedMessage.java @@ -41,7 +41,7 @@ *

* @since 0.50 * @todo #3332:30min Add more decorators for the error message. - * For example, {@link DrParsingErrors} currently contains logic related to the message formatting. + * 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 { diff --git a/eo-parser/src/test/java/org/eolang/parser/UnderlinedMessageTest.java b/eo-parser/src/test/java/org/eolang/parser/UnderlinedMessageTest.java index a6c2a6da46..f7b2215c3d 100644 --- a/eo-parser/src/test/java/org/eolang/parser/UnderlinedMessageTest.java +++ b/eo-parser/src/test/java/org/eolang/parser/UnderlinedMessageTest.java @@ -24,7 +24,6 @@ package org.eolang.parser; import java.util.stream.Stream; -import org.eolang.parser.errors.UnderlinedMessage; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.params.ParameterizedTest; From 5244ed9a28f6e5f9401af0adbb10d9d1272e36d7 Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Tue, 24 Dec 2024 17:16:44 +0300 Subject: [PATCH 09/11] feat(#3742): fix compilation errors --- eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java b/eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java index e5f7ff98cf..b48b559dfd 100644 --- a/eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java +++ b/eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java @@ -107,7 +107,7 @@ public XML parsed() throws IOException { final XML dom = Syntax.CANONICAL.pass( new XMLDocument( new Xembler( - new Directives(xel).append(spy).append(this.extra) + new Directives(xel).append(spy.directives()).append(this.extra) ).domQuietly() ) ); From fbbaa29e8b8c67eb40ea9862e3ea67a19dd578c1 Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Wed, 25 Dec 2024 09:56:42 +0300 Subject: [PATCH 10/11] feat(#3742): add Error interface to conform the review suggestions --- .../main/java/org/eolang/parser/DrErrors.java | 7 ++- .../org/eolang/parser/EoParserErrors.java | 22 ++++----- .../main/java/org/eolang/parser/EoSyntax.java | 2 +- .../main/java/org/eolang/parser/Errors.java | 46 +++++++++++++++++++ .../java/org/eolang/parser/GeneralErrors.java | 22 ++++----- .../java/org/eolang/parser/PhiSyntax.java | 2 +- 6 files changed, 67 insertions(+), 34 deletions(-) create mode 100644 eo-parser/src/main/java/org/eolang/parser/Errors.java diff --git a/eo-parser/src/main/java/org/eolang/parser/DrErrors.java b/eo-parser/src/main/java/org/eolang/parser/DrErrors.java index 4859b0e4ec..f1d35741da 100644 --- a/eo-parser/src/main/java/org/eolang/parser/DrErrors.java +++ b/eo-parser/src/main/java/org/eolang/parser/DrErrors.java @@ -24,7 +24,6 @@ package org.eolang.parser; import java.util.Iterator; -import java.util.List; import org.cactoos.iterable.Mapped; import org.xembly.Directive; import org.xembly.Directives; @@ -38,13 +37,13 @@ final class DrErrors implements Iterable { /** * Errors accumulated. */ - private final List errors; + private final Errors errors; /** * Ctor. * @param errors The errors. */ - DrErrors(final List errors) { + DrErrors(final Errors errors) { this.errors = errors; } @@ -62,7 +61,7 @@ public Iterator iterator() { .attr("line", error.line()) .attr("severity", "critical") .set(error.getMessage()), - this.errors + this.errors.all() ) ).iterator(); } diff --git a/eo-parser/src/main/java/org/eolang/parser/EoParserErrors.java b/eo-parser/src/main/java/org/eolang/parser/EoParserErrors.java index 6947bdd204..e233982411 100644 --- a/eo-parser/src/main/java/org/eolang/parser/EoParserErrors.java +++ b/eo-parser/src/main/java/org/eolang/parser/EoParserErrors.java @@ -24,6 +24,7 @@ package org.eolang.parser; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Objects; import org.antlr.v4.runtime.BaseErrorListener; @@ -34,13 +35,12 @@ import org.antlr.v4.runtime.Recognizer; import org.antlr.v4.runtime.Token; import org.cactoos.Text; -import org.xembly.Directive; /** * Accumulates all parsing errors related to EO parser. * @since 0.50 */ -final class EoParserErrors extends BaseErrorListener { +final class EoParserErrors extends BaseErrorListener implements Errors { /** * Errors accumulated. @@ -70,20 +70,14 @@ private EoParserErrors(final List errors, final Lines lines) { this.lines = lines; } - /** - * The number of errors. - * @return The number of errors. - */ - public int size() { - return this.errors.size(); + @Override + public List all() { + return Collections.unmodifiableList(this.errors); } - /** - * All errors accumulated as directives. - * @return The errors. - */ - public Iterable directives() { - return new DrErrors(this.errors); + @Override + public int size() { + return this.errors.size(); } // @checkstyle ParameterNumberCheck (10 lines) diff --git a/eo-parser/src/main/java/org/eolang/parser/EoSyntax.java b/eo-parser/src/main/java/org/eolang/parser/EoSyntax.java index e9b81129d4..35e161a4b9 100644 --- a/eo-parser/src/main/java/org/eolang/parser/EoSyntax.java +++ b/eo-parser/src/main/java/org/eolang/parser/EoSyntax.java @@ -120,7 +120,7 @@ public XML parsed() throws IOException { final XML dom = Syntax.CANONICAL.pass( new XMLDocument( new Xembler( - new Directives(xel).append(spy.directives()).append(eospy.directives()) + new Directives(xel).append(new DrErrors(spy)).append(new DrErrors(eospy)) ).domQuietly() ) ); diff --git a/eo-parser/src/main/java/org/eolang/parser/Errors.java b/eo-parser/src/main/java/org/eolang/parser/Errors.java new file mode 100644 index 0000000000..645951fb71 --- /dev/null +++ b/eo-parser/src/main/java/org/eolang/parser/Errors.java @@ -0,0 +1,46 @@ +/* + * 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; + +import java.util.List; + +/** + * All parsing errors. + * @since 0.50 + */ +interface Errors { + + /** + * All errors as a list of exceptions. + * @return The list of exceptions. + */ + List all(); + + /** + * The number of errors. + * @return The number of errors + */ + int size(); + +} diff --git a/eo-parser/src/main/java/org/eolang/parser/GeneralErrors.java b/eo-parser/src/main/java/org/eolang/parser/GeneralErrors.java index ed1e383243..1bb8cc0909 100644 --- a/eo-parser/src/main/java/org/eolang/parser/GeneralErrors.java +++ b/eo-parser/src/main/java/org/eolang/parser/GeneralErrors.java @@ -24,20 +24,20 @@ package org.eolang.parser; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import org.antlr.v4.runtime.BaseErrorListener; import org.antlr.v4.runtime.RecognitionException; import org.antlr.v4.runtime.Recognizer; import org.cactoos.Text; import org.cactoos.list.ListOf; -import org.xembly.Directive; /** * Accumulates all parsing errors. * * @since 0.30.0 */ -final class GeneralErrors extends BaseErrorListener { +final class GeneralErrors extends BaseErrorListener implements Errors { /** * Errors accumulated. @@ -97,19 +97,13 @@ public void syntaxError( ); } - /** - * How many errors? - * @return Count of errors accumulated - */ - public int size() { - return this.errors.size(); + @Override + public List all() { + return Collections.unmodifiableList(this.errors); } - /** - * All errors accumulated as directives. - * @return The errors - */ - public Iterable directives() { - return new DrErrors(this.errors); + @Override + public int size() { + return this.errors.size(); } } diff --git a/eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java b/eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java index b48b559dfd..dfd6ffede3 100644 --- a/eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java +++ b/eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java @@ -107,7 +107,7 @@ public XML parsed() throws IOException { final XML dom = Syntax.CANONICAL.pass( new XMLDocument( new Xembler( - new Directives(xel).append(spy.directives()).append(this.extra) + new Directives(xel).append(new DrErrors(spy)).append(this.extra) ).domQuietly() ) ); From d4764a698da350c8338bd0f9eb5035c4f56d8c1a Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Wed, 25 Dec 2024 10:22:30 +0300 Subject: [PATCH 11/11] feat(#3742): remove Errors abstraction --- .../main/java/org/eolang/parser/DrErrors.java | 6 +-- .../org/eolang/parser/EoParserErrors.java | 19 +++----- .../main/java/org/eolang/parser/EoSyntax.java | 8 +++- .../main/java/org/eolang/parser/Errors.java | 46 ------------------- .../java/org/eolang/parser/GeneralErrors.java | 13 ++---- .../java/org/eolang/parser/PhiSyntax.java | 7 ++- 6 files changed, 25 insertions(+), 74 deletions(-) delete mode 100644 eo-parser/src/main/java/org/eolang/parser/Errors.java diff --git a/eo-parser/src/main/java/org/eolang/parser/DrErrors.java b/eo-parser/src/main/java/org/eolang/parser/DrErrors.java index f1d35741da..0835bb4c26 100644 --- a/eo-parser/src/main/java/org/eolang/parser/DrErrors.java +++ b/eo-parser/src/main/java/org/eolang/parser/DrErrors.java @@ -37,13 +37,13 @@ final class DrErrors implements Iterable { /** * Errors accumulated. */ - private final Errors errors; + private final Iterable errors; /** * Ctor. * @param errors The errors. */ - DrErrors(final Errors errors) { + DrErrors(final Iterable errors) { this.errors = errors; } @@ -61,7 +61,7 @@ public Iterator iterator() { .attr("line", error.line()) .attr("severity", "critical") .set(error.getMessage()), - this.errors.all() + this.errors ) ).iterator(); } diff --git a/eo-parser/src/main/java/org/eolang/parser/EoParserErrors.java b/eo-parser/src/main/java/org/eolang/parser/EoParserErrors.java index e233982411..aa9dc9d463 100644 --- a/eo-parser/src/main/java/org/eolang/parser/EoParserErrors.java +++ b/eo-parser/src/main/java/org/eolang/parser/EoParserErrors.java @@ -24,7 +24,7 @@ package org.eolang.parser; import java.util.ArrayList; -import java.util.Collections; +import java.util.Iterator; import java.util.List; import java.util.Objects; import org.antlr.v4.runtime.BaseErrorListener; @@ -40,7 +40,7 @@ * Accumulates all parsing errors related to EO parser. * @since 0.50 */ -final class EoParserErrors extends BaseErrorListener implements Errors { +final class EoParserErrors extends BaseErrorListener implements Iterable { /** * Errors accumulated. @@ -70,16 +70,6 @@ private EoParserErrors(final List errors, final Lines lines) { this.lines = lines; } - @Override - public List all() { - return Collections.unmodifiableList(this.errors); - } - - @Override - public int size() { - return this.errors.size(); - } - // @checkstyle ParameterNumberCheck (10 lines) @Override public void syntaxError( @@ -151,4 +141,9 @@ public void syntaxError( ); } } + + @Override + public Iterator iterator() { + return this.errors.iterator(); + } } diff --git a/eo-parser/src/main/java/org/eolang/parser/EoSyntax.java b/eo-parser/src/main/java/org/eolang/parser/EoSyntax.java index 35e161a4b9..7b3be07cdd 100644 --- a/eo-parser/src/main/java/org/eolang/parser/EoSyntax.java +++ b/eo-parser/src/main/java/org/eolang/parser/EoSyntax.java @@ -34,6 +34,8 @@ import org.cactoos.Text; import org.cactoos.io.InputOf; import org.cactoos.list.ListOf; +import org.cactoos.scalar.LengthOf; +import org.cactoos.scalar.Unchecked; import org.cactoos.text.FormattedText; import org.cactoos.text.Joined; import org.cactoos.text.Split; @@ -124,7 +126,9 @@ public XML parsed() throws IOException { ).domQuietly() ) ); - if (spy.size() + eospy.size() == 0) { + final long errors = new Unchecked<>(new LengthOf(spy)).value() + + new Unchecked<>(new LengthOf(eospy)).value(); + if (errors == 0) { Logger.debug( this, "The %s program of %d EO lines compiled, no errors", @@ -133,7 +137,7 @@ public XML parsed() throws IOException { } else { Logger.debug( this, "The %s program of %d EO lines compiled with %d error(s)", - this.name, lines.size(), spy.size() + eospy.size() + this.name, lines.size(), errors ); } return dom; diff --git a/eo-parser/src/main/java/org/eolang/parser/Errors.java b/eo-parser/src/main/java/org/eolang/parser/Errors.java deleted file mode 100644 index 645951fb71..0000000000 --- a/eo-parser/src/main/java/org/eolang/parser/Errors.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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; - -import java.util.List; - -/** - * All parsing errors. - * @since 0.50 - */ -interface Errors { - - /** - * All errors as a list of exceptions. - * @return The list of exceptions. - */ - List all(); - - /** - * The number of errors. - * @return The number of errors - */ - int size(); - -} diff --git a/eo-parser/src/main/java/org/eolang/parser/GeneralErrors.java b/eo-parser/src/main/java/org/eolang/parser/GeneralErrors.java index 1bb8cc0909..8c765869c7 100644 --- a/eo-parser/src/main/java/org/eolang/parser/GeneralErrors.java +++ b/eo-parser/src/main/java/org/eolang/parser/GeneralErrors.java @@ -24,7 +24,7 @@ package org.eolang.parser; import java.util.ArrayList; -import java.util.Collections; +import java.util.Iterator; import java.util.List; import org.antlr.v4.runtime.BaseErrorListener; import org.antlr.v4.runtime.RecognitionException; @@ -37,7 +37,7 @@ * * @since 0.30.0 */ -final class GeneralErrors extends BaseErrorListener implements Errors { +final class GeneralErrors extends BaseErrorListener implements Iterable { /** * Errors accumulated. @@ -98,12 +98,7 @@ public void syntaxError( } @Override - public List all() { - return Collections.unmodifiableList(this.errors); - } - - @Override - public int size() { - return this.errors.size(); + public Iterator iterator() { + return this.errors.iterator(); } } diff --git a/eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java b/eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java index dfd6ffede3..37d6a8f275 100644 --- a/eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java +++ b/eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java @@ -32,6 +32,8 @@ import org.antlr.v4.runtime.tree.ParseTreeWalker; import org.cactoos.Text; import org.cactoos.io.InputStreamOf; +import org.cactoos.scalar.LengthOf; +import org.cactoos.scalar.Unchecked; import org.xembly.Directive; import org.xembly.Directives; import org.xembly.Xembler; @@ -111,12 +113,13 @@ public XML parsed() throws IOException { ).domQuietly() ) ); - if (spy.size() == 0) { + final long errors = new Unchecked<>(new LengthOf(spy)).value(); + if (errors == 0) { Logger.debug(this, "Input of PHI calculus compiled, no errors"); } else { Logger.debug( this, "Input of PHI calculus failed to compile (%d errors)", - spy.size() + errors ); } return dom;