-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
751562d
commit 518cc87
Showing
4 changed files
with
41 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use SyntaxKind; | ||
|
||
#[derive(Clone, Copy)] | ||
pub(crate) struct TokenSet(pub(crate) u128); | ||
|
||
fn mask(kind: SyntaxKind) -> u128 { | ||
1u128 << (kind as usize) | ||
} | ||
|
||
impl TokenSet { | ||
pub const EMPTY: TokenSet = TokenSet(0); | ||
|
||
pub fn contains(&self, kind: SyntaxKind) -> bool { | ||
self.0 & mask(kind) != 0 | ||
} | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! token_set { | ||
($($t:ident),*) => { TokenSet($(1u128 << ($t as usize))|*) }; | ||
($($t:ident),* ,) => { token_set!($($t),*) }; | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! token_set_union { | ||
($($ts:expr),*) => { TokenSet($($ts.0)|*) }; | ||
($($ts:expr),* ,) => { token_set_union!($($ts),*) }; | ||
} | ||
|
||
#[test] | ||
fn token_set_works_for_tokens() { | ||
use SyntaxKind::*; | ||
let ts = token_set! { EOF, SHEBANG }; | ||
assert!(ts.contains(EOF)); | ||
assert!(ts.contains(SHEBANG)); | ||
assert!(!ts.contains(PLUS)); | ||
} |