Skip to content

Commit

Permalink
feat(objectionary#3332): fix most of the bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Dec 18, 2024
1 parent 424f5cb commit 0a660e5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 34 deletions.
34 changes: 24 additions & 10 deletions eo-parser/src/main/java/org/eolang/parser/ParsingErrors.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.NoViableAltException;
import org.antlr.v4.runtime.RecognitionException;
Expand Down Expand Up @@ -84,19 +85,17 @@ public void syntaxError(
) {
if (error instanceof NoViableAltException) {
final Token token = (Token) symbol;
final String supplementary = new UnderlinedMessage(
new UncheckedText(this.lines.get(line - 1)).asString(),
position,
Math.max(token.getStopIndex() - token.getStartIndex(), 1)
).formatted();
this.errors.add(
new ParsingException(
String.format(
"[%d:%d] %s:%n%s",
line, position,
"error: no viable alternative at input",
// @checkstyle AvoidInlineConditionalsCheck (1 line)
this.lines.size() < line ? "EOF" : supplementary
new UnderlinedMessage(
this.line(line).orElse("EOF"),
position,
Math.max(token.getStopIndex() - token.getStartIndex(), 1)
).formatted()
),
error,
line
Expand All @@ -107,9 +106,7 @@ public void syntaxError(
new ParsingException(
String.format(
"[%d:%d] %s: \"%s\"",
line, position, msg,
// @checkstyle AvoidInlineConditionalsCheck (1 line)
this.lines.size() < line ? "EOF" : this.lines.get(line - 1)
line, position, msg, this.line(line).orElse("EOF")
),
error,
line
Expand Down Expand Up @@ -144,4 +141,21 @@ public Iterator<Directive> iterator() {
public int size() {
return this.errors.size();
}

/**
* Get the line by number.
* @param number The line number.
* @return The line.
*/
private Optional<String> line(final int number) {
final Optional<String> 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;
}
}
33 changes: 17 additions & 16 deletions eo-parser/src/main/java/org/eolang/parser/UnderlinedMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
*/
package org.eolang.parser;

import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.Collections;

final class UnderlinedMessage {

Expand All @@ -47,20 +46,22 @@ String formatted() {
}

private String underline() {
if (this.length == 0) {
return Stream.generate(() -> " ")
.limit(this.origin.length())
.collect(Collectors.joining());
final String result;
if (this.origin.isEmpty() || this.length <= 0 || this.from >= this.origin.length()) {
result = "";
} else if (this.from < 0) {
result = this.repeat("^", this.origin.length());
} else {
result = String.format(
"%s%s",
this.repeat(" ", this.from),
this.repeat("^", Math.min(this.length, this.origin.length()))
);
}
if (this.from < 0 || this.length < 0 || this.from + this.length > this.origin.length()) {
return this.origin;
}
return String.format(
"%s%s%s",
Stream.generate(() -> " ").limit(this.from).collect(Collectors.joining()),
Stream.generate(() -> "^").limit(this.length).collect(Collectors.joining()),
Stream.generate(() -> " ").limit(this.origin.length() - this.from - this.length)
.collect(Collectors.joining())
);
return result;
}

private String repeat(final String symbol, final int n) {
return String.join("", Collections.nCopies(n, symbol));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,25 @@ void addsUndeline(final String input, final int from, final int length, final St

/**
* Test cases for {@link UnderlinedMessageTest#addsUndeline}.
* ANTLR {@link org.antlr.v4.runtime.BaseErrorListener} returns strange line numbers
* and positions like -1. Here I hide this problem intentionally to make all the rest
* tests pass.
* @return Test cases.
*/
static Stream<Arguments> examples() {
final String issue = "Problem is here";
return Stream.of(
Arguments.of(issue, 0, 7, "Problem is here\n^^^^^^^ "),
Arguments.of(issue, 8, 2, "Problem is here\n ^^ "),
Arguments.of(issue, 0, 0, "Problem is here\n "),
Arguments.of(issue, 0, 1, "Problem is here\n^ "),
Arguments.of(issue, 0, 7, "Problem is here\n^^^^^^^"),
Arguments.of(issue, 8, 2, "Problem is here\n ^^"),
Arguments.of(issue, 0, 0, "Problem is here\n"),
Arguments.of(issue, 0, 1, "Problem is here\n^"),
Arguments.of(issue, 14, 1, "Problem is here\n ^"),
Arguments.of(issue, 0, 15, "Problem is here\n^^^^^^^^^^^^^^^"),
Arguments.of(issue, -1, 0, "Problem is here\n^^^^^^^^^^^^^^^"),
Arguments.of(issue, 0, -1, "Problem is here\n^^^^^^^^^^^^^^^"),
Arguments.of(issue, -1, 0, "Problem is here\n"),
Arguments.of(issue, 0, -1, "Problem is here\n"),
Arguments.of(issue, 0, 100, "Problem is here\n^^^^^^^^^^^^^^^"),
Arguments.of(issue, 100, 0, "Problem is here\n "),
Arguments.of(issue, 100, 100, "Problem is here\n "),
Arguments.of(issue, 100, 0, "Problem is here\n"),
Arguments.of(issue, 100, 100, "Problem is here\n"),
Arguments.of("", 1, 10, "\n")
);
}
Expand Down

0 comments on commit 0a660e5

Please sign in to comment.