1 //! `tt` crate defines a `TokenTree` data structure: this is the interface (both
2 //! input and output) of macros. It closely mirrors `proc_macro` crate's
11 pub use smol_str::SmolStr;
13 /// Represents identity of the token.
15 /// For hygiene purposes, we need to track which expanded tokens originated from
16 /// which source tokens. We do it by assigning an distinct identity to each
17 /// source token and making sure that identities are preserved during macro
19 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
20 pub struct TokenId(pub u32);
23 pub const fn unspecified() -> TokenId {
28 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
33 impl_from!(Leaf, Subtree for TokenTree);
36 pub fn empty() -> Self {
37 TokenTree::Subtree(Subtree::default())
41 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
47 impl_from!(Literal, Punct, Ident for Leaf);
49 #[derive(Clone, PartialEq, Eq, Hash, Default)]
51 pub delimiter: Option<Delimiter>,
52 pub token_trees: Vec<TokenTree>,
55 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
56 pub struct Delimiter {
58 pub kind: DelimiterKind,
61 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
62 pub enum DelimiterKind {
68 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
74 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
81 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
87 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
93 fn print_debug_subtree(f: &mut fmt::Formatter<'_>, subtree: &Subtree, level: usize) -> fmt::Result {
94 let align = std::iter::repeat(" ").take(level).collect::<String>();
96 let aux = match subtree.delimiter.map(|it| (it.kind, it.id.0)) {
97 None => "$".to_string(),
98 Some((DelimiterKind::Parenthesis, id)) => format!("() {}", id),
99 Some((DelimiterKind::Brace, id)) => format!("{{}} {}", id),
100 Some((DelimiterKind::Bracket, id)) => format!("[] {}", id),
103 if subtree.token_trees.is_empty() {
104 write!(f, "{}SUBTREE {}", align, aux)?;
106 writeln!(f, "{}SUBTREE {}", align, aux)?;
107 for (idx, child) in subtree.token_trees.iter().enumerate() {
108 print_debug_token(f, child, level + 1)?;
109 if idx != subtree.token_trees.len() - 1 {
118 fn print_debug_token(f: &mut fmt::Formatter<'_>, tkn: &TokenTree, level: usize) -> fmt::Result {
119 let align = std::iter::repeat(" ").take(level).collect::<String>();
122 TokenTree::Leaf(leaf) => match leaf {
123 Leaf::Literal(lit) => write!(f, "{}LITERAL {} {}", align, lit.text, lit.id.0)?,
124 Leaf::Punct(punct) => write!(
126 "{}PUNCH {} [{}] {}",
129 if punct.spacing == Spacing::Alone { "alone" } else { "joint" },
132 Leaf::Ident(ident) => write!(f, "{}IDENT {} {}", align, ident.text, ident.id.0)?,
134 TokenTree::Subtree(subtree) => {
135 print_debug_subtree(f, subtree, level)?;
142 impl Debug for Subtree {
143 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
144 print_debug_subtree(f, self, 0)
148 impl fmt::Display for TokenTree {
149 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
151 TokenTree::Leaf(it) => fmt::Display::fmt(it, f),
152 TokenTree::Subtree(it) => fmt::Display::fmt(it, f),
157 impl fmt::Display for Subtree {
158 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
159 let (l, r) = match self.delimiter_kind() {
160 Some(DelimiterKind::Parenthesis) => ("(", ")"),
161 Some(DelimiterKind::Brace) => ("{", "}"),
162 Some(DelimiterKind::Bracket) => ("[", "]"),
166 let mut needs_space = false;
167 for tt in self.token_trees.iter() {
173 TokenTree::Leaf(Leaf::Punct(p)) => {
174 needs_space = p.spacing == Spacing::Alone;
175 fmt::Display::fmt(p, f)?
177 tt => fmt::Display::fmt(tt, f)?,
185 impl fmt::Display for Leaf {
186 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
188 Leaf::Ident(it) => fmt::Display::fmt(it, f),
189 Leaf::Literal(it) => fmt::Display::fmt(it, f),
190 Leaf::Punct(it) => fmt::Display::fmt(it, f),
195 impl fmt::Display for Ident {
196 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
197 fmt::Display::fmt(&self.text, f)
201 impl fmt::Display for Literal {
202 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
203 fmt::Display::fmt(&self.text, f)
207 impl fmt::Display for Punct {
208 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
209 fmt::Display::fmt(&self.char, f)
214 /// Count the number of tokens recursively
215 pub fn count(&self) -> usize {
216 let children_count = self
220 TokenTree::Subtree(c) => c.count(),
225 self.token_trees.len() + children_count
228 pub fn delimiter_kind(&self) -> Option<DelimiterKind> {
229 self.delimiter.map(|it| it.kind)
235 #[derive(Debug, PartialEq, Eq, Clone)]
236 pub enum ExpansionError {
240 ExpansionError(String),
243 impl fmt::Display for ExpansionError {
244 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
246 ExpansionError::IOError(e) => write!(f, "I/O error: {}", e),
247 ExpansionError::JsonError(e) => write!(f, "JSON decoding error: {}", e),
248 ExpansionError::Unknown(e) => write!(f, "{}", e),
249 ExpansionError::ExpansionError(e) => write!(f, "proc macro returned error: {}", e),
254 pub trait TokenExpander: Debug + Send + Sync + RefUnwindSafe {
255 fn expand(&self, subtree: &Subtree, attrs: Option<&Subtree>)
256 -> Result<Subtree, ExpansionError>;