Skip to content

Commit

Permalink
feat(objectionary#3756): add LocationMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Dec 25, 2024
1 parent 5bde75e commit e399d30
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 39 deletions.
51 changes: 12 additions & 39 deletions eo-parser/src/main/java/org/eolang/parser/EoParserErrors.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public void syntaxError(
)
);
}
final List<String> msgs = new ArrayList<>(0);
if (error instanceof NoViableAltException || error instanceof InputMismatchException) {
final Token token = (Token) symbol;
final Parser parser = (Parser) recognizer;
Expand All @@ -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
Expand Down
62 changes: 62 additions & 0 deletions eo-parser/src/main/java/org/eolang/parser/LocationMessage.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
14 changes: 14 additions & 0 deletions eo-parser/src/main/java/org/eolang/parser/ParsingException.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
package org.eolang.parser;

import java.util.List;

/**
* When parsing fails.
*
Expand All @@ -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<String> msgs, final Exception cause, final int line) {
this(String.join("\n", msgs), cause, line);
}


/**
* Ctor.
* @param msg Message
Expand Down

0 comments on commit e399d30

Please sign in to comment.