Alpha Chen 2 years ago
parent e8f5059c9b
commit 3dc84e9f28
Signed by: alpha
SSH Key Fingerprint: SHA256:3fOT8fiYQG/aK9ntivV3Bqtg8AYQ7q4nV6ZgihOA20g

@ -67,7 +67,7 @@ impl<'a> Scanner<'a> {
self.source.get(self.current + 1).map(|&x| x as char) self.source.get(self.current + 1).map(|&x| x as char)
} }
fn string(&'a mut self) -> Token<'a> { fn string(&mut self) -> Token<'a> {
while matches!(self.peek(), Some(c) if c != '"') { while matches!(self.peek(), Some(c) if c != '"') {
if self.peek() == Some('\n') { if self.peek() == Some('\n') {
self.line += 1; self.line += 1;
@ -85,7 +85,7 @@ impl<'a> Scanner<'a> {
self.make_token(TokenKind::String) self.make_token(TokenKind::String)
} }
fn number(&'a mut self) -> Token<'a> { fn number(&mut self) -> Token<'a> {
while matches!(self.peek(), Some(c) if c.is_ascii_digit()) { while matches!(self.peek(), Some(c) if c.is_ascii_digit()) {
self.advance(); self.advance();
} }
@ -102,7 +102,7 @@ impl<'a> Scanner<'a> {
self.make_token(TokenKind::Number) self.make_token(TokenKind::Number)
} }
fn identifier(&mut self) -> Token { fn identifier(&mut self) -> Token<'a> {
while matches!(self.peek(), Some(c) if is_alpha(c) || c.is_ascii_digit()) { while matches!(self.peek(), Some(c) if is_alpha(c) || c.is_ascii_digit()) {
self.advance(); self.advance();
} }
@ -139,7 +139,7 @@ impl<'a> Scanner<'a> {
} }
} }
fn make_token(&'a self, kind: TokenKind) -> Token<'a> { fn make_token(&self, kind: TokenKind) -> Token<'a> {
Token { Token {
kind: TokenKind::And, kind: TokenKind::And,
value: std::str::from_utf8(&self.source[..self.current]).unwrap(), value: std::str::from_utf8(&self.source[..self.current]).unwrap(),
@ -156,18 +156,10 @@ impl<'a> Iterator for Scanner<'a> {
self.peek()?; self.peek()?;
Some(match self.advance() { Some(match self.advance() {
// _ => self.make_token(TokenKind::And), c if is_alpha(c) => self.identifier(),
_ => Token { c if c.is_ascii_digit() => self.number(),
kind: TokenKind::And, // '(' => Token::new(self, TokenKind::LeftParen),
value: std::str::from_utf8(&self.source[..self.current]).unwrap(), '(' => self.make_token(TokenKind::LeftParen),
line: self.line,
},
})
// Some(match self.advance() {
// c if is_alpha(c) => self.identifier(),
// c if c.is_ascii_digit() => self.number(),
// '(' => 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),
// '}' => self.make_token(TokenKind::RightBrace), // '}' => self.make_token(TokenKind::RightBrace),
@ -207,8 +199,8 @@ impl<'a> Iterator for Scanner<'a> {
// } // }
// } // }
// '"' => self.string(), // '"' => self.string(),
// _ => Token::error(&self, "Unexpected character."), _ => Token::error(self, "Unexpected character."),
// }) })
} }
} }

Loading…
Cancel
Save