Skip to main content

rslint_parser/
token_set.rs

1use crate::SyntaxKind;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4pub struct TokenSet([u128; 2]);
5
6impl TokenSet {
7    pub const EMPTY: TokenSet = TokenSet([0; 2]);
8
9    pub const fn singleton(kind: SyntaxKind) -> TokenSet {
10        TokenSet(mask(kind))
11    }
12
13    pub const fn union(self, other: TokenSet) -> TokenSet {
14        TokenSet([self.0[0] | other.0[0], self.0[1] | other.0[1]])
15    }
16
17    pub fn contains(&self, kind: SyntaxKind) -> bool {
18        let num = kind as usize;
19        match num {
20            0..=127 => self.0[0] & mask(kind)[0] != 0,
21            _ => self.0[1] & mask(kind)[1] != 0,
22        }
23    }
24}
25
26const fn mask(kind: SyntaxKind) -> [u128; 2] {
27    let num = kind as usize;
28    match num {
29        0..=127 => [1u128 << num, 0],
30        _ => [0, 1u128 << (num - 127)],
31    }
32}
33
34/// Utility macro for making a new token set
35#[macro_export]
36macro_rules! token_set {
37    ($($t:expr),*) => { TokenSet::EMPTY$(.union(TokenSet::singleton($t)))* };
38    ($($t:expr),* ,) => { token_set!($($t),*) };
39}