aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/token_set.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src/token_set.rs')
-rw-r--r--crates/ra_syntax/src/token_set.rs30
1 files changed, 17 insertions, 13 deletions
diff --git a/crates/ra_syntax/src/token_set.rs b/crates/ra_syntax/src/token_set.rs
index d407dfa48..b3fe633e0 100644
--- a/crates/ra_syntax/src/token_set.rs
+++ b/crates/ra_syntax/src/token_set.rs
@@ -1,30 +1,34 @@
1use crate::SyntaxKind; 1use crate::SyntaxKind;
2 2
3#[derive(Clone, Copy)] 3#[derive(Clone, Copy)]
4pub(crate) struct TokenSet(pub(crate) u128); 4pub(crate) struct TokenSet(u128);
5
6fn mask(kind: SyntaxKind) -> u128 {
7 1u128 << (kind as usize)
8}
9 5
10impl TokenSet { 6impl TokenSet {
11 pub const EMPTY: TokenSet = TokenSet(0); 7 pub const fn empty() -> TokenSet {
8 TokenSet(0)
9 }
10
11 pub const fn singleton(kind: SyntaxKind) -> TokenSet {
12 TokenSet(mask(kind))
13 }
14
15 pub const fn union(self, other: TokenSet) -> TokenSet {
16 TokenSet(self.0 | other.0)
17 }
12 18
13 pub fn contains(&self, kind: SyntaxKind) -> bool { 19 pub fn contains(&self, kind: SyntaxKind) -> bool {
14 self.0 & mask(kind) != 0 20 self.0 & mask(kind) != 0
15 } 21 }
16} 22}
17 23
18#[macro_export] 24const fn mask(kind: SyntaxKind) -> u128 {
19macro_rules! token_set { 25 1u128 << (kind as usize)
20 ($($t:ident),*) => { TokenSet($(1u128 << ($t as usize))|*) };
21 ($($t:ident),* ,) => { token_set!($($t),*) };
22} 26}
23 27
24#[macro_export] 28#[macro_export]
25macro_rules! token_set_union { 29macro_rules! token_set {
26 ($($ts:expr),*) => { TokenSet($($ts.0)|*) }; 30 ($($t:ident),*) => { TokenSet::empty()$(.union(TokenSet::singleton($t)))* };
27 ($($ts:expr),* ,) => { token_set_union!($($ts),*) }; 31 ($($t:ident),* ,) => { token_set!($($t),*) };
28} 32}
29 33
30#[test] 34#[test]