diff --git a/src/token.rs b/src/token.rs index 0717c632..fcbcadf6 100644 --- a/src/token.rs +++ b/src/token.rs @@ -9,6 +9,7 @@ use crate::stdlib::{ char, fmt, format, iter::Peekable, num::NonZeroUsize, + ops::{Add, AddAssign}, str::{Chars, FromStr}, string::{String, ToString}, }; @@ -120,7 +121,7 @@ impl Position { /// Is this [`Position`] at the beginning of a line? #[inline(always)] pub fn is_beginning_of_line(self) -> bool { - self.line == 0 && !self.is_none() + self.pos == 0 && !self.is_none() } /// Is there no [`Position`]? #[inline(always)] @@ -154,6 +155,31 @@ impl fmt::Debug for Position { } } +impl Add for Position { + type Output = Self; + + fn add(self, rhs: Self) -> Self::Output { + if rhs.is_none() { + self + } else { + Self { + line: self.line + rhs.line - 1, + pos: if rhs.is_beginning_of_line() { + self.pos + } else { + self.pos + rhs.pos - 1 + }, + } + } + } +} + +impl AddAssign for Position { + fn add_assign(&mut self, rhs: Self) { + *self = *self + rhs; + } +} + /// _(INTERNALS)_ A Rhai language token. /// Exported under the `internals` feature only. ///