rust/crates/tt/src/lib.rs

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

397 lines
12 KiB
Rust
Raw Normal View History

//! `tt` crate defines a `TokenTree` data structure: this is the interface (both
//! input and output) of macros. It closely mirrors `proc_macro` crate's
//! `TokenTree`.
#![warn(rust_2018_idioms, unused_lifetimes)]
use std::fmt;
2019-01-31 17:16:02 +03:00
use stdx::impl_from;
2020-03-27 00:41:44 +08:00
pub use smol_str::SmolStr;
2023-11-25 15:10:31 +01:00
pub use text_size::{TextRange, TextSize};
2019-01-30 23:02:27 +03:00
2023-12-20 14:02:40 +01:00
pub trait Span: std::fmt::Debug + Copy + Sized + Eq {}
2023-12-20 14:02:40 +01:00
impl<Ctx> Span for span::SpanData<Ctx> where span::SpanData<Ctx>: std::fmt::Debug + Copy + Sized + Eq
{}
2023-01-31 11:49:49 +01:00
2019-11-28 13:41:58 +08:00
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum TokenTree<S> {
Leaf(Leaf<S>),
Subtree(Subtree<S>),
}
impl_from!(Leaf<S>, Subtree<S> for TokenTree);
impl<S: Span> TokenTree<S> {
pub fn empty(span: S) -> Self {
Self::Subtree(Subtree {
delimiter: Delimiter::invisible_spanned(span),
token_trees: Box::new([]),
})
2023-11-17 19:07:31 +01:00
}
2023-12-20 14:00:14 +01:00
pub fn subtree_or_wrap(self, span: DelimSpan<S>) -> Subtree<S> {
2023-11-17 19:07:31 +01:00
match self {
2023-12-20 14:00:14 +01:00
TokenTree::Leaf(_) => Subtree {
delimiter: Delimiter::invisible_delim_spanned(span),
token_trees: Box::new([self]),
2023-12-20 14:00:14 +01:00
},
2023-11-17 19:07:31 +01:00
TokenTree::Subtree(s) => s,
}
}
2023-12-20 13:06:46 +01:00
pub fn first_span(&self) -> S {
match self {
2023-12-20 13:06:46 +01:00
TokenTree::Leaf(l) => *l.span(),
TokenTree::Subtree(s) => s.delimiter.open,
}
}
2019-01-30 23:02:27 +03:00
}
2023-01-31 11:49:49 +01:00
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Leaf<S> {
Literal(Literal<S>),
Punct(Punct<S>),
Ident(Ident<S>),
2023-01-31 11:49:49 +01:00
}
impl<S> Leaf<S> {
pub fn span(&self) -> &S {
2023-01-31 11:49:49 +01:00
match self {
Leaf::Literal(it) => &it.span,
Leaf::Punct(it) => &it.span,
Leaf::Ident(it) => &it.span,
}
}
}
impl_from!(Literal<S>, Punct<S>, Ident<S> for Leaf);
2023-01-31 11:49:49 +01:00
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Subtree<S> {
pub delimiter: Delimiter<S>,
pub token_trees: Box<[TokenTree<S>]>,
}
impl<S: Span> Subtree<S> {
pub fn empty(span: DelimSpan<S>) -> Self {
Subtree { delimiter: Delimiter::invisible_delim_spanned(span), token_trees: Box::new([]) }
}
/// This is slow, and should be avoided, as it will always reallocate!
pub fn push(&mut self, subtree: TokenTree<S>) {
let mut mutable_trees = std::mem::take(&mut self.token_trees).into_vec();
// Reserve exactly space for one element, to avoid `into_boxed_slice` having to reallocate again.
mutable_trees.reserve_exact(1);
mutable_trees.push(subtree);
self.token_trees = mutable_trees.into_boxed_slice();
}
2019-01-30 23:02:27 +03:00
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct SubtreeBuilder<S> {
pub delimiter: Delimiter<S>,
pub token_trees: Vec<TokenTree<S>>,
}
impl<S> SubtreeBuilder<S> {
pub fn build(self) -> Subtree<S> {
Subtree { delimiter: self.delimiter, token_trees: self.token_trees.into_boxed_slice() }
}
2019-01-30 23:02:27 +03:00
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct DelimSpan<S> {
pub open: S,
pub close: S,
}
2019-11-28 13:41:58 +08:00
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Delimiter<S> {
pub open: S,
pub close: S,
2019-12-13 01:41:44 +08:00
pub kind: DelimiterKind,
}
impl<S: Span> Delimiter<S> {
pub const fn invisible_spanned(span: S) -> Self {
Delimiter { open: span, close: span, kind: DelimiterKind::Invisible }
}
pub const fn invisible_delim_spanned(span: DelimSpan<S>) -> Self {
Delimiter { open: span.open, close: span.close, kind: DelimiterKind::Invisible }
}
pub fn delim_span(&self) -> DelimSpan<S> {
DelimSpan { open: self.open, close: self.close }
}
}
2019-12-13 01:41:44 +08:00
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum DelimiterKind {
2019-01-30 23:02:27 +03:00
Parenthesis,
Brace,
Bracket,
2023-01-31 11:49:49 +01:00
Invisible,
2019-01-30 23:02:27 +03:00
}
2019-11-28 13:41:58 +08:00
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Literal<S> {
2019-01-31 13:40:05 +03:00
pub text: SmolStr,
pub span: S,
2019-01-30 23:02:27 +03:00
}
2019-11-28 13:41:58 +08:00
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Punct<S> {
2019-01-31 13:40:05 +03:00
pub char: char,
2019-01-31 18:51:17 +03:00
pub spacing: Spacing,
pub span: S,
2019-01-31 18:51:17 +03:00
}
2019-11-28 13:41:58 +08:00
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2019-01-31 18:51:17 +03:00
pub enum Spacing {
Alone,
/// Whether the following token is joint to the current one.
2019-01-31 18:51:17 +03:00
Joint,
2019-01-30 23:02:27 +03:00
}
2019-11-28 13:41:58 +08:00
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2023-01-31 11:49:49 +01:00
/// Identifier or keyword. Unlike rustc, we keep "r#" prefix when it represents a raw identifier.
pub struct Ident<S> {
2019-01-31 13:40:05 +03:00
pub text: SmolStr,
pub span: S,
2019-01-30 23:02:27 +03:00
}
2019-01-31 17:16:02 +03:00
impl<S> Ident<S> {
pub fn new(text: impl Into<SmolStr>, span: S) -> Self {
Ident { text: text.into(), span }
}
}
fn print_debug_subtree<S: fmt::Debug>(
2023-01-31 11:49:49 +01:00
f: &mut fmt::Formatter<'_>,
subtree: &Subtree<S>,
2023-01-31 11:49:49 +01:00
level: usize,
) -> fmt::Result {
2021-06-13 09:37:28 +05:30
let align = " ".repeat(level);
2020-04-09 23:48:08 +08:00
2023-01-31 11:49:49 +01:00
let Delimiter { kind, open, close } = &subtree.delimiter;
let aux = match kind {
DelimiterKind::Invisible => format!("$$ {:?} {:?}", open, close),
DelimiterKind::Parenthesis => format!("() {:?} {:?}", open, close),
DelimiterKind::Brace => format!("{{}} {:?} {:?}", open, close),
DelimiterKind::Bracket => format!("[] {:?} {:?}", open, close),
2020-04-09 23:48:08 +08:00
};
if subtree.token_trees.is_empty() {
write!(f, "{align}SUBTREE {aux}")?;
2020-04-09 23:48:08 +08:00
} else {
writeln!(f, "{align}SUBTREE {aux}")?;
2020-04-09 23:48:08 +08:00
for (idx, child) in subtree.token_trees.iter().enumerate() {
print_debug_token(f, child, level + 1)?;
if idx != subtree.token_trees.len() - 1 {
2020-08-10 15:05:01 +03:00
writeln!(f)?;
2020-04-09 23:48:08 +08:00
}
}
}
Ok(())
}
fn print_debug_token<S: fmt::Debug>(
2023-01-31 11:49:49 +01:00
f: &mut fmt::Formatter<'_>,
tkn: &TokenTree<S>,
2023-01-31 11:49:49 +01:00
level: usize,
) -> fmt::Result {
2021-06-13 09:37:28 +05:30
let align = " ".repeat(level);
2020-04-09 23:48:08 +08:00
match tkn {
TokenTree::Leaf(leaf) => match leaf {
2023-01-31 11:49:49 +01:00
Leaf::Literal(lit) => write!(f, "{}LITERAL {} {:?}", align, lit.text, lit.span)?,
2020-04-09 23:48:08 +08:00
Leaf::Punct(punct) => write!(
f,
2023-01-31 11:49:49 +01:00
"{}PUNCH {} [{}] {:?}",
2020-04-09 23:48:08 +08:00
align,
punct.char,
if punct.spacing == Spacing::Alone { "alone" } else { "joint" },
2023-01-31 11:49:49 +01:00
punct.span
2020-04-09 23:48:08 +08:00
)?,
2023-01-31 11:49:49 +01:00
Leaf::Ident(ident) => write!(f, "{}IDENT {} {:?}", align, ident.text, ident.span)?,
2020-04-09 23:48:08 +08:00
},
TokenTree::Subtree(subtree) => {
print_debug_subtree(f, subtree, level)?;
}
}
Ok(())
}
impl<S: fmt::Debug> fmt::Debug for Subtree<S> {
2020-04-09 23:48:08 +08:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
print_debug_subtree(f, self, 0)
}
}
impl<S> fmt::Display for TokenTree<S> {
2022-07-20 15:02:08 +02:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2019-01-31 17:16:02 +03:00
match self {
TokenTree::Leaf(it) => fmt::Display::fmt(it, f),
TokenTree::Subtree(it) => fmt::Display::fmt(it, f),
}
}
}
impl<S> fmt::Display for Subtree<S> {
2022-07-20 15:02:08 +02:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2023-01-31 11:49:49 +01:00
let (l, r) = match self.delimiter.kind {
DelimiterKind::Parenthesis => ("(", ")"),
DelimiterKind::Brace => ("{", "}"),
DelimiterKind::Bracket => ("[", "]"),
DelimiterKind::Invisible => ("", ""),
2019-01-31 17:16:02 +03:00
};
2019-01-31 18:51:17 +03:00
f.write_str(l)?;
let mut needs_space = false;
for tt in self.token_trees.iter() {
2019-01-31 18:51:17 +03:00
if needs_space {
f.write_str(" ")?;
}
needs_space = true;
match tt {
TokenTree::Leaf(Leaf::Punct(p)) => {
needs_space = p.spacing == Spacing::Alone;
fmt::Display::fmt(p, f)?;
2019-01-31 18:51:17 +03:00
}
tt => fmt::Display::fmt(tt, f)?,
}
}
f.write_str(r)?;
Ok(())
2019-01-31 17:16:02 +03:00
}
}
impl<S> fmt::Display for Leaf<S> {
2022-07-20 15:02:08 +02:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2019-01-31 17:16:02 +03:00
match self {
Leaf::Ident(it) => fmt::Display::fmt(it, f),
Leaf::Literal(it) => fmt::Display::fmt(it, f),
Leaf::Punct(it) => fmt::Display::fmt(it, f),
}
}
}
impl<S> fmt::Display for Ident<S> {
2022-07-20 15:02:08 +02:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2019-01-31 17:16:02 +03:00
fmt::Display::fmt(&self.text, f)
}
}
impl<S> fmt::Display for Literal<S> {
2022-07-20 15:02:08 +02:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2019-01-31 17:16:02 +03:00
fmt::Display::fmt(&self.text, f)
}
}
impl<S> fmt::Display for Punct<S> {
2022-07-20 15:02:08 +02:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2019-01-31 17:16:02 +03:00
fmt::Display::fmt(&self.char, f)
}
}
impl<S> Subtree<S> {
/// Count the number of tokens recursively
pub fn count(&self) -> usize {
let children_count = self
.token_trees
.iter()
.map(|c| match c {
TokenTree::Subtree(c) => c.count(),
2021-10-03 23:45:08 +11:00
TokenTree::Leaf(_) => 0,
})
.sum::<usize>();
self.token_trees.len() + children_count
}
}
2019-05-22 12:30:36 +08:00
impl<S> Subtree<S> {
2021-03-13 20:14:21 +08:00
/// A simple line string used for debugging
pub fn as_debug_string(&self) -> String {
2023-01-31 11:49:49 +01:00
let delim = match self.delimiter.kind {
DelimiterKind::Brace => ("{", "}"),
DelimiterKind::Bracket => ("[", "]"),
DelimiterKind::Parenthesis => ("(", ")"),
DelimiterKind::Invisible => ("$", "$"),
2021-03-13 20:14:21 +08:00
};
let mut res = String::new();
res.push_str(delim.0);
let mut last = None;
for child in self.token_trees.iter() {
2021-03-13 20:14:21 +08:00
let s = match child {
TokenTree::Leaf(it) => {
let s = match it {
Leaf::Literal(it) => it.text.to_string(),
Leaf::Punct(it) => it.char.to_string(),
Leaf::Ident(it) => it.text.to_string(),
};
match (it, last) {
(Leaf::Ident(_), Some(&TokenTree::Leaf(Leaf::Ident(_)))) => {
" ".to_owned() + &s
2021-03-13 20:14:21 +08:00
}
2023-01-31 11:49:49 +01:00
(Leaf::Punct(_), Some(TokenTree::Leaf(Leaf::Punct(punct)))) => {
2021-03-13 20:14:21 +08:00
if punct.spacing == Spacing::Alone {
" ".to_owned() + &s
2021-03-13 20:14:21 +08:00
} else {
s
}
}
_ => s,
}
}
TokenTree::Subtree(it) => it.as_debug_string(),
};
res.push_str(&s);
last = Some(child);
}
res.push_str(delim.1);
res
}
}
2019-05-22 12:30:36 +08:00
pub mod buffer;
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(),
TokenTree::Leaf(Leaf::Punct(punct)) => format!("{}", punct.char),
TokenTree::Subtree(subtree) => {
let content = pretty(&subtree.token_trees);
2023-01-31 11:49:49 +01:00
let (open, close) = match subtree.delimiter.kind {
DelimiterKind::Brace => ("{", "}"),
DelimiterKind::Bracket => ("[", "]"),
DelimiterKind::Parenthesis => ("(", ")"),
DelimiterKind::Invisible => ("", ""),
};
format!("{open}{content}{close}")
}
}
}
tkns.iter()
.fold((String::new(), true), |(last, last_to_joint), tkn| {
let s = [last, tokentree_to_text(tkn)].join(if last_to_joint { "" } else { " " });
let mut is_joint = false;
if let TokenTree::Leaf(Leaf::Punct(punct)) = tkn {
if punct.spacing == Spacing::Joint {
is_joint = true;
}
}
(s, is_joint)
})
.0
}