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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
use std::{
    convert::TryFrom,
    fmt::{self, Display},
};

use proc_macro2::Punct;
use syn::{
    ext::IdentExt,
    parse::{discouraged::Speculative, Parse, ParseStream, Peek},
    punctuated::{Pair, Punctuated},
    token::{Brace, Colon, Dot, PathSep},
    Block, ExprPath, Ident, LitInt, Path, PathSegment,
};

use super::{atoms::tokens::Dash, path_to_string};
use crate::{node::parse::block_expr, Error};

#[derive(Clone, Debug, syn_derive::Parse, syn_derive::ToTokens)]
pub enum NodeNameFragment {
    #[parse(peek = Ident::peek_any)]
    Ident(#[parse(Ident::parse_any)] Ident),
    #[parse(peek = LitInt)]
    Literal(LitInt),
    // In case when name contain more than one Punct in series
    Empty,
}

impl PartialEq<NodeNameFragment> for NodeNameFragment {
    fn eq(&self, other: &NodeNameFragment) -> bool {
        match (self, other) {
            (NodeNameFragment::Ident(s), NodeNameFragment::Ident(o)) => s == o,
            // compare literals by their string representation
            // So 0x00 and 0 is would be different literals.
            (NodeNameFragment::Literal(s), NodeNameFragment::Literal(o)) => {
                s.to_string() == o.to_string()
            }
            (NodeNameFragment::Empty, NodeNameFragment::Empty) => true,
            _ => false,
        }
    }
}
impl Eq for NodeNameFragment {}

impl Display for NodeNameFragment {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            NodeNameFragment::Ident(i) => i.fmt(f),
            NodeNameFragment::Literal(l) => l.fmt(f),
            NodeNameFragment::Empty => Ok(()),
        }
    }
}

/// Name of the node.
#[derive(Clone, Debug, syn_derive::ToTokens)]
pub enum NodeName {
    /// A plain identifier like `div` is a path of length 1, e.g. `<div />`. Can
    /// be separated by double colons, e.g. `<foo::bar />`.
    Path(ExprPath),

    ///
    /// Name separated by punctuation, e.g. `<div data-foo="bar" />` or `<div
    /// data:foo="bar" />`.
    ///
    /// It is fully compatible with SGML (ID/NAME) tokens format.
    /// Which is described as follow:
    /// ID and NAME tokens must begin with a letter ([A-Za-z]) and may be
    /// followed by any number of letters, digits ([0-9]), hyphens ("-"),
    /// underscores ("_"), colons (":"), and periods (".").
    ///
    /// Support more than one punctuation in series, in this case
    /// `NodeNameFragment::Empty` would be used.
    ///
    /// Note: that punct and `NodeNameFragment` has different `Spans` and IDE
    /// (rust-analyzer/idea) can controll them independently.
    /// So if one needs to add semantic highlight or go-to definition to entire
    /// `NodeName` it should emit helper statements for each `Punct` and
    /// `NodeNameFragment` (excludeing `Empty` fragment).
    Punctuated(Punctuated<NodeNameFragment, Punct>),

    /// Arbitrary rust code in braced `{}` blocks.
    Block(Block),
}

impl NodeName {
    /// Returns true if `NodeName` parsed as block of code.
    ///
    /// Example:
    /// {"Foo"}
    pub fn is_block(&self) -> bool {
        matches!(self, Self::Block(_))
    }

    /// Returns true if `NodeName` is dash seperated.
    ///
    /// Example:
    /// foo-bar
    pub fn is_dashed(&self) -> bool {
        match self {
            Self::Punctuated(p) => {
                let p = p.pairs().next().unwrap();
                p.punct().unwrap().as_char() == '-'
            }
            _ => false,
        }
    }

    /// Returns true if `NodeName` is wildcard ident.
    ///
    /// Example:
    /// _
    pub fn is_wildcard(&self) -> bool {
        match self {
            Self::Path(e) => {
                if e.path.segments.len() != 1 {
                    return false;
                }
                let Some(last_ident) = e.path.segments.last() else {
                    return false;
                };
                last_ident.ident == "_"
            }
            _ => false,
        }
    }

    /// Parse the stream as punctuated idents.
    ///
    /// We can't replace this with [`Punctuated::parse_separated_nonempty`]
    /// since that doesn't support reserved keywords. Might be worth to
    /// consider a PR upstream.
    ///
    /// [`Punctuated::parse_separated_nonempty`]: https://docs.rs/syn/1.0.58/syn/punctuated/struct.Punctuated.html#method.parse_separated_nonempty
    pub(crate) fn node_name_punctuated_ident<T: Parse, F: Peek, X: From<Ident>>(
        input: ParseStream,
        punct: F,
    ) -> syn::Result<Punctuated<X, T>> {
        let fork = &input.fork();
        let mut segments = Punctuated::<X, T>::new();

        while !fork.is_empty() && fork.peek(Ident::peek_any) {
            let ident = Ident::parse_any(fork)?;
            segments.push_value(ident.clone().into());

            if fork.peek(punct) {
                segments.push_punct(fork.parse()?);
            } else {
                break;
            }
        }

        if segments.len() > 1 {
            input.advance_to(fork);
            Ok(segments)
        } else {
            Err(fork.error("expected punctuated node name"))
        }
    }

