From 50a02eb3593591a02677e1b56f24d7ff0459b9d0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 12 Aug 2020 17:06:49 +0200 Subject: Rename ra_parser -> parser --- crates/parser/src/token_set.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 crates/parser/src/token_set.rs (limited to 'crates/parser/src/token_set.rs') diff --git a/crates/parser/src/token_set.rs b/crates/parser/src/token_set.rs new file mode 100644 index 000000000..994017acf --- /dev/null +++ b/crates/parser/src/token_set.rs @@ -0,0 +1,42 @@ +//! A bit-set of `SyntaxKind`s. + +use crate::SyntaxKind; + +/// A bit-set of `SyntaxKind`s +#[derive(Clone, Copy)] +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 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: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]; + assert!(ts.contains(EOF)); + assert!(ts.contains(SHEBANG)); + assert!(!ts.contains(PLUS)); +} -- cgit v1.2.3