remove is_at_end

main
Alpha Chen 2 years ago
parent 7baa16ee8e
commit ce9eb0de17
Signed by: alpha
SSH Key Fingerprint: SHA256:3fOT8fiYQG/aK9ntivV3Bqtg8AYQ7q4nV6ZgihOA20g

@ -18,7 +18,7 @@ impl<'a> Scanner<'a> {
pub fn scan(&mut self) -> Result<Token> { pub fn scan(&mut self) -> Result<Token> {
self.skip_whitespace(); self.skip_whitespace();
if self.is_at_end() { if self.peek().is_none() {
return self.make_token(TokenKind::Eof); 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<Token> { fn make_token(&self, kind: TokenKind) -> Result<Token> {
Ok(Token { Ok(Token {
kind, kind,
@ -101,11 +97,7 @@ impl<'a> Scanner<'a> {
} }
fn is_match(&mut self, expected: char) -> bool { fn is_match(&mut self, expected: char) -> bool {
if self.is_at_end() { if self.peek() != Some(expected) {
return false;
}
if self.source[self.current] as char != expected {
return false; return false;
} }
@ -146,11 +138,7 @@ impl<'a> Scanner<'a> {
} }
fn peek_next(&mut self) -> Option<char> { fn peek_next(&mut self) -> Option<char> {
if self.is_at_end() { self.source.get(self.current + 1).map(|&x| x as char)
None
} else {
Some(self.source[self.current + 1] as char)
}
} }
fn string(&mut self) -> Result<Token> { fn string(&mut self) -> Result<Token> {
@ -161,7 +149,7 @@ impl<'a> Scanner<'a> {
self.advance(); self.advance();
} }
if self.is_at_end() { if self.peek().is_none() {
return self.error_token("Unterminated string."); return self.error_token("Unterminated string.");
} }

Loading…
Cancel
Save