diff options
Diffstat (limited to 'src/tools/rust-analyzer/crates/parser/src/token_set.rs')
-rw-r--r-- | src/tools/rust-analyzer/crates/parser/src/token_set.rs | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/tools/rust-analyzer/crates/parser/src/token_set.rs b/src/tools/rust-analyzer/crates/parser/src/token_set.rs new file mode 100644 index 000000000..cd4894c1e --- /dev/null +++ b/src/tools/rust-analyzer/crates/parser/src/token_set.rs @@ -0,0 +1,42 @@ +//! A bit-set of `SyntaxKind`s. + +use crate::SyntaxKind; + +/// A bit-set of `SyntaxKind`s +#[derive(Clone, Copy)] +pub(crate) struct TokenSet(u128); + +impl TokenSet { + pub(crate) const EMPTY: TokenSet = TokenSet(0); + + pub(crate) const fn new(kinds: &[SyntaxKind]) -> TokenSet { + let mut res = 0u128; + let mut i = 0; + while i < kinds.len() { + res |= mask(kinds[i]); + i += 1; + } + TokenSet(res) + } + + pub(crate) const fn union(self, other: TokenSet) -> TokenSet { + TokenSet(self.0 | other.0) + } + + pub(crate) const fn contains(&self, kind: SyntaxKind) -> bool { + self.0 & mask(kind) != 0 + } +} + +const fn mask(kind: SyntaxKind) -> u128 { + 1u128 << (kind as usize) +} + +#[test] +fn token_set_works_for_tokens() { + use crate::SyntaxKind::*; + let ts = TokenSet::new(&[EOF, SHEBANG]); + assert!(ts.contains(EOF)); + assert!(ts.contains(SHEBANG)); + assert!(!ts.contains(PLUS)); +} |