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
extern crate proc_macro;
use crate::parser::HtmlParser;
use crate::tag::Tag;
use quote::quote;
use syn::parse::{Parse, ParseStream, Result};
use syn::{parse_macro_input, Expr};
mod parser;
mod tag;
#[proc_macro]
pub fn text(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let text: Expr = syn::parse(input).expect("Text variable");
let text = quote! {
VirtualNode::text(#text)
};
text.into()
}
#[proc_macro]
pub fn html(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let parsed = parse_macro_input!(input as Html);
let mut html_parser = HtmlParser::new();
for tag in parsed.tags.into_iter() {
html_parser.push_tag(tag);
}
html_parser.finish().into()
}
#[derive(Debug)]
struct Html {
tags: Vec<Tag>,
}
impl Parse for Html {
fn parse(input: ParseStream) -> Result<Self> {
let mut tags = Vec::new();
while !input.is_empty() {
let tag: Tag = input.parse()?;
tags.push(tag);
}
Ok(Html { tags })
}
}