/
html_list.rs
194 lines (173 loc) · 5.17 KB
/
html_list.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
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
use super::HtmlTree;
use crate::html_tree::{HtmlProp, HtmlPropSuffix};
use crate::PeekValue;
use boolinator::Boolinator;
use quote::{quote, quote_spanned, ToTokens};
use syn::buffer::Cursor;
use syn::parse;
use syn::parse::{Parse, ParseStream, Result as ParseResult};
use syn::spanned::Spanned;
use syn::{Expr, Token};
pub struct HtmlList {
pub children: Vec<HtmlTree>,
pub key: Option<Expr>,
}
impl PeekValue<()> for HtmlList {
fn peek(cursor: Cursor) -> Option<()> {
HtmlListOpen::peek(cursor)
.or_else(|| HtmlListClose::peek(cursor))
.map(|_| ())
}
}
impl Parse for HtmlList {
fn parse(input: ParseStream) -> ParseResult<Self> {
if HtmlListClose::peek(input.cursor()).is_some() {
return match input.parse::<HtmlListClose>() {
Ok(close) => Err(syn::Error::new_spanned(
close,
"this close tag has no corresponding open tag",
)),
Err(err) => Err(err),
};
}
let open = input.parse::<HtmlListOpen>()?;
if !HtmlList::verify_end(input.cursor()) {
return Err(syn::Error::new_spanned(
open,
"this open tag has no corresponding close tag",
));
}
let mut children: Vec<HtmlTree> = vec![];
while HtmlListClose::peek(input.cursor()).is_none() {
children.push(input.parse()?);
}
input.parse::<HtmlListClose>()?;
Ok(HtmlList {
children,
key: open.key,
})
}
}
impl ToTokens for HtmlList {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
let children = &self.children;
let key = if let Some(key) = &self.key {
quote_spanned! {key.span() => Some(#key)}
} else {
quote! {None }
};
tokens.extend(quote! {
::yew::virtual_dom::VNode::VList(
::yew::virtual_dom::VList::new_with_children({
let mut v = ::std::vec::Vec::new();
#(v.extend(::yew::utils::NodeSeq::from(#children));)*
v
},
#key)
)
});
}
}
impl HtmlList {
fn verify_end(mut cursor: Cursor) -> bool {
let mut list_stack_count = 1;
loop {
if HtmlListOpen::peek(cursor).is_some() {
list_stack_count += 1;
} else if HtmlListClose::peek(cursor).is_some() {
list_stack_count -= 1;
if list_stack_count == 0 {
break;
}
}
if let Some((_, next)) = cursor.token_tree() {
cursor = next;
} else {
break;
}
}
list_stack_count == 0
}
}
struct HtmlListOpen {
lt: Token![<],
key: Option<Expr>,
gt: Token![>],
}
impl PeekValue<()> for HtmlListOpen {
fn peek(cursor: Cursor) -> Option<()> {
let (punct, cursor) = cursor.punct()?;
(punct.as_char() == '<').as_option()?;
if let Some((ident, _)) = cursor.ident() {
(ident == "key").as_option()
} else {
let (punct, _) = cursor.punct()?;
(punct.as_char() == '>').as_option()
}
}
}
impl Parse for HtmlListOpen {
fn parse(input: ParseStream) -> ParseResult<Self> {
let lt = input.parse()?;
if input.cursor().ident().is_some() {
let HtmlPropSuffix { stream, gt, .. } = input.parse()?;
let props = parse::<ParseKey>(stream)?;
Ok(HtmlListOpen {
lt,
key: Some(props.key.value),
gt,
})
} else {
let gt = input.parse()?;
Ok(HtmlListOpen { lt, key: None, gt })
}
}
}
struct ParseKey {
key: HtmlProp,
}
impl Parse for ParseKey {
fn parse(input: ParseStream) -> ParseResult<Self> {
let key = input.parse::<HtmlProp>()?;
if !input.is_empty() {
input.error("Only a single key element is allowed on a <></>");
}
Ok(ParseKey { key })
}
}
impl ToTokens for HtmlListOpen {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
let HtmlListOpen { lt, gt, .. } = self;
tokens.extend(quote! {#lt#gt});
}
}
struct HtmlListClose {
lt: Token![<],
div: Token![/],
gt: Token![>],
}
impl PeekValue<()> for HtmlListClose {
fn peek(cursor: Cursor) -> Option<()> {
let (punct, cursor) = cursor.punct()?;
(punct.as_char() == '<').as_option()?;
let (punct, cursor) = cursor.punct()?;
(punct.as_char() == '/').as_option()?;
let (punct, _) = cursor.punct()?;
(punct.as_char() == '>').as_option()
}
}
impl Parse for HtmlListClose {
fn parse(input: ParseStream) -> ParseResult<Self> {
Ok(HtmlListClose {
lt: input.parse()?,
div: input.parse()?,
gt: input.parse()?,
})
}
}
impl ToTokens for HtmlListClose {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
let HtmlListClose { lt, div, gt } = self;
tokens.extend(quote! {#lt#div#gt});
}
}