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§
- Visit
- Syntax tree traversal to walk a shared borrow of a syntax tree.
Functions§
- visit_
abi - visit_
angle_ bracketed_ generic_ arguments - visit_
assoc_ const - visit_
assoc_ type - visit_
attr_ style - visit_
attribute - visit_
bare_ fn_ arg - visit_
bare_ variadic - visit_
bin_ op - 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_ binary - visit_
expr_ call - visit_
expr_ cast - visit_
expr_ field - visit_
expr_ group - visit_
expr_ index - visit_
expr_ lit - visit_
expr_ macro - visit_
expr_ method_ call - visit_
expr_ paren - visit_
expr_ path - visit_
expr_ reference - visit_
expr_ struct - visit_
expr_ tuple - visit_
expr_ unary - visit_
field - visit_
field_ mutability - visit_
field_ value - visit_
fields - visit_
fields_ named - visit_
fields_ unnamed - visit_
generic_ argument - visit_
generic_ param - visit_
generics - visit_
ident - visit_
index - visit_
lifetime - visit_
lifetime_ param - visit_
lit - visit_
lit_ bool - visit_
lit_ byte - visit_
lit_ byte_ str - visit_
lit_ char - visit_
lit_ cstr - visit_
lit_ float - visit_
lit_ int - visit_
lit_ str - visit_
macro - visit_
macro_ delimiter - visit_
member - visit_
meta - visit_
meta_ list - visit_
meta_ name_ value - visit_
parenthesized_ generic_ arguments - visit_
path - visit_
path_ arguments - visit_
path_ segment - visit_
predicate_ lifetime - visit_
predicate_ type - visit_
qself - visit_
return_ type - visit_
span - visit_
trait_ bound - visit_
trait_ bound_ modifier - 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_
variant - visit_
vis_ restricted - visit_
visibility - visit_
where_ clause - visit_
where_ predicate