Change syntax Pos to use 1-based indices for lines and columns by default

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2020-05-19 15:42:13 -04:00
parent 98f455e6fa
commit fb3f217d7d

View File

@@ -8,11 +8,12 @@ pub struct Pos {
pub col: usize,
pub byte: usize,
pub len: usize,
pub is_newline: bool,
}
impl Display for Pos {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
write!(fmt, "line {} at {}", self.line + 1, self.col + 1)
write!(fmt, "line {} at {}", self.line, self.col)
}
}
@@ -20,10 +21,11 @@ impl Default for Pos {
fn default() -> Self {
Pos {
source: 0,
line: 0,
col: 0,
line: 1,
col: 1,
byte: 0,
len: 1,
is_newline: false,
}
}
}
@@ -43,13 +45,14 @@ impl Pos {
col,
byte,
len: c.len_utf8(),
is_newline: c == '\n',
}
}
pub fn next_char(&self, c: char) -> Self {
Pos {
source: self.source + 1,
line: if c == '\n' {
line: if self.is_newline {
self.line + 1
} else {
self.line
@@ -57,6 +60,7 @@ impl Pos {
col: self.col + 1,
byte: self.byte + self.len,
len: c.len_utf8(),
is_newline: c == '\n',
}
}