java-commons/muscript/src/main/java/io/gitlab/jfronny/muscript/error/LocationalException.java

52 lines
1.4 KiB
Java

package io.gitlab.jfronny.muscript.error;
import java.util.LinkedList;
import java.util.List;
/**
* An exception type with a location
* For use in MuScript, can be converted to a pretty LocationalError with asPrintable
*/
public class LocationalException extends RuntimeException {
private final int start, end;
private final List<StackFrame> callStack = new LinkedList<>();
public LocationalException(int start, int end) {
super();
this.start = start;
this.end = end;
}
public LocationalException(int start, int end, String message) {
super(message);
this.start = start;
this.end = end;
}
public LocationalException(int start, int end, String message, Throwable cause) {
super(message, cause);
this.start = start;
this.end = end;
}
public LocationalException(int start, int end, Throwable cause) {
super(cause);
this.start = start;
this.end = end;
}
public LocationalError asPrintable(String source) {
return LocationalError.builder()
.setSource(source)
.setLocation(start, end)
.setMessage(getLocalizedMessage())
.setCallStack(callStack)
.build();
}
public LocationalException appendStack(StackFrame frame) {
callStack.add(frame);
return this;
}
}