2018-10-15 11:55:32 -05:00
|
|
|
use crate::SyntaxKind;
|
2018-09-06 08:54:54 -05:00
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
2019-01-18 02:02:30 -06:00
|
|
|
pub(crate) struct TokenSet(u128);
|
2018-09-06 08:54:54 -05:00
|
|
|
|
|
|
|
impl TokenSet {
|
2019-01-18 02:02:30 -06:00
|
|
|
pub const fn empty() -> TokenSet {
|
|
|
|
TokenSet(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const fn singleton(kind: SyntaxKind) -> TokenSet {
|
|
|
|
TokenSet(mask(kind))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const fn union(self, other: TokenSet) -> TokenSet {
|
|
|
|
TokenSet(self.0 | other.0)
|
|
|
|
}
|
2018-09-06 08:54:54 -05:00
|
|
|
|
|
|
|
pub fn contains(&self, kind: SyntaxKind) -> bool {
|
|
|
|
self.0 & mask(kind) != 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-18 02:02:30 -06:00
|
|
|
const fn mask(kind: SyntaxKind) -> u128 {
|
|
|
|
1u128 << (kind as usize)
|
2018-09-06 08:54:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
2019-01-18 02:02:30 -06:00
|
|
|
macro_rules! token_set {
|
|
|
|
($($t:ident),*) => { TokenSet::empty()$(.union(TokenSet::singleton($t)))* };
|
|
|
|
($($t:ident),* ,) => { token_set!($($t),*) };
|
2018-09-06 08:54:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn token_set_works_for_tokens() {
|
2018-10-15 11:55:32 -05:00
|
|
|
use crate::SyntaxKind::*;
|
2018-09-06 08:54:54 -05:00
|
|
|
let ts = token_set! { EOF, SHEBANG };
|
|
|
|
assert!(ts.contains(EOF));
|
|
|
|
assert!(ts.contains(SHEBANG));
|
|
|
|
assert!(!ts.contains(PLUS));
|
|
|
|
}
|