diff options
author | Aleksey Kladov <[email protected]> | 2018-09-16 10:54:24 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-09-16 11:07:39 +0100 |
commit | b5021411a84822cb3f1e3aeffad9550dd15bdeb6 (patch) | |
tree | 9dca564f8e51b298dced01c4ce669c756dce3142 /crates/ra_syntax/src/token_set.rs | |
parent | ba0bfeee12e19da40b5eabc8d0408639af10e96f (diff) |
rename all things
Diffstat (limited to 'crates/ra_syntax/src/token_set.rs')
-rw-r--r-- | crates/ra_syntax/src/token_set.rs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/crates/ra_syntax/src/token_set.rs b/crates/ra_syntax/src/token_set.rs new file mode 100644 index 000000000..c83fba81b --- /dev/null +++ b/crates/ra_syntax/src/token_set.rs | |||
@@ -0,0 +1,37 @@ | |||
1 | use SyntaxKind; | ||
2 | |||
3 | #[derive(Clone, Copy)] | ||
4 | pub(crate) struct TokenSet(pub(crate) u128); | ||
5 | |||
6 | fn mask(kind: SyntaxKind) -> u128 { | ||
7 | 1u128 << (kind as usize) | ||
8 | } | ||
9 | |||
10 | impl TokenSet { | ||
11 | pub const EMPTY: TokenSet = TokenSet(0); | ||
12 | |||
13 | pub fn contains(&self, kind: SyntaxKind) -> bool { | ||
14 | self.0 & mask(kind) != 0 | ||
15 | } | ||
16 | } | ||
17 | |||
18 | #[macro_export] | ||
19 | macro_rules! token_set { | ||
20 | ($($t:ident),*) => { TokenSet($(1u128 << ($t as usize))|*) }; | ||
21 | ($($t:ident),* ,) => { token_set!($($t),*) }; | ||
22 | } | ||
23 | |||
24 | #[macro_export] | ||
25 | macro_rules! token_set_union { | ||
26 | ($($ts:expr),*) => { TokenSet($($ts.0)|*) }; | ||
27 | ($($ts:expr),* ,) => { token_set_union!($($ts),*) }; | ||
28 | } | ||
29 | |||
30 | #[test] | ||
31 | fn token_set_works_for_tokens() { | ||
32 | use SyntaxKind::*; | ||
33 | let ts = token_set! { EOF, SHEBANG }; | ||
34 | assert!(ts.contains(EOF)); | ||
35 | assert!(ts.contains(SHEBANG)); | ||
36 | assert!(!ts.contains(PLUS)); | ||
37 | } | ||