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