Available on crate feature
fold
only.Expand description
Syntax tree traversal to transform the nodes of an owned syntax tree.
Each method of the Fold
trait is a hook that can be overridden to
customize the behavior when transforming the corresponding type of node.
By default, every method recursively visits the substructure of the
input by invoking the right visitor method of each of its fields.
pub trait Fold {
/* ... */
fn fold_expr_binary(&mut self, node: ExprBinary) -> ExprBinary {
fold_expr_binary(self, node)
}
/* ... */
}
pub fn fold_expr_binary<V>(v: &mut V, node: ExprBinary) -> ExprBinary
where
V: Fold + ?Sized,
{
ExprBinary {
attrs: node
.attrs
.into_iter()
.map(|attr| v.fold_attribute(attr))
.collect(),
left: Box::new(v.fold_expr(*node.left)),
op: v.fold_bin_op(node.op),
right: Box::new(v.fold_expr(*node.right)),
}
}
/* ... */
§Example
This fold inserts parentheses to fully parenthesizes any expression.
// [dependencies]
// quote = "1.0"
// syn = { version = "2.0", features = ["fold", "full"] }
use quote::quote;
use syn::fold::{fold_expr, Fold};
use syn::{token, Expr, ExprParen};
struct ParenthesizeEveryExpr;
impl Fold for ParenthesizeEveryExpr {
fn fold_expr(&mut self, expr: Expr) -> Expr {
Expr::Paren(ExprParen {
attrs: Vec::new(),
expr: Box::new(fold_expr(self, expr)),
paren_token: token::Paren::default(),
})
}
}
fn main() {
let code = quote! { a() + b(1) * c.d };
let expr: Expr = syn::parse2(code).unwrap();
let parenthesized = ParenthesizeEveryExpr.fold_expr(expr);
println!("{}", quote!(#parenthesized));
// Output: (((a)()) + (((b)((1))) * ((c).d)))
}
Traits§
- Syntax tree traversal to transform the nodes of an owned syntax tree.
Functions§
- fold_
abi derive
orfull
- fold_
angle_ bracketed_ generic_ arguments derive
orfull
- fold_
arm full
- fold_
assoc_ const derive
orfull
- fold_
assoc_ type derive
orfull
- fold_
attr_ style derive
orfull
- fold_
attribute derive
orfull
- fold_
bare_ fn_ arg derive
orfull
- fold_
bare_ variadic derive
orfull
- fold_
bin_ op derive
orfull
- fold_
block full
- fold_
bound_ lifetimes derive
orfull
- fold_
const_ param derive
orfull
- fold_
constraint derive
orfull
- fold_
data derive
- fold_
data_ enum derive
- fold_
data_ struct derive
- fold_
data_ union derive
- fold_
derive_ input derive
- fold_
expr derive
orfull
- fold_
expr_ array full
- fold_
expr_ assign full
- fold_
expr_ async full
- fold_
expr_ await full
- fold_
expr_ binary derive
orfull
- fold_
expr_ block full
- fold_
expr_ break full
- fold_
expr_ call derive
orfull
- fold_
expr_ cast derive
orfull
- fold_
expr_ const full
- fold_
expr_ field derive
orfull
- fold_
expr_ group derive
orfull
- fold_
expr_ if full
- fold_
expr_ index derive
orfull
- fold_
expr_ infer full
- fold_
expr_ let full
- fold_
expr_ lit derive
orfull
- fold_
expr_ loop full
- fold_
expr_ macro derive
orfull
- fold_
expr_ match full
- fold_
expr_ method_ call derive
orfull
- fold_
expr_ paren derive
orfull
- fold_
expr_ path derive
orfull
- fold_
expr_ range full
- fold_
expr_ reference derive
orfull
- fold_
expr_ repeat full
- fold_
expr_ return full
- fold_
expr_ struct derive
orfull
- fold_
expr_ try full
- fold_
expr_ tuple derive
orfull
- fold_
expr_ unary derive
orfull
- fold_
expr_ unsafe full
- fold_
expr_ while full
- fold_
expr_ yield full
- fold_
field derive
orfull
- fold_
field_ mutability derive
orfull
- fold_
field_ pat full
- fold_
field_ value derive
orfull
- fold_
fields derive
orfull
- fold_
fields_ named derive
orfull
- fold_
fields_ unnamed derive
orfull
- fold_
file full
- fold_
fn_ arg full
- fold_
generic_ argument derive
orfull
- fold_
generic_ param derive
orfull
- fold_
generics derive
orfull
- fold_
impl_ item full
- fold_
index derive
orfull
- fold_
item full
- fold_
item_ const full
- fold_
item_ enum full
- fold_
item_ fn full
- fold_
item_ impl full
- fold_
item_ macro full
- fold_
item_ mod full
- fold_
item_ static full
- fold_
item_ struct full
- fold_
item_ trait full
- fold_
item_ type full
- fold_
item_ union full
- fold_
item_ use full
- fold_
label full
- fold_
lifetime_ param derive
orfull
- fold_
local full
- fold_
local_ init full
- fold_
macro derive
orfull
- fold_
macro_ delimiter derive
orfull
- fold_
member derive
orfull
- fold_
meta derive
orfull
- fold_
meta_ list derive
orfull
- fold_
meta_ name_ value derive
orfull
- fold_
parenthesized_ generic_ arguments derive
orfull
- fold_
pat full
- fold_
pat_ ident full
- fold_
pat_ or full
- fold_
pat_ paren full
- fold_
pat_ rest full
- fold_
pat_ slice full
- fold_
pat_ struct full
- fold_
pat_ tuple full
- fold_
pat_ type full
- fold_
pat_ wild full
- fold_
path derive
orfull
- fold_
path_ arguments derive
orfull
- fold_
path_ segment derive
orfull
- fold_
predicate_ lifetime derive
orfull
- fold_
predicate_ type derive
orfull
- fold_
qself derive
orfull
- fold_
receiver full
- fold_
return_ type derive
orfull
- fold_
signature full
- fold_
stmt full
- fold_
stmt_ macro full
- fold_
trait_ bound derive
orfull
- fold_
trait_ bound_ modifier derive
orfull
- fold_
trait_ item full
- fold_
type derive
orfull
- fold_
type_ array derive
orfull
- fold_
type_ bare_ fn derive
orfull
- fold_
type_ group derive
orfull
- fold_
type_ impl_ trait derive
orfull
- fold_
type_ infer derive
orfull
- fold_
type_ macro derive
orfull
- fold_
type_ never derive
orfull
- fold_
type_ param derive
orfull
- fold_
type_ param_ bound derive
orfull
- fold_
type_ paren derive
orfull
- fold_
type_ path derive
orfull
- fold_
type_ ptr derive
orfull
- fold_
type_ reference derive
orfull
- fold_
type_ slice derive
orfull
- fold_
type_ trait_ object derive
orfull
- fold_
type_ tuple derive
orfull
- fold_
un_ op derive
orfull
- fold_
use_ glob full
- fold_
use_ group full
- fold_
use_ name full
- fold_
use_ path full
- fold_
use_ rename full
- fold_
use_ tree full
- fold_
variadic full
- fold_
variant derive
orfull
- fold_
vis_ restricted derive
orfull
- fold_
visibility derive
orfull
- fold_
where_ clause derive
orfull
- fold_
where_ predicate derive
orfull