2019-12-19 16:13:08 +00:00
|
|
|
//! A bit-set of `SyntaxKind`s.
|
2019-09-30 08:58:53 +00:00
|
|
|
|
2018-10-15 16:55:32 +00:00
|
|
|
use crate::SyntaxKind;
|
2018-09-06 13:54:54 +00:00
|
|
|
|
2019-02-21 12:24:42 +00:00
|
|
|
/// A bit-set of `SyntaxKind`s
|
2018-09-06 13:54:54 +00:00
|
|
|
#[derive(Clone, Copy)]
|
2019-01-18 08:02:30 +00:00
|
|
|
pub(crate) struct TokenSet(u128);
|
2018-09-06 13:54:54 +00:00
|
|
|
|
|
|
|
impl TokenSet {
|
2019-12-19 16:13:08 +00:00
|
|
|
pub(crate) const EMPTY: TokenSet = TokenSet(0);
|
2019-01-18 08:02:30 +00:00
|
|
|
|
2020-08-27 16:11:33 +00:00
|
|
|
pub(crate) const fn new(kinds: &[SyntaxKind]) -> TokenSet {
|
|
|
|
let mut res = 0u128;
|
|
|
|
let mut i = 0;
|
|
|
|
while i < kinds.len() {
|
|
|
|
res |= mask(kinds[i]);
|
2021-10-03 12:39:43 +00:00
|
|
|
i += 1;
|
2020-08-27 16:11:33 +00:00
|
|
|
}
|
|
|
|
TokenSet(res)
|
2019-01-18 08:02:30 +00:00
|
|
|
}
|
|
|
|
|
2019-02-20 12:47:32 +00:00
|
|
|
pub(crate) const fn union(self, other: TokenSet) -> TokenSet {
|
2019-01-18 08:02:30 +00:00
|
|
|
TokenSet(self.0 | other.0)
|
|
|
|
}
|
2018-09-06 13:54:54 +00:00
|
|
|
|
2020-08-27 16:11:33 +00:00
|
|
|
pub(crate) const fn contains(&self, kind: SyntaxKind) -> bool {
|
2018-09-06 13:54:54 +00:00
|
|
|
self.0 & mask(kind) != 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-18 08:02:30 +00:00
|
|
|
const fn mask(kind: SyntaxKind) -> u128 {
|
|
|
|
1u128 << (kind as usize)
|
2018-09-06 13:54:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn token_set_works_for_tokens() {
|
2018-10-15 16:55:32 +00:00
|
|
|
use crate::SyntaxKind::*;
|
2020-08-27 16:11:33 +00:00
|
|
|
let ts = TokenSet::new(&[EOF, SHEBANG]);
|
2018-09-06 13:54:54 +00:00
|
|
|
assert!(ts.contains(EOF));
|
|
|
|
assert!(ts.contains(SHEBANG));
|
|
|
|
assert!(!ts.contains(PLUS));
|
|
|
|
}
|