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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(#3743): add more human-readable message to negative binding
  • Loading branch information
volodya-lombrozo committed Dec 26, 2024
commit 3ba98a06ec7ef6f8913556c42048c1d80cb3d458
26 changes: 24 additions & 2 deletions eo-parser/src/main/java/org/eolang/parser/XeEoListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
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 @@ -1086,16 +1088,36 @@ public void enterAs(final EoParser.AsContext ctx) {
this.newerrors.add(
new ParsingException(
ctx.getStart().getLine(),
"Object binding can't be negative"
new MsgLocated(
ctx.getStart().getLine(),
ctx.getStart().getCharPositionInLine(),
"Object binding can't be negative"
).formatted(),
new MsgUnderlined(
this.line(ctx),
ctx.getStart().getCharPositionInLine(),
ctx.getText().length()
).formatted()
)
);
// this.errors.put(ctx, "Object binding can't be negative");
}
has = String.format("α%d", index);
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
---
line: 4
message: |-
[4:4] error: 'Object binding can't be negative'
[4:6] error: 'Object binding can't be negative'
42:-1
^
^^^
input: |
# No comments
[] > foo
Expand Down