Skip to main content

Tree

Trait Tree 

Source
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§

Source

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§

Source§

impl dyn Tree

Source

pub fn is<T: Tree>(&self) -> bool

Returns true if the actual type of self is T.

Source

pub fn downcast_ref<T: Tree>(&self) -> Option<&T>

Returns Some if the actual type of self is T.

Source

pub fn downcast<T: Tree>(self: Box<Self>) -> Result<Box<T>, Box<Self>>

Returns Ok if the actual type of self is T.

Trait Implementations§

Source§

impl<T: Tree + PartialEq> PartialEq<T> for dyn Tree

Source§

fn eq(&self, other: &T) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl Tree for ()

Source§

impl Tree for char

Implementors§