move parser to a separate crate

This commit is contained in:
Aleksey Kladov
2019-02-21 13:27:45 +03:00
parent 18b0c509f7
commit d334b5a1db
24 changed files with 91 additions and 18 deletions

View File

@@ -0,0 +1,41 @@
use crate::SyntaxKind;
#[derive(Clone, Copy)]
pub(crate) struct TokenSet(u128);
impl TokenSet {
pub(crate) const fn empty() -> TokenSet {
TokenSet(0)
}
pub(crate) const fn singleton(kind: SyntaxKind) -> TokenSet {
TokenSet(mask(kind))
}
pub(crate) const fn union(self, other: TokenSet) -> TokenSet {
TokenSet(self.0 | other.0)
}
pub(crate) fn contains(&self, kind: SyntaxKind) -> bool {
self.0 & mask(kind) != 0
}
}
const fn mask(kind: SyntaxKind) -> u128 {
1u128 << (kind as usize)
}
#[macro_export]
macro_rules! token_set {
($($t:ident),*) => { TokenSet::empty()$(.union(TokenSet::singleton($t)))* };
($($t:ident),* ,) => { token_set!($($t),*) };
}
#[test]
fn token_set_works_for_tokens() {
use crate::SyntaxKind::*;
let ts = token_set![EOF, SHEBANG];
assert!(ts.contains(EOF));
assert!(ts.contains(SHEBANG));
assert!(!ts.contains(PLUS));
}