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§
- Visit
- Syntax tree traversal to walk a shared borrow of a syntax tree.
Functions§
- visit_
abi deriveorfull - visit_
angle_ bracketed_ generic_ arguments deriveorfull - visit_
arm full - visit_
assoc_ const deriveorfull - visit_
assoc_ type deriveorfull - visit_
attr_ style deriveorfull - visit_
attribute deriveorfull - visit_
bare_ fn_ arg deriveorfull - visit_
bare_ variadic deriveorfull - visit_
bin_ op deriveorfull - visit_
block full - visit_
bound_ lifetimes deriveorfull - visit_
captured_ param full - visit_
const_ param deriveorfull - visit_
constraint deriveorfull - visit_
data derive - visit_
data_ enum derive - visit_
data_ struct derive - visit_
data_ union derive - visit_
derive_ input derive - visit_
expr deriveorfull - visit_
expr_ array full - visit_
expr_ assign full - visit_
expr_ async full - visit_
expr_ await full - visit_
expr_ binary deriveorfull - visit_
expr_ block full - visit_
expr_ break full - visit_
expr_ call deriveorfull - visit_
expr_ cast deriveorfull - visit_
expr_ closure full - visit_
expr_ const full - visit_
expr_ continue full - visit_
expr_ field deriveorfull - visit_
expr_ for_ loop full - visit_
expr_ group deriveorfull - visit_
expr_ if full - visit_
expr_ index deriveorfull - visit_
expr_ infer full - visit_
expr_ let full - visit_
expr_ lit deriveorfull - visit_
expr_ loop full - visit_
expr_ macro deriveorfull - visit_
expr_ match full - visit_
expr_ method_ call deriveorfull - visit_
expr_ paren deriveorfull - visit_
expr_ path deriveorfull - visit_
expr_ range full - visit_
expr_ raw_ addr full - visit_
expr_ reference deriveorfull - visit_
expr_ repeat full - visit_
expr_ return full - visit_
expr_ struct deriveorfull - visit_
expr_ try full - visit_
expr_ try_ block full - visit_
expr_ tuple deriveorfull - visit_
expr_ unary deriveorfull - visit_
expr_ unsafe full - visit_
expr_ while full - visit_
expr_ yield full - visit_
field deriveorfull - visit_
field_ mutability deriveorfull - visit_
field_ pat full - visit_
field_ value deriveorfull - visit_
fields deriveorfull - visit_
fields_ named deriveorfull - visit_
fields_ unnamed deriveorfull - visit_
file full - visit_
fn_ arg full - visit_
foreign_ item full - visit_
foreign_ item_ fn full - visit_
foreign_ item_ macro full - visit_
foreign_ item_ static full - visit_
foreign_ item_ type full - visit_
generic_ argument deriveorfull - visit_
generic_ param deriveorfull - visit_
generics deriveorfull - visit_
ident - visit_
impl_ item full - visit_
impl_ item_ const full - visit_
impl_ item_ fn full - visit_
impl_ item_ macro full - visit_
impl_ item_ type full - visit_
impl_ restriction full - visit_
index deriveorfull - visit_
item full - visit_
item_ const full - visit_
item_ enum full - visit_
item_ extern_ crate full - visit_
item_ fn full - visit_
item_ foreign_ mod full - visit_
item_ impl full - visit_
item_ macro full - visit_
item_ mod full - visit_
item_ static full - visit_
item_ struct full - visit_
item_ trait full - visit_
item_ trait_ alias full - visit_
item_ type full - visit_
item_ union full - visit_
item_ use full - visit_
label full - visit_
lifetime - visit_
lifetime_ param deriveorfull - 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_
local full - visit_
local_ init full - visit_
macro deriveorfull - visit_
macro_ delimiter deriveorfull - visit_
member deriveorfull - visit_
meta deriveorfull - visit_
meta_ list deriveorfull - visit_
meta_ name_ value deriveorfull - visit_
parenthesized_ generic_ arguments deriveorfull - visit_
pat full - visit_
pat_ ident full - visit_
pat_ or full - visit_
pat_ paren full - visit_
pat_ reference full - visit_
pat_ rest full - visit_
pat_ slice full - visit_
pat_ struct full - visit_
pat_ tuple full - visit_
pat_ tuple_ struct full - visit_
pat_ type full - visit_
pat_ wild full - visit_
path deriveorfull - visit_
path_ arguments deriveorfull - visit_
path_ segment deriveorfull - visit_
pointer_ mutability full - visit_
precise_ capture full - visit_
predicate_ lifetime deriveorfull - visit_
predicate_ type deriveorfull - visit_
qself deriveorfull - visit_
range_ limits full - visit_
receiver full - visit_
return_ type deriveorfull - visit_
signature full - visit_
span - visit_
static_ mutability full - visit_
stmt full - visit_
stmt_ macro full - visit_
trait_ bound deriveorfull - visit_
trait_ bound_ modifier deriveorfull - visit_
trait_ item full - visit_
trait_ item_ const full - visit_
trait_ item_ fn full - visit_
trait_ item_ macro full - visit_
trait_ item_ type full - visit_
type deriveorfull - visit_
type_ array deriveorfull - visit_
type_ bare_ fn deriveorfull - visit_
type_ group deriveorfull - visit_
type_ impl_ trait deriveorfull - visit_
type_ infer deriveorfull - visit_
type_ macro deriveorfull - visit_
type_ never deriveorfull - visit_
type_ param deriveorfull - visit_
type_ param_ bound deriveorfull - visit_
type_ paren deriveorfull - visit_
type_ path deriveorfull - visit_
type_ ptr deriveorfull - visit_
type_ reference deriveorfull - visit_
type_ slice deriveorfull - visit_
type_ trait_ object deriveorfull - visit_
type_ tuple deriveorfull - visit_
un_ op deriveorfull - 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 deriveorfull - visit_
vis_ restricted deriveorfull - visit_
visibility deriveorfull - visit_
where_ clause deriveorfull - visit_
where_ predicate deriveorfull