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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430

extern crate proc_macro;
use proc_macro::{TokenStream, TokenTree, Ident, Delimiter, Group, Literal, Span};

/*#[proc_macro]
pub fn make_answer(item: TokenStream) -> TokenStream {

    dbg!(item);
    dbg!(32);
    //return item;

    //panic!("ciao");
    //println!("ciao");

    "fn answer() -> u32 { 42 }".parse().unwrap()
}*/

#[proc_macro]
pub fn html(item: TokenStream) -> TokenStream {

    let tokens: Vec<TokenTree> = item.into_iter().collect();

    if false {
        for token in &tokens {
            match token {
                TokenTree::Ident(ident) => {
                    println!("Ident: {}", ident);
                }
                TokenTree::Literal(literal) => {
                    println!("Literal: {}", literal);
                }
                TokenTree::Punct(punct) => {
                    println!("Punct: {}", punct);
                }
                TokenTree::Group(group) => {
                    println!("Group: {}", group);
                }
            }
        }
    }

    let elements = parse_mutliple_elements(&tokens);

    if elements.is_none() {
        panic!("Invalid HTML syntax");
    }

    let elements = elements.unwrap();

    //dbg!(element);

    fn elements_to_token_stream(elements: &[Element]) -> TokenStream {

        let mut tokens = Vec::new();

        tokens.push(TokenTree::Ident(Ident::new("write_html", Span::call_site())));
        tokens.push(TokenTree::Punct(proc_macro::Punct::new(':', proc_macro::Spacing::Joint)));
        tokens.push(TokenTree::Punct(proc_macro::Punct::new(':', proc_macro::Spacing::Alone)));
        tokens.push(TokenTree::Ident(Ident::new("tags", Span::call_site())));
        tokens.push(TokenTree::Punct(proc_macro::Punct::new(':', proc_macro::Spacing::Joint)));
        tokens.push(TokenTree::Punct(proc_macro::Punct::new(':', proc_macro::Spacing::Alone)));
        tokens.push(TokenTree::Ident(Ident::new("fragment", Span::call_site())));
        tokens.extend("(write_html::Empty)".parse::<TokenStream>().unwrap());
        tokens.extend(elements_to_with_token_stream(&elements).into_iter());

        tokens.into_iter().collect()
    }

    fn elements_to_with_token_stream(elements: &[Element]) -> TokenStream {
        let mut tokens = Vec::new();
        for element in elements {
            tokens.push(TokenTree::Punct(proc_macro::Punct::new('.', proc_macro::Spacing::Alone)));
            tokens.push(TokenTree::Ident(Ident::new("child", Span::call_site())));
            {
                let mut args = Vec::new();
                args.extend(element_to_token_stream(element).into_iter());
                let group = Group::new(Delimiter::Parenthesis, args.into_iter().collect());
                tokens.push(TokenTree::Group(group));
            }
        }

        tokens.into_iter().collect()
    }

    fn element_to_token_stream(element: &Element) -> TokenStream {
        let mut tokens = Vec::new();

        match element {
            Element::Expression(group) => {
                tokens.extend(group.clone().into_iter());
            }
            Element::Literal(literal) => {
                tokens.push(TokenTree::Ident(Ident::new("write_html", literal.span())));
                tokens.push(TokenTree::Punct(proc_macro::Punct::new(':', proc_macro::Spacing::Joint)));
                tokens.push(TokenTree::Punct(proc_macro::Punct::new(':', proc_macro::Spacing::Alone)));
                tokens.push(TokenTree::Ident(Ident::new("HtmlTextStr", literal.span())));
                {
                    let mut args = Vec::new();
                    args.push(TokenTree::Literal(literal.clone()));
                    let group = Group::new(Delimiter::Parenthesis, args.into_iter().collect());
                    tokens.push(TokenTree::Group(group));
                }
            }
            Element::Tag(tag) => {
                tokens.push(TokenTree::Ident(Ident::new("write_html", tag.identifier_span)));
                tokens.push(TokenTree::Punct(proc_macro::Punct::new(':', proc_macro::Spacing::Joint)));
                tokens.push(TokenTree::Punct(proc_macro::Punct::new(':', proc_macro::Spacing::Alone)));
                tokens.push(TokenTree::Ident(Ident::new("tags", tag.identifier_span)));
                tokens.push(TokenTree::Punct(proc_macro::Punct::new(':', proc_macro::Spacing::Joint)));
                tokens.push(TokenTree::Punct(proc_macro::Punct::new(':', proc_macro::Spacing::Alone)));
                if is_ident(&tag.identifier) {
                    tokens.push(TokenTree::Ident(Ident::new(&tag.identifier, tag.identifier_span)));
                    {
                        let args = "(write_html::Empty, write_html::Empty)";
                        let token_stream: TokenStream = args.parse().unwrap();
                        tokens.extend(token_stream.into_iter());
                    }
                } else {
                    tokens.push(TokenTree::Ident(Ident::new("tag", tag.identifier_span)));
                    {
                        let mut args = Vec::new();
                        args.push(TokenTree::Literal(Literal::string(&tag.identifier)));
                        let token_stream: TokenStream = ", write_html::Empty, write_html::Empty, write_html::Compactability::Yes{final_slash: true}".parse().unwrap();
                        args.extend(token_stream.into_iter());
                        let group = Group::new(Delimiter::Parenthesis, args.into_iter().collect());
                        tokens.push(TokenTree::Group(group));
                    }
                }
                for (key, value) in &tag.attributes {
                    tokens.push(TokenTree::Punct(proc_macro::Punct::new('.', proc_macro::Spacing::Alone)));
                    tokens.push(TokenTree::Ident(Ident::new("attr", tag.identifier_span)));
                    {
                        let mut args = Vec::new();
                        args.push(TokenTree::Literal(Literal::string(&key)));
                        args.push(TokenTree::Punct(proc_macro::Punct::new(',', proc_macro::Spacing::Alone)));
                        if let Some(value) = value {
                            match value {
                                AttributeValue::Literal(literal) => {
                                    args.push(TokenTree::Literal(literal.clone()));
                                }
                                AttributeValue::Expression(stream) => {
                                    args.extend(stream.clone().into_iter());
                                }
                            }
                        } else {
                            args.push(TokenTree::Literal(Literal::string("None")));
                        }
                        let group = Group::new(Delimiter::Parenthesis, args.into_iter().collect());
                        tokens.push(TokenTree::Group(group));
                    }
                }
                tokens.extend(elements_to_with_token_stream(&tag.children));
            }
        }

        tokens.into_iter().collect()
    }

    //"42".parse().unwrap()
    //let result = element_to_token_stream(element);
    let result = elements_to_token_stream(&elements);

    result
}

