-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
html_prop.rs
81 lines (73 loc) · 2.49 KB
/
html_prop.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
use crate::html_tree::HtmlDashedName as HtmlPropLabel;
use crate::{Peek, PeekValue};
use proc_macro::TokenStream;
use proc_macro2::TokenTree;
use syn::buffer::Cursor;
use syn::parse::{Parse, ParseStream, Result as ParseResult};
use syn::{Expr, Token};
pub struct HtmlProp {
pub label: HtmlPropLabel,
pub value: Expr,
}
impl PeekValue<()> for HtmlProp {
fn peek(cursor: Cursor) -> Option<()> {
HtmlPropLabel::peek(cursor).map(|_| ())
}
}
impl Parse for HtmlProp {
fn parse(input: ParseStream) -> ParseResult<Self> {
let label = input.parse::<HtmlPropLabel>()?;
input
.parse::<Token![=]>()
.map_err(|_| syn::Error::new_spanned(&label, "this prop doesn't have a value"))?;
let value = input.parse::<Expr>()?;
// backwards compat
let _ = input.parse::<Token![,]>();
Ok(HtmlProp { label, value })
}
}
pub struct HtmlPropSuffix {
pub stream: TokenStream,
pub div: Option<Token![/]>,
pub gt: Token![>],
}
impl Parse for HtmlPropSuffix {
fn parse(input: ParseStream) -> ParseResult<Self> {
let mut trees: Vec<TokenTree> = vec![];
let mut div: Option<Token![/]> = None;
let mut angle_count = 1;
let gt: Option<Token![>]>;
loop {
let next = input.parse()?;
if let TokenTree::Punct(punct) = &next {
match punct.as_char() {
'>' => {
angle_count -= 1;
if angle_count == 0 {
gt = Some(syn::token::Gt {
spans: [punct.span()],
});
break;
}
}
'<' => angle_count += 1,
'/' => {
if angle_count == 1 && input.peek(Token![>]) {
div = Some(syn::token::Div {
spans: [punct.span()],
});
gt = Some(input.parse()?);
break;
}
}
_ => {}
};
}
trees.push(next);
}
let gt: Token![>] = gt.ok_or_else(|| input.error("missing tag close"))?;
let stream: proc_macro2::TokenStream = trees.into_iter().collect();
let stream = TokenStream::from(stream);
Ok(HtmlPropSuffix { stream, div, gt })
}
}