]> git.lizzy.rs Git - rust.git/blob - crates/ra_parser/src/token_set.rs
Touch up TokenSet a bit
[rust.git] / crates / ra_parser / src / token_set.rs
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
12     pub(crate) const fn singleton(kind: SyntaxKind) -> TokenSet {
13         TokenSet(mask(kind))
14     }
15
16     pub(crate) const fn union(self, other: TokenSet) -> TokenSet {
17         TokenSet(self.0 | other.0)
18     }
19
20     pub(crate) fn contains(&self, kind: SyntaxKind) -> bool {
21         self.0 & mask(kind) != 0
22     }
23 }
24
25 const fn mask(kind: SyntaxKind) -> u128 {
26     1u128 << (kind as usize)
27 }
28
29 #[macro_export]
30 macro_rules! token_set {
31     ($($t:expr),*) => { TokenSet::EMPTY$(.union(TokenSet::singleton($t)))* };
32     ($($t:expr),* ,) => { token_set!($($t),*) };
33 }
34
35 #[test]
36 fn token_set_works_for_tokens() {
37     use crate::SyntaxKind::*;
38     let ts = token_set![EOF, SHEBANG];
39     assert!(ts.contains(EOF));
40     assert!(ts.contains(SHEBANG));
41     assert!(!ts.contains(PLUS));
42 }