pub trait Tree: Debug + AsAny {
// Provided method
fn declare_keywords(declare: impl FnMut(&'static str, Self))
where Self: Sized { ... }
}Expand description
A type that represents a generous parse tree.
The first parser pass is “generous” in the sense that it accepts a significant superset of the Welly language. If you parse a valid program, the generous parse tree will have the correct structure. If you parse a nearly valid program, you will hopefully nonetheless get a parse tree that is close to the one intended. This helps with reporting helpful errors.
There are many types that implement Tree. It would be inconvenient to
define an enum that can contain any of them. Instead, we use dyn Tree.
You can use the methods of dyn Tree to match its actual type:
use welly_parser::{Tree};
// Invent a new kind of `Tree`.
#[derive(Debug)]
struct Fruit(&'static str);
impl Tree for Fruit {}
// An example `Fruit` wrapped as a `dyn Tree`.
let tree: Box<dyn Tree> = Box::new(Fruit("Apple"));
// Test whether `tree` is a `Fruit`.
let is_fruit: bool = tree.is::<Fruit>();
println!("{}", is_fruit);
// Borrow the `Fruit`.
let borrowed_fruit: Option<&Fruit> = tree.downcast_ref::<Fruit>();
println!("{}", borrowed_fruit.expect("Not a Fruit").0);
// Move the `Fruit`.
let owned_fruit: Result<Box<Fruit>, Box<dyn Tree>> = tree.downcast::<Fruit>();
println!("{}", owned_fruit.expect("Not a Fruit").0);Provided Methods§
Sourcefn declare_keywords(declare: impl FnMut(&'static str, Self))where
Self: Sized,
fn declare_keywords(declare: impl FnMut(&'static str, Self))where
Self: Sized,
declare() all the keywords whose parse trees are Selfs.
The default implementation declares no keywords.
Implementations§
Trait Implementations§
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".