Module syn::fold[][src]

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)),
    }
}

/* ... */

This module is available only if Syn is built with the "fold" feature.


Example

This fold inserts parentheses to fully parenthesizes any expression.

// [dependencies]
// quote = "1.0"
// syn = { version = "1.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

Fold

Syntax tree traversal to transform the nodes of an owned syntax tree.

Functions

fold_abi
fold_angle_bracketed_generic_arguments
fold_arm
fold_attr_style
fold_attribute
fold_bare_fn_arg
fold_bin_op
fold_binding
fold_block
fold_bound_lifetimes
fold_const_param
fold_constraint
fold_data
fold_data_enum
fold_data_struct
fold_data_union
fold_derive_input
fold_expr
fold_expr_array
fold_expr_assign
fold_expr_assign_op
fold_expr_async
fold_expr_await
fold_expr_binary
fold_expr_block
fold_expr_box
fold_expr_break
fold_expr_call
fold_expr_cast
fold_expr_closure
fold_expr_continue
fold_expr_field
fold_expr_for_loop
fold_expr_group
fold_expr_if
fold_expr_index
fold_expr_let
fold_expr_lit
fold_expr_loop
fold_expr_macro
fold_expr_match
fold_expr_method_call
fold_expr_paren
fold_expr_path
fold_expr_range
fold_expr_reference
fold_expr_repeat
fold_expr_return
fold_expr_struct
fold_expr_try
fold_expr_try_block
fold_expr_tuple
fold_expr_type
fold_expr_unary
fold_expr_unsafe
fold_expr_while
fold_expr_yield
fold_field
fold_field_pat
fold_field_value
fold_fields
fold_fields_named
fold_fields_unnamed
fold_file
fold_fn_arg
fold_foreign_item
fold_foreign_item_fn
fold_foreign_item_macro
fold_foreign_item_static
fold_foreign_item_type
fold_generic_argument
fold_generic_method_argument
fold_generic_param
fold_generics
fold_ident
fold_impl_item
fold_impl_item_const
fold_impl_item_macro
fold_impl_item_method
fold_impl_item_type
fold_index
fold_item
fold_item_const
fold_item_enum
fold_item_extern_crate
fold_item_fn
fold_item_foreign_mod
fold_item_impl
fold_item_macro
fold_item_macro2
fold_item_mod
fold_item_static
fold_item_struct
fold_item_trait
fold_item_trait_alias
fold_item_type
fold_item_union
fold_item_use
fold_label
fold_lifetime
fold_lifetime_def
fold_lit
fold_lit_bool
fold_lit_byte
fold_lit_byte_str
fold_lit_char
fold_lit_float
fold_lit_int
fold_lit_str
fold_local
fold_macro
fold_macro_delimiter
fold_member
fold_meta
fold_meta_list
fold_meta_name_value
fold_method_turbofish
fold_nested_meta
fold_parenthesized_generic_arguments
fold_pat
fold_pat_box
fold_pat_ident
fold_pat_lit
fold_pat_macro
fold_pat_or
fold_pat_path
fold_pat_range
fold_pat_reference
fold_pat_rest
fold_pat_slice
fold_pat_struct
fold_pat_tuple
fold_pat_tuple_struct
fold_pat_type
fold_pat_wild
fold_path
fold_path_arguments
fold_path_segment
fold_predicate_eq
fold_predicate_lifetime
fold_predicate_type
fold_qself
fold_range_limits
fold_receiver
fold_return_type
fold_signature
fold_span
fold_stmt
fold_trait_bound
fold_trait_bound_modifier
fold_trait_item
fold_trait_item_const
fold_trait_item_macro
fold_trait_item_method
fold_trait_item_type
fold_type
fold_type_array
fold_type_bare_fn
fold_type_group
fold_type_impl_trait
fold_type_infer
fold_type_macro
fold_type_never
fold_type_param
fold_type_param_bound
fold_type_paren
fold_type_path
fold_type_ptr
fold_type_reference
fold_type_slice
fold_type_trait_object
fold_type_tuple
fold_un_op
fold_use_glob
fold_use_group
fold_use_name
fold_use_path
fold_use_rename
fold_use_tree
fold_variadic
fold_variant
fold_vis_crate
fold_vis_public
fold_vis_restricted
fold_visibility
fold_where_clause
fold_where_predicate