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
use proc_macro2::{TokenStream, TokenTree, Delimiter, Spacing};
use litrs::StringLit;
use crate::{ast, err::{Error, err}};
mod buf;
use self::buf::ParseBuf;
pub(crate) trait Parse {
fn parse(buf: &mut ParseBuf) -> Result<Self, Error>
where
Self: Sized;
}
impl ast::Input {
pub(crate) fn parse_input(tokens: TokenStream) -> Result<Self, Error> {
let mut buf = ParseBuf::from_stream(tokens);
let out = ast::Input::parse(&mut buf)?;
buf.expect_eof()?;
Ok(out)
}
}
fn is_punct(tt: &TokenTree, c: char) -> bool {
matches!(tt, TokenTree::Punct(p) if p.as_char() == c)
}
impl Parse for ast::Input {
fn parse(buf: &mut ParseBuf) -> Result<Self, Error> {
let mut buffer = None;
let mut format = None;
loop {
match buf.curr()? {
// A meta attribute
TokenTree::Punct(p) if p.as_char() == '#' => {
let _ = buf.expect_punct('#')?;
let g = buf.expect_group(Delimiter::Bracket)?;
let mut inner = ParseBuf::from_group(g);
let key = inner.expect_ident()?;
match key.to_string().as_str() {
"format" => {
let _ = inner.expect_punct('=')?;
format = Some(inner.collect_rest());
}
other => return Err(err!(
@key.span(),
"unsupported global attribute '{other}'",
)),
}
}
// The XML portion starts
TokenTree::Punct(p) if p.as_char() == '<' => break,
// Something else which we treat as an expression defining the
// buffer to append to.
_ => {
let mut tokens = vec![];
while buf.curr().is_ok_and(|tt| !is_punct(tt, ',')) {
tokens.push(buf.bump()?);
}
let _ = buf.bump()?; // Eat `,`
buffer = Some(TokenStream::from_iter(tokens));
}
}
}
// At this point, the XML part starts
let prolog = if is_punct(buf.next()?, '?') {
Some(buf.parse()?)
} else {
None
};
buf.expect_punct('<')?;
let root = buf.parse()?;
Ok(Self {
buffer,
format,
prolog,
root,
})
}
}
// Assumes `<` is already eaten.
impl Parse for ast::Prolog {
fn parse(buf: &mut ParseBuf) -> Result<Self, Error> {
let _ = buf.bump(); // Eat '<'
let _ = buf.bump(); // Eat '?'
let ident = buf.expect_ident()?;
if ident.to_string() != "xml" {
return Err(err!(@ident.span(), "expected 'xml'"));
}
let next_span = buf.curr().unwrap().span();
let mut parse_attr = |name: &str| -> Result<Option<String>, Error> {
if is_punct(buf.curr()?, '?') {
return Ok(None);
}
let ident = buf.expect_ident()?;
if ident.to_string() != name {
return Err(err!(
@ident.span(),
"expected '{name}' (prolog attributes have a fixed order)",
));
}
buf.expect_punct('=')?;
let s = buf.expect_string_lit()?;
Ok(Some(s.into_value().into_owned()))
};
let version = parse_attr("version")?.ok_or_else(|| err!(@next_span, "expected 'xml'"))?;
let encoding = parse_attr("encoding")?;
let standalone = if encoding.is_some() {
parse_attr("standalone")?
} else {
None
};
buf.expect_punct('?')?;
buf.expect_punct('>')?;
if encoding.as_ref().is_some_and(|enc| enc != "UTF-8") {
// TODO: span would be nice
return Err(err!("only encoding 'UTF-8' is allowed"));
};
Ok(Self { version, standalone })
}
}
// Assumes `<` is already eaten.
impl Parse for ast::Element {
fn parse(buf: &mut ParseBuf) -> Result<Self, Error> {
let name = buf.parse()?;
let mut attrs = Vec::new();
loop {
match buf.curr()? {
TokenTree::Punct(p) if p.as_char() == '>' => {
let _ = buf.bump();
break;
}
TokenTree::Punct(p) if p.as_char() == '/' => {
let _ = buf.bump();
buf.expect_punct('>')?;
return Ok(Self {
name,
attrs,
children: vec![],
empty: true,
})
}
TokenTree::Group(g) if g.delimiter() == Delimiter::Brace => {
let g = buf.expect_group(Delimiter::Brace)?;
let mut inner = ParseBuf::from_group(g);
let p = inner.expect_punct('.')?;
if p.spacing() == Spacing::Alone {
return Err(err!(@p.span(), "expected '..' but found single '.'"));
}
inner.expect_punct('.')?;
attrs.push(ast::Attr::Fill(inner.collect_rest()));
}
_ => {
let name = buf.parse()?;
buf.expect_punct('=')?;
let value = buf.parse()?;
attrs.push(ast::Attr::Single(name, value));
}
}
}
let mut children = vec![];
while !(is_punct(buf.curr()?, '<') && is_punct(buf.next()?, '/')) {
children.push(buf.parse()?);
}
let end_span = buf.expect_punct('<')?.span();
buf.expect_punct('/')?;
if is_punct(buf.curr()?, '>') {
let _ = buf.bump();
} else {
let ending_name: ast::Name = buf.parse()?;
if ending_name.0 != name.0 {
return Err(err!(@end_span,
"end tag '{}' does not match start tag '{}'",
ending_name.0,
name.0,
));
}
buf.expect_punct('>')?;
}
Ok(Self { name, attrs, children, empty: false })
}
}
impl Parse for ast::Name {
fn parse(buf: &mut ParseBuf) -> Result<Self, Error> {
// It is impossible for us to exactly parse XML names, unfortunately:
//
// For one, some characters allowed in XML names are not part of
// XID_Continue (and thus not allowed in Rust idents) and are also not
// allowed as punctuation. In other words: those characters just aren't
// part of the Rust lexicographical grammar outside of string
// literals.
//
// Further, from Rusts lexicographical grammar and proc macro
// perspective, the following inputs are all the same:
// - <foo:bar: baz="3">
// - <foo:bar :baz="3">
// - <foo: bar:baz="3">
//
// So since we have no knowledge about "spaces" between tokens, we have
// to use some custom logic to decide whether additional tokens
// are "eaten", i.e. being considered part of the XML name. This
// depends on the previous token kind that was eaten. The first token
// is always part of the XML name. We distinguish three kinds of tokens:
// - ident
// - num_lit: float or integer lit
// - :-. punctuation
//
// These three kinds are enough to cover all valid XML names, ignoring
// the characters mentioned above. A single `_` is actually parsed as
// ident.
//
// These are the rules to eat the next token or not, depending on the
// previous one:
//
// :-. :-. -> eat [^1]
// :-. ident -> eat: XML names can start with `:-.` and an ident
// could start a new XML name -> main problem.
// :-. num_lit -> eat: a num lit always starts with 0-9, but an
// XML name cannot start like that.
// ident :-. -> eat [^1]
// ident ident -> stop: there must be a space between as otherwise
// it would be parsed as one ident.
// ident num_lit -> stop: num lit starts with 0-9 but that would
// still be part of the ident before.
// num_lit :-. -> eat [^1]
// num_lit ident -> stop: ident would be part of literal suffix
// -> thus there is a space.
// num_lit num_lit -> stop: as far as I can tell, this also can only
// happen if there is a space.
//
// [^1]: An XML name is allowed to start with `:`. By using this rule,
// we make it impossible to use a leading `:` in an XML name that
// follows another XML name.
//
// So in summary, we stop when a non-punct follows a non-punct.
let mut eat_non_punct = true;
// Because of all the weirdness explained above, we allow a single
// string literal to define the name.
if let Ok(lit) = StringLit::try_from(buf.curr()?) {
let token = buf.bump().unwrap();
let s = lit.into_value().into_owned();
if !is_name(&s) {
return Err(err!(@token.span(),
"string contains characters that are not allowed in XML names",
));
}
return Ok(Self(s));
}
let mut out = String::new();
loop {
match buf.curr() {
// Identifier including _
Ok(TokenTree::Ident(i)) => {
if !eat_non_punct {
break;
}
eat_non_punct = false;
let s = i.to_string();
// There are four characters that are allowed as part of
// Rust literals but that is not allowed in XML names.
let invalid_char = |c| matches!(c, '\u{AA}' | '\u{B5}' | '\u{BA}' | '\u{2054}');
if let Some(p) = s.find(invalid_char) {
return Err(err!(
"Character '{}' is not allowed in XML names",
s[p..].chars().next().unwrap(),
));
}
out.push_str(&s);
let _ = buf.bump();
}
// Numeric literals
Ok(TokenTree::Literal(lit)) => {
let s = lit.to_string();
if s.starts_with(|c: char| c.is_digit(10)) {
let _ = buf.bump();
out.push_str(&s);
} else {
break;
}
}
// : . -
Ok(TokenTree::Punct(p)) => {
let c = p.as_char();
if c == ':' || (!out.is_empty() && (c == '.' || c == '-')) {
eat_non_punct = true;
let _ = buf.bump();
out.push(c);
} else {
break;
}
}
_ => break,
}
}
if out.is_empty() {
let unexpected = buf.curr().unwrap();
return Err(err!(@unexpected.span(), "expected name, found {unexpected}"));
}
Ok(Self(out))
}
}
impl Parse for ast::Child {
fn parse(buf: &mut ParseBuf) -> Result<Self, Error> {
match buf.bump()? {
TokenTree::Literal(l) => {
let slit = StringLit::try_from(&l)
.map_err(|_| err!(@l.span(), "expected string literal"))?;
let v = slit.into_value().into_owned();
Ok(Self::Text(v))
}
TokenTree::Group(g) if g.delimiter() == Delimiter::Brace => {
let inner = g.stream();
if inner.clone().into_iter().next().is_some_and(|tt| is_punct(&tt, '|')) {
let mut inner = ParseBuf::from_group(g);
let _ = inner.expect_punct('|')?;
let arg = inner.expect_ident()?;
let _ = inner.expect_punct('|')?;
let body = TokenStream::from_iter(std::iter::from_fn(|| inner.bump().ok()));
Ok(Self::Closure { arg, body })
} else {
Ok(Self::TextExpr(inner))
}
}
TokenTree::Punct(p) if p.as_char() == '<' => {
Ok(Self::Element(buf.parse()?))
}
other => Err(err!(
@other.span(),
"expected element child: string literal, {{...}} or '<'",
)),
}
}
}
impl Parse for ast::AttrValue {
fn parse(buf: &mut ParseBuf) -> Result<Self, Error> {
match buf.bump()? {
TokenTree::Literal(l) => {
let slit = StringLit::try_from(&l)
.map_err(|_| err!(@l.span(), "expected string literal"))?;
let v = slit.into_value().into_owned();
Ok(Self::Literal(v))
}
TokenTree::Group(g) if g.delimiter() == Delimiter::Brace => {
Ok(Self::Expr(g.stream()))
}
other => Err(err!(
@other.span(),
"expected attribute value: string literal or {{...}}",
)),
}
}
}
include!("../shared.rs");