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;