syntree/
macros.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/// 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);
/// # Ok::<_,  Box<dyn core::error::Error>>(())
/// ```
#[macro_export]
macro_rules! tree {
    (@o $b:ident,) => {};

    (@o $b:ident, ($expr:expr, ($start:expr, $end:expr)) $(,)?) => {{
        $b.token_with($expr, $crate::Span::new($start, $end))?;
    }};

    (@o $b:ident, ($expr:expr, $len:expr) $(,)?) => {{
        $b.token($expr, $len)?;
    }};

    (@o $b:ident, ($expr:expr, ($start:expr, $end:expr)), $($rest:tt)*) => {{
        $b.token_with($expr, $crate::Span::new($start, $end))?;
        $crate::tree!(@o $b, $($rest)*);
    }};

    (@o $b:ident, ($expr:expr, $len:expr), $($rest:tt)*) => {{
        $b.token($expr, $len)?;
        $crate::tree!(@o $b, $($rest)*);
    }};

    (@o $b:ident, $expr:expr $(,)?) => {{
        $b.token_empty($expr)?;
    }};

    (@o $b:ident, $expr:expr, $($rest:tt)*) => {{
        $b.token_empty($expr)?;
        $crate::tree!(@o $b, $($rest)*);
    }};

    (@o $b:ident, ($expr:expr, ($start:expr, $end:expr)) => { $($tt:tt)* } $(,)?) => {{
        $b.open_with($expr, $crate::Span::new($start, $end))?;
        $crate::tree!(@o $b, $($tt)*);
        $b.close()?;
    }};

    (@o $b:ident, $expr:expr => { $($tt:tt)* } $(,)?) => {{
        $b.open($expr)?;
        $crate::tree!(@o $b, $($tt)*);
        $b.close()?;
    }};

    (@o $b:ident, ($expr:expr, ($start:expr, $end:expr)) => { $($tt:tt)* }, $($rest:tt)*) => {{
        $b.open_with($expr, $crate::Span::new($start, $end))?;
        $crate::tree!(@o $b, $($tt)*);
        $b.close()?;
        $crate::tree!(@o $b, $($rest)*);
    }};

    (@o $b:ident, $expr:expr => { $($tt:tt)* }, $($rest:tt)*) => {{
        $b.open($expr)?;
        $crate::tree!(@o $b, $($tt)*);
        $b.close()?;
        $crate::tree!(@o $b, $($rest)*);
    }};

    ($($tt:tt)*) => {{
        let mut b = $crate::Builder::new();
        $crate::tree!(@o b, $($tt)*);
        b.build()?
    }};
}

/// Helper macro for building a tree in place with a custom span.
///
/// # Examples
///
/// ```
/// use syntree::{Empty, EmptyVec, Tree, TreeIndex};
///
/// syntree::flavor! {
///     struct FlavorEmpty {
///         type Index = Empty;
///         type Indexes = EmptyVec<TreeIndex<Self>>;
///     }
/// };
///
/// let tree: Tree<_, FlavorEmpty> = syntree::tree_with! {
///     "root" => {
///         "child" => {
///             "token"
///         },
///         "child2"
///     }
/// };
///
/// let expected: Tree<_, FlavorEmpty> = syntree::tree_with! {
///     "root" => {
///         "child" => {
///             ("token", Empty)
///         },
///         "child2"
///     }
/// };
///
/// assert_eq!(tree, expected);
/// # Ok::<_,  Box<dyn core::error::Error>>(())
/// ```
#[macro_export]
macro_rules! tree_with {
    ($($tt:tt)*) => {{
        let mut b = $crate::Builder::new_with();
        $crate::tree!(@o b, $($tt)*);
        b.build()?
    }};
}