forked from rust-lang/rustfmt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml.rs
224 lines (219 loc) · 8.54 KB
/
html.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
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
use crate::rewrite::RewriteContext;
use rustc_ast::ptr::P;
use rustc_ast::token;
use rustc_ast::token::{BinOpToken, Lit, TokenKind};
use rustc_ast::tokenstream::TokenStream;
use rustc_ast::{Expr, StrLit};
use rustc_ast::{ast, token::Delimiter};
use rustc_parse::exp;
use rustc_parse::parser::TokenType;
use rustc_parse::parser::{ExpTokenPair, Parser};
use rustc_span::symbol::{self, Ident, kw};
use tracing::{debug, info, warn};
pub(crate) enum HtmlAttributeValue {
Expr(P<Expr>),
Literal(StrLit),
Ident(Ident),
}
pub(crate) enum Html {
Expr(P<Expr>),
Literal(StrLit),
Ident(Ident),
Comment(StrLit),
Open {
tag: Ident,
attrs: Vec<(Ident, Vec<(TokenKind, Ident)>, HtmlAttributeValue)>,
},
Close {
tag: Ident,
},
If {
conditional: P<Expr>,
body: Vec<Html>,
variable: Ident,
result_expr: P<Expr>,
},
}
pub(crate) fn parse_single_html(
context: &RewriteContext<'_>,
ts_string: &str,
parser: &mut Parser<'_>,
) -> Option<Vec<Html>> {
macro_rules! parse_eat {
($($arg:expr),*) => {
if !parser.eat($($arg,)*) {
panic!("{:?} {} {}", parser.token, file!(), line!());
}
}
}
let mut result = vec![];
match &parser.token.kind {
token_kind @ TokenKind::Ident(symbol, ident_is_raw) if symbol.as_str() == "if" => {
eprintln!("got an if");
assert!(parser.eat_keyword(exp!(If)));
let conditional = match parser.parse_expr_cond() {
Ok(expr) => expr,
Err(error) => {
panic!("{:?} {:?}", error, parser.parse_tokens());
}
};
assert!(parser.eat(exp!(OpenBrace)));
let mut body = Vec::new();
while parser.token.kind != TokenKind::CloseDelim(Delimiter::Brace) {
if let Some(some_htmls) = parse_single_html(context, ts_string, parser) {
body.extend(some_htmls);
} else {
panic!();
}
}
assert!(parser.eat(exp!(CloseBrace)));
assert!(parser.eat(exp!(FatArrow)));
let variable = parser.token.ident().unwrap().0;
parser.bump();
assert!(parser.eat(exp!(Eq)));
let result_expr = match parser.parse_expr() {
Ok(expr) => expr,
Err(error) => {
panic!("{:?} {:?}", error, parser.parse_tokens());
}
};
assert!(parser.eat(exp!(Semi)));
result.push(Html::If {
conditional,
body,
variable,
result_expr,
})
}
TokenKind::OpenDelim(Delimiter::Brace) => {
//eprintln!("parsing inner expr");
assert!(parser.eat(exp!(OpenBrace)));
let expr = match parser.parse_expr() {
Ok(expr) => expr,
Err(error) => {
panic!("{:?} {:?}", error, parser.parse_tokens());
}
};
assert!(parser.eat(exp!(CloseBrace)));
result.push(Html::Expr(expr))
}
TokenKind::Literal(_) => {
let Ok(literal) = parser.parse_str_lit() else {
panic!();
};
result.push(Html::Literal(literal))
}
token_kind @ TokenKind::Ident(_, _) => {
//eprintln!("parsing ident {:?}", parser.token);
//let id = parse_or!(parse_ident);
let ident = parser.token.ident().unwrap().0;
parser.bump();
result.push(Html::Ident(ident))
}
TokenKind::Lt => {
//eprintln!("parsing lt");
parse_eat!(exp!(Lt));
match parser.token.kind {
TokenKind::BinOp(BinOpToken::Slash) => {
//eprintln!("parsing slash");
parser.bump();
//eprintln!("parsing ident");
let id = parser.token.ident().unwrap().0;
parser.bump();
//eprintln!("parsing gt");
parse_eat!(exp!(Gt));
result.push(Html::Close { tag: id });
}
TokenKind::Not => {
//eprintln!("parsing not");
parse_eat!(exp!(Not));
parse_eat!(exp!(Minus));
parse_eat!(exp!(Minus));
let Ok(comment) = parser.parse_str_lit() else {
panic!();
};
parse_eat!(exp!(Minus));
parse_eat!(exp!(RArrow));
result.push(Html::Comment(comment));
}
_ => {
//eprintln!("parsing ident");
let id = parser.token.ident().expect(&ts_string).0;
parser.bump();
let mut attrs: Vec<(Ident, Vec<(TokenKind, Ident)>, HtmlAttributeValue)> =
Vec::new();
while parser.token.kind != TokenKind::Gt {
//eprintln!("parsing ident");
let base_id = parser.token.ident().expect(&ts_string).0;
parser.bump();
let mut rest_id = Vec::new();
// also minus?
while parser.token.kind == TokenKind::Colon
|| parser.token.kind == TokenKind::BinOp(BinOpToken::Minus)
{
let delimiter = parser.token.kind.clone();
parser.bump();
let i = parser.token.ident().unwrap().0;
parser.bump();
rest_id.push((delimiter, i));
}
//eprintln!("parsing eq");
parse_eat!(exp!(Eq)); // here
//eprintln!("parsing literal or expr");
match &parser.token.kind {
TokenKind::OpenDelim(Delimiter::Brace) => {
assert!(parser.eat(exp!(OpenBrace)));
//eprintln!("parsing inner expr");
let expr = match parser.parse_expr() {
Ok(expr) => expr,
Err(error) => {
panic!("{:?} {:?}", error, parser.parse_tokens());
}
};
assert!(parser.eat(exp!(CloseBrace)));
attrs.push((base_id, rest_id, HtmlAttributeValue::Expr(expr)));
}
TokenKind::Literal(_) => {
let Ok(literal) = parser.parse_str_lit() else {
panic!();
};
attrs.push((
base_id,
rest_id,
HtmlAttributeValue::Literal(literal),
));
}
token_kind @ TokenKind::Ident(_, _) => {
//eprintln!("parsing ident {:?}", parser.token);
//let id = parse_or!(parse_ident);
let ident = parser.token.ident().unwrap().0;
parser.bump();
attrs.push((base_id, rest_id, HtmlAttributeValue::Ident(ident)))
}
_ => panic!(),
}
}
//eprintln!("parsing gt");
parse_eat!(exp!(Gt));
result.push(Html::Open { tag: id, attrs });
}
}
}
_ => panic!(),
}
Some(result)
}
pub(crate) fn parse_html(context: &RewriteContext<'_>, ts: TokenStream) -> Option<Vec<Html>> {
let ts_string = format!("{:?}", ts);
//eprintln!("parsing token stream {:?}", ts);
let mut result = vec![];
let mut parser = super::build_parser(context, ts);
while parser.token.kind != TokenKind::Eof {
if let Some(val) = parse_single_html(context, &ts_string, &mut parser) {
result.extend(val);
} else {
panic!();
}
}
Some(result)
}