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 col: usize,
pub byte: usize, pub byte: usize,
pub len: usize, pub len: usize,
pub is_newline: bool,
} }
impl Display for Pos { impl Display for Pos {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { 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 { fn default() -> Self {
Pos { Pos {
source: 0, source: 0,
line: 0, line: 1,
col: 0, col: 1,
byte: 0, byte: 0,
len: 1, len: 1,
is_newline: false,
} }
} }
} }
@@ -43,13 +45,14 @@ impl Pos {
col, col,
byte, byte,
len: c.len_utf8(), len: c.len_utf8(),
is_newline: c == '\n',
} }
} }
pub fn next_char(&self, c: char) -> Self { pub fn next_char(&self, c: char) -> Self {
Pos { Pos {
source: self.source + 1, source: self.source + 1,
line: if c == '\n' { line: if self.is_newline {
self.line + 1 self.line + 1
} else { } else {
self.line self.line
@@ -57,6 +60,7 @@ impl Pos {
col: self.col + 1, col: self.col + 1,
byte: self.byte + self.len, byte: self.byte + self.len,
len: c.len_utf8(), len: c.len_utf8(),
is_newline: c == '\n',
} }
} }