rust-analyzer/crates/tt/src/buffer.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

251 lines
7.9 KiB
Rust
Raw Normal View History

2021-05-22 13:53:47 +00:00
//! Stateful iteration over token trees.
//!
//! We use this as the source of tokens for parser.
2021-01-04 16:22:42 +00:00
use crate::{Leaf, Subtree, TokenTree};
2019-05-22 04:30:36 +00:00
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
struct EntryId(usize);
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
struct EntryPtr(
/// The index of the buffer containing the entry.
EntryId,
/// The index of the entry within the buffer.
usize,
);
2019-05-22 04:30:36 +00:00
/// Internal type which is used instead of `TokenTree` to represent a token tree
/// within a `TokenBuffer`.
#[derive(Debug)]
2023-01-31 10:49:49 +00:00
enum Entry<'t, Span> {
2019-05-22 04:30:36 +00:00
// Mimicking types from proc-macro.
2023-01-31 10:49:49 +00:00
Subtree(Option<&'t TokenTree<Span>>, &'t Subtree<Span>, EntryId),
Leaf(&'t TokenTree<Span>),
2023-02-03 20:39:24 +00:00
/// End entries contain a pointer to the entry from the containing
/// token tree, or [`None`] if this is the outermost level.
2019-05-22 04:30:36 +00:00
End(Option<EntryPtr>),
}
/// A token tree buffer
/// The safe version of `syn` [`TokenBuffer`](https://github.com/dtolnay/syn/blob/6533607f91686545cb034d2838beea338d9d0742/src/buffer.rs#L41)
#[derive(Debug)]
2023-01-31 10:49:49 +00:00
pub struct TokenBuffer<'t, Span> {
buffers: Vec<Box<[Entry<'t, Span>]>>,
2019-05-22 04:30:36 +00:00
}
2023-01-31 10:49:49 +00:00
trait TokenList<'a, Span> {
fn entries(
&self,
) -> (Vec<(usize, (&'a Subtree<Span>, Option<&'a TokenTree<Span>>))>, Vec<Entry<'a, Span>>);
2021-01-04 16:22:42 +00:00
}
2019-05-22 04:30:36 +00:00
2023-01-31 10:49:49 +00:00
impl<'a, Span> TokenList<'a, Span> for &'a [TokenTree<Span>] {
fn entries(
&self,
) -> (Vec<(usize, (&'a Subtree<Span>, Option<&'a TokenTree<Span>>))>, Vec<Entry<'a, Span>>)
{
// Must contain everything in tokens and then the Entry::End
2021-01-04 16:22:42 +00:00
let start_capacity = self.len() + 1;
let mut entries = Vec::with_capacity(start_capacity);
2019-05-22 04:30:36 +00:00
let mut children = vec![];
2021-01-04 16:22:42 +00:00
for (idx, tt) in self.iter().enumerate() {
2019-05-22 04:30:36 +00:00
match tt {
2019-06-02 16:54:33 +00:00
TokenTree::Leaf(_) => {
entries.push(Entry::Leaf(tt));
2019-05-22 04:30:36 +00:00
}
TokenTree::Subtree(subtree) => {
entries.push(Entry::End(None));
2021-01-04 16:22:42 +00:00
children.push((idx, (subtree, Some(tt))));
2019-05-22 04:30:36 +00:00
}
}
}
2021-01-04 16:22:42 +00:00
(children, entries)
}
}
2023-01-31 10:49:49 +00:00
impl<'a, Span> TokenList<'a, Span> for &'a Subtree<Span> {
fn entries(
&self,
) -> (Vec<(usize, (&'a Subtree<Span>, Option<&'a TokenTree<Span>>))>, Vec<Entry<'a, Span>>)
{
2021-01-04 16:22:42 +00:00
// Must contain everything in tokens and then the Entry::End
let mut entries = vec![];
let mut children = vec![];
entries.push(Entry::End(None));
children.push((0usize, (*self, None)));
(children, entries)
}
}
2023-01-31 10:49:49 +00:00
impl<'t, Span> TokenBuffer<'t, Span> {
pub fn from_tokens(tokens: &'t [TokenTree<Span>]) -> TokenBuffer<'t, Span> {
2021-01-04 16:22:42 +00:00
Self::new(tokens)
}
2023-01-31 10:49:49 +00:00
pub fn from_subtree(subtree: &'t Subtree<Span>) -> TokenBuffer<'t, Span> {
2021-01-04 16:22:42 +00:00
Self::new(subtree)
}
2023-01-31 10:49:49 +00:00
fn new<T: TokenList<'t, Span>>(tokens: T) -> TokenBuffer<'t, Span> {
2021-01-04 16:22:42 +00:00
let mut buffers = vec![];
let idx = TokenBuffer::new_inner(tokens, &mut buffers, None);
assert_eq!(idx, 0);
TokenBuffer { buffers }
}
2023-01-31 10:49:49 +00:00
fn new_inner<T: TokenList<'t, Span>>(
2021-01-04 16:22:42 +00:00
tokens: T,
2023-01-31 10:49:49 +00:00
buffers: &mut Vec<Box<[Entry<'t, Span>]>>,
2021-01-04 16:22:42 +00:00
next: Option<EntryPtr>,
) -> usize {
let (children, mut entries) = tokens.entries();
2019-05-22 04:30:36 +00:00
entries.push(Entry::End(next));
let res = buffers.len();
buffers.push(entries.into_boxed_slice());
2019-06-02 16:54:33 +00:00
for (child_idx, (subtree, tt)) in children {
2019-05-22 04:30:36 +00:00
let idx = TokenBuffer::new_inner(
&*subtree.token_trees,
2019-05-22 04:30:36 +00:00
buffers,
Some(EntryPtr(EntryId(res), child_idx + 1)),
);
2021-01-04 16:22:42 +00:00
buffers[res].as_mut()[child_idx] = Entry::Subtree(tt, subtree, EntryId(idx));
2019-05-22 04:30:36 +00:00
}
res
}
/// Creates a cursor referencing the first token in the buffer and able to
/// traverse until the end of the buffer.
2023-01-31 10:49:49 +00:00
pub fn begin(&self) -> Cursor<'_, Span> {
2019-05-22 04:30:36 +00:00
Cursor::create(self, EntryPtr(EntryId(0), 0))
}
2023-01-31 10:49:49 +00:00
fn entry(&self, ptr: &EntryPtr) -> Option<&Entry<'_, Span>> {
2019-05-22 04:30:36 +00:00
let id = ptr.0;
self.buffers[id.0].get(ptr.1)
}
}
2021-01-04 16:22:42 +00:00
#[derive(Debug)]
2023-01-31 10:49:49 +00:00
pub enum TokenTreeRef<'a, Span> {
Subtree(&'a Subtree<Span>, Option<&'a TokenTree<Span>>),
Leaf(&'a Leaf<Span>, &'a TokenTree<Span>),
2021-01-04 16:22:42 +00:00
}
2023-06-29 14:27:28 +00:00
impl<Span: Clone> TokenTreeRef<'_, Span> {
2023-01-31 10:49:49 +00:00
pub fn cloned(&self) -> TokenTree<Span> {
match self {
2021-01-04 16:22:42 +00:00
TokenTreeRef::Subtree(subtree, tt) => match tt {
Some(it) => (*it).clone(),
None => (*subtree).clone().into(),
},
TokenTreeRef::Leaf(_, tt) => (*tt).clone(),
}
}
}
/// A safe version of `Cursor` from `syn` crate <https://github.com/dtolnay/syn/blob/6533607f91686545cb034d2838beea338d9d0742/src/buffer.rs#L125>
2019-05-22 04:30:36 +00:00
#[derive(Copy, Clone, Debug)]
2023-01-31 10:49:49 +00:00
pub struct Cursor<'a, Span> {
buffer: &'a TokenBuffer<'a, Span>,
2019-05-22 04:30:36 +00:00
ptr: EntryPtr,
}
2023-06-29 14:27:28 +00:00
impl<Span> PartialEq for Cursor<'_, Span> {
2023-01-31 10:49:49 +00:00
fn eq(&self, other: &Cursor<'_, Span>) -> bool {
2019-05-22 04:30:36 +00:00
self.ptr == other.ptr && std::ptr::eq(self.buffer, other.buffer)
}
}
2023-06-29 14:27:28 +00:00
impl<Span> Eq for Cursor<'_, Span> {}
2019-05-22 04:30:36 +00:00
2023-01-31 10:49:49 +00:00
impl<'a, Span> Cursor<'a, Span> {
2019-05-22 04:30:36 +00:00
/// Check whether it is eof
pub fn eof(self) -> bool {
2020-06-28 01:02:03 +00:00
matches!(self.buffer.entry(&self.ptr), None | Some(Entry::End(None)))
2019-05-22 04:30:36 +00:00
}
/// If the cursor is pointing at the end of a subtree, returns
/// the parent subtree
2023-01-31 10:49:49 +00:00
pub fn end(self) -> Option<&'a Subtree<Span>> {
2019-05-22 04:30:36 +00:00
match self.entry() {
Some(Entry::End(Some(ptr))) => {
let idx = ptr.1;
2021-01-04 16:22:42 +00:00
if let Some(Entry::Subtree(_, subtree, _)) =
2019-05-22 04:30:36 +00:00
self.buffer.entry(&EntryPtr(ptr.0, idx - 1))
{
return Some(subtree);
}
None
}
_ => None,
}
}
2023-01-31 10:49:49 +00:00
fn entry(&self) -> Option<&'a Entry<'a, Span>> {
2019-05-22 04:30:36 +00:00
self.buffer.entry(&self.ptr)
}
/// If the cursor is pointing at a `Subtree`, returns
/// a cursor into that subtree
2023-01-31 10:49:49 +00:00
pub fn subtree(self) -> Option<Cursor<'a, Span>> {
2019-05-22 04:30:36 +00:00
match self.entry() {
2021-01-04 16:22:42 +00:00
Some(Entry::Subtree(_, _, entry_id)) => {
2019-05-22 04:30:36 +00:00
Some(Cursor::create(self.buffer, EntryPtr(*entry_id, 0)))
}
_ => None,
}
}
/// If the cursor is pointing at a `TokenTree`, returns it
2023-01-31 10:49:49 +00:00
pub fn token_tree(self) -> Option<TokenTreeRef<'a, Span>> {
2019-05-22 04:30:36 +00:00
match self.entry() {
2021-01-04 16:22:42 +00:00
Some(Entry::Leaf(tt)) => match tt {
TokenTree::Leaf(leaf) => Some(TokenTreeRef::Leaf(leaf, tt)),
2021-01-04 16:22:42 +00:00
TokenTree::Subtree(subtree) => Some(TokenTreeRef::Subtree(subtree, Some(tt))),
},
Some(Entry::Subtree(tt, subtree, _)) => Some(TokenTreeRef::Subtree(subtree, *tt)),
2021-10-03 12:45:08 +00:00
Some(Entry::End(_)) | None => None,
2019-05-22 04:30:36 +00:00
}
}
2023-01-31 10:49:49 +00:00
fn create(buffer: &'a TokenBuffer<'_, Span>, ptr: EntryPtr) -> Cursor<'a, Span> {
2019-05-22 04:30:36 +00:00
Cursor { buffer, ptr }
}
/// Bump the cursor
2023-01-31 10:49:49 +00:00
pub fn bump(self) -> Cursor<'a, Span> {
2019-05-22 04:30:36 +00:00
if let Some(Entry::End(exit)) = self.buffer.entry(&self.ptr) {
match exit {
Some(exit) => Cursor::create(self.buffer, *exit),
None => self,
2019-05-22 04:30:36 +00:00
}
} else {
Cursor::create(self.buffer, EntryPtr(self.ptr.0, self.ptr.1 + 1))
}
}
2019-05-27 15:33:23 +00:00
/// Bump the cursor, if it is a subtree, returns
/// a cursor into that subtree
2023-01-31 10:49:49 +00:00
pub fn bump_subtree(self) -> Cursor<'a, Span> {
2019-05-27 15:33:23 +00:00
match self.entry() {
2023-02-03 20:39:24 +00:00
Some(&Entry::Subtree(_, _, entry_id)) => {
Cursor::create(self.buffer, EntryPtr(entry_id, 0))
}
Some(Entry::End(exit)) => match exit {
Some(exit) => Cursor::create(self.buffer, *exit),
None => self,
},
_ => Cursor::create(self.buffer, EntryPtr(self.ptr.0, self.ptr.1 + 1)),
2019-05-27 15:33:23 +00:00
}
}
/// Check whether it is a top level
pub fn is_root(&self) -> bool {
let entry_id = self.ptr.0;
2019-06-03 14:01:27 +00:00
entry_id.0 == 0
2019-05-27 15:33:23 +00:00
}
2019-05-22 04:30:36 +00:00
}