use std::collections::{BTreeMap, BTreeSet};
use std::fmt;
use proc_macro2::{TokenStream, TokenTree};
use quote::ToTokens;
use syn::spanned::Spanned;
use syn::visit::Visit;
#[path = "tokens.rs"]
mod tokens;
use tokens::end_location;
pub use tokens::{
compact_tokens, location, path_to_string, split_face_fields, split_top_level, syntax_error,
};
#[path = "fields.rs"]
mod fields;
use super::literal_value::normalized_literal_value;
use fields::parse_face_fields;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SyntaxLocation {
pub line: usize,
pub column: usize,
}
#[derive(Clone, Debug)]
struct FieldSyntax {
tokens: TokenStream,
location: SyntaxLocation,
}
#[derive(Clone, Debug)]
pub struct FaceSyntax {
pub macro_name: String,
pub cfg: Option<String>,
pub location: SyntaxLocation,
pub end: SyntaxLocation,
macro_span: proc_macro2::Span,
fields: BTreeMap<String, FieldSyntax>,
imports: super::imports::Imports,
expression: Option<(SyntaxLocation, SyntaxLocation)>,
pub declaration_start: SyntaxLocation,
struct_literal: bool,
}
impl FaceSyntax {
pub fn is_struct_literal(&self) -> bool {
self.struct_literal
}
fn absorb_contract_items(
&mut self,
items: &BTreeMap<String, String>,
asserted: Option<&[String]>,
) -> Result<(), FaceSyntaxError> {
if !self.struct_literal || items.is_empty() {
return Ok(());
}
if let Some(paths) = asserted {
let asserted: Vec<&str> = paths
.iter()
.map(|path| path.rsplit("::").next().unwrap_or(path.as_str()).trim())
.filter(|label| !label.is_empty())
.collect();
for field in ["handle_traits", "part_traits"] {
let Some(written) = items.get(field) else {
continue;
};
let labels = quoted_strings(written);
if labels.is_empty() {
continue;
}
if !asserted
.iter()
.any(|label| labels.iter().any(|written| written == label))
{
return Err(super::syntax_error(
self.macro_span,
format!(
"`{field}` and the `assert_contracts!` line name different traits: the labels are two statements of one fact, and a rule that requires one of them must not find a face that never claimed it"
),
));
}
}
}
let mut text = String::new();
for (field, value) in items {
if self.fields.contains_key(field) {
continue;
}
text.push_str(&format!("{field}: {value}, "));
}
if text.is_empty() {
return Ok(());
}
let tokens = text
.parse::<TokenStream>()
.map_err(|error| FaceSyntaxError {
message: format!("cannot read the `FaceContract` block: {error}"),
location: Some(self.location.clone()),
})?;
self.fields
.extend(parse_face_fields(tokens, self.macro_span)?);
Ok(())
}
}
fn contract_field(name: &str) -> Option<&'static str> {
match name {
"Preset" => Some("preset"),
"Parts" => Some("parts"),
"HANDLE_TRAITS" => Some("handle_traits"),
"PART_TRAITS" => Some("part_traits"),
_ => None,
}
}
fn quoted_strings(text: &str) -> Vec<String> {
let Ok(tokens) = text.parse::<TokenStream>() else {
return Vec::new();
};
fn string_literals_in(stream: TokenStream, found: &mut Vec<String>) {
for tree in stream {
match tree {
proc_macro2::TokenTree::Literal(literal) => {
if let Ok(value) = syn::parse_str::<syn::LitStr>(&literal.to_string()) {
found.push(value.value());
}
}
proc_macro2::TokenTree::Group(group) => string_literals_in(group.stream(), found),
_ => {}
}
}
}
let mut found = Vec::new();
string_literals_in(tokens, &mut found);
found
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ParentSyntax {
Root,
FromPath {
source: String,
kind: String,
},
NodePath(String),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FaceSyntaxError {
pub message: String,
pub location: Option<SyntaxLocation>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct SourceReferences {
pub paths: BTreeSet<String>,
pub conservative: bool,
}
impl fmt::Display for FaceSyntaxError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(location) = &self.location {
write!(
formatter,
"{}:{}: {}",
location.line, location.column, self.message
)
} else {
formatter.write_str(&self.message)
}
}
}
impl std::error::Error for FaceSyntaxError {}
pub fn parse_faces(source: &str) -> Result<Vec<FaceSyntax>, FaceSyntaxError> {
let file = super::nesting::parse_file(source)?;
let mut visitor = FaceVisitor {
faces: Vec::new(),
error: None,
imports: Default::default(),
contract_items: Default::default(),
asserted_contracts: None,
};
visitor.visit_file(&file);
if let Some(error) = visitor.error {
Err(error)
} else {
for face in &mut visitor.faces {
face.absorb_contract_items(
&visitor.contract_items,
visitor.asserted_contracts.as_deref(),
)?;
}
Ok(visitor.faces)
}
}
pub fn relocate_face_declaration(
source: &str,
parent: &str,
alias: Option<(&str, &str)>,
) -> Result<String, FaceSyntaxError> {
let face = parse_face(source)?.ok_or_else(|| FaceSyntaxError {
message: "source has no registration face".to_owned(),
location: None,
})?;
let field = face.fields.get("parent").ok_or_else(|| FaceSyntaxError {
message: "registration face has no explicit parent".to_owned(),
location: Some(face.location.clone()),
})?;
syn::parse_str::<syn::Expr>(parent)
.map_err(|error| syntax_error(field.tokens.span(), error.to_string()))?;
let span_start = location(field.tokens.span());
let span_end = end_location(field.tokens.span());
let range = match (
source_offset(source, &span_start),
source_offset(source, &span_end),
) {
(Ok(start), Ok(end)) => (start, end),
_ => {
let start =
super::face_region::value_start(source, source_offset(source, &field.location)?);
(start, super::face_region::expression_end(source, start))
}
};
let mut edits = vec![(range.0, range.1, parent.to_owned())];
if let Some((before, after)) = alias.filter(|_| !face.is_native()) {
if face.macro_name != before {
return Err(syntax_error(
face.macro_span,
format!(
"expected declaration alias `{before}`, found `{}`",
face.macro_name
),
));
}
syn::parse_str::<syn::Ident>(after)
.map_err(|error| syntax_error(face.macro_span, error.to_string()))?;
let alias_start = source_offset(source, &location(face.macro_span))?;
edits.push((
alias_start,
alias_start + face.macro_name.len(),
after.to_owned(),
));
}
let mut ranges = edits;
ranges.sort_by_key(|(start, _, _)| std::cmp::Reverse(*start));
let mut output = source.to_owned();
for (start, end, text) in ranges {
output.replace_range(start..end, &text);
}
Ok(output)
}
pub fn is_face_source(source: &str, marker: &str) -> bool {
source.lines().any(|line| line == marker)
|| ((source.contains("_object") || source.contains("soil") || source.contains("face"))
&& matches!(parse_face(source), Ok(Some(_))))
}
pub fn parse_face(source: &str) -> Result<Option<FaceSyntax>, FaceSyntaxError> {
let mut faces = parse_faces(source)?;
if faces.len() > 1 {
return Err(FaceSyntaxError {
message: "expected one registration face in this file".to_owned(),
location: faces.get(1).map(|face| face.location.clone()),
});
}
Ok(faces.pop())
}
pub fn replace_face_macro(source: &str, replacement: &str) -> Result<String, FaceSyntaxError> {
let current = parse_face(source)?.ok_or_else(|| FaceSyntaxError {
message: "source has no registration face".to_owned(),
location: None,
})?;
let next = parse_face(replacement)?.ok_or_else(|| FaceSyntaxError {
message: "replacement has no registration face".to_owned(),
location: None,
})?;
if current.is_struct_literal() != next.is_struct_literal() {
return Err(FaceSyntaxError {
message: format!(
"cannot rewrite a {} declaration into the {} form yet: the two spellings are the same \
face, but the writer for that form has not landed (H/A2e step ③)",
if current.is_struct_literal() {
"struct-literal"
} else {
"macro"
},
if next.is_struct_literal() {
"struct-literal"
} else {
"macro"
},
),
location: Some(current.location.clone()),
});
}
if let (Some((start, end)), Some((next_start, next_end))) =
(¤t.expression, &next.expression)
{
let mut output = source.to_owned();
output.replace_range(
source_offset(source, start)?..source_offset(source, end)?,
&replacement
[source_offset(replacement, next_start)?..source_offset(replacement, next_end)?],
);
return Ok(output);
}
let current_start = source_offset(source, ¤t.declaration_start)?;
let current_end = source_offset(source, ¤t.end)?;
let replacement_start = source_offset(replacement, &next.location)?;
let replacement_end = source_offset(replacement, &next.end)?;
let mut output =
String::with_capacity(source.len() + replacement_end.saturating_sub(replacement_start));
output.push_str(&source[..current_start]);
output.push_str(&replacement[replacement_start..replacement_end]);
output.push_str(&source[current_end..]);
Ok(output)
}
pub(crate) fn source_offset(
source: &str,
location: &SyntaxLocation,
) -> Result<usize, FaceSyntaxError> {
let line_start = if location.line <= 1 {
0
} else {
source
.match_indices('\n')
.nth(location.line - 2)
.map(|(index, _)| index + 1)
.ok_or_else(|| FaceSyntaxError {
message: "macro span points outside source".to_owned(),
location: Some(location.clone()),
})?
};
let line = source[line_start..].split('\n').next().unwrap_or_default();
line.char_indices()
.map(|(offset, _)| offset)
.chain(std::iter::once(line.len()))
.nth(location.column.saturating_sub(1))
.map(|offset| line_start + offset)
.ok_or_else(|| FaceSyntaxError {
message: "macro span column points outside source line".to_owned(),
location: Some(location.clone()),
})
}
pub fn source_references(source: &str) -> Result<SourceReferences, FaceSyntaxError> {
let file = super::nesting::parse_file(source)?;
Ok(super::reference_scan::scan(&file))
}
struct FaceVisitor {
faces: Vec<FaceSyntax>,
error: Option<FaceSyntaxError>,
imports: super::imports::Imports,
contract_items: BTreeMap<String, String>,
asserted_contracts: Option<Vec<String>>,
}
impl<'ast> Visit<'ast> for FaceVisitor {
fn visit_file(&mut self, file: &'ast syn::File) {
super::imports::collect(&file.items, &mut self.imports);
syn::visit::visit_file(self, file);
}
fn visit_item_mod(&mut self, item: &'ast syn::ItemMod) {
let saved = self.imports.clone();
if let Some((_, items)) = &item.content {
super::imports::collect(items, &mut self.imports);
}
syn::visit::visit_item_mod(self, item);
self.imports = saved;
}
fn visit_item_const(&mut self, item: &'ast syn::ItemConst) {
if self.error.is_some() || !super::has_face_attribute(&item.attrs) {
return;
}
match super::native_face_fields(item)
.and_then(|tokens| parse_face_fields(tokens, item.span()))
{
Ok(fields) => self.faces.push(FaceSyntax {
expression: Some((location(item.expr.span()), end_location(item.expr.span()))),
declaration_start: location(item.span()),
macro_name: super::native_face::face_attribute_name(&item.attrs)
.unwrap()
.to_string(),
macro_span: item.ident.span(),
cfg: item
.attrs
.iter()
.find(|a| a.path().is_ident("cfg"))
.and_then(|a| a.parse_args::<TokenStream>().ok())
.map(|t| compact(&t)),
location: location(item.span()),
end: end_location(item.span()),
fields,
imports: self.imports.clone(),
struct_literal: false,
}),
Err(error) => self.error = Some(error),
}
}
fn visit_item_impl(&mut self, item: &'ast syn::ItemImpl) {
if self.error.is_some() {
return;
}
for entry in &item.items {
match entry {
syn::ImplItem::Const(constant) => {
let name = constant.ident.to_string();
if let Some(field) = contract_field(&name) {
self.contract_items.insert(
field.to_owned(),
constant.expr.to_token_stream().to_string(),
);
}
}
syn::ImplItem::Type(associated) => {
let name = associated.ident.to_string();
if let Some(field) = contract_field(&name) {
self.contract_items.insert(
field.to_owned(),
associated.ty.to_token_stream().to_string(),
);
}
}
_ => {}
}
}
let Some(kind) = last_segment(&item.self_ty) else {
return;
};
for entry in &item.items {
let syn::ImplItem::Const(constant) = entry else {
continue;
};
if constant.ident != "REGISTRATION"
|| last_segment(&constant.ty).as_deref() != Some("Registration")
{
continue;
}
let syn::Expr::Struct(literal) = &constant.expr else {
continue;
};
if last_segment(&syn::Type::Path(syn::TypePath {
qself: None,
path: literal.path.clone(),
}))
.as_deref()
!= Some("Registration")
{
continue;
}
let mut text = format!("kind: {kind}");
for field in &literal.fields {
let syn::Member::Named(name) = &field.member else {
continue;
};
let printed = normalized_literal_value(&field.expr);
text.push_str(&format!(", {name}: {printed}"));
}
let parsed = text
.parse::<TokenStream>()
.map_err(|error| FaceSyntaxError {
message: format!("cannot read the registration literal: {error}"),
location: Some(location(constant.span())),
})
.and_then(|tokens| parse_face_fields(tokens, constant.span()));
match parsed {
Ok(fields) => {
let span = constant.span();
let cfg =
constant
.attrs
.iter()
.chain(item.attrs.iter())
.find_map(|attribute| {
attribute
.path()
.is_ident("cfg")
.then(|| {
attribute
.parse_args::<TokenStream>()
.ok()
.map(|tokens| compact(&tokens))
})
.flatten()
});
self.faces.push(FaceSyntax {
expression: Some((
location(constant.expr.span()),
end_location(constant.expr.span()),
)),
declaration_start: location(span),
macro_name: "registration".to_owned(),
macro_span: constant.ident.span(),
cfg,
location: location(span),
end: end_location(span),
fields,
imports: self.imports.clone(),
struct_literal: true,
})
}
Err(error) => self.error = Some(error),
}
}
}
fn visit_item_macro(&mut self, item: &'ast syn::ItemMacro) {
if item
.mac
.path
.segments
.last()
.is_some_and(|segment| segment.ident == "assert_contracts")
{
let tokens = item.mac.tokens.to_string();
if let Some((_, traits)) = tokens.split_once(':') {
self.asserted_contracts = Some(
traits
.split(',')
.map(|path| path.trim().to_owned())
.filter(|path| !path.is_empty())
.collect(),
);
}
}
if self.error.is_some() {
return;
}
let Some(segment) = item.mac.path.segments.last() else {
return;
};
let macro_name = segment.ident.to_string();
let is_face_macro = matches!(macro_name.as_str(), "control_object" | "external_object")
|| macro_name.ends_with("_object");
if !is_face_macro {
return;
}
match parse_face_fields(item.mac.tokens.clone(), item.mac.span()) {
Ok(fields) => {
let span = item.span();
let cfg = item.attrs.iter().find_map(|attribute| {
attribute
.path()
.is_ident("cfg")
.then(|| {
attribute
.parse_args::<TokenStream>()
.ok()
.map(|tokens| compact(&tokens))
})
.flatten()
});
self.faces.push(FaceSyntax {
expression: None,
declaration_start: location(item.mac.path.span()),
macro_name,
macro_span: segment.ident.span(),
cfg,
location: location(span),
end: end_location(span),
fields,
imports: self.imports.clone(),
struct_literal: false,
})
}
Err(error) => self.error = Some(error),
}
}
}
fn last_segment(ty: &syn::Type) -> Option<String> {
let syn::Type::Path(path) = ty else {
return None;
};
Some(path.path.segments.last()?.ident.to_string())
}
pub(super) fn compact(tokens: &TokenStream) -> String {
compact_tokens(tokens.clone())
}
pub(super) fn split_typed_range(tokens: Vec<TokenTree>) -> Option<(String, String)> {
let position = tokens
.iter()
.position(|token| matches!(token, TokenTree::Ident(value) if value == "to"))?;
let start = tokens[..position].iter().cloned().collect::<TokenStream>();
let finish = tokens[position + 1..]
.iter()
.cloned()
.collect::<TokenStream>();
if start.is_empty() || finish.is_empty() {
return None;
}
Some((compact_tokens(start), compact_tokens(finish)))
}
#[cfg(test)]
#[path = "face_tests.rs"]
mod face_tests;
#[cfg(test)]
#[path = "face_relocation_tests.rs"]
mod relocation_tests;