diff options
Diffstat (limited to 'crates/parser/src/token_set.rs')
-rw-r--r-- | crates/parser/src/token_set.rs | 20 |
1 files changed, 10 insertions, 10 deletions
diff --git a/crates/parser/src/token_set.rs b/crates/parser/src/token_set.rs index 994017acf..a68f0144e 100644 --- a/crates/parser/src/token_set.rs +++ b/crates/parser/src/token_set.rs | |||
@@ -9,15 +9,21 @@ pub(crate) struct TokenSet(u128); | |||
9 | impl TokenSet { | 9 | impl TokenSet { |
10 | pub(crate) const EMPTY: TokenSet = TokenSet(0); | 10 | pub(crate) const EMPTY: TokenSet = TokenSet(0); |
11 | 11 | ||
12 | pub(crate) const fn singleton(kind: SyntaxKind) -> TokenSet { | 12 | pub(crate) const fn new(kinds: &[SyntaxKind]) -> TokenSet { |
13 | TokenSet(mask(kind)) | 13 | let mut res = 0u128; |
14 | let mut i = 0; | ||
15 | while i < kinds.len() { | ||
16 | res |= mask(kinds[i]); | ||
17 | i += 1 | ||
18 | } | ||
19 | TokenSet(res) | ||
14 | } | 20 | } |
15 | 21 | ||
16 | pub(crate) const fn union(self, other: TokenSet) -> TokenSet { | 22 | pub(crate) const fn union(self, other: TokenSet) -> TokenSet { |
17 | TokenSet(self.0 | other.0) | 23 | TokenSet(self.0 | other.0) |
18 | } | 24 | } |
19 | 25 | ||
20 | pub(crate) fn contains(&self, kind: SyntaxKind) -> bool { | 26 | pub(crate) const fn contains(&self, kind: SyntaxKind) -> bool { |
21 | self.0 & mask(kind) != 0 | 27 | self.0 & mask(kind) != 0 |
22 | } | 28 | } |
23 | } | 29 | } |
@@ -26,16 +32,10 @@ const fn mask(kind: SyntaxKind) -> u128 { | |||
26 | 1u128 << (kind as usize) | 32 | 1u128 << (kind as usize) |
27 | } | 33 | } |
28 | 34 | ||
29 | #[macro_export] | ||
30 | macro_rules! token_set { | ||
31 | ($($t:expr),*) => { TokenSet::EMPTY$(.union(TokenSet::singleton($t)))* }; | ||
32 | ($($t:expr),* ,) => { token_set!($($t),*) }; | ||
33 | } | ||
34 | |||
35 | #[test] | 35 | #[test] |
36 | fn token_set_works_for_tokens() { | 36 | fn token_set_works_for_tokens() { |
37 | use crate::SyntaxKind::*; | 37 | use crate::SyntaxKind::*; |
38 | let ts = token_set![EOF, SHEBANG]; | 38 | let ts = TokenSet::new(&[EOF, SHEBANG]); |
39 | assert!(ts.contains(EOF)); | 39 | assert!(ts.contains(EOF)); |
40 | assert!(ts.contains(SHEBANG)); | 40 | assert!(ts.contains(SHEBANG)); |
41 | assert!(!ts.contains(PLUS)); | 41 | assert!(!ts.contains(PLUS)); |