aboutsummaryrefslogtreecommitdiff
path: root/crates/libsyntax2/src/token_set.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/libsyntax2/src/token_set.rs')
-rw-r--r--crates/libsyntax2/src/token_set.rs37
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 @@
1use SyntaxKind;
2
3#[derive(Clone, Copy)]
4pub(crate) struct TokenSet(pub(crate) u128);
5
6fn mask(kind: SyntaxKind) -> u128 {
7 1u128 << (kind as usize)
8}
9
10impl 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]
19macro_rules! token_set {
20 ($($t:ident),*) => { TokenSet($(1u128 << ($t as usize))|*) };
21 ($($t:ident),* ,) => { token_set!($($t),*) };
22}
23
24#[macro_export]
25macro_rules! token_set_union {
26 ($($ts:expr),*) => { TokenSet($($ts.0)|*) };
27 ($($ts:expr),* ,) => { token_set_union!($($ts),*) };
28}
29
30#[test]
31fn 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}