make token set a const-fn

This commit is contained in:
Aleksey Kladov 2019-01-18 11:02:30 +03:00
parent b028472481
commit b82fe73d1a
8 changed files with 52 additions and 57 deletions

View file

@ -1,30 +1,34 @@
use crate::SyntaxKind;
#[derive(Clone, Copy)]
pub(crate) struct TokenSet(pub(crate) u128);
fn mask(kind: SyntaxKind) -> u128 {
1u128 << (kind as usize)
}
pub(crate) struct TokenSet(u128);
impl TokenSet {
pub const EMPTY: TokenSet = TokenSet(0);
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)
}
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),*) };
const fn mask(kind: SyntaxKind) -> u128 {
1u128 << (kind as usize)
}
#[macro_export]
macro_rules! token_set_union {
($($ts:expr),*) => { TokenSet($($ts.0)|*) };
($($ts:expr),* ,) => { token_set_union!($($ts),*) };
macro_rules! token_set {
($($t:ident),*) => { TokenSet::empty()$(.union(TokenSet::singleton($t)))* };
($($t:ident),* ,) => { token_set!($($t),*) };
}
#[test]