syntree

Macro tree

Source
macro_rules! tree {
    (@o $b:ident,) => { ... };
    (@o $b:ident, ($expr:expr, ($start:expr, $end:expr)) $(,)?) => { ... };
    (@o $b:ident, ($expr:expr, $len:expr) $(,)?) => { ... };
    (@o $b:ident, ($expr:expr, ($start:expr, $end:expr)), $($rest:tt)*) => { ... };
    (@o $b:ident, ($expr:expr, $len:expr), $($rest:tt)*) => { ... };
    (@o $b:ident, $expr:expr $(,)?) => { ... };
    (@o $b:ident, $expr:expr, $($rest:tt)*) => { ... };
    (@o $b:ident, ($expr:expr, ($start:expr, $end:expr)) => { $($tt:tt)* } $(,)?) => { ... };
    (@o $b:ident, $expr:expr => { $($tt:tt)* } $(,)?) => { ... };
    (@o $b:ident, ($expr:expr, ($start:expr, $end:expr)) => { $($tt:tt)* }, $($rest:tt)*) => { ... };
    (@o $b:ident, $expr:expr => { $($tt:tt)* }, $($rest:tt)*) => { ... };
    ($($tt:tt)*) => { ... };
}
Expand description

Helper macro for building a tree in place.

ยงExamples

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Syntax {
    Root,
    Number,
    Lit,
    Whitespace,
}

let mut tree = syntree::Builder::new();

tree.open(Syntax::Root)?;

tree.open(Syntax::Number)?;
tree.token(Syntax::Lit, 1)?;
tree.close()?;

tree.token(Syntax::Whitespace, 3)?;

tree.open(Syntax::Number)?;
tree.token(Syntax::Lit, 2)?;
tree.token_empty(Syntax::Lit)?;
tree.close()?;

tree.close()?;

let tree = tree.build()?;

let expected = syntree::tree! {
    Syntax::Root => {
        Syntax::Number => {
            (Syntax::Lit, 1)
        },
        (Syntax::Whitespace, 3),
        Syntax::Number => {
            (Syntax::Lit, 2),
            Syntax::Lit
        }
    }
};

assert_eq!(expected, tree);