Struct source_span::Position
source · pub struct Position {
pub line: usize,
pub column: usize,
}
Expand description
Position in a source file.
This holds the line and column position of a character in a source file.
Some operations are available to move position in a file. In partular, the next
method
computes the next cursor position after reading a given char
.
§Display
The struct implements two different format traits:
fmt::Display
will format the position asline {line} column {column}
fmt::Debug
will format the position as{line}:{column}
.
Both of them will display lines and columns starting at 1
even though the internal
representation starts at 0
.
Fields§
§line: usize
Line number, starting at 0
.
column: usize
Column number, starting at 0
.
Implementations§
source§impl Position
impl Position
sourcepub fn new(line: usize, column: usize) -> Position
pub fn new(line: usize, column: usize) -> Position
Create a new position given a line and column.
Indexes starts at 0
.
sourcepub fn next_column(&self) -> Position
pub fn next_column(&self) -> Position
Move to the next column.
sourcepub fn reset_column(&self) -> Position
pub fn reset_column(&self) -> Position
Move to the begining of the line.
sourcepub fn next(&self, c: char) -> Position
pub fn next(&self, c: char) -> Position
Move to the position following the given char
.
§Control characters
This crate is intended to help with incremental lexing/parsing. Therefore, any control character moving the cursor backward will be ignored: it will be treated as a 0-width character with no semantics.
§New lines
The \n
character is interpreted with the Unix semantics, as the new line (NL) character.
It will reset the column position to 0
and move to the next line.
§Tabulations
The \t
will move the cursor to the next horizontal tab-top.
This function assumes there is a tab-stop every 8 columns.
Note that there is no standard on the size of a tabulation, however a length of 8 columns
seems typical.
As of today, there is no way to use another tab length.
I understand that this lacks of flexibility may become an issue in the near future, and I will try to add this possibility. In the meantime, you are very welcome to contribute if you need this feature right away.
§Full-width characters
As for now, double-width characters of full-width characters are not supported. They will move the cursor by only one column as any other regular-width character. You are welcome to contribute to handle them.
Trait Implementations§
source§impl Ord for Position
impl Ord for Position
source§impl PartialEq for Position
impl PartialEq for Position
source§impl PartialOrd for Position
impl PartialOrd for Position
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moreimpl Copy for Position
impl Eq for Position
impl StructuralPartialEq for Position
Auto Trait Implementations§
impl Freeze for Position
impl RefUnwindSafe for Position
impl Send for Position
impl Sync for Position
impl Unpin for Position
impl UnwindSafe for Position
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)