diff options
author | Igor Aleksanov <[email protected]> | 2020-08-14 05:34:07 +0100 |
---|---|---|
committer | Igor Aleksanov <[email protected]> | 2020-08-14 05:34:07 +0100 |
commit | c26c911ec1e6c2ad1dcb7d155a6a1d528839ad1a (patch) | |
tree | 7cff36c38234be0afb65273146d8247083a5cfeb /crates/parser/src/token_set.rs | |
parent | 3c018bf84de5c693b5ee1c6bec0fed3b201c2060 (diff) | |
parent | f1f73649a686dc6e6449afc35e0fa6fed00e225d (diff) |
Merge branch 'master' into add-disable-diagnostics
Diffstat (limited to 'crates/parser/src/token_set.rs')
-rw-r--r-- | crates/parser/src/token_set.rs | 42 |
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 | |||
3 | use crate::SyntaxKind; | ||
4 | |||
5 | /// A bit-set of `SyntaxKind`s | ||
6 | #[derive(Clone, Copy)] | ||
7 | pub(crate) struct TokenSet(u128); | ||
8 | |||
9 | impl 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 | |||
25 | const fn mask(kind: SyntaxKind) -> u128 { | ||
26 | 1u128 << (kind as usize) | ||
27 | } | ||
28 | |||
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] | ||
36 | fn 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 | } | ||