aboutsummaryrefslogtreecommitdiff
path: root/crates/parser/src/token_set.rs
diff options
context:
space:
mode:
authorDmitry <[email protected]>2020-08-14 19:32:05 +0100
committerDmitry <[email protected]>2020-08-14 19:32:05 +0100
commit178c3e135a2a249692f7784712492e7884ae0c00 (patch)
treeac6b769dbf7162150caa0c1624786a4dd79ff3be /crates/parser/src/token_set.rs
parent06ff8e6c760ff05f10e868b5d1f9d79e42fbb49c (diff)
parentc2594daf2974dbd4ce3d9b7ec72481764abaceb5 (diff)
Merge remote-tracking branch 'origin/master'
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}