use proc_macro2::{TokenStream, TokenTree};
pub(crate) const LEFT_OUT: &str = "__tmt_left_out__";
pub(crate) fn same_tokens(a: &str, b: &str) -> Option<bool> {
same_tokens_of(a, b, false)
}
pub(crate) fn same_built_tokens(a: &str, b: &str) -> Option<bool> {
same_tokens_of(a, b, true)
}
const KEYWORDS: [&str; 39] = [
"as", "async", "await", "box", "break", "const", "continue", "crate", "do", "dyn", "else",
"enum", "extern", "fn", "for", "if", "impl", "in", "let", "loop", "match", "mod", "move",
"mut", "pub", "ref", "return", "static", "struct", "super", "trait", "try", "type", "unsafe",
"use", "where", "while", "yield", "union",
];
fn same_tokens_of(a: &str, b: &str, holes: bool) -> Option<bool> {
fn tokens(
stream: TokenStream,
plain: bool,
holes: bool,
list: Option<(proc_macro2::Delimiter, bool)>,
) -> Vec<String> {
let mut out = vec![];
let mut trees = stream.into_iter().peekable();
let (mut named, mut called) = (false, false);
let mut callable = false;
let mut ticked = false;
let mut hashed = false;
while let Some(tree) = trees.next() {
let (was_named, was_called, was_callable) = (named, called, callable);
let (was_ticked, was_hashed) = (ticked, hashed);
(named, called, callable, ticked, hashed) = (false, false, false, false, false);
match tree {
TokenTree::Punct(hash) if hash.as_char() == '#' => {
let bang =
matches!(trees.peek(), Some(TokenTree::Punct(p)) if p.as_char() == '!')
.then(|| trees.next());
let is_doc = matches!(trees.peek(), Some(TokenTree::Group(group))
if group.delimiter() == proc_macro2::Delimiter::Bracket
&& matches!(group.stream().into_iter().next(),
Some(TokenTree::Ident(name)) if name == "doc")
&& (plain
|| group
.span()
.source_text()
.is_some_and(|written| written.starts_with('/'))));
if is_doc {
trees.next();
continue;
}
out.push("#".to_string());
if bang.is_some() {
out.push("!".to_string());
}
hashed = true;
}
TokenTree::Group(group) => {
let inner = tokens(
group.stream(),
plain && !was_called,
holes,
Some((group.delimiter(), was_callable)),
);
out.push(format!("{:?}({})", group.delimiter(), inner.join(" ")));
callable = !was_hashed
&& matches!(
group.delimiter(),
proc_macro2::Delimiter::Parenthesis | proc_macro2::Delimiter::Bracket
);
}
TokenTree::Punct(mark) => {
called = was_named && mark.as_char() == '!';
ticked = mark.as_char() == '\'';
out.push(mark.to_string());
}
TokenTree::Ident(name) => {
named = true;
called = was_called;
let name = name.to_string();
callable = name == "fn"
|| (!was_ticked
&& !KEYWORDS.contains(&name.as_str())
&& !(holes && name == LEFT_OUT));
out.push(name);
}
other => out.push(other.to_string()),
}
}
if !plain {
return out;
}
let mut kept: Vec<String> = Vec::with_capacity(out.len());
let mut items = out.into_iter().peekable();
while let Some(item) = items.next() {
if !holes || item != LEFT_OUT {
kept.push(item);
continue;
}
if items.peek().is_some_and(|next| next == "," || next == ";") {
items.next();
} else if items.peek().is_none() && kept.last().is_some_and(|last| last == ",") {
let may_be_tuple =
matches!(list, Some((proc_macro2::Delimiter::Parenthesis, false)));
if !may_be_tuple {
kept.pop();
}
}
}
if kept.last().is_some_and(|last| last == ",") {
let says_nothing = match list {
Some((proc_macro2::Delimiter::Brace | proc_macro2::Delimiter::Bracket, _)) => true,
Some((proc_macro2::Delimiter::Parenthesis, after_a_name)) => after_a_name,
_ => false,
};
if says_nothing {
kept.pop();
}
}
kept
}
let lex = |text: &str| Some(tokens(text.parse::<TokenStream>().ok()?, true, holes, None));
Some(lex(a)? == lex(b)?)
}