1 //! A bit-set of `SyntaxKind`s. 2 3 use crate::SyntaxKind; 4 5 /// A bit-set of `SyntaxKind`s 6 #[derive(Clone, Copy)] 7 pub(crate) struct TokenSet(u128); 8 9 impl TokenSet { 10 pub(crate) const EMPTY: TokenSet = TokenSet(0); 11 new(kinds: &[SyntaxKind]) -> TokenSet12 pub(crate) const fn new(kinds: &[SyntaxKind]) -> TokenSet { 13 let mut res = 0u128; 14 let mut i = 0; 15 while i < kinds.len() { 16 res |= mask(kinds[i]); 17 i += 1; 18 } 19 TokenSet(res) 20 } 21 union(self, other: TokenSet) -> TokenSet22 pub(crate) const fn union(self, other: TokenSet) -> TokenSet { 23 TokenSet(self.0 | other.0) 24 } 25 contains(&self, kind: SyntaxKind) -> bool26 pub(crate) const fn contains(&self, kind: SyntaxKind) -> bool { 27 self.0 & mask(kind) != 0 28 } 29 } 30 mask(kind: SyntaxKind) -> u12831const fn mask(kind: SyntaxKind) -> u128 { 32 1u128 << (kind as usize) 33 } 34 35 #[test] token_set_works_for_tokens()36fn token_set_works_for_tokens() { 37 use crate::SyntaxKind::*; 38 let ts = TokenSet::new(&[EOF, SHEBANG]); 39 assert!(ts.contains(EOF)); 40 assert!(ts.contains(SHEBANG)); 41 assert!(!ts.contains(PLUS)); 42 } 43