2019-09-30 03:58:53 -05:00
|
|
|
//! `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`.
|
2022-07-20 07:59:42 -05:00
|
|
|
|
|
|
|
#![warn(rust_2018_idioms, unused_lifetimes, semicolon_in_expressions_from_macros)]
|
|
|
|
|
2020-12-11 07:24:02 -06:00
|
|
|
use std::fmt;
|
2019-01-31 08:16:02 -06:00
|
|
|
|
2020-07-13 09:16:53 -05:00
|
|
|
use stdx::impl_from;
|
2023-09-29 05:37:57 -05:00
|
|
|
use text_size::{TextRange, TextSize};
|
2020-07-13 09:16:53 -05:00
|
|
|
|
2020-03-26 11:41:44 -05:00
|
|
|
pub use smol_str::SmolStr;
|
2019-01-30 14:02:27 -06:00
|
|
|
|
2019-02-11 12:31:54 -06:00
|
|
|
/// Represents identity of the token.
|
|
|
|
///
|
|
|
|
/// For hygiene purposes, we need to track which expanded tokens originated from
|
|
|
|
/// which source tokens. We do it by assigning an distinct identity to each
|
|
|
|
/// source token and making sure that identities are preserved during macro
|
|
|
|
/// expansion.
|
2023-01-31 04:49:49 -06:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
|
2019-02-11 10:28:39 -06:00
|
|
|
pub struct TokenId(pub u32);
|
|
|
|
|
2023-01-31 04:49:49 -06:00
|
|
|
impl fmt::Debug for TokenId {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
self.0.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-11 10:28:39 -06:00
|
|
|
impl TokenId {
|
2023-01-31 04:49:49 -06:00
|
|
|
pub const UNSPECIFIED: TokenId = TokenId(!0);
|
2019-02-11 10:28:39 -06:00
|
|
|
pub const fn unspecified() -> TokenId {
|
2023-01-31 04:49:49 -06:00
|
|
|
Self::UNSPECIFIED
|
2019-02-11 10:28:39 -06:00
|
|
|
}
|
|
|
|
}
|
2019-01-30 14:02:27 -06:00
|
|
|
|
2023-09-29 05:37:57 -05:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
|
2023-10-06 07:47:11 -05:00
|
|
|
pub struct SpanData<Anchor, Ctx> {
|
2023-09-29 05:37:57 -05:00
|
|
|
/// The text range of this span, relative to the anchor.
|
2023-10-06 07:47:11 -05:00
|
|
|
/// We need the anchor for incrementality, as storing absolute ranges will require
|
|
|
|
/// recomputation on every change in a file at all times.
|
2023-09-29 05:37:57 -05:00
|
|
|
pub range: TextRange,
|
|
|
|
pub anchor: Anchor,
|
2023-10-06 07:47:11 -05:00
|
|
|
/// The syntax context of the span.
|
|
|
|
pub ctx: Ctx,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<Anchor: SpanAnchor, Ctx: SyntaxContext> Span for SpanData<Anchor, Ctx> {
|
|
|
|
const DUMMY: Self = SpanData {
|
|
|
|
range: TextRange::empty(TextSize::new(0)),
|
|
|
|
anchor: Anchor::DUMMY,
|
|
|
|
ctx: Ctx::DUMMY,
|
|
|
|
};
|
2023-06-29 04:12:48 -05:00
|
|
|
}
|
2023-07-13 02:17:07 -05:00
|
|
|
|
2023-10-06 07:47:11 -05:00
|
|
|
pub trait SpanAnchor: std::fmt::Debug + Copy + Sized + Eq {
|
|
|
|
const DUMMY: Self;
|
2020-03-13 07:03:31 -05:00
|
|
|
}
|
|
|
|
|
2023-09-29 05:37:57 -05:00
|
|
|
pub trait Span: std::fmt::Debug + Copy + Sized + Eq {
|
|
|
|
const DUMMY: Self;
|
|
|
|
}
|
2023-10-06 07:47:11 -05:00
|
|
|
|
|
|
|
pub trait SyntaxContext: std::fmt::Debug + Copy + Sized + Eq {
|
|
|
|
const DUMMY: Self;
|
|
|
|
}
|
2023-01-31 04:49:49 -06:00
|
|
|
|
2019-11-27 23:41:58 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2023-06-29 04:12:48 -05:00
|
|
|
pub enum TokenTree<S> {
|
|
|
|
Leaf(Leaf<S>),
|
|
|
|
Subtree(Subtree<S>),
|
|
|
|
}
|
|
|
|
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![] })
|
|
|
|
}
|
2019-01-30 14:02:27 -06:00
|
|
|
}
|
|
|
|
|
2023-01-31 04:49:49 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2023-06-29 04:12:48 -05:00
|
|
|
pub enum Leaf<S> {
|
|
|
|
Literal(Literal<S>),
|
|
|
|
Punct(Punct<S>),
|
|
|
|
Ident(Ident<S>),
|
2023-01-31 04:49:49 -06:00
|
|
|
}
|
|
|
|
|
2023-06-29 04:12:48 -05:00
|
|
|
impl<S> Leaf<S> {
|
|
|
|
pub fn span(&self) -> &S {
|
2023-01-31 04:49:49 -06:00
|
|
|
match self {
|
|
|
|
Leaf::Literal(it) => &it.span,
|
|
|
|
Leaf::Punct(it) => &it.span,
|
|
|
|
Leaf::Ident(it) => &it.span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-29 04:12:48 -05:00
|
|
|
impl_from!(Literal<S>, Punct<S>, Ident<S> for Leaf);
|
2023-01-31 04:49:49 -06:00
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
2023-06-29 04:12:48 -05:00
|
|
|
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),
|
|
|
|
})
|
|
|
|
}
|
2019-01-30 14:02:27 -06:00
|
|
|
}
|
|
|
|
|
2019-11-27 23:41:58 -06:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
2023-06-29 04:12:48 -05:00
|
|
|
pub struct Delimiter<S> {
|
|
|
|
pub open: S,
|
|
|
|
pub close: S,
|
2019-12-12 11:41:44 -06:00
|
|
|
pub kind: DelimiterKind,
|
|
|
|
}
|
|
|
|
|
2023-06-29 04:12:48 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-12 11:41:44 -06:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
|
|
|
pub enum DelimiterKind {
|
2019-01-30 14:02:27 -06:00
|
|
|
Parenthesis,
|
|
|
|
Brace,
|
|
|
|
Bracket,
|
2023-01-31 04:49:49 -06:00
|
|
|
Invisible,
|
2019-01-30 14:02:27 -06:00
|
|
|
}
|
|
|
|
|
2019-11-27 23:41:58 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2023-06-29 04:12:48 -05:00
|
|
|
pub struct Literal<S> {
|
2019-01-31 04:40:05 -06:00
|
|
|
pub text: SmolStr,
|
2023-06-29 04:12:48 -05:00
|
|
|
pub span: S,
|
2019-01-30 14:02:27 -06:00
|
|
|
}
|
|
|
|
|
2019-11-27 23:41:58 -06:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
2023-06-29 04:12:48 -05:00
|
|
|
pub struct Punct<S> {
|
2019-01-31 04:40:05 -06:00
|
|
|
pub char: char,
|
2019-01-31 09:51:17 -06:00
|
|
|
pub spacing: Spacing,
|
2023-06-29 04:12:48 -05:00
|
|
|
pub span: S,
|
2019-01-31 09:51:17 -06:00
|
|
|
}
|
|
|
|
|
2019-11-27 23:41:58 -06:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
2019-01-31 09:51:17 -06:00
|
|
|
pub enum Spacing {
|
|
|
|
Alone,
|
|
|
|
Joint,
|
2019-01-30 14:02:27 -06:00
|
|
|
}
|
|
|
|
|
2019-11-27 23:41:58 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2023-01-31 04:49:49 -06:00
|
|
|
/// Identifier or keyword. Unlike rustc, we keep "r#" prefix when it represents a raw identifier.
|
2023-06-29 04:12:48 -05:00
|
|
|
pub struct Ident<S> {
|
2019-01-31 04:40:05 -06:00
|
|
|
pub text: SmolStr,
|
2023-06-29 04:12:48 -05:00
|
|
|
pub span: S,
|
2019-01-30 14:02:27 -06:00
|
|
|
}
|
2019-01-31 08:16:02 -06:00
|
|
|
|
2023-05-03 05:44:47 -05:00
|
|
|
impl<S> Ident<S> {
|
|
|
|
pub fn new(text: impl Into<SmolStr>, span: S) -> Self {
|
|
|
|
Ident { text: text.into(), span }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-29 04:12:48 -05:00
|
|
|
fn print_debug_subtree<S: fmt::Debug>(
|
2023-01-31 04:49:49 -06:00
|
|
|
f: &mut fmt::Formatter<'_>,
|
2023-06-29 04:12:48 -05:00
|
|
|
subtree: &Subtree<S>,
|
2023-01-31 04:49:49 -06:00
|
|
|
level: usize,
|
|
|
|
) -> fmt::Result {
|
2021-06-12 23:07:28 -05:00
|
|
|
let align = " ".repeat(level);
|
2020-04-09 10:48:08 -05:00
|
|
|
|
2023-01-31 04:49:49 -06: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 10:48:08 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
if subtree.token_trees.is_empty() {
|
2022-12-23 12:42:58 -06:00
|
|
|
write!(f, "{align}SUBTREE {aux}")?;
|
2020-04-09 10:48:08 -05:00
|
|
|
} else {
|
2022-12-23 12:42:58 -06:00
|
|
|
writeln!(f, "{align}SUBTREE {aux}")?;
|
2020-04-09 10:48:08 -05: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 07:05:01 -05:00
|
|
|
writeln!(f)?;
|
2020-04-09 10:48:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-06-29 04:12:48 -05:00
|
|
|
fn print_debug_token<S: fmt::Debug>(
|
2023-01-31 04:49:49 -06:00
|
|
|
f: &mut fmt::Formatter<'_>,
|
2023-06-29 04:12:48 -05:00
|
|
|
tkn: &TokenTree<S>,
|
2023-01-31 04:49:49 -06:00
|
|
|
level: usize,
|
|
|
|
) -> fmt::Result {
|
2021-06-12 23:07:28 -05:00
|
|
|
let align = " ".repeat(level);
|
2020-04-09 10:48:08 -05:00
|
|
|
|
|
|
|
match tkn {
|
|
|
|
TokenTree::Leaf(leaf) => match leaf {
|
2023-01-31 04:49:49 -06:00
|
|
|
Leaf::Literal(lit) => write!(f, "{}LITERAL {} {:?}", align, lit.text, lit.span)?,
|
2020-04-09 10:48:08 -05:00
|
|
|
Leaf::Punct(punct) => write!(
|
|
|
|
f,
|
2023-01-31 04:49:49 -06:00
|
|
|
"{}PUNCH {} [{}] {:?}",
|
2020-04-09 10:48:08 -05:00
|
|
|
align,
|
|
|
|
punct.char,
|
|
|
|
if punct.spacing == Spacing::Alone { "alone" } else { "joint" },
|
2023-01-31 04:49:49 -06:00
|
|
|
punct.span
|
2020-04-09 10:48:08 -05:00
|
|
|
)?,
|
2023-01-31 04:49:49 -06:00
|
|
|
Leaf::Ident(ident) => write!(f, "{}IDENT {} {:?}", align, ident.text, ident.span)?,
|
2020-04-09 10:48:08 -05:00
|
|
|
},
|
|
|
|
TokenTree::Subtree(subtree) => {
|
|
|
|
print_debug_subtree(f, subtree, level)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-06-29 04:12:48 -05:00
|
|
|
impl<S: fmt::Debug> fmt::Debug for Subtree<S> {
|
2020-04-09 10:48:08 -05:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
print_debug_subtree(f, self, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-29 04:12:48 -05:00
|
|
|
impl<S> fmt::Display for TokenTree<S> {
|
2022-07-20 08:02:08 -05:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-01-31 08:16:02 -06:00
|
|
|
match self {
|
|
|
|
TokenTree::Leaf(it) => fmt::Display::fmt(it, f),
|
|
|
|
TokenTree::Subtree(it) => fmt::Display::fmt(it, f),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-29 04:12:48 -05:00
|
|
|
impl<S> fmt::Display for Subtree<S> {
|
2022-07-20 08:02:08 -05:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2023-01-31 04:49:49 -06:00
|
|
|
let (l, r) = match self.delimiter.kind {
|
|
|
|
DelimiterKind::Parenthesis => ("(", ")"),
|
|
|
|
DelimiterKind::Brace => ("{", "}"),
|
|
|
|
DelimiterKind::Bracket => ("[", "]"),
|
|
|
|
DelimiterKind::Invisible => ("", ""),
|
2019-01-31 08:16:02 -06:00
|
|
|
};
|
2019-01-31 09:51:17 -06:00
|
|
|
f.write_str(l)?;
|
|
|
|
let mut needs_space = false;
|
2021-10-03 07:45:08 -05:00
|
|
|
for tt in &self.token_trees {
|
2019-01-31 09:51:17 -06:00
|
|
|
if needs_space {
|
|
|
|
f.write_str(" ")?;
|
|
|
|
}
|
|
|
|
needs_space = true;
|
|
|
|
match tt {
|
|
|
|
TokenTree::Leaf(Leaf::Punct(p)) => {
|
|
|
|
needs_space = p.spacing == Spacing::Alone;
|
2021-10-03 07:39:43 -05:00
|
|
|
fmt::Display::fmt(p, f)?;
|
2019-01-31 09:51:17 -06:00
|
|
|
}
|
|
|
|
tt => fmt::Display::fmt(tt, f)?,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f.write_str(r)?;
|
|
|
|
Ok(())
|
2019-01-31 08:16:02 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-29 04:12:48 -05:00
|
|
|
impl<S> fmt::Display for Leaf<S> {
|
2022-07-20 08:02:08 -05:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-01-31 08:16:02 -06: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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-29 04:12:48 -05:00
|
|
|
impl<S> fmt::Display for Ident<S> {
|
2022-07-20 08:02:08 -05:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-01-31 08:16:02 -06:00
|
|
|
fmt::Display::fmt(&self.text, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-29 04:12:48 -05:00
|
|
|
impl<S> fmt::Display for Literal<S> {
|
2022-07-20 08:02:08 -05:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-01-31 08:16:02 -06:00
|
|
|
fmt::Display::fmt(&self.text, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-29 04:12:48 -05:00
|
|
|
impl<S> fmt::Display for Punct<S> {
|
2022-07-20 08:02:08 -05:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-01-31 08:16:02 -06:00
|
|
|
fmt::Display::fmt(&self.char, f)
|
|
|
|
}
|
|
|
|
}
|
2019-04-22 02:33:55 -05:00
|
|
|
|
2023-06-29 04:12:48 -05:00
|
|
|
impl<S> Subtree<S> {
|
2019-04-22 02:33:55 -05:00
|
|
|
/// Count the number of tokens recursively
|
|
|
|
pub fn count(&self) -> usize {
|
2019-04-22 04:37:27 -05:00
|
|
|
let children_count = self
|
|
|
|
.token_trees
|
|
|
|
.iter()
|
|
|
|
.map(|c| match c {
|
2019-04-22 02:33:55 -05:00
|
|
|
TokenTree::Subtree(c) => c.count(),
|
2021-10-03 07:45:08 -05:00
|
|
|
TokenTree::Leaf(_) => 0,
|
2019-04-22 04:37:27 -05:00
|
|
|
})
|
|
|
|
.sum::<usize>();
|
|
|
|
|
|
|
|
self.token_trees.len() + children_count
|
2019-04-22 02:33:55 -05:00
|
|
|
}
|
|
|
|
}
|
2019-05-21 23:30:36 -05:00
|
|
|
|
2023-06-29 04:12:48 -05:00
|
|
|
impl<S> Subtree<S> {
|
2021-03-13 06:14:21 -06:00
|
|
|
/// A simple line string used for debugging
|
|
|
|
pub fn as_debug_string(&self) -> String {
|
2023-01-31 04:49:49 -06:00
|
|
|
let delim = match self.delimiter.kind {
|
|
|
|
DelimiterKind::Brace => ("{", "}"),
|
|
|
|
DelimiterKind::Bracket => ("[", "]"),
|
|
|
|
DelimiterKind::Parenthesis => ("(", ")"),
|
|
|
|
DelimiterKind::Invisible => ("$", "$"),
|
2021-03-13 06:14:21 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut res = String::new();
|
|
|
|
res.push_str(delim.0);
|
|
|
|
let mut last = None;
|
2021-03-21 09:33:18 -05:00
|
|
|
for child in &self.token_trees {
|
2021-03-13 06:14:21 -06: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_string() + &s
|
|
|
|
}
|
2023-01-31 04:49:49 -06:00
|
|
|
(Leaf::Punct(_), Some(TokenTree::Leaf(Leaf::Punct(punct)))) => {
|
2021-03-13 06:14:21 -06:00
|
|
|
if punct.spacing == Spacing::Alone {
|
|
|
|
" ".to_string() + &s
|
|
|
|
} 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-21 23:30:36 -05:00
|
|
|
pub mod buffer;
|
2021-09-27 12:02:03 -05:00
|
|
|
|
2023-06-29 04:12:48 -05:00
|
|
|
pub fn pretty<S>(tkns: &[TokenTree<S>]) -> String {
|
|
|
|
fn tokentree_to_text<S>(tkn: &TokenTree<S>) -> String {
|
2021-09-27 12:02:03 -05:00
|
|
|
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 04:49:49 -06:00
|
|
|
let (open, close) = match subtree.delimiter.kind {
|
|
|
|
DelimiterKind::Brace => ("{", "}"),
|
|
|
|
DelimiterKind::Bracket => ("[", "]"),
|
|
|
|
DelimiterKind::Parenthesis => ("(", ")"),
|
|
|
|
DelimiterKind::Invisible => ("", ""),
|
2021-09-27 12:02:03 -05:00
|
|
|
};
|
2022-12-23 12:42:58 -06:00
|
|
|
format!("{open}{content}{close}")
|
2021-09-27 12:02:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|