Module syn::visit[][src]

Syntax tree traversal to walk a shared borrow of a syntax tree.

Each method of the Visit trait is a hook that can be overridden to customize the behavior when visiting 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 Visit<'ast> {
    /* ... */

    fn visit_expr_binary(&mut self, node: &'ast ExprBinary) {
        visit_expr_binary(self, node);
    }

    /* ... */
}

pub fn visit_expr_binary<'ast, V>(v: &mut V, node: &'ast ExprBinary)
where
    V: Visit<'ast> + ?Sized,
{
    for attr in &node.attrs {
        v.visit_attribute(attr);
    }
    v.visit_expr(&*node.left);
    v.visit_bin_op(&node.op);
    v.visit_expr(&*node.right);
}

/* ... */

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


Example

This visitor will print the name of every freestanding function in the syntax tree, including nested functions.

// [dependencies]
// quote = "1.0"
// syn = { version = "1.0", features = ["full", "visit"] }

use quote::quote;
use syn::visit::{self, Visit};
use syn::{File, ItemFn};

struct FnVisitor;

impl<'ast> Visit<'ast> for FnVisitor {
    fn visit_item_fn(&mut self, node: &'ast ItemFn) {
        println!("Function with name={}", node.sig.ident);

        // Delegate to the default impl to visit any nested functions.
        visit::visit_item_fn(self, node);
    }
}

fn main() {
    let code = quote! {
        pub fn f() {
            fn g() {}
        }
    };

    let syntax_tree: File = syn::parse2(code).unwrap();
    FnVisitor.visit_file(&syntax_tree);
}

The 'ast lifetime on the input references means that the syntax tree outlives the complete recursive visit call, so the visitor is allowed to hold on to references into the syntax tree.

use quote::quote;
use syn::visit::{self, Visit};
use syn::{File, ItemFn};

struct FnVisitor<'ast> {
    functions: Vec<&'ast ItemFn>,
}

impl<'ast> Visit<'ast> for FnVisitor<'ast> {
    fn visit_item_fn(&mut self, node: &'ast ItemFn) {
        self.functions.push(node);
        visit::visit_item_fn(self, node);
    }
}

fn main() {
    let code = quote! {
        pub fn f() {
            fn g() {}
        }
    };

    let syntax_tree: File = syn::parse2(code).unwrap();
    let mut visitor = FnVisitor { functions: Vec::new() };
    visitor.visit_file(&syntax_tree);
    for f in visitor.functions {
        println!("Function with name={}", f.sig.ident);
    }
}

Traits

Visit

Syntax tree traversal to walk a shared borrow of a syntax tree.

Functions

visit_abi
visit_angle_bracketed_generic_arguments
visit_arm
visit_attr_style
visit_attribute
visit_bare_fn_arg
visit_bin_op
visit_binding
visit_block
visit_bound_lifetimes
visit_const_param
visit_constraint
visit_data
visit_data_enum
visit_data_struct
visit_data_union
visit_derive_input
visit_expr
visit_expr_array
visit_expr_assign
visit_expr_assign_op
visit_expr_async
visit_expr_await
visit_expr_binary
visit_expr_block
visit_expr_box
visit_expr_break
visit_expr_call
visit_expr_cast
visit_expr_closure
visit_expr_continue
visit_expr_field
visit_expr_for_loop
visit_expr_group
visit_expr_if
visit_expr_index
visit_expr_let
visit_expr_lit
visit_expr_loop
visit_expr_macro
visit_expr_match
visit_expr_method_call
visit_expr_paren
visit_expr_path
visit_expr_range
visit_expr_reference
visit_expr_repeat
visit_expr_return
visit_expr_struct
visit_expr_try
visit_expr_try_block
visit_expr_tuple
visit_expr_type
visit_expr_unary
visit_expr_unsafe
visit_expr_while
visit_expr_yield
visit_field
visit_field_pat
visit_field_value
visit_fields
visit_fields_named
visit_fields_unnamed
visit_file
visit_fn_arg
visit_foreign_item
visit_foreign_item_fn
visit_foreign_item_macro
visit_foreign_item_static
visit_foreign_item_type
visit_generic_argument
visit_generic_method_argument
visit_generic_param
visit_generics
visit_ident
visit_impl_item
visit_impl_item_const
visit_impl_item_macro
visit_impl_item_method
visit_impl_item_type
visit_index
visit_item
visit_item_const
visit_item_enum
visit_item_extern_crate
visit_item_fn
visit_item_foreign_mod
visit_item_impl
visit_item_macro
visit_item_macro2
visit_item_mod
visit_item_static
visit_item_struct
visit_item_trait
visit_item_trait_alias
visit_item_type
visit_item_union
visit_item_use
visit_label
visit_lifetime
visit_lifetime_def
visit_lit
visit_lit_bool
visit_lit_byte
visit_lit_byte_str
visit_lit_char
visit_lit_float
visit_lit_int
visit_lit_str
visit_local
visit_macro
visit_macro_delimiter
visit_member
visit_meta
visit_meta_list
visit_meta_name_value
visit_method_turbofish
visit_nested_meta
visit_parenthesized_generic_arguments
visit_pat
visit_pat_box
visit_pat_ident
visit_pat_lit
visit_pat_macro
visit_pat_or
visit_pat_path
visit_pat_range
visit_pat_reference
visit_pat_rest
visit_pat_slice
visit_pat_struct
visit_pat_tuple
visit_pat_tuple_struct
visit_pat_type
visit_pat_wild
visit_path
visit_path_arguments
visit_path_segment
visit_predicate_eq
visit_predicate_lifetime
visit_predicate_type
visit_qself
visit_range_limits
visit_receiver
visit_return_type
visit_signature
visit_span
visit_stmt
visit_trait_bound
visit_trait_bound_modifier
visit_trait_item
visit_trait_item_const
visit_trait_item_macro
visit_trait_item_method
visit_trait_item_type
visit_type
visit_type_array
visit_type_bare_fn
visit_type_group
visit_type_impl_trait
visit_type_infer
visit_type_macro
visit_type_never
visit_type_param
visit_type_param_bound
visit_type_paren
visit_type_path
visit_type_ptr
visit_type_reference
visit_type_slice
visit_type_trait_object
visit_type_tuple
visit_un_op
visit_use_glob
visit_use_group
visit_use_name
visit_use_path
visit_use_rename
visit_use_tree
visit_variadic
visit_variant
visit_vis_crate
visit_vis_public
visit_vis_restricted
visit_visibility
visit_where_clause
visit_where_predicate