diff --git a/rust/src/scanner.rs b/rust/src/scanner.rs index 879d7cb..3a2d6aa 100644 --- a/rust/src/scanner.rs +++ b/rust/src/scanner.rs @@ -18,7 +18,7 @@ impl<'a> Scanner<'a> { pub fn scan(&mut self) -> Result { self.skip_whitespace(); - if self.is_at_end() { + if self.peek().is_none() { return self.make_token(TokenKind::Eof); } @@ -75,10 +75,6 @@ impl<'a> Scanner<'a> { }; } - fn is_at_end(&self) -> bool { - self.current == self.source.len() - } - fn make_token(&self, kind: TokenKind) -> Result { Ok(Token { kind, @@ -101,11 +97,7 @@ impl<'a> Scanner<'a> { } fn is_match(&mut self, expected: char) -> bool { - if self.is_at_end() { - return false; - } - - if self.source[self.current] as char != expected { + if self.peek() != Some(expected) { return false; } @@ -146,11 +138,7 @@ impl<'a> Scanner<'a> { } fn peek_next(&mut self) -> Option { - if self.is_at_end() { - None - } else { - Some(self.source[self.current + 1] as char) - } + self.source.get(self.current + 1).map(|&x| x as char) } fn string(&mut self) -> Result { @@ -161,7 +149,7 @@ impl<'a> Scanner<'a> { self.advance(); } - if self.is_at_end() { + if self.peek().is_none() { return self.error_token("Unterminated string."); }