This commit is contained in:
Aleksey Kladov 2021-12-12 19:36:14 +03:00
parent 980dd56cdc
commit 3b5b988526

View file

@ -18,11 +18,16 @@ pub struct Tokens {
contextual_kind: Vec<SyntaxKind>, contextual_kind: Vec<SyntaxKind>,
} }
/// `pub` impl used by callers to create `Tokens`.
impl Tokens { impl Tokens {
#[inline] #[inline]
pub fn push(&mut self, kind: SyntaxKind) { pub fn push(&mut self, kind: SyntaxKind) {
self.push_impl(kind, SyntaxKind::EOF) self.push_impl(kind, SyntaxKind::EOF)
} }
#[inline]
pub fn push_ident(&mut self, contextual_kind: SyntaxKind) {
self.push_impl(SyntaxKind::IDENT, contextual_kind)
}
/// Sets jointness for the last token we've pushed. /// Sets jointness for the last token we've pushed.
/// ///
/// This is a separate API rather than an argument to the `push` to make it /// This is a separate API rather than an argument to the `push` to make it
@ -41,11 +46,9 @@ impl Tokens {
/// ``` /// ```
#[inline] #[inline]
pub fn was_joint(&mut self) { pub fn was_joint(&mut self) {
self.set_joint(self.len() - 1); let n = self.len() - 1;
} let (idx, b_idx) = self.bit_index(n);
#[inline] self.joint[idx] |= 1 << b_idx;
pub fn push_ident(&mut self, contextual_kind: SyntaxKind) {
self.push_impl(SyntaxKind::IDENT, contextual_kind)
} }
#[inline] #[inline]
fn push_impl(&mut self, kind: SyntaxKind, contextual_kind: SyntaxKind) { fn push_impl(&mut self, kind: SyntaxKind, contextual_kind: SyntaxKind) {
@ -56,22 +59,9 @@ impl Tokens {
self.kind.push(kind); self.kind.push(kind);
self.contextual_kind.push(contextual_kind); self.contextual_kind.push(contextual_kind);
} }
fn set_joint(&mut self, n: usize) {
let (idx, b_idx) = self.bit_index(n);
self.joint[idx] |= 1 << b_idx;
}
fn bit_index(&self, n: usize) -> (usize, usize) {
let idx = n / (bits::BITS as usize);
let b_idx = n % (bits::BITS as usize);
(idx, b_idx)
}
fn len(&self) -> usize {
self.kind.len()
}
} }
/// pub(crate) impl used by the parser. /// pub(crate) impl used by the parser to consume `Tokens`.
impl Tokens { impl Tokens {
pub(crate) fn kind(&self, idx: usize) -> SyntaxKind { pub(crate) fn kind(&self, idx: usize) -> SyntaxKind {
self.kind.get(idx).copied().unwrap_or(SyntaxKind::EOF) self.kind.get(idx).copied().unwrap_or(SyntaxKind::EOF)
@ -84,3 +74,14 @@ impl Tokens {
self.joint[idx] & 1 << b_idx != 0 self.joint[idx] & 1 << b_idx != 0
} }
} }
impl Tokens {
fn bit_index(&self, n: usize) -> (usize, usize) {
let idx = n / (bits::BITS as usize);
let b_idx = n % (bits::BITS as usize);
(idx, b_idx)
}
fn len(&self) -> usize {
self.kind.len()
}
}