|
| 1 | +package dev.manere.inscript; |
| 2 | + |
| 3 | +import dev.manere.inscript.format.Line; |
| 4 | +import org.jetbrains.annotations.NotNull; |
| 5 | + |
| 6 | +import java.nio.file.Path; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +public class ErrorContext { |
| 10 | + private final Inscript inscript; |
| 11 | + private final int position; |
| 12 | + private final String line; |
| 13 | + private final String error; |
| 14 | + |
| 15 | + public ErrorContext(final @NotNull Inscript inscript, final int position, final @NotNull String line, final @NotNull String error) { |
| 16 | + this.inscript = inscript; |
| 17 | + this.position = position; |
| 18 | + this.line = line; |
| 19 | + this.error = error; |
| 20 | + } |
| 21 | + |
| 22 | + @NotNull |
| 23 | + public static ErrorContext create(final @NotNull Line line, final @NotNull Inscript inscript, final @NotNull String error) { |
| 24 | + return new ErrorContext(inscript, line.getPosition() + 1, line.getText(), error); |
| 25 | + } |
| 26 | + |
| 27 | + public int getPosition() { |
| 28 | + return position; |
| 29 | + } |
| 30 | + |
| 31 | + @NotNull |
| 32 | + public String getLine() { |
| 33 | + return line; |
| 34 | + } |
| 35 | + |
| 36 | + @NotNull |
| 37 | + public String getError() { |
| 38 | + return error; |
| 39 | + } |
| 40 | + |
| 41 | + @NotNull |
| 42 | + public String buildDefault() { |
| 43 | + if (inscript.getPath().isPresent()) { |
| 44 | + final Path path = inscript.getPath().get(); |
| 45 | + final String name = path.getFileName().toString(); |
| 46 | + |
| 47 | + return String.join("\n", List.of( |
| 48 | + "Failed to parse " + name + ":" + position, |
| 49 | + " " + error + ": " + line |
| 50 | + )); |
| 51 | + } else { |
| 52 | + return String.join("\n", List.of( |
| 53 | + "Failed to parse line " + position, |
| 54 | + " " + error + ": " + line |
| 55 | + )); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public void handle() { |
| 60 | + InscriptConstants.ERROR_HANDLER.getValue().accept(this); |
| 61 | + } |
| 62 | +} |
0 commit comments