compiler: Allow mapping from line/column to offset

Add a method to the SourceFile so that we can map back from line/column
values to offsets.
This commit is contained in:
Tobias Hunger 2023-04-27 17:37:01 +02:00 committed by Tobias Hunger
parent 2b45f9448e
commit bb3109443a

View file

@ -106,6 +106,18 @@ impl SourceFileInner {
)
}
/// Returns the offset that corresponds to the line/column
pub fn offset(&self, line: usize, column: usize) -> usize {
let col_offset = column.saturating_sub(1);
if line <= 1 {
// line == 0 is actually invalid!
return col_offset;
}
let offsets = self.line_offsets();
let index = std::cmp::min(line.saturating_sub(1), offsets.len());
offsets.get(index.saturating_sub(1)).unwrap_or(&0).saturating_add(col_offset)
}
fn line_offsets(&self) -> &[usize] {
self.line_offsets.get_or_init(|| {
self.source