From e399d3019d3b15c9be54d2a044d8ca155ddb3eb2 Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Wed, 25 Dec 2024 15:36:24 +0300 Subject: [PATCH] feat(#3756): add LocationMessage --- .../org/eolang/parser/EoParserErrors.java | 51 ++++----------- .../org/eolang/parser/LocationMessage.java | 62 +++++++++++++++++++ .../org/eolang/parser/ParsingException.java | 14 +++++ 3 files changed, 88 insertions(+), 39 deletions(-) create mode 100644 eo-parser/src/main/java/org/eolang/parser/LocationMessage.java 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 977c655998..2e31882dec 100644 --- a/eo-parser/src/main/java/org/eolang/parser/EoParserErrors.java +++ b/eo-parser/src/main/java/org/eolang/parser/EoParserErrors.java @@ -87,6 +87,7 @@ public void syntaxError( ) ); } + final List msgs = new ArrayList<>(0); if (error instanceof NoViableAltException || error instanceof InputMismatchException) { final Token token = (Token) symbol; final Parser parser = (Parser) recognizer; @@ -102,49 +103,21 @@ public void syntaxError( } else { detailed = "no viable alternative at input"; } - this.errors.add( - new ParsingException( - String.format( - "[%d:%d] %s: %s:%n%s", - line, - position, - "error", - detailed, - new UnderlinedMessage( - this.lines.line(line), - position, - Math.max(token.getStopIndex() - token.getStartIndex(), 1) - ).formatted() - ), - error, - line - ) + msgs.add(new LocationMessage(line, position, detailed).formatted()); + msgs.add( + new UnderlinedMessage( + this.lines.line(line), + position, + Math.max(token.getStopIndex() - token.getStartIndex(), 1) + ).formatted() ); } else if (Objects.isNull(error)) { - this.errors.add( - new ParsingException( - String.format( - "[%d:%d] %s: %s", - line, - position, - "error", - msg - ), - line - ) - ); + msgs.add(new LocationMessage(line, position, msg).formatted()); } else { - this.errors.add( - new ParsingException( - String.format( - "[%d:%d] %s: \"%s\"", - line, position, msg, this.lines.line(line) - ), - error, - line - ) - ); + msgs.add(new LocationMessage(line, position, msg).formatted()); + msgs.add(this.lines.line(line)); } + this.errors.add(new ParsingException(msg, error, line)); } @Override diff --git a/eo-parser/src/main/java/org/eolang/parser/LocationMessage.java b/eo-parser/src/main/java/org/eolang/parser/LocationMessage.java new file mode 100644 index 0000000000..80e9b20b72 --- /dev/null +++ b/eo-parser/src/main/java/org/eolang/parser/LocationMessage.java @@ -0,0 +1,62 @@ +/* + * 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; + +final class LocationMessage { + + /** + * The line where the error occurred. + */ + private final int line; + + /** + * The position in the line where the error occurred. + */ + private final int position; + + /** + * The error message. + */ + private final String message; + + /** + * Ctor. + * @param line The line where the error occurred. + * @param position The position in the line where the error occurred. + * @param message The error message. + */ + LocationMessage(final int line, final int position, final String message) { + this.line = line; + this.position = position; + this.message = message; + } + + /** + * Formats the error message. + * @return The formatted error message. + */ + String formatted() { + return String.format("[%d:%d] error: \"%s\"", this.line, this.position, this.message); + } +} diff --git a/eo-parser/src/main/java/org/eolang/parser/ParsingException.java b/eo-parser/src/main/java/org/eolang/parser/ParsingException.java index 87e067d7f2..b69621ca5a 100644 --- a/eo-parser/src/main/java/org/eolang/parser/ParsingException.java +++ b/eo-parser/src/main/java/org/eolang/parser/ParsingException.java @@ -23,6 +23,8 @@ */ package org.eolang.parser; +import java.util.List; + /** * When parsing fails. * @@ -40,6 +42,18 @@ public final class ParsingException extends RuntimeException { */ private final int place; + /** + * Ctor. + * @param msgs Messages + * @param cause The cause + * @param line The place + * @since 0.1 + */ + ParsingException(final List msgs, final Exception cause, final int line) { + this(String.join("\n", msgs), cause, line); + } + + /** * Ctor. * @param msg Message