aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/token_set.rs
blob: d407dfa485c0508c2384c3449741554f05154566 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use crate::SyntaxKind;

#[derive(Clone, Copy)]
pub(crate) struct TokenSet(pub(crate) u128);

fn mask(kind: SyntaxKind) -> u128 {
    1u128 << (kind as usize)
}

impl TokenSet {
    pub const EMPTY: TokenSet = TokenSet(0);

    pub fn contains(&self, kind: SyntaxKind) -> bool {
        self.0 & mask(kind) != 0
    }
}

#[macro_export]
macro_rules! token_set {
    ($($t:ident),*) => { TokenSet($(1u128 << ($t as usize))|*) };
    ($($t:ident),* ,) => { token_set!($($t),*) };
}

#[macro_export]
macro_rules! token_set_union {
    ($($ts:expr),*) => { TokenSet($($ts.0)|*) };
    ($($ts:expr),* ,) => { token_set_union!($($ts),*) };
}

#[test]
fn token_set_works_for_tokens() {
    use crate::SyntaxKind::*;
    let ts = token_set! { EOF, SHEBANG };
    assert!(ts.contains(EOF));
    assert!(ts.contains(SHEBANG));
    assert!(!ts.contains(PLUS));
}