internal: simplify TokenSet implementation

This commit is contained in:
davidsemakula 2024-04-15 16:46:55 +03:00
parent 8e459125df
commit 69fe457cb5

View File

@ -6,6 +6,9 @@
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub(crate) struct TokenSet([u64; 3]); pub(crate) struct TokenSet([u64; 3]);
/// `TokenSet`s should only include token `SyntaxKind`s, so the discriminant of any passed/included
/// `SyntaxKind` must *not* be greater than that of the last token `SyntaxKind`.
/// See #17037.
const LAST_TOKEN_KIND_DISCRIMINANT: usize = SyntaxKind::SHEBANG as usize; const LAST_TOKEN_KIND_DISCRIMINANT: usize = SyntaxKind::SHEBANG as usize;
impl TokenSet { impl TokenSet {
@ -15,13 +18,13 @@ pub(crate) const fn new(kinds: &[SyntaxKind]) -> TokenSet {
let mut res = [0; 3]; let mut res = [0; 3];
let mut i = 0; let mut i = 0;
while i < kinds.len() { while i < kinds.len() {
let kind = kinds[i]; let discriminant = kinds[i] as usize;
debug_assert!( debug_assert!(
kind as usize <= LAST_TOKEN_KIND_DISCRIMINANT, discriminant <= LAST_TOKEN_KIND_DISCRIMINANT,
"Expected a token `SyntaxKind`" "Expected a token `SyntaxKind`"
); );
let idx = kind as usize / 64; let idx = discriminant / 64;
res[idx] |= mask(kind); res[idx] |= 1 << (discriminant % 64);
i += 1; i += 1;
} }
TokenSet(res) TokenSet(res)
@ -32,20 +35,17 @@ pub(crate) const fn union(self, other: TokenSet) -> TokenSet {
} }
pub(crate) const fn contains(&self, kind: SyntaxKind) -> bool { pub(crate) const fn contains(&self, kind: SyntaxKind) -> bool {
let discriminant = kind as usize;
debug_assert!( debug_assert!(
kind as usize <= LAST_TOKEN_KIND_DISCRIMINANT, discriminant <= LAST_TOKEN_KIND_DISCRIMINANT,
"Expected a token `SyntaxKind`" "Expected a token `SyntaxKind`"
); );
let idx = kind as usize / 64; let idx = discriminant / 64;
self.0[idx] & mask(kind) != 0 let mask = 1 << (discriminant % 64);
self.0[idx] & mask != 0
} }
} }
const fn mask(kind: SyntaxKind) -> u64 {
debug_assert!(kind as usize <= LAST_TOKEN_KIND_DISCRIMINANT, "Expected a token `SyntaxKind`");
1 << (kind as usize % 64)
}
#[test] #[test]
fn token_set_works_for_tokens() { fn token_set_works_for_tokens() {
use crate::SyntaxKind::*; use crate::SyntaxKind::*;