refactoring Scanner

main
Alpha Chen 2 years ago
parent 1320a0988f
commit 5d49f5ceea
Signed by: alpha
SSH Key Fingerprint: SHA256:3fOT8fiYQG/aK9ntivV3Bqtg8AYQ7q4nV6ZgihOA20g

@ -4,8 +4,8 @@ pub struct Scanner<'a> {
line: usize, line: usize,
} }
fn is_alpha(c: char) -> bool { fn is_alpha(c: &char) -> bool {
c.is_ascii_alphabetic() || c == '_' c.is_ascii_alphabetic() || *c == '_'
} }
impl<'a> Scanner<'a> { impl<'a> Scanner<'a> {
@ -17,46 +17,34 @@ impl<'a> Scanner<'a> {
} }
} }
fn advance(&mut self) -> char { fn advance(&mut self) -> Option<char> {
self.current += 1; self.current += 1;
self.source[self.current - 1] as char self.source.get(self.current - 1).map(|&x| x as char)
}
fn is_match(&mut self, expected: char) -> bool {
if self.peek() != Some(expected) {
return false;
} }
fn advance_if<F: Fn(&char) -> bool>(&mut self, f: F) -> bool {
if matches!(self.peek(), Some(c) if f(&c)) {
self.current += 1; self.current += 1;
true true
} else {
false
}
} }
fn skip_whitespace(&mut self) { fn skip_whitespace(&mut self) {
loop { loop {
match self.peek() { while self.advance_if(|&c| c == ' ' || c == '\r' || c == '\t') {}
Some(' ') | Some('\r') | Some('\t') => {
self.advance(); if self.peek() == Some('/') && self.peek_next() == Some('/') {
break; while self.advance_if(|&c| c != '\n') {}
} }
Some('\n') => {
if self.advance_if(|&c| c == '\n') {
self.line += 1; self.line += 1;
self.advance();
break;
}
Some('/') => {
if matches!(self.peek_next(), Some(c) if c == '/') {
while matches!(self.peek(), Some(c) if c != '\n') {
self.advance();
}
} else { } else {
return; return;
} }
} }
_ => {
return;
}
}
}
} }
fn peek(&self) -> Option<char> { fn peek(&self) -> Option<char> {
@ -68,42 +56,34 @@ impl<'a> Scanner<'a> {
} }
fn string(&mut self) -> Token<'a> { fn string(&mut self) -> Token<'a> {
while matches!(self.peek(), Some(c) if c != '"') { loop {
if self.peek() == Some('\n') { match self.advance() {
Some('"') => break,
Some('\n') => {
self.line += 1; self.line += 1;
} }
self.advance(); Some(_) => {}
None => return Token::error(self, "Unterminated string."),
} }
if self.peek().is_none() {
// Maybe this should return an Err?
return Token::error(&self, "Unterminated string.");
} }
self.advance();
Token::new(self, TokenKind::String) Token::new(self, TokenKind::String)
} }
fn number(&mut self) -> Token<'a> { fn number(&mut self) -> Token<'a> {
while matches!(self.peek(), Some(c) if c.is_ascii_digit()) { while self.advance_if(char::is_ascii_digit) {}
self.advance();
}
if self.peek() == Some('.') && matches!(self.peek_next(), Some(c) if c.is_ascii_digit()) { if self.peek() == Some('.') && matches!(self.peek_next(), Some(c) if c.is_ascii_digit()) {
self.advance(); self.advance();
while matches!(self.peek(), Some(c) if c.is_ascii_digit()) { while self.advance_if(char::is_ascii_digit) {}
self.advance();
}
} }
Token::new(self, TokenKind::Number) Token::new(self, TokenKind::Number)
} }
fn identifier(&mut self) -> Token<'a> { fn identifier(&mut self) -> Token<'a> {
while matches!(self.peek(), Some(c) if is_alpha(c) || c.is_ascii_digit()) { while self.advance_if(|c| is_alpha(c) || c.is_ascii_digit()) {}
self.advance();
}
Token::new(self, self.identifier_type()) Token::new(self, self.identifier_type())
} }
@ -143,12 +123,9 @@ impl<'a> Iterator for Scanner<'a> {
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
self.skip_whitespace(); self.skip_whitespace();
if self.peek().is_none() { self.advance()
return Some(Token::new(self, TokenKind::Eof)); .map(|c| match c {
} c if is_alpha(&c) => self.identifier(),
Some(match self.advance() {
c if is_alpha(c) => self.identifier(),
c if c.is_ascii_digit() => self.number(), c if c.is_ascii_digit() => self.number(),
'(' => Token::new(self, TokenKind::LeftParen), '(' => Token::new(self, TokenKind::LeftParen),
')' => Token::new(self, TokenKind::RightParen), ')' => Token::new(self, TokenKind::RightParen),
@ -162,28 +139,28 @@ impl<'a> Iterator for Scanner<'a> {
'/' => Token::new(self, TokenKind::Slash), '/' => Token::new(self, TokenKind::Slash),
'*' => Token::new(self, TokenKind::Star), '*' => Token::new(self, TokenKind::Star),
'!' => { '!' => {
if self.is_match('=') { if self.advance_if(|&c| c == '=') {
Token::new(self, TokenKind::BangEqual) Token::new(self, TokenKind::BangEqual)
} else { } else {
Token::new(self, TokenKind::Bang) Token::new(self, TokenKind::Bang)
} }
} }
'=' => { '=' => {
if self.is_match('=') { if self.advance_if(|&c| c == '=') {
Token::new(self, TokenKind::EqualEqual) Token::new(self, TokenKind::EqualEqual)
} else { } else {
Token::new(self, TokenKind::Equal) Token::new(self, TokenKind::Equal)
} }
} }
'<' => { '<' => {
if self.is_match('=') { if self.advance_if(|&c| c == '=') {
Token::new(self, TokenKind::LessEqual) Token::new(self, TokenKind::LessEqual)
} else { } else {
Token::new(self, TokenKind::Less) Token::new(self, TokenKind::Less)
} }
} }
'>' => { '>' => {
if self.is_match('=') { if self.advance_if(|&c| c == '=') {
Token::new(self, TokenKind::GreaterEqual) Token::new(self, TokenKind::GreaterEqual)
} else { } else {
Token::new(self, TokenKind::Greater) Token::new(self, TokenKind::Greater)
@ -192,6 +169,13 @@ impl<'a> Iterator for Scanner<'a> {
'"' => self.string(), '"' => self.string(),
_ => Token::error(self, "Unexpected character."), _ => Token::error(self, "Unexpected character."),
}) })
.or({
Some(Token {
kind: TokenKind::Eof,
value: "",
line: self.line,
})
})
} }
} }

Loading…
Cancel
Save