Skip to content

Commit 89779ca

Browse files
committed
internal: improve TokenSet implementation
1 parent 5dbe3fe commit 89779ca

1 file changed

Lines changed: 22 additions & 8 deletions

File tree

‎crates/parser/src/token_set.rs‎

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,46 @@ use crate::SyntaxKind;
44

55
/// A bit-set of `SyntaxKind`s
66
#[derive(Clone,Copy)]
7-
pub(crate)structTokenSet(u128);
7+
pub(crate)structTokenSet([u64;3]);
8+
9+
constLAST_TOKEN_KIND_DISCRIMINANT:usize = SyntaxKind::SHEBANGasusize;
810

911
implTokenSet{
10-
pub(crate)constEMPTY:TokenSet = TokenSet(0);
12+
pub(crate)constEMPTY:TokenSet = TokenSet([0;3]);
1113

1214
pub(crate)constfnnew(kinds:&[SyntaxKind]) -> TokenSet{
13-
letmut res = 0u128;
15+
letmut res = [0;3];
1416
letmut i = 0;
1517
while i < kinds.len(){
16-
res |= mask(kinds[i]);
18+
let kind = kinds[i];
19+
debug_assert!(
20+
kind asusize <= LAST_TOKEN_KIND_DISCRIMINANT,
21+
"Expected a token `SyntaxKind`"
22+
);
23+
let idx = kind asusize / 64;
24+
res[idx] |= mask(kind);
1725
i += 1;
1826
}
1927
TokenSet(res)
2028
}
2129

2230
pub(crate)constfnunion(self,other:TokenSet) -> TokenSet{
23-
TokenSet(self.0 | other.0)
31+
TokenSet([self.0[0] | other.0[0],self.0[1] | other.0[1],self.0[2] | other.0[2]])
2432
}
2533

2634
pub(crate)constfncontains(&self,kind:SyntaxKind) -> bool{
27-
self.0&mask(kind) != 0
35+
debug_assert!(
36+
kind asusize <= LAST_TOKEN_KIND_DISCRIMINANT,
37+
"Expected a token `SyntaxKind`"
38+
);
39+
let idx = kind asusize / 64;
40+
self.0[idx]&mask(kind) != 0
2841
}
2942
}
3043

31-
constfnmask(kind:SyntaxKind) -> u128{
32-
1u128 << (kind asusize)
44+
constfnmask(kind:SyntaxKind) -> u64{
45+
debug_assert!(kind asusize <= LAST_TOKEN_KIND_DISCRIMINANT,"Expected a token `SyntaxKind`");
46+
1 << (kind asusize % 64)
3347
}
3448

3549
#[test]

0 commit comments

Comments
 (0)