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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
macro_rules! dom_node_from {
    ($($inner:ty => $name:ident),*) => {
        $(
            impl From<$inner> for Node {
                fn from(inner: $inner) -> Self {
                    Node::$name(inner)
                }
            }
        )*
    };
}

macro_rules! dom_node {
    (
    $(#[$attrs:meta])*
    pub struct $name:ident {
        $(
            $(#[$field_attrs:meta])*
            $field_name:ident: $field_ty:ty,
        )*
    }) => {
    $(#[$attrs])*
    pub struct $name {
            syntax: SyntaxElement,
            $(
                $(#[$field_attrs])*
                $field_name: $field_ty,
            )*
        }

        impl NodeSyntax for $name {
            fn syntax(&self) -> SyntaxElement {
                self.syntax.clone()
            }
        }

        impl core::fmt::Display for $name {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                self.syntax.fmt(f)
            }
        }
    }
}

macro_rules! dom_node_no_display {
    (
    $(#[$attrs:meta])*
    pub struct $name:ident {
        $(
            $(#[$field_attrs:meta])*
            $field_name:ident: $field_ty:ty,
        )*
    }) => {
    $(#[$attrs])*
    pub struct $name {
            syntax: SyntaxElement,
            $(
                $(#[$field_attrs])*
                $field_name: $field_ty,
            )*
        }

        impl NodeSyntax for $name {
            fn syntax(&self) -> SyntaxElement {
                self.syntax.clone()
            }
        }
    }
}

macro_rules! dom_primitives {
    ($($($kind:ident)|* => $ast:ident),*) => {
        $(
            #[derive(Debug, Clone, PartialEq, Eq, Hash)]
            #[repr(transparent)]
            pub struct $ast(SyntaxElement);
            impl Cast for $ast {
                #[allow(unused)]
                fn cast(elem: SyntaxElement) -> Option<Self> {
                    match &elem {
                        rowan::NodeOrToken::Token(t) => {
                            match t.kind() {
                                $($kind)|* => {
                                    Some(Self(elem))
                                }
                                _ => {
                                    None
                                }
                            }
                        },
                        _ => {
                            None
                        }
                    }
                }
            }

            impl NodeSyntax for $ast {
                fn syntax(&self) -> SyntaxElement {
                    self.0.clone()
                }
            }
        )*
    };
}

macro_rules! dom_sealed {
    ($($id:ty,)*) => {
        $(impl Sealed for $id {})*
    };
}

macro_rules! rewrite_node_from {
    ($($inner:ty => $name:ident),*) => {
        $(
            impl From<$inner> for RewriteNode {
                fn from(inner: $inner) -> Self {
                    RewriteNode::New(Node::from(inner))
                }
            }

            impl From<$inner> for Node {
                fn from(inner: $inner) -> Self {
                    Node::$name(inner.into())
                }
            }
        )*
    };
}

macro_rules! rewrite_impl {
    ($(dom::$id:ident,)*) => {
        $(
            impl Rewrite for dom::$id {
                type Builder = builders::$id;

                fn rewrite() -> Self::Builder {
                    Self::Builder::new()
                }
            }
        )*
    };
}

macro_rules! rewrite_value_node_from {
    ($($v:ident => $id:ident,)*) => {
        $(
            impl From<OldOrNew<dom::$id>> for ValueNode {
                fn from(v: OldOrNew<dom::$id>) -> Self {
                    Self::$v(v)
                }
            }

            impl From<$id> for ValueNode {
                fn from(v: $id) -> Self {
                    ValueNode::$v(OldOrNew::New(v))
                }
            }
        )*

    };
}

macro_rules! impl_is {
    ($name:ident;$($method_name:ident() -> $variant:ident;)*) => {
        impl $name {
            $(
                pub fn $method_name(&self) -> bool {
                    matches!(self, $name::$variant(_))
                }
            )*
        }
    }
}