2019-09-30 08:58:53 +00: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`.
|
2019-01-31 18:29:04 +00:00
|
|
|
|
2019-01-31 18:09:43 +00:00
|
|
|
macro_rules! impl_froms {
|
|
|
|
($e:ident: $($v:ident), *) => {
|
|
|
|
$(
|
|
|
|
impl From<$v> for $e {
|
|
|
|
fn from(it: $v) -> $e {
|
|
|
|
$e::$v(it)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-31 14:16:02 +00:00
|
|
|
use std::fmt;
|
|
|
|
|
2019-01-31 10:40:05 +00:00
|
|
|
use smol_str::SmolStr;
|
2019-01-30 20:02:27 +00:00
|
|
|
|
2019-02-11 18:31:54 +00: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 16:28:39 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
|
|
pub struct TokenId(pub u32);
|
|
|
|
|
|
|
|
impl TokenId {
|
|
|
|
pub const fn unspecified() -> TokenId {
|
|
|
|
TokenId(!0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-28 05:41:58 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2019-01-31 10:40:05 +00:00
|
|
|
pub enum TokenTree {
|
2019-01-30 20:02:27 +00:00
|
|
|
Leaf(Leaf),
|
|
|
|
Subtree(Subtree),
|
|
|
|
}
|
2019-01-30 20:58:52 +00:00
|
|
|
impl_froms!(TokenTree: Leaf, Subtree);
|
2019-01-30 20:02:27 +00:00
|
|
|
|
2019-11-28 05:41:58 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2019-01-31 10:40:05 +00:00
|
|
|
pub enum Leaf {
|
2019-01-30 20:02:27 +00:00
|
|
|
Literal(Literal),
|
|
|
|
Punct(Punct),
|
|
|
|
Ident(Ident),
|
|
|
|
}
|
2019-01-30 20:58:52 +00:00
|
|
|
impl_froms!(Leaf: Literal, Punct, Ident);
|
2019-01-30 20:02:27 +00:00
|
|
|
|
2019-12-13 13:53:34 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
|
2019-01-31 10:40:05 +00:00
|
|
|
pub struct Subtree {
|
2019-12-13 13:53:34 +00:00
|
|
|
pub delimiter: Option<Delimiter>,
|
2019-01-31 10:40:05 +00:00
|
|
|
pub token_trees: Vec<TokenTree>,
|
2019-01-30 20:02:27 +00:00
|
|
|
}
|
|
|
|
|
2019-11-28 05:41:58 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
2019-12-12 17:41:44 +00:00
|
|
|
pub struct Delimiter {
|
|
|
|
pub id: TokenId,
|
|
|
|
pub kind: DelimiterKind,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
|
|
|
pub enum DelimiterKind {
|
2019-01-30 20:02:27 +00:00
|
|
|
Parenthesis,
|
|
|
|
Brace,
|
|
|
|
Bracket,
|
|
|
|
}
|
|
|
|
|
2019-11-28 05:41:58 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2019-01-31 10:40:05 +00:00
|
|
|
pub struct Literal {
|
|
|
|
pub text: SmolStr,
|
2019-12-12 13:47:54 +00:00
|
|
|
pub id: TokenId,
|
2019-01-30 20:02:27 +00:00
|
|
|
}
|
|
|
|
|
2019-11-28 05:41:58 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
2019-01-31 10:40:05 +00:00
|
|
|
pub struct Punct {
|
|
|
|
pub char: char,
|
2019-01-31 15:51:17 +00:00
|
|
|
pub spacing: Spacing,
|
2019-12-12 13:47:54 +00:00
|
|
|
pub id: TokenId,
|
2019-01-31 15:51:17 +00:00
|
|
|
}
|
|
|
|
|
2019-11-28 05:41:58 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
2019-01-31 15:51:17 +00:00
|
|
|
pub enum Spacing {
|
|
|
|
Alone,
|
|
|
|
Joint,
|
2019-01-30 20:02:27 +00:00
|
|
|
}
|
|
|
|
|
2019-11-28 05:41:58 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2019-01-31 10:40:05 +00:00
|
|
|
pub struct Ident {
|
|
|
|
pub text: SmolStr,
|
2019-02-11 16:28:39 +00:00
|
|
|
pub id: TokenId,
|
2019-01-30 20:02:27 +00:00
|
|
|
}
|
2019-01-31 14:16:02 +00:00
|
|
|
|
|
|
|
impl fmt::Display for TokenTree {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
TokenTree::Leaf(it) => fmt::Display::fmt(it, f),
|
|
|
|
TokenTree::Subtree(it) => fmt::Display::fmt(it, f),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Subtree {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2019-12-18 03:47:26 +00:00
|
|
|
let (l, r) = match self.delimiter_kind() {
|
2019-12-12 17:41:44 +00:00
|
|
|
Some(DelimiterKind::Parenthesis) => ("(", ")"),
|
|
|
|
Some(DelimiterKind::Brace) => ("{", "}"),
|
|
|
|
Some(DelimiterKind::Bracket) => ("[", "]"),
|
2019-12-13 13:53:34 +00:00
|
|
|
None => ("", ""),
|
2019-01-31 14:16:02 +00:00
|
|
|
};
|
2019-01-31 15:51:17 +00:00
|
|
|
f.write_str(l)?;
|
|
|
|
let mut needs_space = false;
|
|
|
|
for tt in self.token_trees.iter() {
|
|
|
|
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)?
|
|
|
|
}
|
|
|
|
tt => fmt::Display::fmt(tt, f)?,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f.write_str(r)?;
|
|
|
|
Ok(())
|
2019-01-31 14:16:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Leaf {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
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 {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
fmt::Display::fmt(&self.text, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Literal {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
fmt::Display::fmt(&self.text, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Punct {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
fmt::Display::fmt(&self.char, f)
|
|
|
|
}
|
|
|
|
}
|
2019-04-22 07:33:55 +00:00
|
|
|
|
|
|
|
impl Subtree {
|
|
|
|
/// Count the number of tokens recursively
|
|
|
|
pub fn count(&self) -> usize {
|
2019-04-22 09:37:27 +00:00
|
|
|
let children_count = self
|
|
|
|
.token_trees
|
|
|
|
.iter()
|
|
|
|
.map(|c| match c {
|
2019-04-22 07:33:55 +00:00
|
|
|
TokenTree::Subtree(c) => c.count(),
|
|
|
|
_ => 0,
|
2019-04-22 09:37:27 +00:00
|
|
|
})
|
|
|
|
.sum::<usize>();
|
|
|
|
|
|
|
|
self.token_trees.len() + children_count
|
2019-04-22 07:33:55 +00:00
|
|
|
}
|
2019-12-18 03:47:26 +00:00
|
|
|
|
|
|
|
pub fn delimiter_kind(&self) -> Option<DelimiterKind> {
|
|
|
|
self.delimiter.map(|it| it.kind)
|
|
|
|
}
|
2019-04-22 07:33:55 +00:00
|
|
|
}
|
2019-05-22 04:30:36 +00:00
|
|
|
|
|
|
|
pub mod buffer;
|