Infect mbe crate with generic span type parameter

This commit is contained in:
Lukas Wirth 2023-06-29 11:12:48 +02:00
parent 2ee17bc5f2
commit 83f91f61b1
10 changed files with 362 additions and 327 deletions

View file

@ -47,70 +47,44 @@ pub mod token_id {
pub type Cursor<'a> = crate::buffer::Cursor<'a, super::Span>;
pub type TokenTreeRef<'a> = crate::buffer::TokenTreeRef<'a, super::Span>;
}
}
impl Delimiter {
pub const UNSPECIFIED: Self = Self {
open: TokenId::UNSPECIFIED,
close: TokenId::UNSPECIFIED,
kind: DelimiterKind::Invisible,
};
pub const fn unspecified() -> Self {
Self::UNSPECIFIED
}
}
impl Subtree {
pub const fn empty() -> Self {
Subtree { delimiter: Delimiter::unspecified(), token_trees: vec![] }
}
}
impl TokenTree {
pub const fn empty() -> Self {
Self::Subtree(Subtree::empty())
}
}
pub trait Span: std::fmt::Debug + Copy + Sized {
const DUMMY: Self;
fn is_dummy(&self) -> bool;
}
impl Span for TokenId {
const DUMMY: Self = TokenId(!0);
impl Subtree {
pub fn visit_ids(&mut self, f: &mut impl FnMut(TokenId) -> TokenId) {
self.delimiter.open = f(self.delimiter.open);
self.delimiter.close = f(self.delimiter.close);
self.token_trees.iter_mut().for_each(|tt| match tt {
crate::TokenTree::Leaf(leaf) => match leaf {
crate::Leaf::Literal(it) => it.span = f(it.span),
crate::Leaf::Punct(it) => it.span = f(it.span),
crate::Leaf::Ident(it) => it.span = f(it.span),
},
crate::TokenTree::Subtree(s) => s.visit_ids(f),
})
}
fn is_dummy(&self) -> bool {
*self == Self::DUMMY
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SyntaxContext(pub u32);
// #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
// pub struct Span {
// pub id: TokenId,
// pub ctx: SyntaxContext,
// }
// pub type Span = (TokenId, SyntaxContext);
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum TokenTree<Span> {
Leaf(Leaf<Span>),
Subtree(Subtree<Span>),
pub enum TokenTree<S> {
Leaf(Leaf<S>),
Subtree(Subtree<S>),
}
impl_from!(Leaf<Span>, Subtree<Span> for TokenTree);
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Leaf<Span> {
Literal(Literal<Span>),
Punct(Punct<Span>),
Ident(Ident<Span>),
impl_from!(Leaf<S>, Subtree<S> for TokenTree);
impl<S: Span> TokenTree<S> {
pub const fn empty() -> Self {
Self::Subtree(Subtree { delimiter: Delimiter::unspecified(), token_trees: vec![] })
}
}
impl<Span> Leaf<Span> {
pub fn span(&self) -> &Span {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Leaf<S> {
Literal(Literal<S>),
Punct(Punct<S>),
Ident(Ident<S>),
}
impl<S> Leaf<S> {
pub fn span(&self) -> &S {
match self {
Leaf::Literal(it) => &it.span,
Leaf::Punct(it) => &it.span,
@ -118,21 +92,49 @@ impl<Span> Leaf<Span> {
}
}
}
impl_from!(Literal<Span>, Punct<Span>, Ident<Span> for Leaf);
impl_from!(Literal<S>, Punct<S>, Ident<S> for Leaf);
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Subtree<Span> {
pub delimiter: Delimiter<Span>,
pub token_trees: Vec<TokenTree<Span>>,
pub struct Subtree<S> {
pub delimiter: Delimiter<S>,
pub token_trees: Vec<TokenTree<S>>,
}
impl<S: Span> Subtree<S> {
pub const fn empty() -> Self {
Subtree { delimiter: Delimiter::unspecified(), token_trees: vec![] }
}
pub fn visit_ids(&mut self, f: &mut impl FnMut(S) -> S) {
self.delimiter.open = f(self.delimiter.open);
self.delimiter.close = f(self.delimiter.close);
self.token_trees.iter_mut().for_each(|tt| match tt {
crate::TokenTree::Leaf(leaf) => match leaf {
crate::Leaf::Literal(it) => it.span = f(it.span),
crate::Leaf::Punct(it) => it.span = f(it.span),
crate::Leaf::Ident(it) => it.span = f(it.span),
},
crate::TokenTree::Subtree(s) => s.visit_ids(f),
})
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Delimiter<Span> {
pub open: Span,
pub close: Span,
pub struct Delimiter<S> {
pub open: S,
pub close: S,
pub kind: DelimiterKind,
}
impl<S: Span> Delimiter<S> {
pub const UNSPECIFIED: Self =
Self { open: S::DUMMY, close: S::DUMMY, kind: DelimiterKind::Invisible };
pub const fn unspecified() -> Self {
Self::UNSPECIFIED
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum DelimiterKind {
Parenthesis,
@ -142,16 +144,16 @@ pub enum DelimiterKind {
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Literal<Span> {
pub struct Literal<S> {
pub text: SmolStr,
pub span: Span,
pub span: S,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Punct<Span> {
pub struct Punct<S> {
pub char: char,
pub spacing: Spacing,
pub span: Span,
pub span: S,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@ -162,9 +164,9 @@ pub enum Spacing {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
/// Identifier or keyword. Unlike rustc, we keep "r#" prefix when it represents a raw identifier.
pub struct Ident<Span> {
pub struct Ident<S> {
pub text: SmolStr,
pub span: Span,
pub span: S,
}
impl<S> Ident<S> {
@ -173,9 +175,9 @@ impl<S> Ident<S> {
}
}
fn print_debug_subtree<Span: fmt::Debug>(
fn print_debug_subtree<S: fmt::Debug>(
f: &mut fmt::Formatter<'_>,
subtree: &Subtree<Span>,
subtree: &Subtree<S>,
level: usize,
) -> fmt::Result {
let align = " ".repeat(level);
@ -203,9 +205,9 @@ fn print_debug_subtree<Span: fmt::Debug>(
Ok(())
}
fn print_debug_token<Span: fmt::Debug>(
fn print_debug_token<S: fmt::Debug>(
f: &mut fmt::Formatter<'_>,
tkn: &TokenTree<Span>,
tkn: &TokenTree<S>,
level: usize,
) -> fmt::Result {
let align = " ".repeat(level);
@ -231,13 +233,13 @@ fn print_debug_token<Span: fmt::Debug>(
Ok(())
}
impl<Span: fmt::Debug> fmt::Debug for Subtree<Span> {
impl<S: fmt::Debug> fmt::Debug for Subtree<S> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
print_debug_subtree(f, self, 0)
}
}
impl<Span> fmt::Display for TokenTree<Span> {
impl<S> fmt::Display for TokenTree<S> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
TokenTree::Leaf(it) => fmt::Display::fmt(it, f),
@ -246,7 +248,7 @@ impl<Span> fmt::Display for TokenTree<Span> {
}
}
impl<Span> fmt::Display for Subtree<Span> {
impl<S> fmt::Display for Subtree<S> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (l, r) = match self.delimiter.kind {
DelimiterKind::Parenthesis => ("(", ")"),
@ -274,7 +276,7 @@ impl<Span> fmt::Display for Subtree<Span> {
}
}
impl<Span> fmt::Display for Leaf<Span> {
impl<S> fmt::Display for Leaf<S> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Leaf::Ident(it) => fmt::Display::fmt(it, f),
@ -284,25 +286,25 @@ impl<Span> fmt::Display for Leaf<Span> {
}
}
impl<Span> fmt::Display for Ident<Span> {
impl<S> fmt::Display for Ident<S> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.text, f)
}
}
impl<Span> fmt::Display for Literal<Span> {
impl<S> fmt::Display for Literal<S> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.text, f)
}
}
impl<Span> fmt::Display for Punct<Span> {
impl<S> fmt::Display for Punct<S> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.char, f)
}
}
impl<Span> Subtree<Span> {
impl<S> Subtree<S> {
/// Count the number of tokens recursively
pub fn count(&self) -> usize {
let children_count = self
@ -318,7 +320,7 @@ impl<Span> Subtree<Span> {
}
}
impl<Span> Subtree<Span> {
impl<S> Subtree<S> {
/// A simple line string used for debugging
pub fn as_debug_string(&self) -> String {
let delim = match self.delimiter.kind {
@ -366,8 +368,8 @@ impl<Span> Subtree<Span> {
pub mod buffer;
pub fn pretty<Span>(tkns: &[TokenTree<Span>]) -> String {
fn tokentree_to_text<Span>(tkn: &TokenTree<Span>) -> String {
pub fn pretty<S>(tkns: &[TokenTree<S>]) -> String {
fn tokentree_to_text<S>(tkn: &TokenTree<S>) -> String {
match tkn {
TokenTree::Leaf(Leaf::Ident(ident)) => ident.text.clone().into(),
TokenTree::Leaf(Leaf::Literal(literal)) => literal.text.clone().into(),