Module syn::visit[][src]

This is supported on crate feature visit only.
Expand description

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

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

Functions

visit_abiderive or full
visit_attr_stylederive or full
visit_attributederive or full
visit_bare_fn_argderive or full
visit_bin_opderive or full
visit_bindingderive or full
visit_bound_lifetimesderive or full
visit_const_paramderive or full
visit_constraintderive or full
visit_exprderive or full
visit_expr_binaryderive or full
visit_expr_callderive or full
visit_expr_castderive or full
visit_expr_fieldderive or full
visit_expr_indexderive or full
visit_expr_litderive or full
visit_expr_parenderive or full
visit_expr_pathderive or full
visit_expr_unaryderive or full
visit_fieldderive or full
visit_fieldsderive or full
visit_fields_namedderive or full
visit_fields_unnamedderive or full
visit_generic_paramderive or full
visit_genericsderive or full
visit_indexderive or full
visit_lifetime_defderive or full
visit_macroderive or full
visit_macro_delimiterderive or full
visit_memberderive or full
visit_metaderive or full
visit_meta_listderive or full
visit_meta_name_valuederive or full
visit_nested_metaderive or full
visit_pathderive or full
visit_path_argumentsderive or full
visit_path_segmentderive or full
visit_predicate_eqderive or full
visit_predicate_typederive or full
visit_qselfderive or full
visit_return_typederive or full
visit_trait_boundderive or full
visit_typederive or full
visit_type_arrayderive or full
visit_type_bare_fnderive or full
visit_type_groupderive or full
visit_type_impl_traitderive or full
visit_type_inferderive or full
visit_type_macroderive or full
visit_type_neverderive or full
visit_type_paramderive or full
visit_type_parenderive or full
visit_type_pathderive or full
visit_type_ptrderive or full
visit_type_referencederive or full
visit_type_slicederive or full
visit_type_tuplederive or full
visit_un_opderive or full
visit_variadicderive or full
visit_variantderive or full
visit_vis_cratederive or full
visit_vis_publicderive or full
visit_vis_restrictedderive or full
visit_visibilityderive or full
visit_where_clausederive or full
visit_where_predicatederive or full