use proc_macro2::{Delimiter, Group, Span, TokenStream, TokenTree};
use crate::registry_core::declaration::FACE_FIELD_ORDER;
use super::{FaceSyntaxError, SyntaxLocation};
pub fn compact_tokens(tokens: TokenStream) -> String {
tokens
.to_string()
.replace(" :: ", "::")
.replace(" (", "(")
.replace(") ", ")")
}
#[doc(hidden)]
pub fn split_face_fields(tokens: TokenStream) -> Vec<TokenStream> {
let tokens = tokens.into_iter().collect::<Vec<_>>();
let mut fields = Vec::new();
let mut current = TokenStream::new();
let mut value_started = false;
let mut angles = 0usize;
let mut index = 0;
while index < tokens.len() {
if let TokenTree::Punct(punct) = &tokens[index] {
match punct.as_char() {
'<' => {
if closes_before_the_next_field(&tokens, index) {
angles += if punct.spacing() == proc_macro2::Spacing::Joint {
2
} else {
1
};
}
current.extend([tokens[index].clone()]);
value_started = true;
index += 1;
continue;
}
'>' => {
let close = if punct.spacing() == proc_macro2::Spacing::Joint {
2
} else {
1
};
angles = angles.saturating_sub(close);
current.extend([tokens[index].clone()]);
index += 1;
continue;
}
',' | ';' if angles == 0 => {
if !current.is_empty() {
fields.push(std::mem::take(&mut current));
value_started = false;
}
index += 1;
continue;
}
_ => {}
}
}
if angles == 0 && value_started && starts_face_field(&tokens, index) {
fields.push(std::mem::take(&mut current));
value_started = false;
continue;
}
value_started = true;
current.extend([tokens[index].clone()]);
index += 1;
}
if !current.is_empty() {
fields.push(current);
}
fields
}
fn closes_before_the_next_field(tokens: &[TokenTree], index: usize) -> bool {
let mut depth = 1usize;
let mut cursor = index + 1;
while cursor < tokens.len() {
match &tokens[cursor] {
TokenTree::Punct(punct) if punct.as_char() == '<' => {
depth += if punct.spacing() == proc_macro2::Spacing::Joint {
2
} else {
1
};
}
TokenTree::Punct(punct) if punct.as_char() == '>' => {
let close = if punct.spacing() == proc_macro2::Spacing::Joint {
2
} else {
1
};
match depth.checked_sub(close) {
Some(remaining) => depth = remaining,
None => return true,
}
if depth == 0 {
return true;
}
}
_ => {
if starts_face_field(tokens, cursor) {
return false;
}
}
}
cursor += 1;
}
false
}
fn starts_face_field(tokens: &[TokenTree], index: usize) -> bool {
let Some(TokenTree::Ident(name)) = tokens.get(index) else {
return false;
};
if !FACE_FIELD_ORDER.contains(&name.to_string().as_str()) {
return false;
}
match tokens.get(index + 1) {
Some(TokenTree::Punct(colon)) if colon.as_char() == ':' => {
colon.spacing() == proc_macro2::Spacing::Alone
}
_ => false,
}
}
#[doc(hidden)]
pub fn split_top_level(tokens: TokenStream) -> Vec<TokenStream> {
let mut items = Vec::new();
let mut current = TokenStream::new();
let mut depth = 0usize;
let mut after_segment = false;
for token in tokens {
match &token {
TokenTree::Punct(punct) if punct.as_char() == ',' && depth == 0 => {
if !current.is_empty() {
items.push(current);
current = TokenStream::new();
}
after_segment = false;
continue;
}
TokenTree::Punct(punct) if punct.as_char() == '<' && after_segment => depth += 1,
TokenTree::Punct(punct) if punct.as_char() == '>' && depth > 0 => depth -= 1,
_ => {}
}
after_segment = matches!(&token, TokenTree::Ident(_))
|| matches!(&token, TokenTree::Punct(punct) if punct.as_char() == '>');
current.extend([token]);
}
if !current.is_empty() {
items.push(current);
}
items
}
#[doc(hidden)]
pub fn path_to_string(path: &syn::Path) -> String {
let mut output = String::new();
if path.leading_colon.is_some() {
output.push_str("::");
}
for (index, segment) in path.segments.iter().enumerate() {
if index > 0 {
output.push_str("::");
}
output.push_str(&segment.ident.to_string());
}
output
}
#[doc(hidden)]
pub fn is_path_to(path: &str, needle: &str) -> bool {
path == needle
|| path
.strip_suffix(needle)
.is_some_and(|prefix| prefix.ends_with("::"))
}
#[doc(hidden)]
pub fn location(span: Span) -> SyntaxLocation {
let start = span.start();
SyntaxLocation {
line: start.line,
column: start.column + 1,
}
}
pub(super) fn end_location(span: Span) -> SyntaxLocation {
let end = span.end();
SyntaxLocation {
line: end.line,
column: end.column + 1,
}
}
#[doc(hidden)]
pub fn syntax_error(span: Span, message: impl Into<String>) -> FaceSyntaxError {
FaceSyntaxError {
message: message.into(),
location: Some(location(span)),
}
}
pub(super) fn literal_string(expression: &syn::Expr) -> Option<String> {
let syn::Expr::Lit(expression) = expression else {
return None;
};
let syn::Lit::Str(value) = &expression.lit else {
return None;
};
Some(value.value())
}
pub(super) fn list_group(tokens: &TokenStream, delimiter: Delimiter) -> Option<Group> {
let mut stream = tokens.clone().into_iter();
if matches!(stream.clone().next(), Some(TokenTree::Punct(punct)) if punct.as_char() == '&') {
stream.next();
}
only_group(&stream.collect::<TokenStream>(), delimiter)
}
pub(super) fn only_group(tokens: &TokenStream, delimiter: Delimiter) -> Option<Group> {
let mut tokens = tokens.clone().into_iter();
let TokenTree::Group(group) = tokens.next()? else {
return None;
};
(group.delimiter() == delimiter && tokens.next().is_none()).then_some(group)
}
#[cfg(test)]
mod tests {
use super::{split_face_fields, split_top_level};
use proc_macro2::TokenStream;
#[test]
fn a_comma_inside_generic_arguments_is_not_a_list_separator() {
let tokens: TokenStream = "crate::ui::ControlHandle<u8, u16>, crate::parts::ActionParts"
.parse()
.unwrap();
let items = split_top_level(tokens);
assert_eq!(
items.len(),
2,
"one comma is a separator, the other is not: {items:?}"
);
assert_eq!(
items[0].to_string().replace(' ', ""),
"crate::ui::ControlHandle<u8,u16>",
"{items:?}"
);
}
#[test]
fn face_field_splitting_survives_generics_and_closures() {
let body: TokenStream = syn::parse_str(
"kind: Tool, preset: crate::P<u8, u16>, flow: |a: u32| a, needs_registry: true",
)
.expect("token stream");
let fields = split_face_fields(body);
let names = fields
.iter()
.map(|field| {
field
.clone()
.into_iter()
.next()
.map_or_else(String::new, |token| token.to_string())
})
.collect::<Vec<_>>();
assert_eq!(names, ["kind", "preset", "flow", "needs_registry"]);
assert!(
fields[1].to_string().contains("P < u8 , u16 >"),
"the generic comma stays in the value: {}",
fields[1]
);
assert!(
fields[2].to_string().contains("| a : u32 | a"),
"the closure parameter stays in the value: {}",
fields[2]
);
assert_eq!(
split_face_fields(syn::parse_str("kind: X;").expect("token stream")).len(),
1
);
}
#[test]
fn a_comparison_does_not_swallow_the_following_fields() {
let body: TokenStream =
syn::parse_str("kind: Tool, flow: |a: u32| a < b, needs_registry: true")
.expect("token stream");
let fields = split_face_fields(body);
let names = fields
.iter()
.map(|field| {
field
.clone()
.into_iter()
.next()
.map_or_else(String::new, |token| token.to_string())
})
.collect::<Vec<_>>();
assert_eq!(
names,
["kind", "flow", "needs_registry"],
"the comparison keeps the following fields: {fields:?}"
);
assert_eq!(
split_face_fields(
syn::parse_str("kind: Tool, flow: |a: u32| a < b").expect("token stream")
)
.len(),
2,
"a comparison at the end of the body is still just a value"
);
}
}