diff options
author | Aleksey Kladov <[email protected]> | 2019-01-18 08:02:30 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-01-19 12:36:58 +0000 |
commit | b82fe73d1ab9727ff650382d9c86a231b06245be (patch) | |
tree | e919865c68f78492bab5baa574f1a35a094b0339 /crates/ra_syntax/src/token_set.rs | |
parent | b028472481df108537b60104314081b65bf51147 (diff) |
make token set a const-fn
Diffstat (limited to 'crates/ra_syntax/src/token_set.rs')
-rw-r--r-- | crates/ra_syntax/src/token_set.rs | 30 |
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 @@ | |||
1 | use crate::SyntaxKind; | 1 | use crate::SyntaxKind; |
2 | 2 | ||
3 | #[derive(Clone, Copy)] | 3 | #[derive(Clone, Copy)] |
4 | pub(crate) struct TokenSet(pub(crate) u128); | 4 | pub(crate) struct TokenSet(u128); |
5 | |||
6 | fn mask(kind: SyntaxKind) -> u128 { | ||
7 | 1u128 << (kind as usize) | ||
8 | } | ||
9 | 5 | ||
10 | impl TokenSet { | 6 | impl 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] | 24 | const fn mask(kind: SyntaxKind) -> u128 { |
19 | macro_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] |
25 | macro_rules! token_set_union { | 29 | macro_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] |