001package org.clafer.frontend; 002 003/** 004 * Where a clafer, constraint, assertion or objective is in the source model: 005 * from line:column up to, not including, endLine:endColumn. Lines and columns 006 * start at 1, as in the Clafer compiler's error messages. 007 * 008 * @param line the first line 009 * @param column the first column 010 * @param endLine the last line 011 * @param endColumn the column after the last character 012 */ 013public record SourceSpan(int line, int column, int endLine, int endColumn) { 014 015 public SourceSpan { 016 if (line < 1 || column < 1 || endLine < line || (endLine == line && endColumn < column)) { 017 throw new IllegalArgumentException("Not a span: " + line + ":" + column + "-" + endLine + ":" + endColumn); 018 } 019 } 020 021 /** 022 * @param line the line 023 * @param column the column 024 * @return the empty span at a position, for when only the start is known 025 */ 026 public static SourceSpan at(int line, int column) { 027 return new SourceSpan(line, column, line, column); 028 } 029 030 /** 031 * Prefixes a message with the start of this span, as the compiler's 032 * messages are. 033 * 034 * @param message the message 035 * @return "line:column: message" 036 */ 037 public String format(String message) { 038 return line + ":" + column + ": " + message; 039 } 040 041 @Override 042 public String toString() { 043 return line + ":" + column + "-" + endLine + ":" + endColumn; 044 } 045}