CONST LOOPS ARE HERE

This commit is contained in:
Aleksey Kladov
2020-08-27 18:11:33 +02:00
parent 6f6580dec7
commit 4b989009e3
8 changed files with 38 additions and 29 deletions

View File

@@ -9,15 +9,21 @@ pub(crate) struct TokenSet(u128);
impl TokenSet {
pub(crate) const EMPTY: TokenSet = TokenSet(0);
pub(crate) const fn singleton(kind: SyntaxKind) -> TokenSet {
TokenSet(mask(kind))
pub(crate) const fn new(kinds: &[SyntaxKind]) -> TokenSet {
let mut res = 0u128;
let mut i = 0;
while i < kinds.len() {
res |= mask(kinds[i]);
i += 1
}
TokenSet(res)
}
pub(crate) const fn union(self, other: TokenSet) -> TokenSet {
TokenSet(self.0 | other.0)
}
pub(crate) fn contains(&self, kind: SyntaxKind) -> bool {
pub(crate) const fn contains(&self, kind: SyntaxKind) -> bool {
self.0 & mask(kind) != 0
}
}
@@ -26,16 +32,10 @@ const fn mask(kind: SyntaxKind) -> u128 {
1u128 << (kind as usize)
}
#[macro_export]
macro_rules! token_set {
($($t:expr),*) => { TokenSet::EMPTY$(.union(TokenSet::singleton($t)))* };
($($t:expr),* ,) => { token_set!($($t),*) };
}
#[test]
fn token_set_works_for_tokens() {
use crate::SyntaxKind::*;
let ts = token_set![EOF, SHEBANG];
let ts = TokenSet::new(&[EOF, SHEBANG]);
assert!(ts.contains(EOF));
assert!(ts.contains(SHEBANG));
assert!(!ts.contains(PLUS));