aboutsummaryrefslogtreecommitdiff
path: root/crates/parser/src/token_set.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/parser/src/token_set.rs')
-rw-r--r--crates/parser/src/token_set.rs42
1 files changed, 42 insertions, 0 deletions
diff --git a/crates/parser/src/token_set.rs b/crates/parser/src/token_set.rs
new file mode 100644
index 000000000..994017acf
--- /dev/null
+++ b/crates/parser/src/token_set.rs
@@ -0,0 +1,42 @@
1//! A bit-set of `SyntaxKind`s.
2
3use crate::SyntaxKind;
4
5/// A bit-set of `SyntaxKind`s
6#[derive(Clone, Copy)]
7pub(crate) struct TokenSet(u128);
8
9impl TokenSet {
10 pub(crate) const EMPTY: TokenSet = TokenSet(0);
11
12 pub(crate) const fn singleton(kind: SyntaxKind) -> TokenSet {
13 TokenSet(mask(kind))
14 }
15
16 pub(crate) const fn union(self, other: TokenSet) -> TokenSet {
17 TokenSet(self.0 | other.0)
18 }
19
20 pub(crate) fn contains(&self, kind: SyntaxKind) -> bool {
21 self.0 & mask(kind) != 0
22 }
23}
24
25const fn mask(kind: SyntaxKind) -> u128 {
26 1u128 << (kind as usize)
27}
28
29#[macro_export]
30macro_rules! token_set {
31 ($($t:expr),*) => { TokenSet::EMPTY$(.union(TokenSet::singleton($t)))* };
32 ($($t:expr),* ,) => { token_set!($($t),*) };
33}
34
35#[test]
36fn token_set_works_for_tokens() {
37 use crate::SyntaxKind::*;
38 let ts = token_set![EOF, SHEBANG];
39 assert!(ts.contains(EOF));
40 assert!(ts.contains(SHEBANG));
41 assert!(!ts.contains(PLUS));
42}