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,44 +17,32 @@ 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 { fn advance_if<F: Fn(&char) -> bool>(&mut self, f: F) -> bool {
if self.peek() != Some(expected) { if matches!(self.peek(), Some(c) if f(&c)) {
return false; self.current += 1;
true
} else {
false
} }
self.current += 1;
true
} }
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') => {
self.line += 1; if self.advance_if(|&c| c == '\n') {
self.advance(); self.line += 1;
break; } else {
} return;
Some('/') => {
if matches!(self.peek_next(), Some(c) if c == '/') {
while matches!(self.peek(), Some(c) if c != '\n') {
self.advance();
}
} else {
return;
}
}
_ => {
return;
}
} }
} }
} }
@ -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() {
self.line += 1; Some('"') => break,
Some('\n') => {
self.line += 1;
}
Some(_) => {}
None => return Token::error(self, "Unterminated string."),
} }
self.advance();
} }
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,55 +123,59 @@ 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(),
c if c.is_ascii_digit() => self.number(),
Some(match self.advance() { '(' => Token::new(self, TokenKind::LeftParen),
c if is_alpha(c) => self.identifier(), ')' => Token::new(self, TokenKind::RightParen),
c if c.is_ascii_digit() => self.number(), '{' => Token::new(self, TokenKind::LeftBrace),
'(' => Token::new(self, TokenKind::LeftParen), '}' => Token::new(self, TokenKind::RightBrace),
')' => Token::new(self, TokenKind::RightParen), ';' => Token::new(self, TokenKind::Semicolon),
'{' => Token::new(self, TokenKind::LeftBrace), ',' => Token::new(self, TokenKind::Comma),
'}' => Token::new(self, TokenKind::RightBrace), '.' => Token::new(self, TokenKind::Dot),
';' => Token::new(self, TokenKind::Semicolon), '-' => Token::new(self, TokenKind::Minus),
',' => Token::new(self, TokenKind::Comma), '+' => Token::new(self, TokenKind::Plus),
'.' => Token::new(self, TokenKind::Dot), '/' => Token::new(self, TokenKind::Slash),
'-' => Token::new(self, TokenKind::Minus), '*' => Token::new(self, TokenKind::Star),
'+' => Token::new(self, TokenKind::Plus), '!' => {
'/' => Token::new(self, TokenKind::Slash), if self.advance_if(|&c| c == '=') {
'*' => Token::new(self, TokenKind::Star), Token::new(self, TokenKind::BangEqual)
'!' => { } else {
if self.is_match('=') { Token::new(self, TokenKind::Bang)
Token::new(self, TokenKind::BangEqual) }
} else {
Token::new(self, TokenKind::Bang)
} }
} '=' => {
'=' => { if self.advance_if(|&c| c == '=') {
if self.is_match('=') { Token::new(self, TokenKind::EqualEqual)
Token::new(self, TokenKind::EqualEqual) } else {
} else { Token::new(self, TokenKind::Equal)
Token::new(self, TokenKind::Equal) }
} }
} '<' => {
'<' => { if self.advance_if(|&c| c == '=') {
if self.is_match('=') { Token::new(self, TokenKind::LessEqual)
Token::new(self, TokenKind::LessEqual) } else {
} else { Token::new(self, TokenKind::Less)
Token::new(self, TokenKind::Less) }
} }
} '>' => {
'>' => { if self.advance_if(|&c| c == '=') {
if self.is_match('=') { Token::new(self, TokenKind::GreaterEqual)
Token::new(self, TokenKind::GreaterEqual) } else {
} else { Token::new(self, TokenKind::Greater)
Token::new(self, TokenKind::Greater) }
} }
} '"' => 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