1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
use std::fmt;
use std::cmp::{PartialOrd, Ord, Ordering};
/// 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`](Position::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 as `line {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`.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct Position {
/// Line number, starting at `0`.
pub line: usize,
/// Column number, starting at `0`.
pub column: usize
}
impl PartialOrd for Position {
fn partial_cmp(&self, other: &Position) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Position {
fn cmp(&self, other: &Position) -> Ordering {
match self.line.cmp(&other.line) {
Ordering::Equal => self.column.cmp(&other.column),
ord => ord
}
}
}
impl Position {
/// Create a new position given a line and column.
///
/// Indexes starts at `0`.
pub fn new(line: usize, column: usize) -> Position {
Position {
line: line,
column: column
}
}
/// Move to the next column.
pub fn next_column(&self) -> Position {
Position {
line: self.line,
column: self.column+1
}
}
/// Move to the begining of the line.
pub fn reset_column(&self) -> Position {
Position {
line: self.line,
column: 0
}
}
/// Move to the next line, and reset the column position.
pub fn next_line(&self) -> Position {
Position {
line: self.line+1,
column: 0
}
}
/// 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.
pub fn next(&self, c: char) -> Position {
match c {
'\n' => self.next_line(),
'\r' => self.reset_column(),
'\t' => Position {
line: self.line,
column: (self.column/8)*8 + 8
},
c if c.is_control() => *self,
_ => self.next_column()
}
}
}
impl fmt::Display for Position {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "line {} column {}", self.line+1, self.column+1)
}
}
impl fmt::Debug for Position {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}:{}", self.line+1, self.column+1)
}
}