diff options
Diffstat (limited to 'crates/libsyntax2/src/token_set.rs')
-rw-r--r-- | crates/libsyntax2/src/token_set.rs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/crates/libsyntax2/src/token_set.rs b/crates/libsyntax2/src/token_set.rs new file mode 100644 index 000000000..c83fba81b --- /dev/null +++ b/crates/libsyntax2/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 | } | ||