45 lines
1.8 KiB
Plaintext
45 lines
1.8 KiB
Plaintext
#! @file lines.sakura
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
|
#! @copyright Malunal Studios, LLC.
|
|
#! @license MIT
|
|
#!
|
|
#! Contains `lines`, where each line of a source begins, and `place`, the line
|
|
#! and column an offset within the source is at.
|
|
|
|
module sakura::analyzer;
|
|
|
|
#! Defines a place within a source file.
|
|
#!
|
|
#! This is useful for diagnostics and debugging, especially within something
|
|
#! like a language server protocol.
|
|
pub struct place {
|
|
pub val line: u32; #! The line, counted from 1.
|
|
pub val column: u32; #! The byte within the line, counted from 1.
|
|
}
|
|
|
|
#! Stores the starting offsets of the lines of a given source file.
|
|
#!
|
|
#! The length of the `starts` vector is the number of lines in the file and then
|
|
#! the value at a given line (index) is the offset within the file that the line
|
|
#! starts.
|
|
pub type lines = vector<u32>;
|
|
|
|
#! Provides the place of a given offset within some source code.
|
|
#!
|
|
#! This is a binary search that will seek out a specific line and then mark the
|
|
#! column within the line, given the offset. Lines are stored in order by their
|
|
#! offset within the file, so the length of the `starts` vector is the number of
|
|
#! lines in the file and the value at that index is the offset within the file.
|
|
#! The line is the last starting at or before the offset, the one before the
|
|
#! first starting after it; the first line starts at 0, so there is always one.
|
|
#! @param this The lines structure to get the place of an offset.
|
|
#! @param offset The offset from the file to get the place of.
|
|
#! @returns The line and column of the given offset for a given file.
|
|
pub def lines::place_of(&this, offset: u32): place {
|
|
val low := this.span.upper_bound(&offset) - 1;
|
|
return place {
|
|
line = (low + 1) as u32,
|
|
column = offset - this[low] + 1
|
|
};
|
|
}
|