pub trait Host {
// Required method
fn visit<V>(&self, visitor: &mut V)
where V: Visitor;
}
Expand description
Part of the AST that can be visited.
You shouldn’t have to worry about this type nor how to implement it – it’s completely implemented for you. However, it works in a pretty simple way: any implementor of the host trait can be used with a visitor.
The idea is that visiting an AST node is a two-step process:
- First, you can get your visitor called once as soon as an interesting node gets visited. For instance, if your visitor has an implementation for visiting expressions, everytime an expression gets visited, your visitor will run.
- If your implementation of visiting an AST node returns
Visit::Children
and if the given node has children, the visitor will go deeper, invoking other calls if you have defined any. A typical pattern you might want to do is to implement your visitor to gets run on all typenames. Since expressions contains variables, you will get your visitor called once again there. - Notice that since visitors are mutable, you can accumulate a state as you go deeper in the AST to implement various checks and validations.
Note that this trait exists in two versions: an immutable one, Host
, which doesn’t allow you to mutate the
AST (but takes a &
), and a mutable one, HostMut
, which allows for AST mutation.
Required Methods§
Object Safety§
This trait is not object safe.