simplify scanning

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

@ -22,15 +22,9 @@ impl<'a> Scanner<'a> {
return self.make_token(TokenKind::Eof); return self.make_token(TokenKind::Eof);
} }
let c = self.advance(); match self.advance() {
if is_alpha(c) { c if is_alpha(c) => self.identifier(),
return self.identifier(); c if c.is_ascii_digit() => self.number(),
}
if c.is_ascii_digit() {
return self.number();
}
return match c {
'(' => self.make_token(TokenKind::LeftParen), '(' => self.make_token(TokenKind::LeftParen),
')' => self.make_token(TokenKind::RightParen), ')' => self.make_token(TokenKind::RightParen),
'{' => self.make_token(TokenKind::LeftBrace), '{' => self.make_token(TokenKind::LeftBrace),
@ -72,7 +66,7 @@ impl<'a> Scanner<'a> {
} }
'"' => self.string(), '"' => self.string(),
_ => self.error_token("Unexpected character."), _ => self.error_token("Unexpected character."),
}; }
} }
fn make_token(&self, kind: TokenKind) -> Result<Token> { fn make_token(&self, kind: TokenKind) -> Result<Token> {

Loading…
Cancel
Save