Skip to content

Commit

Permalink
feat(#3743): use DrErrors instead of custom error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Dec 26, 2024
1 parent d5ecf75 commit c83ba89
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ public final class ParsingException extends RuntimeException {
*/
private final int place;

/**
* Ctor.
* @param line The place
* @param msgs Messages
*/
ParsingException(final int line, final String... msgs) {
this(new IllegalStateException("Parsing error"), line, List.of(msgs));
}

/**
* Ctor.
* @param cause Cause of failure
Expand Down
105 changes: 80 additions & 25 deletions eo-parser/src/main/java/org/eolang/parser/XeEoListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

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;
Expand Down Expand Up @@ -82,6 +83,8 @@ public final class XeEoListener implements EoListener, Iterable<Directive> {
*/
private final Map<ParserRuleContext, String> errors;

private final List<ParsingException> newerrors;

/**
* Ctor.
*
Expand All @@ -91,6 +94,7 @@ public XeEoListener(final String name) {
this.name = name;
this.dirs = new Directives();
this.errors = new HashMap<>(0);
this.newerrors = new ArrayList<>(0);
this.objects = new Objects.ObjXembly();
this.start = System.nanoTime();
}
Expand All @@ -106,18 +110,33 @@ 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.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();
}

this.dirs
.attr("ms", (System.nanoTime() - this.start) / (1000L * 1000L))
.up();
Expand Down Expand Up @@ -483,12 +502,16 @@ public void exitHapplicationTailReversed(final EoParser.HapplicationTailReversed
}

@Override
public void enterHapplicationTailReversedFirst(final EoParser.HapplicationTailReversedFirstContext ctx) {
public void enterHapplicationTailReversedFirst(
final EoParser.HapplicationTailReversedFirstContext ctx
) {
this.objects.enter();
}

@Override
public void exitHapplicationTailReversedFirst(final EoParser.HapplicationTailReversedFirstContext ctx) {
public void exitHapplicationTailReversedFirst(
final EoParser.HapplicationTailReversedFirstContext ctx
) {
this.objects.leave();
}

Expand Down Expand Up @@ -573,12 +596,16 @@ public void exitVapplicationArgBound(final EoParser.VapplicationArgBoundContext
}

@Override
public void enterVapplicationArgBoundCurrent(final EoParser.VapplicationArgBoundCurrentContext ctx) {
public void enterVapplicationArgBoundCurrent(
final EoParser.VapplicationArgBoundCurrentContext ctx
) {
// Nothing here
}

@Override
public void exitVapplicationArgBoundCurrent(final EoParser.VapplicationArgBoundCurrentContext ctx) {
public void exitVapplicationArgBoundCurrent(
final EoParser.VapplicationArgBoundCurrentContext ctx
) {
// Nothing here
}

Expand All @@ -603,22 +630,30 @@ public void exitVapplicationArgUnbound(final EoParser.VapplicationArgUnboundCont
}

@Override
public void enterVapplicationArgUnboundCurrent(final EoParser.VapplicationArgUnboundCurrentContext ctx) {
public void enterVapplicationArgUnboundCurrent(
final EoParser.VapplicationArgUnboundCurrentContext ctx
) {
// Nothing here
}

@Override
public void exitVapplicationArgUnboundCurrent(final EoParser.VapplicationArgUnboundCurrentContext ctx) {
public void exitVapplicationArgUnboundCurrent(
final EoParser.VapplicationArgUnboundCurrentContext ctx
) {
// Nothing here
}

@Override
public void enterVapplicationArgUnboundNext(final EoParser.VapplicationArgUnboundNextContext ctx) {
public void enterVapplicationArgUnboundNext(
final EoParser.VapplicationArgUnboundNextContext ctx
) {
// Nothing here
}

@Override
public void exitVapplicationArgUnboundNext(final EoParser.VapplicationArgUnboundNextContext ctx) {
public void exitVapplicationArgUnboundNext(
final EoParser.VapplicationArgUnboundNextContext ctx
) {
// Nothing here
}

Expand Down Expand Up @@ -707,12 +742,16 @@ public void exitAttributesAs(final EoParser.AttributesAsContext ctx) {
}

@Override
public void enterVapplicationArgHanonymBoundBody(final EoParser.VapplicationArgHanonymBoundBodyContext ctx) {
public void enterVapplicationArgHanonymBoundBody(
final EoParser.VapplicationArgHanonymBoundBodyContext ctx
) {
// Nothing here
}

@Override
public void exitVapplicationArgHanonymBoundBody(final EoParser.VapplicationArgHanonymBoundBodyContext ctx) {
public void exitVapplicationArgHanonymBoundBody(
final EoParser.VapplicationArgHanonymBoundBodyContext ctx
) {
// Nothing here
}

Expand Down Expand Up @@ -865,12 +904,16 @@ public void exitMethodTailOptional(final EoParser.MethodTailOptionalContext ctx)
}

@Override
public void enterVmethodHeadApplicationTail(final EoParser.VmethodHeadApplicationTailContext ctx) {
public void enterVmethodHeadApplicationTail(
final EoParser.VmethodHeadApplicationTailContext ctx
) {
// Nothing here
}

@Override
public void exitVmethodHeadApplicationTail(final EoParser.VmethodHeadApplicationTailContext ctx) {
public void exitVmethodHeadApplicationTail(
final EoParser.VmethodHeadApplicationTailContext ctx
) {
// Nothing here
}

Expand Down Expand Up @@ -1040,7 +1083,13 @@ public void enterAs(final EoParser.AsContext ctx) {
} else {
final int index = Integer.parseInt(ctx.INT().getText());
if (index < 0) {
this.errors.put(ctx, "Object binding can't be negative");
this.newerrors.add(
new ParsingException(
ctx.getStart().getLine(),
"Object binding can't be negative"
)
);
// this.errors.put(ctx, "Object binding can't be negative");
}
has = String.format("α%d", index);
}
Expand Down Expand Up @@ -1095,7 +1144,13 @@ public void enterData(final EoParser.DataContext ctx) {
} else {
base = "unknown";
data = ctx::getText;
this.errors.put(ctx, String.format("Unknown data type: %s", ctx.getText()));
this.newerrors.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

0 comments on commit c83ba89

Please sign in to comment.