package scotch.compiler.text;
import static lombok.AccessLevel.PRIVATE;
import static scotch.compiler.text.NamedSourcePoint.source;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
@AllArgsConstructor(access = PRIVATE)
@EqualsAndHashCode(callSuper = false)
@ToString
@Getter
public class SourcePoint {
public static SourcePoint point(int offset, int line, int column) {
return new SourcePoint(offset, line, column);
}
private final int offset;
private final int line;
private final int column;
public SourcePoint max(SourcePoint other) {
return new SourcePoint(
Math.max(offset, other.offset),
Math.max(line, other.line),
Math.max(column, other.column)
);
}
public SourcePoint min(SourcePoint other) {
return new SourcePoint(
Math.min(offset, other.offset),
Math.min(line, other.line),
Math.min(column, other.column)
);
}
public SourcePoint nextChar() {
return new SourcePoint(offset + 1, line, column + 1);
}
public SourcePoint nextLine() {
return new SourcePoint(offset + 1, line + 1, 1);
}
public SourcePoint nextTab() {
return new SourcePoint(offset + 1, line, column + 8);
}
public String prettyPrint() {
return "(" + line + ", " + column + ")";
}
public NamedSourcePoint withSource(String source) {
return source(source, offset, line, column);
}
}