rust/crates/tt/src/lib.rs

333 lines
9.6 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, semicolon_in_expressions_from_macros)]
use std::fmt;
2019-01-31 08:16:02 -06:00
use stdx::impl_from;
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.
2019-02-11 10:28:39 -06:00
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TokenId(pub u32);
impl TokenId {
pub const fn unspecified() -> TokenId {
TokenId(!0)
}
}
2019-11-27 23:41:58 -06:00
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2019-01-31 04:40:05 -06:00
pub enum TokenTree {
2019-01-30 14:02:27 -06:00
Leaf(Leaf),
Subtree(Subtree),
}
impl_from!(Leaf, Subtree for TokenTree);
2019-01-30 14:02:27 -06:00
impl TokenTree {
pub fn empty() -> Self {
TokenTree::Subtree(Subtree::default())
}
}
2019-11-27 23:41:58 -06:00
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2019-01-31 04:40:05 -06:00
pub enum Leaf {
2019-01-30 14:02:27 -06:00
Literal(Literal),
Punct(Punct),
Ident(Ident),
}
impl_from!(Literal, Punct, Ident for Leaf);
2019-01-30 14:02:27 -06:00
2020-04-09 10:48:08 -05:00
#[derive(Clone, PartialEq, Eq, Hash, Default)]
2019-01-31 04:40:05 -06:00
pub struct Subtree {
2019-12-13 07:53:34 -06:00
pub delimiter: Option<Delimiter>,
2019-01-31 04:40:05 -06:00
pub token_trees: Vec<TokenTree>,
2019-01-30 14:02:27 -06:00
}
2019-11-27 23:41:58 -06:00
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
2019-12-12 11:41:44 -06:00
pub struct Delimiter {
pub id: TokenId,
pub kind: DelimiterKind,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum DelimiterKind {
2019-01-30 14:02:27 -06:00
Parenthesis,
Brace,
Bracket,
}
2019-11-27 23:41:58 -06:00
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2019-01-31 04:40:05 -06:00
pub struct Literal {
pub text: SmolStr,
2019-12-12 07:47:54 -06:00
pub id: TokenId,
2019-01-30 14:02:27 -06:00
}
2019-11-27 23:41:58 -06:00
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2019-01-31 04:40:05 -06:00
pub struct Punct {
pub char: char,
2019-01-31 09:51:17 -06:00
pub spacing: Spacing,
2019-12-12 07:47:54 -06:00
pub id: TokenId,
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)]
2019-01-31 04:40:05 -06:00
pub struct Ident {
/// Identifier or keyword. Unlike rustc, we keep "r#" prefix when it represents a raw identifier.
2019-01-31 04:40:05 -06:00
pub text: SmolStr,
2019-02-11 10:28:39 -06:00
pub id: TokenId,
2019-01-30 14:02:27 -06:00
}
2019-01-31 08:16:02 -06:00
impl Ident {
/// Constructor intended to be used only by proc macro server. `text` should not contain raw
/// identifier prefix.
pub fn new_with_is_raw(text: SmolStr, id: TokenId, is_raw: bool) -> Self {
let text = if is_raw { SmolStr::from_iter(["r#", &text]) } else { text };
Ident { text, id }
}
}
impl Leaf {
pub fn id(&self) -> TokenId {
match self {
Leaf::Literal(l) => l.id,
Leaf::Punct(p) => p.id,
Leaf::Ident(i) => i.id,
}
}
}
2020-04-09 10:48:08 -05:00
fn print_debug_subtree(f: &mut fmt::Formatter<'_>, subtree: &Subtree, level: usize) -> fmt::Result {
2021-06-12 23:07:28 -05:00
let align = " ".repeat(level);
2020-04-09 10:48:08 -05:00
let aux = match subtree.delimiter.map(|it| (it.kind, it.id.0)) {
None => "$".to_string(),
Some((DelimiterKind::Parenthesis, id)) => format!("() {id}"),
Some((DelimiterKind::Brace, id)) => format!("{{}} {id}"),
Some((DelimiterKind::Bracket, id)) => format!("[] {id}"),
2020-04-09 10:48:08 -05:00
};
if subtree.token_trees.is_empty() {
write!(f, "{align}SUBTREE {aux}")?;
2020-04-09 10:48:08 -05:00
} else {
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(())
}
fn print_debug_token(f: &mut fmt::Formatter<'_>, tkn: &TokenTree, 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 {
Leaf::Literal(lit) => write!(f, "{align}LITERAL {} {}", lit.text, lit.id.0)?,
2020-04-09 10:48:08 -05:00
Leaf::Punct(punct) => write!(
f,
"{}PUNCH {} [{}] {}",
align,
punct.char,
if punct.spacing == Spacing::Alone { "alone" } else { "joint" },
punct.id.0
)?,
Leaf::Ident(ident) => write!(f, "{align}IDENT {} {}", ident.text, ident.id.0)?,
2020-04-09 10:48:08 -05:00
},
TokenTree::Subtree(subtree) => {
print_debug_subtree(f, subtree, level)?;
}
}
Ok(())
}
2020-11-26 12:56:38 -06:00
impl fmt::Debug for Subtree {
2020-04-09 10:48:08 -05:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
print_debug_subtree(f, self, 0)
}
}
2019-01-31 08:16:02 -06:00
impl fmt::Display for TokenTree {
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),
}
}
}
impl fmt::Display for Subtree {
2022-07-20 08:02:08 -05:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2019-12-17 21:47:26 -06:00
let (l, r) = match self.delimiter_kind() {
2019-12-12 11:41:44 -06:00
Some(DelimiterKind::Parenthesis) => ("(", ")"),
Some(DelimiterKind::Brace) => ("{", "}"),
Some(DelimiterKind::Bracket) => ("[", "]"),
2019-12-13 07:53:34 -06:00
None => ("", ""),
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;
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
}
}
impl fmt::Display for Leaf {
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),
}
}
}
impl fmt::Display for Ident {
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)
}
}
impl fmt::Display for Literal {
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)
}
}
impl fmt::Display for Punct {
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)
}
}
impl Subtree {
/// 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 07:45:08 -05:00
TokenTree::Leaf(_) => 0,
})
.sum::<usize>();
self.token_trees.len() + children_count
}
2019-12-17 21:47:26 -06:00
pub fn delimiter_kind(&self) -> Option<DelimiterKind> {
self.delimiter.map(|it| it.kind)
}
}
2019-05-21 23:30:36 -05:00
2021-03-13 06:14:21 -06:00
impl Subtree {
/// A simple line string used for debugging
pub fn as_debug_string(&self) -> String {
let delim = match self.delimiter_kind() {
Some(DelimiterKind::Brace) => ("{", "}"),
Some(DelimiterKind::Bracket) => ("[", "]"),
Some(DelimiterKind::Parenthesis) => ("(", ")"),
None => (" ", " "),
};
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
}
(Leaf::Punct(_), Some(&TokenTree::Leaf(Leaf::Punct(punct)))) => {
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;
pub fn pretty(tkns: &[TokenTree]) -> String {
fn tokentree_to_text(tkn: &TokenTree) -> 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);
let (open, close) = match subtree.delimiter.map(|it| it.kind) {
None => ("", ""),
Some(DelimiterKind::Brace) => ("{", "}"),
Some(DelimiterKind::Parenthesis) => ("(", ")"),
Some(DelimiterKind::Bracket) => ("[", "]"),
};
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
}