remove check_keyword

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

@ -183,49 +183,33 @@ impl<'a> Scanner<'a> {
fn identifier_type(&self) -> TokenKind {
match self.source[0] as char {
'a' => self.check_keyword(1, "nd", TokenKind::And),
'c' => self.check_keyword(1, "lass", TokenKind::Class),
'e' => self.check_keyword(1, "lse", TokenKind::Else),
'i' => self.check_keyword(1, "f", TokenKind::If),
'n' => self.check_keyword(1, "il", TokenKind::Nil),
'o' => self.check_keyword(1, "r", TokenKind::Or),
'p' => self.check_keyword(1, "rint", TokenKind::Print),
'r' => self.check_keyword(1, "eturn", TokenKind::Return),
's' => self.check_keyword(1, "uper", TokenKind::Super),
'v' => self.check_keyword(1, "ar", TokenKind::Var),
'w' => self.check_keyword(1, "hile", TokenKind::While),
'f' => {
if self.current > 1 {
'a' if &self.source[1..self.current] == "nd".as_bytes() => TokenKind::And,
'c' if &self.source[1..self.current] == "lass".as_bytes() => TokenKind::Class,
'e' if &self.source[1..self.current] == "lse".as_bytes() => TokenKind::Else,
'i' if &self.source[1..self.current] == "f".as_bytes() => TokenKind::If,
'n' if &self.source[1..self.current] == "il".as_bytes() => TokenKind::Nil,
'o' if &self.source[1..self.current] == "r".as_bytes() => TokenKind::Or,
'p' if &self.source[1..self.current] == "rint".as_bytes() => TokenKind::Print,
'r' if &self.source[1..self.current] == "eturn".as_bytes() => TokenKind::Return,
's' if &self.source[1..self.current] == "uper".as_bytes() => TokenKind::Super,
'v' if &self.source[1..self.current] == "ar".as_bytes() => TokenKind::Var,
'w' if &self.source[1..self.current] == "hile".as_bytes() => TokenKind::While,
'f' if self.current > 1 => {
match self.source[1] as char {
'a' => self.check_keyword(2, "lse", TokenKind::False),
'o' => self.check_keyword(2, "r", TokenKind::For),
'u' => self.check_keyword(2, "n", TokenKind::Fun),
_ => TokenKind::Identifier
}
} else {
TokenKind::Identifier
}
'a' if &self.source[2..self.current] == "lse".as_bytes() => TokenKind::False,
'o' if &self.source[2..self.current] == "r".as_bytes() => TokenKind::For,
'u' if &self.source[2..self.current] == "n".as_bytes() => TokenKind::Fun,
_ => TokenKind::Identifier,
}
't' => {
if self.current > 1 {
},
't' if self.current > 1 => {
match self.source[1] as char {
'h' => self.check_keyword(2, "is", TokenKind::This),
'r' => self.check_keyword(2, "ue", TokenKind::True),
_ => TokenKind::Identifier
}
} else {
TokenKind::Identifier
}
}
'h' if &self.source[2..self.current] == "is".as_bytes() => TokenKind::This,
'r' if &self.source[2..self.current] == "ue".as_bytes() => TokenKind::True,
_ => TokenKind::Identifier,
}
}
fn check_keyword(&self, start: usize, rest: &str, kind: TokenKind) -> TokenKind {
if &self.source[start..self.current] == rest.as_bytes() {
kind
} else {
TokenKind::Identifier
_ => TokenKind::Identifier,
}
}
}
@ -311,8 +295,12 @@ mod tests {
assert_scan_token("1a", TokenKind::Number);
assert_scan_token("1.1", TokenKind::Number);
assert_scan_token("a", TokenKind::Identifier);
assert_scan_token("an", TokenKind::Identifier);
assert_scan_token("and", TokenKind::And);
assert_scan_token("andy", TokenKind::Identifier);
assert_scan_token("false", TokenKind::False);
assert_scan_token("fa", TokenKind::Identifier);
assert_scan_token("@", TokenKind::Error);
assert_scan_token("", TokenKind::Eof);

Loading…
Cancel
Save