fn is_ident(id: &str) -> bool {
    if id.is_empty() {
        return false;
    }
    if id.chars().next().unwrap().is_numeric() {
        return false;
    }
    for c in id.chars() {
        if !c.is_alphanumeric() && c != '_' {
            return false;
        }
    }
    true
}

#[derive(Debug, Clone)]
enum Element {
    Expression(TokenStream),
    Literal(Literal),
    Tag(Tag),
}

#[derive(Debug, Clone)]
struct ParsedElement {
    element: Element,
    next: usize,
}

fn parse_mutliple_elements(tokens: &[TokenTree]) -> Option<Vec<Element>> {
    let mut elements = Vec::new();

    let mut idx = 0;
    loop {
        if idx >= tokens.len() {
            break;
        }

        let element = parse_element(&tokens[idx..])?;
        elements.push(element.element);
        idx += element.next;
    }

    Some(elements)
}

fn parse_element(tokens: &[TokenTree]) -> Option<ParsedElement> {

    let first = tokens.first()?;

    match first {
        TokenTree::Ident(_ident) => {
            let tag = parse_tag_element(tokens)?;
            Some(ParsedElement {
                element: Element::Tag(tag.tag),
                next: tag.next,
            })
        }
        TokenTree::Literal(literal) => {
            Some(ParsedElement {
                element: Element::Literal(literal.clone()),
                next: 1,
            })
        }
        TokenTree::Punct(_punct) => {
            None
        }
        TokenTree::Group(group) => {
            match group.delimiter() {
                Delimiter::Parenthesis => {
                    Some(ParsedElement {
                        element: Element::Expression(group.stream()),
                        next: 1,
                    })
                }
                Delimiter::Brace => {
                    None
                }
                Delimiter::Bracket => {
                    None
                }
                Delimiter::None => {
                    None
                }
            }
        }
    }
}

#[derive(Debug, Clone)]
struct Tag {
    identifier: String,
    identifier_span: proc_macro::Span,
    attributes: Vec<(String, Option<AttributeValue>)>,
    children: Vec<Element>,
}

#[derive(Debug, Clone)]
struct ParsedTag {
    tag: Tag,
    next: usize,
}

