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 derive
orfull
- 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