    /// Parse the stream as punctuated idents, with two possible punctuations
    /// available
    pub(crate) fn node_name_punctuated_ident_with_two_alternate<
        T: Parse,
        F: Peek,
        G: Peek,
        H: Peek,
        X: From<NodeNameFragment>,
    >(
        input: ParseStream,
        punct: F,
        alternate_punct: G,
        alternate_punct2: H,
    ) -> syn::Result<Punctuated<X, T>> {
        let fork = &input.fork();
        let mut segments = Punctuated::<X, T>::new();

        while !fork.is_empty() {
            let ident = NodeNameFragment::parse(fork)?;
            segments.push_value(ident.clone().into());

            if fork.peek(punct) || fork.peek(alternate_punct) || fork.peek(alternate_punct2) {
                segments.push_punct(fork.parse()?);
            } else {
                break;
            }
        }

        if segments.len() > 1 {
            input.advance_to(fork);
            Ok(segments)
        } else {
            Err(fork.error("expected punctuated node name"))
        }
    }
}

impl TryFrom<&NodeName> for Block {
    type Error = Error;

    fn try_from(node: &NodeName) -> Result<Self, Self::Error> {
        match node {
            NodeName::Block(b) => Ok(b.to_owned()),
            _ => Err(Error::TryFrom(
                "NodeName does not match NodeName::Block(Expr::Block(_))".into(),
            )),
        }
    }
}

impl PartialEq for NodeName {
    fn eq(&self, other: &NodeName) -> bool {
        match self {
            Self::Path(this) => match other {
                Self::Path(other) => this == other,
                _ => false,
            },
            // can't be derived automatically because `Punct` doesn't impl `PartialEq`
            Self::Punctuated(this) => match other {
                Self::Punctuated(other) => {
                    this.pairs()
                        .zip(other.pairs())
                        .all(|(this, other)| match (this, other) {
                            (
                                Pair::Punctuated(this_ident, this_punct),
                                Pair::Punctuated(other_ident, other_punct),
                            ) => {
                                this_ident == other_ident
                                    && this_punct.as_char() == other_punct.as_char()
                            }
                            (Pair::End(this), Pair::End(other)) => this == other,
                            _ => false,
                        })
                }
                _ => false,
            },
            Self::Block(this) => match other {
                Self::Block(other) => this == other,
                _ => false,
            },
        }
    }
}

impl fmt::Display for NodeName {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}",
            match self {
                NodeName::Path(expr) => path_to_string(expr),
                NodeName::Punctuated(name) => {
                    name.pairs()
                        .flat_map(|pair| match pair {
                            Pair::Punctuated(ident, punct) => {
                                [ident.to_string(), punct.to_string()]
                            }
                            Pair::End(ident) => [ident.to_string(), "".to_string()],
                        })
                        .collect::<String>()
                }
                NodeName::Block(_) => String::from("{}"),
            }
        )
    }
}

impl Parse for NodeName {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        if input.peek(LitInt) {
            Err(syn::Error::new(
                input.span(),
                "Name must start with latin character",
            ))
        } else if input.peek2(PathSep) {
            NodeName::node_name_punctuated_ident::<PathSep, fn(_) -> PathSep, PathSegment>(
                input, PathSep,
            )
            .map(|segments| {
                NodeName::Path(ExprPath {
                    attrs: vec![],
                    qself: None,
                    path: Path {
                        leading_colon: None,
                        segments,
                    },
                })
            })
        } else if input.peek2(Colon) || input.peek2(Dash) || input.peek2(Dot) {
            NodeName::node_name_punctuated_ident_with_two_alternate::<
                Punct,
                fn(_) -> Colon,
                fn(_) -> Dash,
                fn(_) -> Dot,
                NodeNameFragment,
            >(input, Colon, Dash, Dot)
            .map(NodeName::Punctuated)
        } else if input.peek(Brace) {
            let fork = &input.fork();
            let value = block_expr(fork)?;
            input.advance_to(fork);
            Ok(NodeName::Block(value))
        } else if input.peek(Ident::peek_any) {
            let mut segments = Punctuated::new();
            let ident = Ident::parse_any(input)?;
            segments.push_value(PathSegment::from(ident));
            Ok(NodeName::Path(ExprPath {
                attrs: vec![],
                qself: None,
                path: Path {
                    leading_colon: None,
                    segments,
                },
            }))
        } else {
            Err(input.error("invalid tag name or attribute key"))
        }
    }
}