fn parse_tag_element(tokens: &[TokenTree]) -> Option<ParsedTag> {

    let (identifier, next) = parse_html_identifier(tokens)?;

    let mut attributes = Vec::new();

    let mut idx = next;
    loop {
        if idx >= tokens.len() {
            break;
        }

        let token = &tokens[idx];
        if let TokenTree::Group(group) = token {
            if group.delimiter() == Delimiter::Brace {
                let delimited = group.stream().into_iter().collect::<Vec<TokenTree>>();
                let children = parse_mutliple_elements(&delimited)?;
                return Some(ParsedTag {
                    tag: Tag {
                        identifier,
                        identifier_span: tokens[0].span(), // TODO this is not correct
                        attributes,
                        children: children,
                    },
                    next: idx + 1,
                });
            }
        }

        if let Some((attribute, next2)) = parse_attribute(&tokens[idx..]) {
            attributes.push(attribute);
            idx += next2;
        } else {
            return None;
        }
    }

    None
}

#[derive(Debug, Clone)]
enum AttributeValue {
    Literal(Literal),
    Expression(TokenStream),
}

fn parse_attribute(tokens: &[TokenTree]) -> Option<((String, Option<AttributeValue>), usize)> {
    // TODO handle # and .

    if tokens.is_empty() {
        return None;
    }

    // class
    if let TokenTree::Punct(punct) = &tokens[0] {
        if punct.as_char() == '.' {
            // TODO use @attribute_value_parsing made in a function
            let (identifier, next) = parse_html_identifier(&tokens[1..])?;
            return Some((("class".to_string(), Some(AttributeValue::Literal(Literal::string(&identifier)))), 1 + next));
        }
    }

    // id
    if let TokenTree::Punct(punct) = &tokens[0] {
        if punct.as_char() == '#' {
            // TODO use @attribute_value_parsing made in a function
            let (identifier, next) = parse_html_identifier(&tokens[1..])?;
            return Some((("id".to_string(), Some(AttributeValue::Literal(Literal::string(&identifier)))), 1 + next));
        }
    }

    let (identifier, next) = parse_html_identifier(tokens)?;

    if next < tokens.len() {
        let token = &tokens[next];
        if let TokenTree::Punct(punct) = token {
            if punct.as_char() == '=' {
                // @attribute_value_parsing
                if let Some((value_string, next2)) = parse_html_identifier(&tokens[next + 1..]) {
                    return Some(((identifier, Some(AttributeValue::Literal(Literal::string(&value_string)))), next + 1 + next2));
                } else {
                    let next_token_index = next + 1;
                    if next_token_index >= tokens.len() {
                        return Some(((identifier, None), next_token_index));
                    }
                    let next_token = &tokens[next_token_index];
                    match next_token {
                        TokenTree::Literal(literal) => {
                            return Some(((identifier, Some(AttributeValue::Literal(literal.clone()))), next_token_index + 1));
                        }
                        TokenTree::Group(group) => {
                            if group.delimiter() == Delimiter::Parenthesis {
                                return Some(((identifier, Some(AttributeValue::Expression(group.stream()))), next_token_index + 1));
                            }
                        }
                        _ => {
                            return Some(((identifier, None), next_token_index));
                        }
                    }
                }
            }

            return Some(((identifier, None), next));
        }
    }

    Some(((identifier, None), next))
}

fn parse_html_identifier(tokens: &[TokenTree]) -> Option<(String, usize)> {
    let mut identifier = String::new();

    let mut next = 0;

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    enum Type {
        Ident,
        Punct,
    }

    let mut prev_type = None;

    // iterate over the tokens until we find a non-ident or non "-" token
    for token in tokens {
        match token {
            TokenTree::Ident(ident) => {
                let typ = Type::Ident;
                if prev_type == Some(typ) {
                    break;
                }
                identifier.push_str(&ident.to_string());
                prev_type = Some(typ);
            }
            TokenTree::Literal(_literal) => {
                // TODO handle numbers
                break;
            }
            TokenTree::Punct(punct) => {
                if punct.as_char() == '-' {
                    let typ = Type::Punct;
                    if prev_type == Some(typ) {
                        break;
                    }
                    identifier.push_str("-");
                    prev_type = Some(typ);
                } else {
                    break;
                }
            }
            TokenTree::Group(_group) => {
                break;
            }
        }

        next += 1;
    }

    if next == 0 || identifier.is_empty() {
        None
    } else {
        Some((identifier, next))
    }
}