Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#3743): Add Error Location for Custom Errors #3766

Merged
merged 6 commits into from
Dec 26, 2024
Merged
Prev Previous commit
Next Next commit
feat(#3743): remove redundant code
  • Loading branch information
volodya-lombrozo committed Dec 26, 2024
commit fe84ab5304eed7a2da0e35c12aa736db7ea12ce7
82 changes: 30 additions & 52 deletions eo-parser/src/main/java/org/eolang/parser/XeEoListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,12 @@
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.misc.Interval;
import org.antlr.v4.runtime.tree.ErrorNode;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.apache.commons.text.StringEscapeUtils;
Expand Down Expand Up @@ -81,11 +77,9 @@ public final class XeEoListener implements EoListener, Iterable<Directive> {
private final long start;

/**
* Errors map.
* Errors.
*/
private final Map<ParserRuleContext, String> errors;

private final List<ParsingException> newerrors;
private final List<ParsingException> errors;

/**
* Ctor.
Expand All @@ -95,8 +89,7 @@ public final class XeEoListener implements EoListener, Iterable<Directive> {
public XeEoListener(final String name) {
this.name = name;
this.dirs = new Directives();
this.errors = new HashMap<>(0);
this.newerrors = new ArrayList<>(0);
this.errors = new ArrayList<>(0);
this.objects = new Objects.ObjXembly();
this.start = System.nanoTime();
}
Expand All @@ -112,33 +105,9 @@ public void enterProgram(final EoParser.ProgramContext ctx) {
@Override
public void exitProgram(final EoParser.ProgramContext ctx) {
this.dirs.xpath("/program").strict(1);
// if (!this.errors.isEmpty()) {
// this.dirs.addIf("errors").strict(1);
// for (final Map.Entry<ParserRuleContext, String> error : this.errors.entrySet()) {
// this.dirs
// .add("error")
// .attr("check", "eo-parser")
// .attr("line", error.getKey().getStart().getLine())
// .attr("severity", "critical")
// .set(error.getValue());
// }
// this.dirs.up().up();
// }

if (!this.newerrors.isEmpty()) {
this.dirs.append(new DrErrors(this.newerrors));
// this.dirs.addIf("errors").strict(1);
// for (final Map.Entry<ParserRuleContext, String> error : this.errors.entrySet()) {
// this.dirs
// .add("error")
// .attr("check", "eo-parser")
// .attr("line", error.getKey().getStart().getLine())
// .attr("severity", "critical")
// .set(error.getValue());
// }
// this.dirs.up().up();
if (!this.errors.isEmpty()) {
this.dirs.append(new DrErrors(this.errors));
}

this.dirs
.attr("ms", (System.nanoTime() - this.start) / (1000L * 1000L))
.up();
Expand Down Expand Up @@ -1085,7 +1054,7 @@ public void enterAs(final EoParser.AsContext ctx) {
} else {
final int index = Integer.parseInt(ctx.INT().getText());
if (index < 0) {
this.newerrors.add(
this.errors.add(
new ParsingException(
ctx.getStart().getLine(),
new MsgLocated(
Expand All @@ -1094,7 +1063,7 @@ public void enterAs(final EoParser.AsContext ctx) {
"Object binding can't be negative"
).formatted(),
new MsgUnderlined(
this.line(ctx),
XeEoListener.line(ctx),
ctx.getStart().getCharPositionInLine(),
ctx.getText().length()
).formatted()
Expand All @@ -1106,18 +1075,6 @@ public void enterAs(final EoParser.AsContext ctx) {
this.objects.prop("as", has);
}

private String line(ParserRuleContext ctx) {
final Token start1 = ctx.start;
final int lineNumber = start1.getLine();
final CharStream stream = start1.getInputStream();
String[] lines = stream.toString().split("\n");
if (lineNumber > 0 && lineNumber <= lines.length) {
return lines[lineNumber - 1]; // Lines are 1-based
} else {
throw new IllegalArgumentException("Line number out of bounds");
}
}

@Override
public void exitAs(final EoParser.AsContext ctx) {
this.objects.leave();
Expand Down Expand Up @@ -1166,13 +1123,12 @@ public void enterData(final EoParser.DataContext ctx) {
} else {
base = "unknown";
data = ctx::getText;
this.newerrors.add(
this.errors.add(
new ParsingException(
ctx.getStart().getLine(),
String.format("Unknown data type: %s", ctx.getText())
)
);
// this.errors.put(ctx, String.format("Unknown data type: %s", ctx.getText()));
}
this.objects.prop("base", base).data(data.get());
}
Expand Down Expand Up @@ -1274,4 +1230,26 @@ private static String trimMargin(final String text, final int indent) {
}
return res.toString();
}

/**
* Get line from context.
* @param ctx Context
* @return Line
*/
private static String line(final ParserRuleContext ctx) {
final Token token = ctx.start;
final int number = token.getLine();
final String[] lines = token.getInputStream().toString().split("\n");
if (number > 0 && number <= lines.length) {
return lines[number - 1]; // Lines are 1-based
} else {
throw new IllegalArgumentException(
String.format(
"Line number '%s' out of bounds, total lines: %d",
number,
lines.length
)
);
}
}
}
Loading