Available 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);
}
/* ... */
§Example
This visitor will print the name of every freestanding function in the syntax tree, including nested functions.
// [dependencies]
// quote = "1.0"
// syn = { version = "2.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_abi
derive
orfull
- visit_angle_bracketed_generic_arguments
derive
orfull
- visit_arm
full
- visit_assoc_const
derive
orfull
- visit_assoc_type
derive
orfull
- visit_attr_style
derive
orfull
- visit_attribute
derive
orfull
- visit_bare_fn_arg
derive
orfull
- visit_bare_variadic
derive
orfull
- visit_bin_op
derive
orfull
- visit_block
full
- visit_bound_lifetimes
derive
orfull
- visit_const_param
derive
orfull
- visit_constraint
derive
orfull
- visit_data
derive
- visit_data_enum
derive
- visit_data_struct
derive
- visit_data_union
derive
- visit_derive_input
derive
- visit_expr
derive
orfull
- visit_expr_array
full
- visit_expr_async
full
- visit_expr_await
full
- visit_expr_binary
derive
orfull
- visit_expr_block
full
- visit_expr_break
full
- visit_expr_call
derive
orfull
- visit_expr_cast
derive
orfull
- visit_expr_const
full
- visit_expr_field
derive
orfull
- visit_expr_group
derive
orfull
- visit_expr_if
full
- visit_expr_index
derive
orfull
- visit_expr_infer
full
- visit_expr_let
full
- visit_expr_lit
derive
orfull
- visit_expr_loop
full
- visit_expr_macro
derive
orfull
- visit_expr_match
full
- visit_expr_method_call
derive
orfull
- visit_expr_paren
derive
orfull
- visit_expr_path
derive
orfull
- visit_expr_range
full
- visit_expr_reference
derive
orfull
- visit_expr_struct
derive
orfull
- visit_expr_try
full
- visit_expr_tuple
full
- visit_expr_unary
derive
orfull
- visit_expr_while
full
- visit_expr_yield
full
- visit_field
derive
orfull
- visit_field_mutability
derive
orfull
- visit_field_pat
full
- visit_field_value
derive
orfull
- visit_fields
derive
orfull
- visit_fields_named
derive
orfull
- visit_fields_unnamed
derive
orfull
- visit_file
full
- visit_fn_arg
full
- visit_generic_argument
derive
orfull
- visit_generic_param
derive
orfull
- visit_generics
derive
orfull
- visit_impl_item
full
- visit_index
derive
orfull
- visit_item
full
- visit_item_const
full
- visit_item_enum
full
- visit_item_fn
full
- visit_item_impl
full
- visit_item_macro
full
- visit_item_mod
full
- visit_item_trait
full
- visit_item_type
full
- visit_item_union
full
- visit_item_use
full
- visit_label
full
- visit_lifetime_param
derive
orfull
- visit_local
full
- visit_local_init
full
- visit_macro
derive
orfull
- visit_macro_delimiter
derive
orfull
- visit_member
derive
orfull
- visit_meta
derive
orfull
- visit_meta_list
derive
orfull
- visit_meta_name_value
derive
orfull
- visit_parenthesized_generic_arguments
derive
orfull
- visit_pat
full
- visit_pat_ident
full
- visit_pat_or
full
- visit_pat_paren
full
- visit_pat_rest
full
- visit_pat_slice
full
- visit_pat_struct
full
- visit_pat_tuple
full
- visit_pat_type
full
- visit_pat_wild
full
- visit_path
derive
orfull
- visit_path_arguments
derive
orfull
- visit_path_segment
derive
orfull
- visit_predicate_lifetime
derive
orfull
- visit_predicate_type
derive
orfull
- visit_qself
derive
orfull
- visit_receiver
full
- visit_return_type
derive
orfull
- visit_signature
full
- visit_stmt
full
- visit_stmt_macro
full
- visit_trait_bound
derive
orfull
- visit_trait_bound_modifier
derive
orfull
- visit_trait_item
full
- visit_type
derive
orfull
- visit_type_array
derive
orfull
- visit_type_bare_fn
derive
orfull
- visit_type_group
derive
orfull
- visit_type_impl_trait
derive
orfull
- visit_type_infer
derive
orfull
- visit_type_macro
derive
orfull
- visit_type_never
derive
orfull
- visit_type_param
derive
orfull
- visit_type_param_bound
derive
orfull
- visit_type_paren
derive
orfull
- visit_type_path
derive
orfull
- visit_type_ptr
derive
orfull
- visit_type_reference
derive
orfull
- visit_type_slice
derive
orfull
- visit_type_trait_object
derive
orfull
- visit_type_tuple
derive
orfull
- visit_un_op
derive
orfull
- visit_use_glob
full
- visit_use_group
full
- visit_use_name
full
- visit_use_path
full
- visit_use_rename
full
- visit_use_tree
full
- visit_variadic
full
- visit_variant
derive
orfull
- visit_vis_restricted
derive
orfull
- visit_visibility
derive
orfull
- visit_where_clause
derive
orfull
- visit_where_predicate
derive
orfull